vaticle-typedbvaticle-typeql

Specifying dynamic number of roles in n-ary relation


I am aware that Grakn allows us to specify n-ary edges/relations. But, can they allow us to specify many-to-1 relationships when we don't know the size of "many" beforehand?

For example:

If I want to connect two entities 'a' and 'b' to entity 'c', I can do that. But, what if I don't know before hand how many entities I would want to connect to 'c'. What if I wanted to keep that dynamic? (sometimes connect 2 entities to 1 or 4 entities to 1) Does Grakn allow me to represent that?

I hope I have been able to describe my question clearly.

Please let me know. Thank you.


Solution

  • In Grakn, these many-to-one connections (where the "many" is dynamic) can be stored as the relation instances themselves.

    For example, suppose you have a family with a parent who has some children, but we don't know how many there are, and new children can be added at any time.

    Then you would

    define
    
    name sub attribute, datatype string;
    person sub entity, has name, plays parent, plays child;
    parenthood sub relation, relates parent, relates child;
    

    Now suppose you have matched four people, $a, $b, $c and $d, then you can make a 2-to-1 relationship by inserting two parenthood instances:

    insert (parent: $a, child: $b) isa parenthood;
    insert (parent: $a, child: $c) isa parenthood;
    

    and it becomes a 3-to-1 relationship when you insert a third parenthood:

    insert (parent: $a, child: $d) isa parenthood;
    

    and so on.