“或"在SPARQL查询中 [英] "or" in a SPARQL query

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

问题描述

我不太明白为什么他们在SPARQL中没有实现基本的逻辑运算符.但是,在大多数情况下,可以通过多种方式获得相同的结果.

I don't quite understand why in SPARQL they haven't implemented the basic logic operators. However in most of the cases is possible to obtain the same result in a number of way.

该问题的目的是对可以替代或"的槽的可能方式进行快速参考.声明.

The purpose of this question is to have a quick reference for the possible way troughs that can substitute an "or" statement.

这就是我能想到的:

例如:

SELECT * WHERE
{  { ?s :propA ?o } UNION { ?s :propB ?o }  }

-通常不适合,因为它可能变得非常冗长,因为

-not often suitable because it can became very verbose because

SELECT * WHERE { 
    { GRAPH ?g {?s ?p ?o. ?o ?pp ?data1}} UNION 
    { GRAPH ?g {?s ?p ?o. ?o ?pp ?data2}}
} 

不能用作

SELECT * WHERE { 
    GRAPH ?g {
       ?s ?p ?o. 
       {?o ?pp ?data1} UNION 
       {?o ?pp ?data2} 
    }
 }

(至少不是在Stardog中)

(at least not with Stardog)

例如:

SELECT * WHERE
    { 
        ?s ?p ?o.
        FILTER (?p = :propA || ?p = :propB )
    }

还有其他想法吗?

推荐答案

我不确定您为什么说SPARQL不提供基本逻辑运算符",因为您自己的示例清楚地表明了它:作为FILTER条件的一部分的逻辑或(||)和逻辑与(&&),以及使用UNION的析取图模式(当然,析取图模式不需要特殊的语法).

I'm not entirely sure why you say SPARQL doesn't supply 'the basic logic operators', because your own examples clearly show that it does: it provides logical-OR (||) and logical-AND (&&) as part of FILTER conditions, and disjunctive graph patterns using UNION (of course, conjunctive graph patterns need no special syntax).

类似OR的构造的其他变体也是可能的.对于形式为此特定值必须是这些可能性之一"的查询,可以使用集合成员运算符IN:

Other variations of OR-like constructs are also possible. For queries of the form "this particular value must be one of these possibilities" you can use the set membership operator, IN:

SELECT * 
WHERE { 
    ?s ?p ?o.
    FILTER (?p IN (:propA, :propB, :propC ) )
}

您也可以将VALUES子句用于这种模式:

You can also use the VALUES clause for this kind of pattern:

SELECT * 
WHERE {
    VALUES ?p { :propA :propB :propC } 
    ?s ?p ?o.
}

更新我忘了一个,也许是最简单的一个.对于诸如您这样的查询,在其中寻找 property 名称的一些替代方法时,实际上也可以使用属性路径表达式,如下所示:

Update I forgot one, perhaps the simplest. For queries such as yours, where you are looking for a few alternatives for a property name, you can actually use a property path expression as well, like so:

SELECT * 
WHERE {
    ?s :propA|:propB|:propC ?o.
}

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

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