在 SPARQL 查询中使用条件 [英] Using condition in SPARQL query

查看:119
本文介绍了在 SPARQL 查询中使用条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 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

现在我只想拥有一列而不是 ?sourced ?mastered ?delivered 并且名称是 ?traceability.该列将显示 ?informationPath 是 Mastered data 还是 Sourced data 或已交付数据,因此我想绑定(Sourced data"作为 ?traceability)或(Mastered data"作为 ?traceability)或(Delivered data"作为?可追溯性)

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)

我是 SPARQL 的新手,我想知道 SPARQL 中是否有任何if"语句可以用作-

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)

任何帮助将不胜感激.

推荐答案

使用 bindif

我认为这是 使用 IF 将变量绑定到两个值之一? 的副本,并且我'已经将其标记为这样,但是 if 中的 conditions 应该是什么可能不是很明显,所以我添加了这个例子作为答案.假设您有以下形式的数据:

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

使用

使用 ifexists 构造来检查各种值.现在,在您的情况下,您要检查特定数量的案例,您实际上可以使用 values 来模拟一种 case 语句.要做到这一点,你会做这样的事情,虽然这不会给你一个未知"的情况.

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

这篇关于在 SPARQL 查询中使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆