使用 SPARQL 匹配属性集 [英] Matching attribute sets using SPARQL
问题描述
这个问题是关于使用带有 SPARQL 端点的三元组存储 (Fuseki 3.8.0) 查找匹配的 candidate
和 path
.
This question is about finding matching candidate
and path
using a triple store with SPARQL endpoint (Fuseki 3.8.0).
匹配条件是candidate
的attribute
必须包含path
的所有requires
代码>.在下面的最小示例数据中,匹配项应该是 candi_1
与 path_1
和 candi_2
与 path_2
.
The matching criteria is that the attribute
's of a candidate
must contain all of the requires
of a path
. In the minimal example data below, the matches should be candi_1
with path_1
and candi_2
with path_2
.
@prefix : <http://example.com/app#> .
:candi_1
a :candidate ;
:attribute "A", "B", "C" .
:candi_2
a :candidate ;
:attribute "C", "D" .
:candi_3
a :candidate ;
:attribute "C", "E" .
:path_1
a :path ;
:requires "A", "C" .
:path_2
a :path ;
:requires "C", "D" .
结果应该是:
+------------+-------------+
| ?candidate | ?valid_path |
+------------+-------------+
| :candi1 | :path1 |
| :candi2 | :path2 |
+------------+-------------+
推荐答案
一种双重否定:
PREFIX : <http://example.com/app#>
SELECT ?cand ?path
WHERE {
?cand a :candidate .
?path a :path .
FILTER NOT EXISTS {
?path :requires ?attr .
FILTER NOT EXISTS {
?cand :attribute ?attr .
}
}
}
上述查询的性能应该不高.也可以尝试以下方法:
The above query shouldn't be very performant. Try also the following one:
PREFIX : <http://example.com/app#>
SELECT ?cand ?path
{
{
SELECT (COUNT(?attr) AS ?count) ?path ?cand
{
?path a :path ; :requires ?attr .
?cand a :candidate ; :attribute ?attr .
} GROUP BY ?path ?cand
}
{
SELECT (COUNT(?attr) AS ?count) ?path
{
?path a :path ; :requires ?attr .
} GROUP BY ?path
}
}
但是,如果存在空"候选和路径,则后一个查询不应该起作用.
However, the latter query shouldn't work if "empty" candidates and paths exist.
这篇关于使用 SPARQL 匹配属性集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!