Documentation
¶
Overview ¶
Package graph provides a thread-safe in-memory knowledge graph backed by a JSON file for persistence. Nodes are entities with a label and name; edges are directed relationships between nodes with a relation type.
Index ¶
- type Edge
- type Graph
- func (g *Graph) AddEdge(from, to, relation string, props map[string]string) (Edge, error)
- func (g *Graph) AddNode(label, name string, props map[string]string) (Node, error)
- func (g *Graph) AllEdges() []Edge
- func (g *Graph) AllNodes() []Node
- func (g *Graph) FindNodes(label, name string) []Node
- func (g *Graph) GetNode(id string) (Node, bool)
- func (g *Graph) ListRelations() []string
- func (g *Graph) Neighbors(id, direction, relation string) (NeighborResult, error)
- func (g *Graph) RemoveEdge(id string) error
- func (g *Graph) RemoveNode(id string) (int, error)
- func (g *Graph) ShortestPath(from, to string, maxDepth int) (PathResult, error)
- func (g *Graph) Stats() (nodes, edges int)
- type NeighborResult
- type Node
- type PathResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Edge ¶
type Edge struct {
ID string `json:"id"`
From string `json:"from"`
To string `json:"to"`
Relation string `json:"relation"` // e.g. "knows", "depends_on", "uses"
Props map[string]string `json:"props,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Edge represents a directed relationship from one node to another.
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph is a thread-safe knowledge graph backed by a JSON file.
func (*Graph) AddNode ¶
AddNode creates a new node with the given label, name, and optional properties.
func (*Graph) FindNodes ¶
FindNodes returns nodes matching the optional label and/or name substring. Both comparisons are case-insensitive. Empty strings match everything.
func (*Graph) ListRelations ¶
ListRelations returns all unique relation types in the graph, sorted.
func (*Graph) Neighbors ¶
func (g *Graph) Neighbors(id, direction, relation string) (NeighborResult, error)
Neighbors returns the direct neighbors of a node and the connecting edges. direction is "out" (default outgoing), "in" (incoming), or "both". relation optionally filters by edge relation type (case-insensitive).
func (*Graph) RemoveEdge ¶
RemoveEdge deletes an edge by ID.
func (*Graph) RemoveNode ¶
RemoveNode deletes a node and all its incident edges. Returns the number of edges removed.
func (*Graph) ShortestPath ¶
func (g *Graph) ShortestPath(from, to string, maxDepth int) (PathResult, error)
ShortestPath finds the shortest directed path from → to using BFS. maxDepth caps the search depth (0 → use default of 10).
type NeighborResult ¶
NeighborResult holds the neighbors of a node and the connecting edges.
type Node ¶
type Node struct {
ID string `json:"id"`
Label string `json:"label"` // e.g. "Person", "Concept", "Technology"
Name string `json:"name"` // display name
Props map[string]string `json:"props,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Node represents an entity in the knowledge graph.
type PathResult ¶
PathResult holds the ordered nodes and edges of a graph path.