简体   繁体   English

在SPARQL中测试谓词的存在

[英]Testing the presence of a predicate in SPARQL

I have the following RDF model: 我有以下RDF模型:

@prefix : <http://test.com/#> .

:graph1   :hasNode   :node1 ;
          :centerNode   :node1 .

:graph1   :hasNode   :node2 .
:graph1   :hasNode   :node3 .

I want to run a SPARQL query in which if a :nodeX is related to a :graphX with a predicate :centerNode I return true (or some other indication) otherwise false ; 我想运行一个SPARQL查询中,如果:nodeX是关系到一个:graphX与谓词:centerNode我返回true (或其他指示),否则false ; The result would look something like the following: 结果如下所示:

?n       ?g        ?centered
-----------------------------
:node1   :graph1   true
:node2   :graph1   false
:node3   :graph1   false

Is there a way to do this in SPARQL 1.0? 有没有办法在SPARQL 1.0中执行此操作? if not, can it be done with in SPARQL 1.1? 如果没有,可以在SPARQL 1.1中完成吗?

In SPARQL 1.0, 在SPARQL 1.0中,

SELECT * { 
  ?graph :hasNode ?node .
  OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) }
}

and ?node1 will be bound or not bound in the answers. 和?node1将在答案中绑定或不绑定。 Cardinality is messy though. 但基数很混乱。

OPTIONAL/!BOUND can do a sort of NOT EXISTS: OPTIONAL /!BOUND可以做一些NOT EXISTS:

SELECT * { 
  ?graph :hasNode ?node .
  OPTIONAL{ ?graph :centerNode ?node1 FILTER sameterm(?node, ?node1) }
  FILTER( !bound(?node1) )
}

You can combine EXISTS and BIND as follows in SPARQL 1.1: 您可以在SPARQL 1.1中将EXISTSBIND组合如下:

PREFIX : <http://test.com/#>

SELECT * WHERE { 
  ?graph :hasNode ?node .
  BIND( EXISTS { ?graph :centerNode ?node } as ?isCentered )
}

Using Jena's ARQ, I get these results: 使用Jena的ARQ,我得到了以下结果:

$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
   --data predicate.n3 \
   --query predicate.sparql
---------------------------------
| graph   | node   | isCentered |
=================================
| :graph1 | :node3 | false      |
| :graph1 | :node2 | false      |
| :graph1 | :node1 | true       |
---------------------------------

That's exactly the purpose of ASK queries in SPARQL: 这正是SPARQL中ASK查询的目的:

PREFIX : <http://test.com/#>
ASK WHERE { 
  ?graph :hasNode ?node .
  ?graph :centerNode ?node .
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM