Matching attribute sets using SPARQL

烈酒焚心 提交于 2021-02-10 18:44:38

问题


This question is about finding matching candidate and path using a triple store with SPARQL endpoint (Fuseki 3.8.0).

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" .

The result should be:

+------------+-------------+
| ?candidate | ?valid_path |
+------------+-------------+
| :candi1    | :path1      |
| :candi2    | :path2      |
+------------+-------------+

回答1:


A kind of double negation:

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.



来源:https://stackoverflow.com/questions/52899050/matching-attribute-sets-using-sparql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!