use*_*165 5 rdf semantic-web ontology sparql protege
好吧假设我有5个数据类型属性,其中整数作为值.这些属性声明属于"WWS"类的个人.这个班有4个人.但是这些个体中只存在一些数据类型属性.如何查询此类中满足值5的个体.我希望变量仅显示其属性满足的个体,其余部分不应出现.
我希望这更清楚!
谢谢!
数据:
datatype properties (range:integers): #greaterthantoc #lessthantoc #lowerlimtoc #upperlimtoc #equalstoc
individuals: #ww1, #ww2, #ww3 , #ww4 belong to class #WWS
#ww1 has #greaterthantoc "0"^^xsd:integer
#ww2 has #lessthantoc "5"^^xsd:integer
#ww3 has #greaterthantoc "5"^^xsd:integer
#ww4 has #lowerlimtoc "9"^^xsd:integer and #upperlimtoc "10"^^xsd:integer
Conditions for each property (filter?):
#greaterthantoc <= "a number"
#lessthantoc >= "a number"
#lowerlimtoc <= "a number" && #upperlimtoc >= "a number"
#equalstoc = "a number"
Run Code Online (Sandbox Code Playgroud)
结果应该是满足其中一些条件的WWS个体.例如,当number为4时,结果应为WW1和WW2
我怀疑我的情况需要这样的东西,但它仍然不会返回结果:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ww:<#>
SELECT ?str
WHERE {?str rdf:type ww:WWS .
OPTIONAL { ?str ww:greaterthantoc ?gr; ww:lessthantoc ?les ; ww:lowerlimtoc ?low ; ww:upperlimtoc ?up ; ww:equalstoc ?eq . }
FILTER ( ?les >= 3 || ?gr <= 3 || (?low <= 3 && ?up >=3) || ?eq = 3)
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*lor 12
您没有显示您的数据,并且您的查询以奇怪的方式更改(例如,使用不同的URI作为ww:前缀),因此很难说您提供的任何查询实际返回的内容.但是,||在SPARQL过滤器中使用多个条件当然是可能的.
使用该values构造,我们可以在查询中提供一些数据,并看到多向||工作:
prefix : <http://stackoverflow.com/q/23561351/1281433/>
select ?s where {
values (?s ?gr ?les ?eq ?low ?up) {
(:s1 3 0 0 0 0)
(:s2 0 3 0 0 0)
(:s3 0 0 3 0 0)
(:s4 0 0 0 3 0)
(:s5 0 0 0 0 3)
(:s6 0 0 0 0 0)
(:s7 1 1 1 1 1)
}
filter ( ?eq > 2 || ?les > 2 || ?gr > 2 || ?low > 2 || ?up > 2 )
}
Run Code Online (Sandbox Code Playgroud)
请注意,与之关联的值:s6并且:s7没有任何大于2的值,因此我们不希望在输出中看到它们.其余每个都有一个值,使其中一个分离成立.以下是查询结果:
-------
| s |
=======
| :s1 |
| :s2 |
| :s3 |
| :s4 |
| :s5 |
-------
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,与问题的原始版本一样,每个变量的约束都是相同的.如果是这种情况,您可以通过使用析取属性路径或使用来缩短此查询values.使用析取属性路径,您可以编写:
select ?s where {
?s :prop1|:prop2|:prop3|:prop4 ?value
filter ( ?value > 2 )
}
Run Code Online (Sandbox Code Playgroud)
使用值,你可以写:
select ?s where {
values ?prop { :prop1 :prop2 :prop3 :prop4 }
?s ?prop ?value
filter ( ?value > 2 )
}
Run Code Online (Sandbox Code Playgroud)
由于您提到了与之交互optional,请注意,每个都具有?s不必为所有属性定义值的优点,但是如果?s对于任何指定的属性具有大于2的值,您将能够找到它.在一些原始查询中,您有类似的模式
?s :prop1 ?value1 ;
:prop2 ?value2 ;
:prop3 ?value3 .
Run Code Online (Sandbox Code Playgroud)
你只能得到绑定?s为那些具有价值的资源:prop1 ,并为一个值:prop2 和一个值:prop3.
如果个体并非所有属性都具有值,则事情会变得有点棘手,因为您需要选择性地匹配值,因为有些值不存在.但这并不难.只需放置optional块中可能不存在的部分:
select ?s where {
?s a :desiredType .
optional { ?s :prop1 ?value1 }
optional { ?s :prop2 ?value2 }
optional { ?s :prop3 ?value3 }
filter ( ?value1 > 2 || ?value2 < 5 || ?value3 = 42 )
}
Run Code Online (Sandbox Code Playgroud)