graph

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package graph provides business logic for managing relationships between observations in the knowledge graph.

The graph service enables semantic navigation through observations by creating, querying, and traversing edges (relationships) between them. This supports use cases like finding related knowledge, discovering contradiction chains, and understanding concept hierarchies.

Index

Constants

View Source
const (
	DefaultWeight     = 1.0
	MinWeight         = 0.0
	MaxWeight         = 10.0
	DefaultMaxDepth   = 5
	MaxTraversalDepth = 10 // Prevent infinite loops
)

Business rule constants

Variables

View Source
var (
	ErrSelfReference = errors.New("cannot create edge from observation to itself")
	ErrInvalidWeight = errors.New("weight must be between 0 and 10")
	ErrInvalidDepth  = errors.New("depth must be between 1 and 10")
	ErrEdgeNotFound  = errors.New("edge not found")
	ErrDuplicateEdge = errors.New("edge already exists with same from_obs_id, to_obs_id, and relation_type")
)

Common errors

ValidRelationTypes contains all allowed relation types for edges.

Functions

func GetValidRelationTypes

func GetValidRelationTypes() []string

GetValidRelationTypes returns a list of all valid relation types.

func ValidateRelationType

func ValidateRelationType(relationType string) bool

ValidateRelationType checks if a relation type is valid.

Types

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides graph operations for managing relationships between observations.

func NewService

func NewService(repo domain.GraphRepository) *Service

NewService creates a new graph service with the given repository.

func (*Service) CreateEdge

func (s *Service) CreateEdge(ctx context.Context, edge *domain.Edge) error

CreateEdge creates a relationship between two observations.

Business Rules:

  • Cannot create edge from observation to itself (self-reference)
  • Weight must be > 0 (defaults to 1.0)
  • Relation type must be valid
  • (from_obs_id, to_obs_id, relation_type) must be unique

func (*Service) DeleteEdge

func (s *Service) DeleteEdge(ctx context.Context, id int64) error

DeleteEdge removes a relationship between observations. Returns ErrEdgeNotFound if the edge doesn't exist.

func (*Service) FindPath

func (s *Service) FindPath(ctx context.Context, fromID, toID int64, maxDepth int) ([]int64, error)

FindPath finds a path between two observations using Breadth-First Search (BFS). Returns the sequence of observation IDs from fromID to toID, or nil if no path exists.

The maxDepth parameter limits how far to search to prevent performance issues. If maxDepth is 0, DefaultMaxDepth (5) is used.

func (*Service) GetRelated

func (s *Service) GetRelated(ctx context.Context, obsID int64, depth int) ([]*domain.Observation, error)

GetRelated retrieves all observations related to a given observation, traversing the graph up to the specified depth.

Depth meanings:

  • depth=1: Only directly connected observations
  • depth=2: Observations 1 or 2 hops away
  • depth=N: Observations up to N hops away

Returns observations in order of proximity (closer observations first).

func (*Service) GetRelationships

func (s *Service) GetRelationships(ctx context.Context, obsID int64) ([]*domain.Edge, error)

GetRelationships retrieves all edges for an observation (both outgoing and incoming). This is useful for displaying the full context of relationships for a given observation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL