Categorygithub.com/cloudprivacylabs/lpg/v2
modulepackage
2.0.2
Repository: https://github.com/cloudprivacylabs/lpg.git
Documentation: pkg.go.dev

# README

GoDoc Go Report Card Build Status

Labeled property graphs

This labeled property graph package implements the openCypher model of labeled property graphs. A labeled property graph (LPG) contains nodes and directed edges between those nodes. Every node contains:

  • Labels: Set of string tokens that usually identify the type of the node,
  • Properties: Key-value pairs.

Every edge contains:

  • A label: String token that identifies a relationship, and
  • Properties: Key-value pairs.

A Graph objects keeps an index of the nodes and edges included in it. Create a graph using NewGraph function:

g := lpg.NewGraph()
// Create two nodes
n1 := g.NewNode([]string{"label1"},map[string]any{"prop": "value1" })
n2 := g.NewNode([]string{"label2"},map[string]any{"prop": "value2" })
// Connect the two nodes with an edge
edge:=g.NewEdge(n1,n2,"relatedTo",nil)

The LPG library uses iterators to address nodes and edges.

for nodes:=graph.GetNodes(); nodes.Next(); {
  node:=nodes.Node()
}
for edges:=graph.GetEdges(); edges.Next(); {
  edge:edges.Edge()
}

Every node knows its adjacent edges.

// Get outgoing edges
for edges:=node1.GetEdges(lpg.OutgoingEdge); edges.Next(); {
  edge:=edges.Edge
}

// Get all edges
for edges:=node1.GetEdges(lpg.AnyEdge); edges.Next(); {
  edge:=edges.Edge
}

The graph indexes nodes by label, so access to nodes using labels is fast. You can add additional indexes on properties:

g := lpg.NewGraph()
// Index all nodes with property 'prop'
g.AddNodePropertyIndex("prop")

// This access should be fast
nodes := g.GetNodesWithProperty("prop")

// This will go through all nodes
slowNodes:= g.GetNodesWithProperty("propWithoutIndex")

Pattern Searches

Graph library supports searching patterns within a graph. The following example searches for the pattern that match

(:label1) -[]->({prop:value})`

and returns the head nodes for every matching path:

pattern := lpg.Pattern{ 
 // Node containing label 'label1'
 {
   Labels: lpg.NewStringSet("label1"),
 },
 // Edge of length 1
 {
   Min: 1, 
   Max: 1,
 },
 // Node with property prop=value
 {
   Properties: map[string]interface{} {"prop":"value"},
 }}
nodes, err:=pattern.FindNodes(g,nil)

Variable length paths are supported:

pattern := lpg.Pattern{ 
 // Node containing label 'label1'
 {
   Labels: lpg.NewStringSet("label1"),
 },
 // Minimum paths of length 2, no maximum length
 {
   Min: 2, 
   Max: -1,
 },
 // Node with property prop=value
 {
   Properties: map[string]interface{} {"prop":"value"},
 }}

JSON Encoding

This graph library uses the following JSON representation:

{
  "nodes": [
     {
       "n": 0,
       "labels": [ "l1", "l2",... ],
       "properties": {
          "key1": value,
          "key2": value,
          ...
        },
        "edges": [
           {
             "to": "1",
             "label": "edgeLabel",
             "properties": {
               "key1": value,
               "key2": value,
               ...
             }
           },
           ...
        ]
     },
      ...
  ],
  "edges": [
     {
        "from": 0,
        "to": 1,
        "label": "edgeLabel",
        "properties": {
           "key1": value1,
           "key2": value2,
           ...
        }
     },
     ...
  ]
}

All graph nodes are under the nodes key as an array. The n key identifies the node using a unique index. All node references in edges use these indexes. A node may include all outgoing edges embedded in it, or edges may be included under a separate top-level array edges. If the edge is included in the node, the edge only has a to field that gives the target node index as the node containing the edge is assumed to be the source node. Edges under the top-level edges array include both a from and a to index.

Standard library JSON marshaler/unmarshaler does not work with graphs, because the edge and node property values are of type any. The JSON struct can be used to marshal and unmarshal graphs with custom property marshaler and unmarshalers.

This Go module is part of the Layered Schema Architecture.

# Functions

CheckIsomoprhism checks to see if graphs given are equal as defined by the edge equivalence and node equivalence functions.
No description provided by the author
CollectAllPaths iterates the variable length paths that have the edges in firstLeg.
ComparePropertyValue compares a and b.
CopyEdge copies the edge into graph.
CopyGraph copies source graph into target, using clonePropertyFunc to clone properties.
CopyGraphf copies source graph into target, using the copeNodeFunc func to clone nodes.
CopyNode copies the sourceNode into target graph.
CopySubgraph copies all nodes that are accessible from sourceNode to the target graph.
DefaultDOTEdgeRender renders the edge with a label if there is one, or without a label if there is not a label.
DefaultDOTNodeRender renders the node with the given ID.
EdgesBetweenNodes finds all the edges that go from 'from' to 'to'.
EdgeSlice reads all the remaining items of an edge iterator and returns them in a slice.
ForEachNode iterates through all the nodes of g until predicate returns false or all nodes are processed.
GetEdgeFilterFunc returns a function that can be used to select edges that have at least one of the specified labels, with correct property values.
GetNodeFilterFunc returns a filter function that can be used to select nodes that have all the specified labels, with correct property values.
MultiIterator returns an iterator that contatenates all the given iterators.
No description provided by the author
NewGraph constructs and returns a new graph.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NextNodesWith returns the nodes reachable from source with the given label at one step.
NodeSlice reads all the remaining items of a node iterator and returns them in a slice.
PathFromNode creates a path containing a single node.
PrevNodesWith returns the nodes reachable from source with the given label at one step.
Sinks finds all the sink nodes in the graph.
Sinks finds all the sink nodes in the graph.
SourceNodes returns the source nodes of all edges.
Sources finds all the source nodes in the graph.
Sources finds all the source nodes in the graph.
TargetNodes returns the target nodes of all edges.

# Constants

Incoming and outgoing edge direction constants.
No description provided by the author
No description provided by the author
Incoming and outgoing edge direction constants.
Incoming and outgoing edge direction constants.

# Structs

Cursor is a convenience class to move around a graph.
No description provided by the author
DOTRenderer renders a graph in Graphviz dot format.
An Edge connects two nodes of a graph.
EdgeSet keeps an unordered set of edges.
No description provided by the author
A Graph is a labeled property graph containing nodes, and directed edges combining those nodes.
JSON marshals/unmarshals a graph to/from JSON.
No description provided by the author
No description provided by the author
A Node represents a graph node.
An NodeMap stores nodes indexed by node labels.
No description provided by the author
A Path can be a node, or node-edge-node...-edge-node sequence.
No description provided by the author
A PatternItem can be a node or an edge element of a pattern.
A PatternSymbol contains either nodes, or edges.
No description provided by the author

# Interfaces

EdgeIterator iterates the edges of an underlying list.
Interner is a string interface, that can be as simple as a map[string]string, that is used to intern property keys.
An Iterator iterates the items of a collection.
No description provided by the author
NodeIterator iterates nodes of an underlying list.
WithNativeValue is used to return a native value for property values.
No description provided by the author

# Type aliases

EdgeDir is used to show edge direction.
No description provided by the author
No description provided by the author
No description provided by the author
MapInterner is a basic interner that uses a map[string]string to intern strings.
Pattern contains pattern items, with even numbered elements corresponding to nodes, and odd numbered elements corresponding to edges.