简体   繁体   English

SPARQL查询中的使用条件

[英]Using condition in SPARQL query

I have a SPARQL query which is like this- 我有一个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

Now I want to have only one column instead of ?sourced ?mastered ?delivered and name is ?traceability. 现在,我只希望有一列而不是“源”,“主”,“交付”,而名称是“可追溯性”。 And the column will show if an ?informationPath is Mastered data or Sourced data or delivered data and accordingly I want to BIND ("Sourced data" as ?traceability) or ("Mastered data" as ?traceability) or ("Delivered data" as ?traceability) 并且该列将显示?informationPath是主数据还是源数据或交付的数据,因此我想将BIND(“源数据”作为?可跟踪性)或(“主数据”作为?可跟踪性)或( ?可追溯性)

I am new in SPARQL, and I was wondering if there is any 'if' statement in SPARQL which can be used as- 我是SPARQL的新手,我想知道SPARQL中是否有任何'if'语句可以用作-

if(?informationPath a dd:SourcedData)
BIND("SourcedData" as ?traceability)

Any help will be much appreciated. 任何帮助都感激不尽。

Using bind and if 使用bindif

I think that this is a duplicate of Binding a variable to one of two values with IF? 我认为这是使用IF将变量绑定到两个值之一的重复吗? , and I've marked it as such, but it might not be immediately obvious what the conditions in the if should be, so I'm adding this example as an answer. ,而我已将其标记为,但是if条件应该是什么,可能不会立即显而易见,因此,我添加了此示例作为答案。 Suppose you've got data of the form: 假设您具有以下形式的数据:

@prefix : <https://stackoverflow.com/q/22985157/1281433/> .

:x1 a :A .
:x2 a :B .
:x3 a :C .
:x4 a :D .

Then you can use a query like the following to get some results: 然后,您可以使用类似以下的查询来获得一些结果:

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" |
------------------------

Using values 使用values

That uses if and the exists construct to check various values. 它使用ifexists结构检查各种值。 Now, in your case, where you have a specific number of cases that you want to check for, you can actually use values to simulate a sort of case statement. 现在,在您的案例中,如果要检查特定数量的案例,则实际上可以使用values来模拟某种case语句。 To do that, you'd do something like this, although this won't give you an "unknown" 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" |
------------------

To expand on Joshua Taylor's answer, you actually can handle the default case as well thanks to the UNDEF keyword: 要扩展约书亚·泰勒的答案,实际上也可以使用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.

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