graph-databases - GRAQL 中的递归查询?
问题描述
有没有办法在 GRAQL 中定义递归查询,即匹配实体之间的确切谓词路径未知的模式(例如,它们中有多少)?
SPARQL 在 1.1 版本中增加了对这些的支持。来自Apache Jena 文档的示例:
# Find the types of :x, following subClassOf
SELECT *
{
:x rdf:type/rdfs:subClassOf* ?t
}
CYPHER 从一开始也允许它们。例子:
MATCH (alice:Person { name:"Alice" })-[:friend *1..2]->(friend:Person)
RETURN friend.name;
是否可以在 GRAQL 中做类似的事情?
解决方案
使用 Grakn 的推理引擎可以在 Graql 中实现这一点。
Graqlmatch
查询不支持循环查询语法(还没有,但计划中),但您可以在 Grakn 中使用rule
. 为了实现递归,应该有一个规则,其中包含与规则中when
推断的相同类型的东西then
。
在 Graql 中,这个确切的friend
例子如下。此示例不使用递归,因为我们只寻找 1 或 2 跳循环。
首先你需要一个模式:
define
name sub attribute, value string;
person sub entity,
has name,
plays friend;
friend sub relation,
relates friend;
如果这是您的起始模式,您需要按如下方式扩展它以在新n-degree-friendship
关系中添加递归:
define
friend-of-a-friend isa relation,
relates connected-friend;
person sub entity,
plays connected-friend;
infer-friend-of-a-friend sub rule,
when {
(friend: $p1, friend: $p2) isa friendship;
(friend: $p2, friend: $p3) isa friendship;
}, then {
(connected-friend: $p1, connected-friend: $p3) isa friend-of-a-friend;
};
然后您可以查询由任意数量的friendship
关系连接的朋友,如下所示:
match $p1 isa person, has name "John";
$p2 isa person, has name $n;
{ (connected-friend: $p1, connected-friend: $p2) isa friend-of-a-friend; }
or { (friend: $p1, friend: $p2) isa friendship; };
get $n;
这个friend
例子不是递归的,但它可以扩展为递归的。Grakn 目前无法支持的是循环次数。
我们可以在示例中看到一个很好的递归subClassOf
示例:
define
class sub entity,
plays subclass,
plays superclass;
class-hierarchy sub relation,
relates subclass,
relates superclass;
class-hierarchy-is-recursive sub rule,
when {
(subclass: $c1, superclass: $c2) isa class-hierarchy;
(subclass: $c2, superclass: $c3) isa class-hierarchy;
}, then {
(subclass: $c1, superclass: $c3) isa class-hierarchy;
};
然后匹配以查找 的所有子类x
:
match $x isa class;
$y isa class;
(superclass: $x, subclass: $x) isa subclass;
get $y;
推荐阅读
- git - Docker:在构建时克隆私有 GitHub 存储库
- wixsharp - 用于 .NET Framework 和 MariaDB 的 WixSharp Bootstrapper,具有属性和功能的静默安装,MSI 设置
- ejabberd - Ejabbed 音频/视频通话在不同的网络上不起作用
- java - RestTemplate 的错误请求-> postForObject (Spring Boot)
- wolfram-mathematica - 运动矢量预测或运动矢量差分如何在编码器中提供压缩?
- python - 在python中对每行的正则表达式匹配进行分组
- node.js - react js前端和nodejs后端中的pubnub集成
- python - Python tksheet,如何更改列名
- svelte - Svelte Mapbox-gl-draw:如何将多边形坐标获取到父组件?
- javascript - 更新 Discord Music Bot .js 中嵌入的“正在播放”