我有一个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是Mastered数据还是源数据或交付数据,因此我想BIND(“源数据”为?可追溯性)或(“Mastered data”为?可追溯性)或(“已交付数据”为?可追溯性)
我是SPARQL的新手,我想知道SPARQL中是否有任何'if'语句可用作 -
if(?informationPath a dd:SourcedData)
BIND("SourcedData" as ?traceability)
非常感谢任何帮助。
答案 0 :(得分:3)
bind
和if
我认为这是Binding a variable to one of two values with IF?的副本,我已将其标记为此类,但可能不会立即明白{{1> 条件应该是,所以我将此示例添加为答案。假设您已获得以下格式的数据:
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
语句。要做到这一点,你可以做这样的事情,虽然这不会给你一个&#34;未知&#34;情况下。
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
}
答案 1 :(得分:1)
为了扩展Joshua Taylor的答案,你实际上可以处理默认情况,这要归功于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
}