使用 SPARQL 匹配属性集
Matching attribute sets using SPARQL
这个问题是关于使用带有 SPARQL 端点 (Fuseki 3.8.0) 的三重存储来查找匹配的 candidate
和 path
。
匹配条件是 candidate
的 attribute
必须包含 path
的所有 requires
。在下面的最小示例数据中,匹配应为 candi_1
与 path_1
和 candi_2
与 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 .
}
}
}
上述查询的性能应该不是很高。也可以尝试以下方法:
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
}
}
但是,如果 "empty" 个候选人和路径存在,后一个查询将不起作用。
这个问题是关于使用带有 SPARQL 端点 (Fuseki 3.8.0) 的三重存储来查找匹配的 candidate
和 path
。
匹配条件是 candidate
的 attribute
必须包含 path
的所有 requires
。在下面的最小示例数据中,匹配应为 candi_1
与 path_1
和 candi_2
与 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 .
}
}
}
上述查询的性能应该不是很高。也可以尝试以下方法:
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
}
}
但是,如果 "empty" 个候选人和路径存在,后一个查询将不起作用。