graph

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 New

func New(dir string) (*Graph, error)

New loads or creates a Graph whose data is stored in dir/graph.json.

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to, relation string, props map[string]string) (Edge, error)

AddEdge creates a directed edge from→to with the given relation type.

func (*Graph) AddNode

func (g *Graph) AddNode(label, name string, props map[string]string) (Node, error)

AddNode creates a new node with the given label, name, and optional properties.

func (*Graph) AllEdges

func (g *Graph) AllEdges() []Edge

AllEdges returns all edges sorted by ID.

func (*Graph) AllNodes

func (g *Graph) AllNodes() []Node

AllNodes returns all nodes sorted by ID.

func (*Graph) FindNodes

func (g *Graph) FindNodes(label, name string) []Node

FindNodes returns nodes matching the optional label and/or name substring. Both comparisons are case-insensitive. Empty strings match everything.

func (*Graph) GetNode

func (g *Graph) GetNode(id string) (Node, bool)

GetNode returns a single node by ID.

func (*Graph) ListRelations

func (g *Graph) ListRelations() []string

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

func (g *Graph) RemoveEdge(id string) error

RemoveEdge deletes an edge by ID.

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(id string) (int, error)

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).

func (*Graph) Stats

func (g *Graph) Stats() (nodes, edges int)

Stats returns the total number of nodes and edges.

type NeighborResult

type NeighborResult struct {
	Nodes []Node `json:"nodes"`
	Edges []Edge `json:"edges"`
}

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

type PathResult struct {
	Nodes []Node `json:"nodes"`
	Edges []Edge `json:"edges"`
}

PathResult holds the ordered nodes and edges of a graph path.

Jump to

Keyboard shortcuts

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