SPARQL中的递归查询以浏览集合的集合

时间:2021-01-19 14:52:11

I am trying to create an RDF graph from a Mulgara RDF store, using a Sparql query to return results. I'm just beginning to get comfortable with simple queries, effectively asking, "which objects are Members of a particular collection?"

我正在尝试从Mulgara RDF存储区创建一个RDF图,使用Sparql查询来返回结果。我刚刚开始习惯使用简单的查询,有效地询问“哪些对象是特定集合的成员?”

My question is, and I would greatly appreciate any advice, whether I can take the results from this simple query and reroute them back through as the object of the query?

我的问题是,如果我能从这个简单的查询中获得结果并将其重新作为查询的对象,我将非常感谢您的建议。

For example, I have this sparql query:

例如,我有一个sparql查询:

SELECT ?x WHERE {?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>}

With these results:

与这些结果:

"x"
info:fedora/ramsey:ThelifeandadventuresofRobinsonCrusoe 
info:fedora/ramsey:Jackanapes 
info:fedora/ramsey:SundayJournalvol01no0219951126 
info:fedora/ramsey:Ideologyandchange 
info:fedora/ramsey:theshepherdofthepyrenees 
info:fedora/ramsey:ScenesinAmerica
...

My goal, is to then take these unique identifiers and replace the object, <info:fedora/collection:ramsey>, from the original query and run the query again.

我的目标是,然后使用这些惟一的标识符并替换对象, ,来自原始查询并再次运行查询。

I'm imagining a scenario where I would identify a root element in the initial query, have the results return all member objects, then return all those objects' member objects, ad infinitum...

我正在设想这样一种场景:在初始查询中识别根元素,让结果返回所有成员对象,然后返回所有这些对象的成员对象,直到无穷无尽……

Is this possible with Sparql queries? Specifically, I believe I'm querying a Mulgara RDF database. Any thoughts, even if its' not doable, greatly appreciated.

Sparql查询是否可以实现这一点?具体地说,我认为我正在查询Mulgara RDF数据库。任何想法,即使“不可行”,也会受到极大的赞赏。

1 个解决方案

#1


8  

Lets assume you have to stick to SPARQL 1.0. I believe that mulgara has limited support for SPARQL 1.1 if any.

假设您必须坚持SPARQL 1.0。我相信mulgara对SPARQL 1.1的支持是有限的。

With SPARQL 1.0 if you probably know how many levels you want to query you can do things like:

使用SPARQL 1.0,如果您可能知道要查询多少级别,您可以做以下事情:

SELECT ?y WHERE {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?y  <fedora-rels-ext:isMemberOfCollection> ?x
}

Here ?y will be bound with 2nd level elements from your root. With UNIONS you can query multiple levels with one query. An example for one and two levels from root in one query:

这里?y将与根中的二级元素绑定。使用联合,您可以使用一个查询查询查询多个级别。一个查询中的一个和两个层次的示例:

SELECT ?x WHERE {
     {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey> .
     } UNION {
        ?zz  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?x  <fedora-rels-ext:isMemberOfCollection> ?zz .
     }
}

The problem with this is that you do not really know at what level ?x is bound. Therefore you cannot paint a tree with this type of query. In SPARQL 1.1 this gets solved with BIND AS

问题是你不知道在什么水平上x是有界的。因此,不能用这种类型的查询来绘制树。在SPARQL 1.1中,使用BIND AS解决了这个问题

SELECT ?x ?level WHERE {
     {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey> .
        BIND (1 AS ?level)
     } UNION {
        ?zz  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?x  <fedora-rels-ext:isMemberOfCollection> ?zz .
        BIND (2 AS ?level)
     }
}

This second query will return at what level ?x is bound. You can imagine some programatically generated query with lots of unions trying to reach the max depth of the tree. If you want full support for SPARQL 1.1 if you can try to use Jena/ARQ. In Jena you can also use Property paths and with something like the following:

第二个查询将返回到什么级别?您可以想象一些编程生成的查询,其中有许多联合试图达到树的最大深度。如果您想要对SPARQL 1.1的全面支持,如果您可以尝试使用Jena/ARQ。在耶拿,你也可以使用财产路径和类似的东西:

SELECT ?x WHERE {
    ?x  <fedora-rels-ext:isMemberOfCollection>+ <info:fedora/collection:ramsey> .
}

You would bind in ?x all the nodes reachable from <info:fedora/collection:ramsey> via the predicate <fedora-rels-ext:isMemberOfCollection>.

您将通过谓词 绑定?x从 可访问的所有节点。

#1


8  

Lets assume you have to stick to SPARQL 1.0. I believe that mulgara has limited support for SPARQL 1.1 if any.

假设您必须坚持SPARQL 1.0。我相信mulgara对SPARQL 1.1的支持是有限的。

With SPARQL 1.0 if you probably know how many levels you want to query you can do things like:

使用SPARQL 1.0,如果您可能知道要查询多少级别,您可以做以下事情:

SELECT ?y WHERE {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?y  <fedora-rels-ext:isMemberOfCollection> ?x
}

Here ?y will be bound with 2nd level elements from your root. With UNIONS you can query multiple levels with one query. An example for one and two levels from root in one query:

这里?y将与根中的二级元素绑定。使用联合,您可以使用一个查询查询查询多个级别。一个查询中的一个和两个层次的示例:

SELECT ?x WHERE {
     {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey> .
     } UNION {
        ?zz  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?x  <fedora-rels-ext:isMemberOfCollection> ?zz .
     }
}

The problem with this is that you do not really know at what level ?x is bound. Therefore you cannot paint a tree with this type of query. In SPARQL 1.1 this gets solved with BIND AS

问题是你不知道在什么水平上x是有界的。因此,不能用这种类型的查询来绘制树。在SPARQL 1.1中,使用BIND AS解决了这个问题

SELECT ?x ?level WHERE {
     {
        ?x  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey> .
        BIND (1 AS ?level)
     } UNION {
        ?zz  <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:ramsey>
        ?x  <fedora-rels-ext:isMemberOfCollection> ?zz .
        BIND (2 AS ?level)
     }
}

This second query will return at what level ?x is bound. You can imagine some programatically generated query with lots of unions trying to reach the max depth of the tree. If you want full support for SPARQL 1.1 if you can try to use Jena/ARQ. In Jena you can also use Property paths and with something like the following:

第二个查询将返回到什么级别?您可以想象一些编程生成的查询,其中有许多联合试图达到树的最大深度。如果您想要对SPARQL 1.1的全面支持,如果您可以尝试使用Jena/ARQ。在耶拿,你也可以使用财产路径和类似的东西:

SELECT ?x WHERE {
    ?x  <fedora-rels-ext:isMemberOfCollection>+ <info:fedora/collection:ramsey> .
}

You would bind in ?x all the nodes reachable from <info:fedora/collection:ramsey> via the predicate <fedora-rels-ext:isMemberOfCollection>.

您将通过谓词 绑定?x从 可访问的所有节点。