Search code examples
vaticle-typedbvaticle-typeql

Getting edge attributes from path in "compute path"


I went through the example for "compute path" in your Documentation.

The result of the compute path is a list of nodes.

I wanted to access the edges that connect that the nodes and get attributes from that.

Is it possible to do that?

How would you recommend I do so?


Solution

  • Grakn is a hypergraph - it has hyperedges rather than edges. A Grakn hyperedge is called a relation. When you use compute count you get back a ConceptList. A relation is a type of Concept, and so you will find the IDs of the relations on the shortest path in this list.

    You can find them by iterating over the list. For each element you can take the id and make a query to get the Concepts from its id, along with its attributes:

    match $c id V12345; get;
    

    substituting V12345 for the id you find, of course.

    You have all sorts of options here. Once you have retrieved the Concept object for c you can check if the is a relation using concept.isRelation() (here) and then concept.attributes() (here) to get the attributes. Do this for each of the IDs in the ConceptList.

    Alternatively, you can do all of this with a single query:

    match $c id V12345; $c isa relation; $c has attribute $a; get;
    

    This will return no results if V12345 is not a relation, or if it has no attributes. If it is a relation and has attributes then you will get one answer per attribute.