graph

package
v0.13.3 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Overview

Package graph separates shared rule definitions from mutable search results.

Data owns class/rule identity and compact integer topology. Node IDs identify classes; line IDs identify expanded (start class, goal class, rule) records. A Graph contains Node and Line objects for Gonum algorithms, rendering or results. It may contain only a subset of Data's topology. An Edge groups all parallel lines between two nodes; it has no separate identity in Data.

Searches can use Data's ID-based adjacency without constructing a Graph. Mutable Nodes, Lines and Queries belong to a search/result, not to Data. View adapts Data directly to Gonum algorithms without result-bearing objects. This package does not query stores; the engine does that.

Index

Constants

This section is empty.

Variables

View Source
var (
	NodesOf = graph.NodesOf
	LinesOf = graph.LinesOf
	EdgesOf = graph.EdgesOf
)

Pull useful functions into this package.

Functions

This section is empty.

Types

type Attrs

type Attrs map[string]string

Attributes for nodes and lines rendered by Graphviz.

func (Attrs) Attributes

func (a Attrs) Attributes() (enc []encoding.Attribute)

type Data

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

Data is the shared, immutable graph of all rules. It assigns immutable integer IDs to nodes (classes), rules, and lines, and provides fast, allocation-free iteration via an adjacency list.

Mutable Node and Line values are created and managed separately to hold results and queries. A View provides a fast read-only Gonum graph of the Data.

func NewData

func NewData(rules ...korrel8r.Rule) *Data

NewData builds a rule graph definition. Input order determines node and line IDs; parallel lines, self-loops and repeated input rules are preserved.

func (*Data) Class added in v0.12.3

func (d *Data) Class(id int64) korrel8r.Class

Class returns the class associated with a node ID. Invalid IDs panic.

func (*Data) Classes

func (d *Data) Classes() []korrel8r.Class

Classes returns the classes in node-ID order, in an independent slice.

func (*Data) EachLineIDFrom added in v0.12.3

func (d *Data) EachLineIDFrom(node int64, visit func(int))

EachLineIDFrom visits outgoing line IDs in creation order without allocating. Invalid node IDs are treated as empty adjacency lists.

func (*Data) EachLineIDTo added in v0.12.3

func (d *Data) EachLineIDTo(node int64, visit func(int))

EachLineIDTo visits incoming line IDs in creation order without allocating. Invalid node IDs are treated as empty adjacency lists.

func (*Data) EdgeWeight added in v0.12.3

func (d *Data) EdgeWeight(from, to int64) (float64, bool)

EdgeWeight returns the least weight of any line from->to, and whether the edge exists.

func (*Data) Endpoints added in v0.12.3

func (d *Data) Endpoints(id int) (int64, int64)

Endpoints returns the start and goal node IDs of a line. Invalid IDs panic.

func (*Data) FullGraph added in v0.6.1

func (d *Data) FullGraph() *Graph

FullGraph creates an independent mutable Graph containing all of Data. Nodes and lines have fresh, empty result/query/attribute state.

func (*Data) Graph added in v0.12.3

func (d *Data) Graph() View

Graph returns a read-only Gonum view without building or caching another graph.

func (*Data) LineCount added in v0.12.3

func (d *Data) LineCount() int

LineCount returns the number of expanded topology lines.

func (*Data) NewLine added in v0.12.3

func (d *Data) NewLine(id int, from, to *Node) *Line

NewLine creates independently owned mutable line state between explicit search/graph-owned nodes. Endpoint IDs must match the topology line or it panics. Retain the returned pointer to accumulate Queries; each call creates fresh state.

func (*Data) NewNode added in v0.12.3

func (d *Data) NewNode(id int64) *Node

NewNode creates a search-owned or graph-owned mutable node for a class identity. Each call creates independent state; Data retains no reference to the node.

func (*Data) NodeCount added in v0.12.3

func (d *Data) NodeCount() int

NodeCount returns the number of topology classes.

func (*Data) NodeID added in v0.12.3

func (d *Data) NodeID(c korrel8r.Class) (int64, bool)

NodeID returns the class's node ID and whether it exists in the topology.

func (*Data) RuleForLine added in v0.12.3

func (d *Data) RuleForLine(id int) korrel8r.Rule

RuleForLine returns the rule associated with a line. Invalid IDs panic.

func (*Data) Rules

func (d *Data) Rules() []korrel8r.Rule

Rules returns a copy of the input rule list, in input order. Each input entry appears once, regardless of how many topology lines it generates. Rules with empty start/goal sets and explicitly repeated input entries are preserved.

type Edge added in v0.1.1

type Edge struct{ multi.Edge }

Edge groups all parallel Lines between a pair of nodes in a Gonum Graph. It is an algorithm/iteration view, not a separate topology record.

func EdgeFor added in v0.8.4

func EdgeFor(e graph.Edge) Edge

func (Edge) EachLine added in v0.1.1

func (e Edge) EachLine(visit func(*Line))

EachLine consumes this Edge's iterator. For independent concurrent iteration, use Graph.EachLineBetween instead.

func (Edge) Goal added in v0.1.1

func (e Edge) Goal() *Node

func (Edge) Start added in v0.1.1

func (e Edge) Start() *Node

type Graph

type Graph struct {
	*multi.DirectedGraph
	GraphAttrs, NodeAttrs, EdgeAttrs Attrs
	Data                             *Data
	// Truncation is set if the search returned a partial graph. It is a field
	// rather than a GraphAttrs entry, which GraphViz would reject on render.
	Truncation *Truncation
	// contains filtered or unexported fields
}

Graph is a Gonum directed multigraph built from a Data definition. Nodes and Lines carry results, query accounting and GraphViz attributes. Graphs sharing Data use the same IDs, but normally own independent mutable objects.

Graph is not safe for concurrent mutation. For concurrent read-only topology access, use Data.Graph's View instead. Use AddLine (not the embedded SetLine) to maintain the ordered line index.

func New

func New(data *Data) *Graph

New creates an empty mutable Graph using data's identity space. It does not add any nodes or lines.

func (*Graph) AddLine added in v0.12.0

func (g *Graph) AddLine(l *Line)

AddLine inserts a previously absent line into Gonum and the ordered line index. It retains l itself (including Queries), rather than copying it. Its endpoints must be the nodes owned by this Graph. Use copyLine for fresh mutable copies.

func (*Graph) DOTAttributers

func (g *Graph) DOTAttributers() (graph, node, edge encoding.Attributer)

func (*Graph) DOTID

func (g *Graph) DOTID() string

func (*Graph) EachEdge added in v0.1.1

func (g *Graph) EachEdge(visit func(*Edge))

func (*Graph) EachLine

func (g *Graph) EachLine(visit func(*Line))

EachLine visits the graph's line objects in insertion order without allocating.

func (*Graph) EachLineBetween added in v0.8.0

func (g *Graph) EachLineBetween(start, goal *Node, visit func(l *Line))

EachLineBetween calls visit(l) for each line between start and goal (if there are any).

func (*Graph) EachLineFrom added in v0.8.0

func (g *Graph) EachLineFrom(start *Node, visit func(*Line))

EachLineFrom calls visit(l) for each line from start.

func (*Graph) EachLineTo added in v0.8.0

func (g *Graph) EachLineTo(goal *Node, visit func(*Line))

EachLineTo calls visit(l) for each line to goal.

func (*Graph) EachNode

func (g *Graph) EachNode(visit func(*Node))

func (*Graph) FindLine added in v0.8.3

func (g *Graph) FindLine(start, goal korrel8r.Class, rule korrel8r.Rule) *Line

FindLine finds a line between start and goal with rule. Nil if not found.

func (*Graph) LineStrings added in v0.8.4

func (g *Graph) LineStrings() (lines []string)

func (*Graph) NodeFor

func (g *Graph) NodeFor(c korrel8r.Class) *Node

NodeFor returns this Graph's node for c, or nil if it is not in this Graph. Data.NodeID instead looks up topology membership without creating a Node.

func (*Graph) NodeForErr added in v0.8.1

func (g *Graph) NodeForErr(c korrel8r.Class) (*Node, error)

func (*Graph) NodeStrings added in v0.8.4

func (g *Graph) NodeStrings(sorted bool) (nodes []string)

func (*Graph) RemoveEmpty added in v0.8.4

func (g *Graph) RemoveEmpty()

Remove empty nodes and lines from the graph.

func (*Graph) RemoveEmptyGoalPaths added in v0.10.0

func (g *Graph) RemoveEmptyGoalPaths(goals []korrel8r.Class)

RemoveEmptyGoalPaths removes nodes not connected to a non-empty goal.

func (*Graph) Select

func (g *Graph) Select(keep func(*Line) bool) *Graph

Select creates a mutable sub-graph of all lines where keep(line) is true.

func (*Graph) Weight added in v0.8.4

func (g *Graph) Weight(u, v int64) (w float64, ok bool)

Weight an edge by the "spread" of its rules.

Wildcard rules in domains with many classes (e.g. k8s, DependentToOwner) create many speculative graph lines, but following these lines often leads nowhere. The weight of an edge is the spread of its least expensive rule. In other words consider an edge expensive if it only has expensive rules.

type Line

type Line struct {
	multi.Line
	Attrs
	Rule    korrel8r.Rule
	Queries Queries // Queries generated by this rule for this pair of classes.
}

Line is a graph object for a topology line: one rule connecting a specific pair of class nodes. Its ID is assigned by Data, independent of its pointer identity. Searches and mutable Graphs own Lines and their Queries; View uses only IDs. Different rules (or repeated input rules) may connect the same pair of nodes.

func (*Line) Copy added in v0.11.2

func (l *Line) Copy(from, to *Node) *Line

Copy preserves line identity and rule, binds the supplied endpoints, and initializes fresh, empty mutable state. Queries and attributes are not copied.

func (*Line) DOTID

func (l *Line) DOTID() string

func (*Line) Goal added in v0.8.0

func (l *Line) Goal() *Node

func (*Line) Start added in v0.8.0

func (l *Line) Start() *Node

func (*Line) String

func (l *Line) String() string

type Node

type Node struct {
	multi.Node
	Attrs
	Class   korrel8r.Class
	Result  result.Result
	Queries Queries // Queries selecting objects in this class.
}

Node is a graph-owned object representing a class. A search-owned node holds mutable results, queries and display attributes. Read-only topology algorithms use View's lightweight node IDs instead of these mutable objects. Data stores class identity (node ID and class), not Node objects. Node IDs are meaningful within the Data that assigned them.

func (*Node) Copy added in v0.11.2

func (n *Node) Copy() *Node

Copy preserves identity but initializes fresh, empty mutable state. It does not copy accumulated results, queries or display attributes.

func (*Node) DOTID

func (n *Node) DOTID() string

func (*Node) Empty added in v0.1.1

func (n *Node) Empty() bool

func (*Node) String

func (n *Node) String(sorted bool) string

type Queries added in v0.1.2

type Queries map[korrel8r.Query]QueryCount

Queries holds mutable query accounting for a Node or Line, keyed by query identity. It belongs to a search or result Graph, never to immutable topology. Concurrent access requires synchronization by the owner.

func (Queries) AddStatuses added in v0.11.0

func (qs Queries) AddStatuses(q korrel8r.Query, statuses map[string]int)

AddStatuses merges status counts into the QueryCount for q.

func (Queries) Get added in v0.6.0

func (qs Queries) Get(q korrel8r.Query) int

func (Queries) Has added in v0.6.0

func (qs Queries) Has(q korrel8r.Query) bool

func (Queries) Set added in v0.6.0

func (qs Queries) Set(q korrel8r.Query, n int)

func (Queries) Total added in v0.1.2

func (qs Queries) Total() (total int)

Total sums positive object counts, excluding unevaluated queries.

type QueryCount

type QueryCount struct {
	Query        korrel8r.Query
	Count        int
	StatusCounts map[string]int
}

QueryCount records the number of objects resulting from a query. Count == -1 means the query has not been evaluated.

type Truncation added in v0.12.3

type Truncation struct {
	Condition string // Name of the exceeded limit.
	Limit     int    // Value of the exceeded limit.
}

Truncation records why a search stopped before exploring the whole graph.

type View added in v0.12.3

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

View adapts Data's compact topology to Gonum's read-only weighted directed graph interface. It owns no Node/Line objects or cached graph representation. Nodes are lightweight IDs, not result-bearing *Node objects. Parallel lines form one edge whose weight is the least rule spread, as in Graph.Weight. Views and Data can be read concurrently; each iterator has independent state.

func (View) Edge added in v0.12.3

func (g View) Edge(from, to int64) graph.Edge

func (View) From added in v0.12.3

func (g View) From(id int64) graph.Nodes

func (View) HasEdgeBetween added in v0.12.3

func (g View) HasEdgeBetween(x, y int64) bool

func (View) HasEdgeFromTo added in v0.12.3

func (g View) HasEdgeFromTo(from, to int64) bool

Edge lookups use Data's pre-computed edge index, so they do not scan the source node's adjacency. Shortest-path searches relax every edge repeatedly, so a scan here would cost O(out-degree) per relaxation.

func (View) Node added in v0.12.3

func (g View) Node(id int64) graph.Node

func (View) Nodes added in v0.12.3

func (g View) Nodes() graph.Nodes

func (View) To added in v0.12.3

func (g View) To(id int64) graph.Nodes

func (View) Weight added in v0.12.3

func (g View) Weight(from, to int64) (float64, bool)

Weight preserves Graph.Weight's zero self-weight (even for an absent node). A self-weight does not imply that a self-loop edge exists.

func (View) WeightedEdge added in v0.12.3

func (g View) WeightedEdge(from, to int64) graph.WeightedEdge

Jump to

Keyboard shortcuts

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