T
TypeDB•3y ago
David

is it possible to have transitive rules

is it possible to have transitive rules for certain verbs if all verb are the same type but i distinguish on id? in the "then" part of my transitivity rule, it won't let me have: (arg0: $x0, arg1: $x2) isa ul-verb, has id "IsA"; because i can't attach the id
15 Replies
krishnan
krishnan•3y ago
Could you post the whole rule to give some more context? You can't (yet) attach an attribute when inferring a relation , but you could potentially add it as a role-player instead.
David
DavidOP•3y ago
this is the rule I want to define, I need the output attribute =/
define
rule whatever: when {
$x0 isa ul-entity;
$x1 isa ul-entity;
$x2 isa ul-entity;
(arg0: $x0, arg1: $x1) isa ul-verb, has id "IsA";
(arg0: $x1, arg1: $x2) isa ul-verb, has id "IsSubclassOf";
} then {
(arg0: $x0, arg1: $x2) isa ul-verb, has id "IsA";
};
define
rule whatever: when {
$x0 isa ul-entity;
$x1 isa ul-entity;
$x2 isa ul-entity;
(arg0: $x0, arg1: $x1) isa ul-verb, has id "IsA";
(arg0: $x1, arg1: $x2) isa ul-verb, has id "IsSubclassOf";
} then {
(arg0: $x0, arg1: $x2) isa ul-verb, has id "IsA";
};
krishnan
krishnan•3y ago
Just for clarity: Is this in your current schema or if you were to model individual verbs as entities/relations? If it's in the approach where you're modelling verbs as instances rather than types, I'd suggest:
undefine
ul-verb owns id; # Move it to the verb entity

define
verb isa entity, owns id;
verb plays ul-verb:verb;

rule whatever: when {
$x0 isa ul-entity;
$x1 isa ul-entity;
$x2 isa ul-entity;
$isa isa verb, has id "IsA";
$isSubclassOf isa verb, has id "IsSubclassOf"
(arg0: $x0, verb: $isa, arg1: $x1) isa ul-verb;
(arg0: $x1, verb: $isSubclassOf, arg1: $x2) isa ul-verb;
} then {
(arg0: $x0, verb: $isa, arg1: $x2) isa ul-verb;
};
undefine
ul-verb owns id; # Move it to the verb entity

define
verb isa entity, owns id;
verb plays ul-verb:verb;

rule whatever: when {
$x0 isa ul-entity;
$x1 isa ul-entity;
$x2 isa ul-entity;
$isa isa verb, has id "IsA";
$isSubclassOf isa verb, has id "IsSubclassOf"
(arg0: $x0, verb: $isa, arg1: $x1) isa ul-verb;
(arg0: $x1, verb: $isSubclassOf, arg1: $x2) isa ul-verb;
} then {
(arg0: $x0, verb: $isa, arg1: $x2) isa ul-verb;
};
David
DavidOP•3y ago
hmm, very clever... i'd need to remove the "has id" bit at the end of the 2nd last line of the "when", right?
krishnan
krishnan•3y ago
Yes. I missed that
David
DavidOP•3y ago
very clever!! when i want to add a relation, can i do:
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
insert
$verb isa verb, has id "{verb_id}";
$r ($verb: verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
insert
$verb isa verb, has id "{verb_id}";
$r ($verb: verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
i've added id as @key on the verb; i only want one instance of a verb with a given id, right? will the insert here crash if the key is taken?
krishnan
krishnan•3y ago
Yes, it should.
David
DavidOP•3y ago
how can i avoid this?
krishnan
krishnan•3y ago
We plan to introduce 'optionals' in a coming release, which should solve this. Till then, you can try a modelling hack: If verb owns id as key, you could model the verb as the id attribute itself instead of an entity which owns id.
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
insert
$verb = "{verb_id}" isa id;
$r ($verb: verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
insert
$verb = "{verb_id}" isa id;
$r ($verb: verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
David
DavidOP•3y ago
thanks - i'm just keeping a local cache of verbs i've added. i have another problem now 😦 all my (small set of) data inserts fine, but when I go to query, the iterator "next" call never comes back and it just hangs:
def check_relation(rt, verb_id: str, arg0_id: str, arg1_id: str):
print('Checking relation')
res_iter = rt.query().match(f'''
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
$verb isa verb, has id "{verb_id}";
$r (verb: $verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
get $r;
''')
print('Got iter')
res = next(res_iter, None)
return True if res else False
def check_relation(rt, verb_id: str, arg0_id: str, arg1_id: str):
print('Checking relation')
res_iter = rt.query().match(f'''
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
$verb isa verb, has id "{verb_id}";
$r (verb: $verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
get $r;
''')
print('Got iter')
res = next(res_iter, None)
return True if res else False
it prints "Got iter" but never returns
krishnan
krishnan•3y ago
The iterator is lazy. It should only fetch the next result when you call next. Is there reasoning involved in this query? Could you try running the same query through console? (Also, should you be passing the format arguments for "{arg0_id}" and the others somewhere?
David
DavidOP•3y ago
inference is turned on. i'm calling next but it never returns. i'm passing the ids in through the python function args
krishnan
krishnan•3y ago
Do you not need to specify it after the f'''<query>''' ? I'd have expected a :
res_iter = rt.query().match(f'''
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
$verb isa verb, has id "{verb_id}";
$r (verb: $verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
get $r;
'''.format({arg0_id: ... }))
res_iter = rt.query().match(f'''
match
$arg0 isa ul-entity, has id "{arg0_id}";
$arg1 isa ul-entity, has id "{arg1_id}";
$verb isa verb, has id "{verb_id}";
$r (verb: $verb, arg0: $arg0, arg1: $arg1) isa ul-verb;
get $r;
'''.format({arg0_id: ... }))
Either way, could you print out the query and try running it through console so we can isolate the problem to the server?
David
DavidOP•3y ago
it's a format string - the {syntax} pastes in the str of the named value the ids are just strings Hi, following on from yesterday, I did this query in TypeDB Studio and it never comes back:
$e isa ul-entity, has id "Brie";
$t isa ul-entity, has id "Cheese";
$v isa verb, has id "IsA";
$r (verb: $v, arg0: $e, arg1: $t) isa ul-verb;
get $r;
$e isa ul-entity, has id "Brie";
$t isa ul-entity, has id "Cheese";
$v isa verb, has id "IsA";
$r (verb: $v, arg0: $e, arg1: $t) isa ul-verb;
get $r;
krishnan
krishnan•3y ago
Do you have inference turned on or off? (I'm guessing on, given that we discussed a rule) Could you quickly confirm 1. if it runs fine with inference turned off. 2. How it performs when you don't specify $t? (i.e., comment out $t isa ul-entity, has id "Cheese";)

Did you find this page helpful?