使用SPARQL查询的UNION和交集 [英] UNION and intersection using SPARQL queries
问题描述
我正在将用户定义的查询转换为SPARQL.例如,当用户说"abc"时,这意味着将具有给定类型的所有具有"abc"属性的节点提供给我.作为对此的扩展,如果用户说"abc或(pqr和lmn)",则需要找到给定类型的所有属性为"abc或(pqr和lmn)"的所有节点.以下是我提出的查询:
I am converting user-defined-queries into SPARQL. For example, when user says, "abc", it means give me all nodes of a given type which have some attribute named "abc". As an extension of this, if user says, "abc or (pqr and lmn)", I need to find all nodes of a given type for which some attribute is "abc or (pqr and lmn)". Following is the query I have come up with:
SELECT DISTINCT ?node, ?type
WHERE
{
{
?node a ?type .
FILTER ( ?type != <sometype>)
}
{
{
?node ?reln0 ?obj0 .
FILTER ( regex(str(?obj0), 'abc', "i") )
}
UNION
{
{
?node ?reln1 ?obj1 .
FILTER ( regex(str(?obj1), 'pqr', "i") )
}
{
?node ?reln2 ?obj2 .
FILTER ( regex(str(?obj2), 'lmn', "i") )
}
}
}
}
ORDER BY ?node
但是它不会返回正确的结果.上述查询有问题吗?我不想使用以下内容,因为我需要动态生成条件,并且每个子句都必须分开.
But it doesn't return proper results. Is there something wrong with the above given query? I don't want to use the following because I need to generate the conditions dynamically and each clause needs to be separate.
FILTER (regex(str(?obj2), 'abc', "i") || regex(str(?obj2), 'pqr', "i") && regex(str(?obj2), 'lmn', "i"))
推荐答案
我使查询具体如下(为第一个过滤器输入确定的类型):
I made your query concrete as follows (putting in a definite type for the first filter):
PREFIX : <http://example.org/>
SELECT DISTINCT ?node ?type
WHERE
{
{
?node a ?type .
FILTER ( ?type != :Type1 )
}
{
{
?node ?reln0 ?obj0 .
FILTER ( regex(str(?obj0), 'abc', "i") )
}
UNION
{
{
?node ?reln1 ?obj1 .
FILTER ( regex(str(?obj1), 'pqr', "i") )
}
{
?node ?reln2 ?obj2 .
FILTER ( regex(str(?obj2), 'lmn', "i") )
}
}
}
}
ORDER BY ?node
然后我生成了以下数据:
I then generated the following data:
@prefix : <http://example.org/> .
:n1 a :Type2 ; # keep
:r0 :NodeABC .
:n2 a :Type2 ;
:r0 :NodeBCD .
:n3 a :Type2 ; # keep
:r1 :NodePQR ;
:r2 :NodeLMN .
:n4 a :Type2 ;
:r1 :NodeQRS ;
:r2 :NodeLMN .
:n5 a :Type2 ;
:r1 :NodePQR ;
:r2 :NodeMNO .
:n6 a :Type2 ;
:r1 :NodeQRS ;
:r2 :NodeMNO .
仅应保留:n1
和:n3
.我可以使用 Jena的命令行ARQ 或
Only :n1
and :n3
should be kept. I can run this with Jena's command line ARQ, or the Redland based roqet
, and I get these rules in both cases.
使用ARQ:
$ arq --data data.n3 --query query.sparql
-----------------
| node | type |
=================
| :n1 | :Type2 |
| :n3 | :Type2 |
-----------------
$ arq --version
Jena: VERSION: 2.10.0
Jena: BUILD_DATE: 2013-02-20T12:04:26+0000
ARQ: VERSION: 2.10.0
ARQ: BUILD_DATE: 2013-02-20T12:04:26+0000
使用roqet
:
$ roqet query.sparql -D data.n3 -r table
roqet: Querying from file query.sparql
--------------------------------------------------------------
| node | type |
==============================================================
| uri<http://example.org/n1> | uri<http://example.org/Type2> |
| uri<http://example.org/n3> | uri<http://example.org/Type2> |
--------------------------------------------------------------
$ roqet -v
0.9.28
这篇关于使用SPARQL查询的UNION和交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!