[英]Using condition in SPARQL query
我有一个SPARQL查询,就像这样-
SELECT ?informationPath ?businessEntitylabel ?path ?sourced ?mastered ?delivered
WHERE {
?businessEntity dd:hasPropertyPath ?informationPath .
?businessEntity rdfs:label ?businessEntitylabel .
?informationPath dd:hasPath ?path .
OPTIONAL {
?informationPath a dd:SourcedData .
BIND("Yes" as ?sourced)
}
OPTIONAL {
?informationPath a dd:MasteredData .
BIND("Yes" as ?mastered)
}
OPTIONAL {
?informationPath a dd:DeliveredData .
BIND("Yes" as ?delivered)
}
} ORDER BY ?businessEntitylabel ?path
现在,我只希望有一列而不是“源”,“主”,“交付”,而名称是“可追溯性”。 并且该列将显示?informationPath是主数据还是源数据或交付的数据,因此我想将BIND(“源数据”作为?可跟踪性)或(“主数据”作为?可跟踪性)或( ?可追溯性)
我是SPARQL的新手,我想知道SPARQL中是否有任何'if'语句可以用作-
if(?informationPath a dd:SourcedData)
BIND("SourcedData" as ?traceability)
任何帮助都感激不尽。
bind
和if
我认为这是使用IF将变量绑定到两个值之一的重复吗? ,而我已将其标记为,但是if
的条件应该是什么,可能不会立即显而易见,因此,我添加了此示例作为答案。 假设您具有以下形式的数据:
@prefix : <https://stackoverflow.com/q/22985157/1281433/> .
:x1 a :A .
:x2 a :B .
:x3 a :C .
:x4 a :D .
然后,您可以使用类似以下的查询来获得一些结果:
prefix : <https://stackoverflow.com/q/22985157/1281433/>
select ?x ?typename where {
?x a [] .
bind( if ( exists { ?x a :A },
"type A" ,
if ( exists { ?x a :B },
"type B",
if ( exists { ?x a :C },
"type C",
"unknown type" )))
as ?typename )
}
------------------------
| x | typename |
========================
| :x1 | "type A" |
| :x2 | "type B" |
| :x3 | "type C" |
| :x4 | "unknown type" |
------------------------
values
它使用if
和exists
结构检查各种值。 现在,在您的案例中,如果要检查特定数量的案例,则实际上可以使用values
来模拟某种case
语句。 为此,您将执行类似的操作,尽管这不会给您带来“未知”的情况。
prefix : <https://stackoverflow.com/q/22985157/1281433/>
select ?x ?typename where {
values (?type ?typename) {
(:A "type A")
(:B "type B")
(:C "type C")
}
?x a ?type
}
------------------
| x | typename |
==================
| :x1 | "type A" |
| :x2 | "type B" |
| :x3 | "type C" |
------------------
要扩展约书亚·泰勒的答案,实际上也可以使用UNDEF关键字来处理默认情况:
prefix : <http://stackoverflow.com/q/22985157/1281433/>
select ?x ?typename where {
values (?type ?typename) {
(:A "type A")
(:B "type B")
(:C "type C")
(UNDEF "unknown type")
}
?x a ?type
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.