如何使用 Grakn / Graql 对角色层次结构(以及它们之间的关系)进行建模?
How to model roles hierarchies (and relations between them) with Grakn / Graql?
我开始使用 Grakn,我很难理解角色和关系 "linked"。具体来说,我在建模角色层次结构时遇到了一些困难。
考虑一个具有两个场景的虚构示例,首先假设我只有两种类型:人和对象;以及客户角色层次结构和产品角色层次结构之间的采购关系。
所以,我会有这样的东西:
类型:人物
- 角色:客户级别 1
- 角色:客户2级(子客户1级)
角色:客户三级(子客户二级)
...
类型:对象
- 角色:产品级别 1
- 角色:产品级别 2(子产品级别 1)
- 角色:产品级别 3(子产品级别 2)
...
关系是唯一的(购买),用于考虑层次结构中的各个级别来关联角色:
customer level 1 --- buys --- product level 1
customer level 2 --- buys --- product level 2
customer level 3 --- buys --- product level 3
在第二种情况下,我将只有一个角色层次结构,即客户角色和对象类型将只有一个角色:产品。因此,关系将类似于:
customer level 1 --- buys --- product
customer level 2 --- buys --- product
customer level 3 --- buys --- product
如何使用 Grakn (Graql) 对此类问题建模?如果有人可以展示如何使用 Graql 对这个(或类似的)问题建模,我将不胜感激。
要在 Graql 中构建角色层次结构,您需要通过定义子关系来覆盖具有子角色的父角色。
我认为对于您的第一种情况,有两种可能的选择,具体取决于您要限制的内容:
define
buys sub relation,
abstract,
relates product,
relates customer;
buys-level-1 sub buys,
relates product-level-1 as product,
relates customer-level-1 as customer;
buys-level-2 sub buys-level-1,
relates product-level-2 as product-level-1,
relates customer-level-2 as customer-level-1;
buys-level-3 sub buys-level-2,
relates product-level-3 as product-level-2,
relates customer-level-3 as customer-level-2;
person sub entity,
plays customer-level-1,
plays customer-level-2,
plays customer-level-3;
object sub entity,
plays product-level-1,
plays product-level-2,
plays product-level-3;
在这种情况下,如果您插入 buys-level-3
关系:
insert
$per isa person; $obj isa object;
$b(product-level-3: $obj, customer-level-3: $per) isa buys-level-3;
您可以使用 buys-level-2
、buys-level-1
和 buys
来匹配它。所以这个查询将返回我们刚刚插入的人和关系:
match $b(customer-level-1: $per) isa buys-level-1; get;
或者,您可以像这样更改限制:
buys-level-3 sub buys,
relates product-level-3 as product,
relates customer-level-3 as customer;
在这种情况下,我们创建的 match
不会 return 插入的关系或人。让您想对 queries/data 施加的限制在此处指导您选择最佳型号。
第二种情况很容易看出,我们有:
define
buys sub relation,
abstract,
relates product,
relates customer;
buys-level-1 sub buys,
relates product,
relates customer-level-1 as customer;
buys-level-2 sub buys-level-1,
relates product,
relates customer-level-2 as customer-level-1;
buys-level-3 sub buys-level-2,
relates product,
relates customer-level-3 as customer-level-2;
person sub entity,
plays customer-level-1,
plays customer-level-2,
plays customer-level-3;
object sub entity,
plays product;
在任何这些场景中,您都可以检索所有 buys
关系及其角色扮演者:
match $b(customer: $per, product: $obj) isa buys; get;
我开始使用 Grakn,我很难理解角色和关系 "linked"。具体来说,我在建模角色层次结构时遇到了一些困难。
考虑一个具有两个场景的虚构示例,首先假设我只有两种类型:人和对象;以及客户角色层次结构和产品角色层次结构之间的采购关系。
所以,我会有这样的东西:
类型:人物
- 角色:客户级别 1
- 角色:客户2级(子客户1级)
角色:客户三级(子客户二级)
...
类型:对象
- 角色:产品级别 1
- 角色:产品级别 2(子产品级别 1)
- 角色:产品级别 3(子产品级别 2) ...
关系是唯一的(购买),用于考虑层次结构中的各个级别来关联角色:
customer level 1 --- buys --- product level 1
customer level 2 --- buys --- product level 2
customer level 3 --- buys --- product level 3
在第二种情况下,我将只有一个角色层次结构,即客户角色和对象类型将只有一个角色:产品。因此,关系将类似于:
customer level 1 --- buys --- product
customer level 2 --- buys --- product
customer level 3 --- buys --- product
如何使用 Grakn (Graql) 对此类问题建模?如果有人可以展示如何使用 Graql 对这个(或类似的)问题建模,我将不胜感激。
要在 Graql 中构建角色层次结构,您需要通过定义子关系来覆盖具有子角色的父角色。
我认为对于您的第一种情况,有两种可能的选择,具体取决于您要限制的内容:
define
buys sub relation,
abstract,
relates product,
relates customer;
buys-level-1 sub buys,
relates product-level-1 as product,
relates customer-level-1 as customer;
buys-level-2 sub buys-level-1,
relates product-level-2 as product-level-1,
relates customer-level-2 as customer-level-1;
buys-level-3 sub buys-level-2,
relates product-level-3 as product-level-2,
relates customer-level-3 as customer-level-2;
person sub entity,
plays customer-level-1,
plays customer-level-2,
plays customer-level-3;
object sub entity,
plays product-level-1,
plays product-level-2,
plays product-level-3;
在这种情况下,如果您插入 buys-level-3
关系:
insert
$per isa person; $obj isa object;
$b(product-level-3: $obj, customer-level-3: $per) isa buys-level-3;
您可以使用 buys-level-2
、buys-level-1
和 buys
来匹配它。所以这个查询将返回我们刚刚插入的人和关系:
match $b(customer-level-1: $per) isa buys-level-1; get;
或者,您可以像这样更改限制:
buys-level-3 sub buys,
relates product-level-3 as product,
relates customer-level-3 as customer;
在这种情况下,我们创建的 match
不会 return 插入的关系或人。让您想对 queries/data 施加的限制在此处指导您选择最佳型号。
第二种情况很容易看出,我们有:
define
buys sub relation,
abstract,
relates product,
relates customer;
buys-level-1 sub buys,
relates product,
relates customer-level-1 as customer;
buys-level-2 sub buys-level-1,
relates product,
relates customer-level-2 as customer-level-1;
buys-level-3 sub buys-level-2,
relates product,
relates customer-level-3 as customer-level-2;
person sub entity,
plays customer-level-1,
plays customer-level-2,
plays customer-level-3;
object sub entity,
plays product;
在任何这些场景中,您都可以检索所有 buys
关系及其角色扮演者:
match $b(customer: $per, product: $obj) isa buys; get;