使用 SPARQL 匹配属性集

Matching attribute sets using SPARQL

这个问题是关于使用带有 SPARQL 端点 (Fuseki 3.8.0) 的三重存储来查找匹配的 candidatepath

匹配条件是 candidateattribute 必须包含 path 的所有 requires。在下面的最小示例数据中,匹配应为 candi_1path_1candi_2path_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" 个候选人和路径存在,后一个查询将不起作用。