嗨,我有以下问题。
在我的数据库中,有一些实体(需求)具有一组属性(名称、req类型、描述)。一个实体可以从另一个实体中“分支”。的想法是推断出子实体没有覆盖的所有属性都应该从主实体.中提取。
下面是现有的模式。
> database schema test-req-branch
define
description sub attribute,
value string;
name sub attribute,
value string;
req-type sub attribute,
value string;
req-branching sub relation,
relates branch-item,
relates branched-from;
requirement sub entity,
owns description,
owns name,
owns req-type,
plays req-branching:branch-item,
plays req-branching:branched-from;和虚拟数据
test-req-branch::data::read> match $e isa entity, has $a;
get $e, $a; group $e;
iid 0x826e80018000000000000001 isa requirement => {
{
$a "master" isa name;
$e iid 0x826e80018000000000000001 isa requirement;
}
{
$a "demo master" isa description;
$e iid 0x826e80018000000000000001 isa requirement;
}
{
$a "whatever" isa req-type;
$e iid 0x826e80018000000000000001 isa requirement;
}
}
iid 0x826e80018000000000000000 isa requirement => {
{
$a "other" isa name;
$e iid 0x826e80018000000000000000 isa requirement;
}
}
answers: 2, total (with concept details) duration: 12 ms
test-req-branch::data::read> match $r (branch-item:$bi, branched-from:$bf)isa req-branching;
$bf has name $bfn;
$bi has name $bin;
{
$r iid 0x847080018000000000000000 (branch-item: iid 0x826e80018000000000000000, branched-from: iid 0x826e80018000000000000001) isa req-branching;
$bf iid 0x826e80018000000000000001 isa requirement;
$bin "other" isa name;
$bi iid 0x826e80018000000000000000 isa requirement;
$bfn "master" isa name;
}
answers: 1, total (with concept details) duration: 8 ms如果主需求有(名称、req-类型、描述)和子实体"other“只有(名称),我想从主目录中推断req-类型和描述。
我设计了一个查询,它获取子实体没有的属性。
test-req-branch::data::read> match
$rr (branched-from:$rm, branch-item:$cr) isa req-branching;
$rm has $ma;
$cr has attribute $ca;
$ma isa! $t;
not {
$ca isa! $t;
};
get $ma;
{ $ma "demo master" isa description; }
{ $ma "whatever" isa req-type; }
answers: 2, total (with concept details) duration: 5 ms我把这个变成了一条规则:
rule when-not-defined-attributes-derive-from-parent:
when
{
$rr (branched-from: $rm, branch-item: $cr) isa req-branching;
$rm has $ma;
$cr has attribute $ca;
$ma isa! $t;
not
{
$ca isa! $t;
};
}
then
{
$cr has $ma;
};但是当我询问属性时,我也得到了附加在孩子身上的主人的名字。
> transaction test-req-branch data read --infer true
test-req-branch::data::read> match $rr (branched-from: $rm, branch-item: $cr) isa req-branching;
$cr has $a;
get $a;
{ $a "other" isa name; }
{ $a "whatever" isa req-type; }
{ $a "demo master" isa description; }
{ $a "master" isa name; }
answers: 4, total (with concept details) duration: 91 ms我做错了什么?
PS:我使用的是踏板码头形象:
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8e4d1200059 vaticle/typedb:latest "/opt/typedb-all-lin…" 4 days ago Up 4 days 0.0.0.0:1729->1729/tcp, :::1729->1729/tcp typedb./typedb console --version
2.11.0
2.11.0发布于 2022-08-25 09:34:26
这个match查询对于您想要达到的目标是不正确的,如果branch-item有另一个属性,您就会注意到这一点。我在这个项目中添加了req-type "blah",这就是我得到的:
test-req-branch::data::read> match
$rr (branched-from:$rm, branch-item:$cr) isa req-branching;
$rm has $ma;
$cr has attribute $ca;
$ma isa! $t;
not {
$ca isa! $t;
};
get $ma;
{ $ma "master" isa name; }
{ $ma "demo master" isa description; }
{ $ma "whatever" isa req-type; }为了理解为什么,让我们看一下在否定之前查询的结果是什么。
test-req-branch::data::read> match
$rr (branched-from:$rm, branch-item:$cr) isa req-branching;
$rm has $ma;
$cr has attribute $ca;
get $ma, $ca;
{ $ma "whatever" isa req-type;
$ca "blah" isa req-type; }
{ $ma "master" isa name;
$ca "blah" isa req-type; }
{ $ma "demo master" isa description;
$ca "blah" isa req-type; }
{ $ma "whatever" isa req-type;
$ca "other" isa name; }
{ $ma "master" isa name;
$ca "other" isa name; }
{ $ma "demo master" isa description;
$ca "other" isa name; }否定在所有这些属性( $ca和$ma )上充当一种筛选器。如果它们有不同的类型,即$ma isa! $t; not { $ca isa! $t; };,那么$ma就是一个有效的输出。但是如果$ca有两种潜在的类型,我们肯定会找到一种与$t不匹配的!因此,我们将获得branched-from实体的所有属性。
一个解决方案是对查询进行如下调整:
test-req-branch::data::read> match
$rr (branched-from:$rm, branch-item:$cr) isa req-branching;
$rm has $ma;
$ma isa! $t;
not {
$cr has $ca;
$ca isa! $t;
};
get $ma;
{ $ma "demo master" isa description; }在这里,否定式仅作为$ma上的一个过滤器。对于每个$ma,只要至少有一个具有相同类型的$ca,我们就拒绝它。
不幸的是,这将不能作为一个规则。作为查询的一部分,否定本质上将规则简化为“如果$cr没有具有$ma类型的属性,那么它就有$ma”。但这意味着它有一个具有$ma类型的属性,即$ma!这导致了一个矛盾(A cycle containing negation(s) that can cause inference contradictions),因此该规则是无效的。
感谢克里希南·戈文德拉吉对这个答案的帮助。
https://stackoverflow.com/questions/72731366
复制相似问题