Discussion Forum

Comparator attribute in rule

in grakn 1.6.2, I defined a rule like below:

define
test1 sub rule,
when {
$equ1 isa equipment;
$equ2 isa equipment;
$equ1 has unit $u1;
$equ2 has unit $u2;
$u1==$u2;
}, then {
(inc: $equ1, con: $equ2) isa link;
};

when update to typeDB2.3.2,I defined:

define
rule test_1:
when {
$equ1 isa equipment;
$equ2 isa equipment;
$equ1 has unit $u1;
$equ2 has unit $u2;
$u1==$u2;
} then {
(inc: $equ1, con: $equ2) isa link;
};

[TQL03] TypeQL Error: There is a syntax error at line 8:
$u1==$u2;
^
mismatched input ‘$u1’ expecting {‘match’, ‘define’, ‘undefine’, ‘insert’, ‘compute’}

how to define this rule correctly?

Hi
I tried to follow your thoughts. Maybe I’m wrong but if you have two equipment pieces it means they are different. So it isn’t possible that these two are the same. This is logically my first thought. Syntactically the equality will be written only with one equals sign. But this is only technic.

Best, Frank

Thank you, I defined rules successfully.
The rule like below:

define
rule test_1:
when {
$equ1 isa equipment;
$equ2 isa equipment;
$equ1 has sblx “glkg”;
$equ1 has jgdylx “34”;
$equ1 has xdwz “xl”;
$equ1 has jgdy $jgdy1;
$equ2 has sblx “dlq”;
$equ2 has jgdy $jgdy2;
$jgdy1=$jgdy2;
} then {
(upstream: $equ1, downstream: $equ2) isa link;
};

However,after I inserted data, no relation has been built.

match $l (upstream: $equ1, downstream: $equ2) isa link; get $equ1; offset 0; limit 30000;

answers: 0, duration: 3 ms

I can get data by match query like:

match $equ1 isa equipment;
$equ2 isa equipment;
$equ1 has sblx “glkg”;
$equ1 has jgdylx “34”;
$equ1 has xdwz “xl”;
$equ1 has jgdy $jgdy1;
$equ2 has sblx “dlq”;
$equ2 has jgdy $jgdy2;
$jgdy1=$jgdy2; get $equ1; offset 0; limit 30000;

{ $equ1 iid 0x966e8001800000000000006c isa equipment; }
{ $equ1 iid 0x966e8001800000000000000e isa equipment; }
{ $equ1 iid 0x966e80018000000000000080 isa equipment; }

Hi,
Would it be possible to get a schema file and a little data file so can experiment a little bit with you environment. So I can only guess what you try to achieve and this is very time consuming.
One thought I have: Where you work with this query. Do you use a client or do you work with Workbase?

Best, Frank

My orerating system is Windows 10, typedb-all-linux-2.3.2 has installed on my vitrual machine with ubuntu 16.04.

a simple demo like below:

equtestschema.tql

define equ_name sub attribute,
value string;

define sblx sub attribute,
value string;

define jgdy sub attribute,
value string;

define jgdylx sub attribute,
value string;

define xdwz sub attribute,
value string;

define link sub relation,
relates upstream,
relates downstream;

define equipment sub entity,
owns equ_name,
owns sblx,
owns jgdy,
owns jgdylx,
owns xdwz,
plays link:upstream,
plays link:downstream;

define
rule dlq_1:
when {
$equ1 isa equipment;
$equ2 isa equipment;
$equ1 has sblx “glkg”;
$equ1 has jgdylx “34”;
$equ1 has xdwz “xl”;
$equ1 has jgdy $jgdy1;
$equ2 has sblx “dlq”;
$equ2 has jgdy $jgdy2;
$jgdy1=$jgdy2;
} then {
(upstream: $equ1, downstream: $equ2) isa link;
};

equtestdata.tql

insert

$equ-1 isa equipment, has equ_name “02dlq”, has sblx “dlq”, has jgdy “kq02”, has jgdylx “34”;
$equ-2 isa equipment, has equ_name “021glkg”, has sblx “glkg”, has jgdy “kq02”, has jgdylx “34”, has xdwz “xl”;

I operated in typeDB console:

database create equtest2
Database ‘equtest2’ created
transaction equtest2 schema write
equtest2::schema::write> source /home/celie/typeDB-2.3.2/typedb-all-linux-2.3.2/equtestschema.tql
Concepts have been defined
Concepts have been defined
Concepts have been defined
Concepts have been defined
Concepts have been defined
Concepts have been defined
Concepts have been defined
Concepts have been defined
equtest2::schema::write> commit
Transaction changes committed
database schema equtest2
define

equ_name sub attribute,
value string;

jgdy sub attribute,
value string;

jgdylx sub attribute,
value string;

sblx sub attribute,
value string;

xdwz sub attribute,
value string;

link sub relation,
relates downstream,
relates upstream;

equipment sub entity,
owns equ_name,
owns jgdy,
owns jgdylx,
owns sblx,
owns xdwz,
plays link:downstream,
plays link:upstream;

rule dlq_1:
when
{
$equ1 isa equipment;
$equ2 isa equipment;
$equ1 has sblx “glkg”;
$equ1 has jgdylx “34”;
$equ1 has xdwz “xl”;
$equ1 has jgdy $jgdy1;
$equ2 has sblx “dlq”;
$equ2 has jgdy $jgdy2;
$jgdy1 = $jgdy2;
}
then
{
(upstream: $equ1, downstream: $equ2) isa link;
};

transaction equtest2 data write
equtest2::data::write> source /home/celie/typeDB-2.3.2/typedb-all-linux-2.3.2/equtestdata.tql
{ $_0 “02dlq” isa equ_name; $_1 “dlq” isa sblx; $_2 “kq02” isa jgdy; $_3 “34” isa jgdylx; $_4 “021glkg” isa equ_name; $_5 “glkg” isa sblx; $_6 “kq02” isa jgdy; $_7 “34” isa jgdylx; $_8 “xl” isa xdwz; $equ-1 iid 0x966e80017fffffffffffffff isa equipment; $equ-2 iid 0x966e80017ffffffffffffffe isa equipment; }
answers: 1, duration: 118 ms
equtest2::data::write> commit
Transaction changes committed
transaction equtest2 data read
equtest2::data::read> match $link (upstream: $equ1, downstream: $equ2) isa link; get $equ1; offset 0; limit 30000;

answers: 0, duration: 84 ms
equtest2::data::read> match $equ1 isa equipment;
$equ2 isa equipment;
$equ1 has sblx “glkg”;
$equ1 has jgdylx “34”;
$equ1 has xdwz “xl”;
$equ1 has jgdy $jgdy1;
$equ2 has sblx “dlq”;
$equ2 has jgdy $jgdy2;
$jgdy1=$jgdy2; get $equ1; offset 0; limit 30000;

{ $equ1 iid 0x966e80018000000000000000 isa equipment; }
answers: 1, duration: 10 ms

Remember, to allow rules to trigger you have to enable reasoning. In console you do this when opening a transaction:

> transaction <db> data read --infer true

By the way you can shorten this as:

$equ1 isa equipment, has sblx "glgk", has jgdylx "34", has xdwz "xl", has jgdy $jgdy;
$equ2 isa equipment, has sblx "dlq", has jgdy $jgdy;