cayleygraph / cayley

An open-source graph database
https://cayley.io
Apache License 2.0
14.8k stars 1.25k forks source link

Filter path by Label field of quads #976

Closed paralin closed 1 year ago

paralin commented 1 year ago

How can I filter a Path in Go such that only quads with a Label field matching a given value are traversed?

Example:

    store, err := cayley.NewMemoryGraph()

    // Add some quads to the graph
    store.AddQuad(quad.MakeIRI("node1", "predicate", "node2", ""))
    store.AddQuad(quad.MakeIRI("node1", "predicate", "node3", "myvalue"))
    store.AddQuad(quad.MakeIRI("node1", "predicate", "node4", "othervalue"))

        // TODO: How do I filter to only the quad with "myvalue" here?
    p := cayley.
        StartPath(store, quad.IRI("node1")).
        Out(quad.IRI("predicate"))
    it := p.Iterate(nil)
    err = it.EachValue(nil, func(value quad.Value) error {
        nativeValue := quad.NativeOf(value) // this converts RDF values to normal Go types
        fmt.Println(nativeValue)
        return nil
    })
    if err != nil {
        fmt.Println(err.Error())
    }
}

Thanks.

paralin commented 1 year ago

Answer: use LabelContext

    p := cayley.
        StartPath(store, quad.IRI("node1")).
        LabelContext(quad.IRI("myvalue")).
        Out(quad.IRI("predicate"))

Glad I was able to solve it :)