Query to extract edges directly using go

golang

#1

Hi,

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 (
	"github.com/cayleygraph/cayley"
	"github.com/cayleygraph/cayley/quad"
	"context"
	"github.com/cayleygraph/cayley/graph"
	"fmt"
)

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().
		Tag("SUB").
		OutWithTags([]string{"PRED"}, quad.IRI("has_friend")).
		Tag("OBJ").
		Unique()

	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.


#2

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?