Query to extract edges directly using go




I’m using cayley embedded in a go application. So far its worked out really nicely, however i’m not sure if my method for extracting a graph using the go API is correct, it works, but doesn’t feel quite right.

Below is a minimal example to demonstrate how i’m querying the graph to extract edges.

package main

import (

type edge struct {
	Src  string
	Dest string
	Type string

func main() {
	edges := make(map[string]edge)
	store,_ := cayley.NewGraph("memstore", "", nil)

	store.AddQuad(quad.Make(quad.IRI("bob"), quad.IRI("has_friend"), quad.IRI("sam"), ""))
	store.AddQuad(quad.Make(quad.IRI("bob"), quad.IRI("has_friend"), quad.IRI("sarah"), ""))
	store.AddQuad(quad.Make(quad.IRI("sarah"), quad.IRI("has_friend"), quad.IRI("lilly"), ""))

	morph := cayley.StartMorphism().
		OutWithTags([]string{"PRED"}, quad.IRI("has_friend")).

	p := cayley.StartPath(store, quad.IRI("bob")).FollowRecursive(morph, 2, nil).Iterate(context.TODO())
	p.Paths(true).TagEach(func(tags map[string]graph.Value) {

		s := store.NameOf(tags["SUB"]).String()
		p := store.NameOf(tags["PRED"]).String()
		o := store.NameOf(tags["OBJ"]).String()

		id := fmt.Sprintf("%s:%s:%s", s, p, o)
		e := edge{
			Src:  s,
			Dest: o,
			Type: p,
		edges[id] = e

	for _,e := range edges {
		fmt.Printf("%s -> %s -> %s\n", e.Src, e.Type, e.Dest)

Expected output:

<bob> -> <has_friend> -> <sam>
<bob> -> <has_friend> -> <sarah>
<sarah> -> <has_friend> -> <lilly>

I can easily insert quads into cayley, but to get a quad out seems a bit convoluted (using my method anyway).

In my code the morphism is using tags to mark the subject, predicate and object and then the TagEach function to iterate through each map of tags to construct the edges of the graph.

I’m just looking for some guidance on how this approach looks. To extract a graph (in this case just some edges) would it be written this way, or have I overlooked a simpler or more robust method?

Any suggestions you have are welcome, cheers.


I too am interested in extracting quads of particular nodes.

I’d like to construct a temporal graph, which means either using the quad predicate or label to store the times of availability and then filtering quads based on some condition.

For this I’d like to get edges incident to a node and iterate through them.

Given the fact we have to identify the quads to find the resulting ‘object’ nodes for our ‘subject’ node it seems sensible to think that recreating the quads as in the above example is redundant.

Any insight on how to get these quads more directly?


I came up with a different convoluted method and posted it: Filter by label (via Path or Shape)

I hope there is an easier way and I’m just missing it.


The best way to extract edges is to use shape package, as opposed to paths.

Here is an example:

// shape that contains quad filters, you can add more or remove some/all
quads := shape.Quads{
  {Dir: quad.Subject, Nodes: shape.Lookup{someNode}},
  {Dir: quad.Predicate, Nodes: shape.Lookup{somePred}},
  {Dir: quad.Label, Nodes: shape.Lookup{label1, label2}}, // logic for Lookup is OR
it := shape.BuildIterator(quads, qs) // gives a quad iterator