my model consists of organizations that have projects and the projects have buckets. The buckets can be shared to other projects in the same organization. I defined a rule for project sharing which means that two projects are in this relation when first is sharing a bucket with the other.
define
organization sub entity,
plays organizationRole,
key identifier;
project sub entity,
plays projectRole,
plays projectSourceRole,
plays projectTargetRole,
plays transitiveProjectSourceRole,
plays transitiveProjectTargetRole,
key identifier;
bucket sub entity,
plays bucketRole,
plays sharedBucketSourceRole,
plays sharedBucketTargetRole,
key identifier;
organizationToProject sub relation,
relates organizationRole,
relates projectRole;
projectToBucket sub relation,
relates projectRole,
relates bucketRole;
sharedBucket sub relation,
relates sharedBucketSourceRole,
relates sharedBucketTargetRole;
projectSharing sub relation,
relates projectSourceRole,
relates projectTargetRole;
project-sharing sub rule,
when {
(projectRole: $ps, bucketRole: $bs) isa projectToBucket;
(projectRole: $pt, bucketRole: $bt) isa projectToBucket;
(sharedBucketSourceRole: $bs, sharedBucketTargetRole: $bt) isa sharedBucket;
$ps != $pt;
}, then {
(projectSourceRole: $ps, projectTargetRole: $pt) isa projectSharing;
};
It works fine. (see )
But I want to define a rule for transitive relation (according to some ancestry example I found in the docs) so I added to the schema:
transitiveProjectSharing sub relation,
relates transitiveProjectSourceRole,
relates transitiveProjectTargetRole;
transitive-project-sharing sub rule,
when {
(projectSourceRole: $a, projectTargetRole: $b) isa projectSharing;
(projectSourceRole: $b, projectTargetRole: $c) isa projectSharing;
}, then {
(transitiveProjectSourceRole: $a, transitiveProjectTargetRole: $c) isa transitiveProjectSharing;
};
It finds nothing (but there are several transitive ones, see )
Would anyone have a suggestion where is the mistake?
In mathematics, a relation R on a set X is transitive if, for all elements a, b, c in X, whenever R relates a to b and b to c, then R also relates a to c.
Transitive relations are binary relations defined on a set such that if the first element is related to the second element, and the second element is related to the third element of the set, then the first element must be related to the third element.
Say we have the relation R on the set {a,b}. Then provided aRa, bRb, aRb AND bRa, so that R{(a,a),(b,b),(a,b),(b,a)}, then R is reflexive, and symmetric, and must therefore be transitive, given there are only two elements. This is what really clarified it for me!
Ans. 3 Transitive relations are binary relations in set theory that are defined on a set X such that component 'p' must be associated to element 'r', if 'p' is related to 'q' and 'q' is related to 'r', for p, q, r in X. Q.
I've got a hint from Grakn.ai Slack. The transitivity should be defined on the projectSharing
role itself, additional transitiveProjectSharing
is not necessary. So the definition should be:
transitive-project-sharing sub rule,
when {
(projectSourceRole: $a, projectTargetRole: $b) isa projectSharing;
(projectSourceRole: $b, projectTargetRole: $c) isa projectSharing;
}, then {
(projectSourceRole: $a, projectTargetRole: $c) isa projectSharing;
};
and the query works well:
As a slight improvement over your awesomely elegant answer (thanks!!), you can also use subtyping to differentiate explicit relationships from inferred relationships:
indirectProjectSharing sub projectSharing;
transitive-project-sharing sub rule,
when {
(projectSourceRole: $a, projectTargetRole: $b) isa projectSharing;
(projectSourceRole: $b, projectTargetRole: $c) isa projectSharing;
}, then {
(projectSourceRole: $a, projectTargetRole: $c) isa indirectProjectSharing;
};
Since indirectProjectSharing
is a subtype of projectSharing
, the rule's condition will match both relations, and you'll be able to refer to either type in your own queries depending on the use case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With