首页 > 解决方案 > 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 中做类似的事情?

标签: graph-databasesvaticle-typedbvaticle-typeqlknowledge-graph

解决方案


使用 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;

推荐阅读