使用SPARQL

时间:2018-10-19 19:51:24

标签: sparql rdf fuseki

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

匹配条件是attribute的{​​{1}}必须包含candidate的所有requires。在下面的最小示例数据中,匹配项应为pathcandi_1,而path_1candi_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" .

1 个答案:

答案 0 :(得分:3)

一种双重否定:

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
    }
}

但是,如果存在“空”的候选对象和路径,则后一个查询不起作用。