Using condition in SPARQL query

会有一股神秘感。 提交于 2021-01-03 06:21:07

问题


I have a SPARQL query which is like this-

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)

I am new in SPARQL, and I was wondering if there is any 'if' statement in SPARQL which can be used as-

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

Any help will be much appreciated.


回答1:


Using bind and if

I think that this is a duplicate of Binding a variable to one of two values with 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. 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

That uses if and the exists construct to check various values. 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. 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" |
------------------



回答2:


To expand on Joshua Taylor's answer, you actually can handle the default case as well thanks to the UNDEF keyword:

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
}


来源:https://stackoverflow.com/questions/22985157/using-condition-in-sparql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!