Reputation: 117
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.
Upvotes: 3
Views: 174
Reputation: 920
I think you don't necessarily want to do exactly as you described, adding a new role would require dynamically changing the schema. Possible but more likely you want to dynamically choose the number of role players. It's worth clarifying that a role can be played many times within the same relation instance.
Let's re-use Alex Walker's parenthood
example. He creates a new relation instance for each parenthood
relation. This is a perfectly valid and encouraged approach.
The alternative approach is to have an unknown number of parents and children in the same relation. The schema remains exactly the same, but you are able to add data in this way:
match [ do matching for $a, $b, $c, $d ]
insert (parent: $a, child: $b, child: $c, child: $d) isa parenthood;
The two approaches have different semantic meanings. In this second case there is a single relation connecting the parent to their three children. Modelling with a single relation says that the combination of role players are necessary for the relation.
In the parenthood
case, the 3 children are not prerequisite, but two parents are prerequisite to having a child (most of the time):
match [ do matching for $p1, $p2, $c1 ]
insert (parent: $p1, parent: $p2, child: $c1) isa parenthood;
If those two parents have another child, you have two options. Either create a new relation between the two parents and the new child (approach 1), or add the new child to the existing relation (approach 2). The latter you can do like this:
match
[ do matching for $p1, $p2, $c1, $c2 ]
$ph(parent: $p1, parent: $p2, child: $c1) isa parenthood;
insert
$ph(child: $c2) isa parenthood;
The one relation will then look like
$ph(parent: $p1, parent: $p2, child: $c1, child: $c2) isa parenthood;
Notice the use of a variable $ph
to match for an existing relation and add a role player to it.
Upvotes: 1
Reputation: 2356
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.
Upvotes: 1