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
- Variables
- func GetValidRelationTypes() []string
- func ValidateRelationType(relationType string) bool
- type Service
- func (s *Service) CreateEdge(ctx context.Context, edge *domain.Edge) error
- func (s *Service) DeleteEdge(ctx context.Context, id int64) error
- func (s *Service) FindPath(ctx context.Context, fromID, toID int64, maxDepth int) ([]int64, error)
- func (s *Service) GetRelated(ctx context.Context, obsID int64, depth int) ([]*domain.Observation, error)
- func (s *Service) GetRelationships(ctx context.Context, obsID int64) ([]*domain.Edge, error)
Constants ¶
const ( DefaultWeight = 1.0 MinWeight = 0.0 MaxWeight = 10.0 DefaultMaxDepth = 5 MaxTraversalDepth = 10 // Prevent infinite loops )
Business rule constants
Variables ¶
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
var ValidRelationTypes = map[string]bool{ domain.RelationReferences: true, domain.RelationRelatesTo: true, domain.RelationFollows: true, domain.RelationSupersedes: true, domain.RelationContradicts: true, }
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 ¶
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 ¶
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 ¶
DeleteEdge removes a relationship between observations. Returns ErrEdgeNotFound if the edge doesn't exist.
func (*Service) FindPath ¶
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 ¶
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.