Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using condition in SPARQL query

Tags:

rdf

sparql

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.

like image 865
Som Sarkar Avatar asked Dec 10 '25 05:12

Som Sarkar


2 Answers

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" |
------------------
like image 95
Joshua Taylor Avatar answered Dec 12 '25 01:12

Joshua Taylor


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
}
like image 37
Stefan Reisner Avatar answered Dec 12 '25 02:12

Stefan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!