graph

package
v0.0.0-...-8b2ea39 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultEdgeLabel is the default edge label.
	DefaultEdgeLabel = "HypherEdge"
	// DefaultEdgeWeight is the default edge weight.
	DefaultEdgeWeight = 1.0
)
View Source
const (
	// DefaultNodeLabel is the default node label.
	DefaultNodeLabel = "HypherNode"
	// NoneID is non-existent ID.
	// Thanks Go for not having optionals!
	NoneID int64 = -1
)
View Source
const (
	// DefaultGraphLabel is the default label.
	DefaultGraphLabel = "HypherGraph"
)

Variables

This section is empty.

Functions

func AttrsToStringMap

func AttrsToStringMap(a map[string]any) map[string]string

AttrsToStringMap attempts to convert a map to a map of strings. It first checks if the stored attribute value is stringly i.e. either of string, fmt.Stringer or fmt.GoStringer. If it is it returns its stringe representation. If the attribute value is not stringly we attempt to convert well known attributes to strings. If the attribute is neither stringly nor is it known how to convert it to a string the attribute is omitted from the returned map.

Types

type Edge

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

Edge is a weighted graph edge.

func EdgeDeepCopy

func EdgeDeepCopy(e *Edge) *Edge

EdgeDeepCopy makes a deep copy of Edge and returns it

func NewEdge

func NewEdge(from, to hypher.Node, opts ...hypher.Option) (*Edge, error)

NewEdge creates a new edge and returns it.

func (*Edge) Attributes

func (e *Edge) Attributes() []encoding.Attribute

Attributes returns node DOT attributes.

func (*Edge) Attrs

func (e *Edge) Attrs() map[string]any

Attrs returns node attributes.

func (*Edge) From

func (e *Edge) From() gonum.Node

From returns the from node of the first non-nil edge, or nil.

func (*Edge) Label

func (e *Edge) Label() string

Label returns edge label.

func (*Edge) ReversedEdge

func (e *Edge) ReversedEdge() gonum.Edge

ReversedEdge returns a new edge with end points of the pair swapped.

func (*Edge) SetLabel

func (e *Edge) SetLabel(l string)

SetLabel sets edge label.

func (*Edge) SetUID

func (e *Edge) SetUID(uid string)

SetUID sets edge UID.

func (*Edge) SetWeight

func (e *Edge) SetWeight(w float64)

SetWeight sets edge weight.

func (*Edge) String

func (e *Edge) String() string

String implements fmt.Stringer.

func (*Edge) To

func (e *Edge) To() gonum.Node

To returns the to node of the first non-nil edge, or nil.

func (*Edge) UID

func (e *Edge) UID() string

UID returns edge UID.

func (*Edge) Weight

func (e *Edge) Weight() float64

Weight returns edge weight

type Graph

type Graph struct {
	*simple.WeightedDirectedGraph
	// contains filtered or unexported fields
}

Graph is an in-memory graph.

func DeepCopy

func DeepCopy(g *Graph) *Graph

DeepCopy return s deep copy of a memory graph.

func NewGraph

func NewGraph(opts ...hypher.Option) (*Graph, error)

NewGraph creates a new graph and returns it.

func (*Graph) AddNode

func (g *Graph) AddNode(n *Node) error

AddNode adds a node to the graph or returns error. If the node's graph is the same as g it returns nil. Otherwise, it tries to preserve the ID of the node unless the ID is not set (NoneID) or a node with the same ID already exists in g, in which case a new ID is generated before the node is added to g. If node's graph is nil, it's set to g.

func (*Graph) Attributes

func (g *Graph) Attributes() []encoding.Attribute

Attributes returns graph DOT attributes.

func (*Graph) Attrs

func (g *Graph) Attrs() map[string]any

Attrs returns graph attributes. TODO: consider cloning these

func (*Graph) DOTAttributers

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

DOTAttributers are graph.Graph values that specify top-level DOT attributes TODO: figure out node and edge top level attributes

func (*Graph) DOTID

func (g *Graph) DOTID() string

DOTID returns GraphVIz DOT ID.

func (*Graph) HasEdgeFromTo

func (g *Graph) HasEdgeFromTo(uid, vid int64) bool

HasEdgeFromTo returns whether an edge exist between two nodoes with the given IDs.

func (*Graph) Inputs

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

Inputs returns graph input nodes. TODO: consider cloning inputs.

func (*Graph) Label

func (g *Graph) Label() string

Label returns graph label.

func (*Graph) NewEdge

func (g *Graph) NewEdge(from, to hypher.Node, opts ...hypher.Option) (*Edge, error)

NewEdge creates a new edge link its node in the graph. It returns the new edge or fails with error.

func (*Graph) NewNode

func (g *Graph) NewNode(opts ...hypher.Option) (*Node, error)

NewNode creates a new node and adds it to the graph. It returns the new node or fails with error.

func (*Graph) Outputs

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

Outputs returns graph output nodes. TODO: consider cloning outputs

func (*Graph) Reset

func (g *Graph) Reset()

Reset resets node inputs and outputs.

func (*Graph) Run

func (g *Graph) Run(ctx context.Context, inputs map[string]hypher.Value, opts ...hypher.Option) error

Run runs the graph with the given inputs. The given inputs are set as inputs to the graph input nodes. Run executes all the graph nodes operations in the run mode passed via options. Run is a blocking call. It returns when the execution finished or if any of the executed nodes failed with error.

func (*Graph) SetDOTID

func (g *Graph) SetDOTID(dotid string)

SetDOTID sets GraphVIz DOT ID.

func (*Graph) SetEdge

func (g *Graph) SetEdge(e hypher.Edge) error

SetEdge adds the edge e to the graph linking the edge nodes. It adds the edge nodes to the graph if they don't already exist. It returns error if the new edge creates a graph cycle.

func (*Graph) SetInputs

func (g *Graph) SetInputs(nodes []*Node)

SetInputs sets graph input nodes.

func (*Graph) SetLabel

func (g *Graph) SetLabel(l string)

SetLabel sets label.

func (*Graph) SetOutputs

func (g *Graph) SetOutputs(nodes []*Node)

SetOutputs sets graph output nodes.

func (*Graph) SetUID

func (g *Graph) SetUID(uid string)

SetUID sets UID.

func (*Graph) String

func (g *Graph) String() string

String implements fmt.Stringer.

func (*Graph) SubGraph

func (g *Graph) SubGraph(inputNodes, outputNodes Nodes) (*Graph, error)

SubGraph returns a sub-graph of g which contains all the nodes which are either outputNodes or are on the path to the outputNodes when starting the graph traversal in inputNodes, including the inputNodes.

func (*Graph) To

func (g *Graph) To(id int64) gonum.Nodes

To returns all nodes that can reach directly to the node with the given ID.

func (*Graph) TopoSort

func (g *Graph) TopoSort() ([]gonum.Node, error)

TopoSort performs a topological sort of the graph. It returns nodes sorted in ascending order of their incoming edge counts (i.e. in degree) starting with nodes with zero incoming edges aka "roots". If there isn't at least one node with zero incoming edge the graph must ehter have a cycle, or have no nodes, so an empty slice is returned.

func (*Graph) TopoSortWithLevels

func (g *Graph) TopoSortWithLevels() ([][]gonum.Node, error)

TopoSortWithLevels does the same topological sort as TopoSort, but groups the sorted nodes to graph levels.

func (*Graph) UID

func (g *Graph) UID() string

UID returns graph UID.

type NoOp

type NoOp struct{}

NoOp is a no-op Op.

func (NoOp) Desc

func (op NoOp) Desc() string

func (NoOp) Do

func (op NoOp) Do(_ context.Context, _ ...hypher.Value) ([]hypher.Value, error)

func (NoOp) String

func (op NoOp) String() string

func (NoOp) Type

func (op NoOp) Type() string

type Node

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

Node is a graph node.

func NewNode

func NewNode(opts ...hypher.Option) (*Node, error)

NewNode creates a new Node and returns it.

func NodeDeepCopy

func NodeDeepCopy(n *Node) *Node

NodeDeepCopy makes a deep copy of Node and returns it. It does not copy node inputs or outputs.

func (*Node) Attributes

func (n *Node) Attributes() []encoding.Attribute

Attributes returns node DOT attributes.

func (*Node) Attrs

func (n *Node) Attrs() map[string]any

Attrs returns node attributes.

func (*Node) Clone

func (n *Node) Clone() (*Node, error)

Node clones a node and returns it. The cloned node has a new UID. The node ID is reset to NoneID. The graph is not copied to the cloned node. No inputs or outputs are copied either.

func (*Node) CloneTo

func (n *Node) CloneTo(g *Graph) (*Node, error)

CloneTo clones a node to graph g. The cloned node has a new UID even if g is the same as n.Graph().

func (*Node) DOTID

func (n *Node) DOTID() string

DOTID returns GraphViz DOT ID.

func (*Node) Exec

func (n *Node) Exec(ctx context.Context, inputs ...hypher.Value) ([]hypher.Value, error)

Exec executes a node Op and returns its result. It appends the output of the Op to its outputs.

func (*Node) Graph

func (n *Node) Graph() hypher.Graph

Graph returns the node graph.

func (*Node) ID

func (n *Node) ID() int64

ID returns node ID.

func (*Node) Inputs

func (n *Node) Inputs() []hypher.Value

Inputs return node inputs.

func (*Node) Label

func (n *Node) Label() string

Label returns node label.

func (*Node) Op

func (n *Node) Op() hypher.Op

Op returns node Op.

func (*Node) Outputs

func (n *Node) Outputs() []hypher.Value

Outputs returns node outputs.

func (*Node) Reset

func (n *Node) Reset()

Reset resets node inputs and outputs.

func (*Node) SetDOTID

func (n *Node) SetDOTID(dotid string)

SetDOTID sets GraphVIz DOT ID.

func (*Node) SetInputs

func (n *Node) SetInputs(inputs ...hypher.Value) error

SetInputs sets the node inputs.

func (*Node) SetLabel

func (n *Node) SetLabel(l string)

SetLabel sets node label.

func (*Node) SetUID

func (n *Node) SetUID(uid string)

SetUID sets UID.

func (*Node) String

func (n *Node) String() string

String implements fmt.Stringer.

func (*Node) UID

func (n *Node) UID() string

UID returns node UID.

type Nodes

type Nodes []*Node

Nodes is a slice of Nodes.

Directories

Path Synopsis
marshal
dot

Jump to

Keyboard shortcuts

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