使用OWL和RDF在SPARQL查询中连接

时间:2017-07-16 10:32:39

标签: sparql rdf owl

我想用Matrix电影查询共享类型最多的电影。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

SELECT ?movie_name (count(distinct ?atype) as ?numatype)
FROM <http://dbpedia.org/>
WHERE {

?movie rdf:type dbo:Film;
       rdf:type ?ftype.

dbr:The_Matrix rdf:type ?ttype.

?atype a owl:class;
       owl:intersectionOf [?ftype ?ttype].


?movie rdfs:label ?movie_name.
FILTER (LANG(?movie_name)="en").
}
GROUP BY ?movie_name 
ORDER BY DESC(?numatype)
LIMIT 100

我将?ttype定义为矩阵电影的类型,将?ftype定义为?电影的类型。

当我在http://dbpedia.org/sparq中查询时,没有结果。

1 个答案:

答案 0 :(得分:3)

我们的想法是在类型上使用简单的连接:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT (SAMPLE(?l) as ?movie_name) 
       (count(distinct ?ttype) as ?numSharedTypes) 
WHERE {
  VALUES ?s {dbr:The_Matrix}
  ?s a ?ttype .
  ?movie a dbo:Film ;
         a ?ttype .
  FILTER(?movie != ?s)
  ?movie rdfs:label ?l .
  FILTER (LANGMATCHES(LANG(?l), 'en'))
}
GROUP BY ?movie
ORDER BY desc(?numSharedTypes)
LIMIT 100

JOIN本身可能很昂贵,因此,您可以获得超时响应。由于Virtuoso的任何时候功能得到了不完整的结果。

看起来查询优化器并不够聪明,特别是标签会使性能变差。一堆子SELECT使得它更快,但在阅读查询时更复杂:

PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dbr:  <http://dbpedia.org/resource/>

SELECT  ?movie_name ?numSharedTypes
WHERE
  { ?movie  rdfs:label  ?l
    FILTER langMatches(lang(?l), "en")
    BIND(replace(replace(str(?l), "\\(film\\)$", ""), "[^0-9]*\\sfilm\\)$", ")") AS ?movie_name)
    { SELECT  ?movie (COUNT(?type) AS ?numSharedTypes)
      WHERE
        { ?movie  rdf:type  dbo:Film ;
                  rdf:type  ?type
          { SELECT  ?type
            WHERE
              { dbr:The_Matrix rdf:type  ?type
              }
          }
          FILTER ( ?movie != dbr:The_Matrix )
        }
      GROUP BY ?movie
      ORDER BY DESC(?numSharedTypes) ASC(?movie)
      LIMIT   100
    }
  }
ORDER BY DESC(?numSharedTypes) ASC(?movie_name)

结果(块):

+------------------------+----------------+
|       movie_name       | numSharedTypes |
+------------------------+----------------+
| The Matrix Reloaded    |             36 |
| The Matrix Revolutions |             33 |
| The Matrix (franchise) |             30 |
| Demolition Man         |             28 |
| Freejack               |             28 |
| Conspiracy Theory      |             27 |
| Deep Blue Sea (1999)   |             27 |
| Fair Game (1995)       |             27 |
| Judge Dredd            |             27 |
| Revenge Quest          |             27 |
| Screamers (1995)       |             27 |
| Soldier (1998)         |             27 |
| The Invasion           |             27 |
| Timecop                |             27 |
| Total Recall (1990)    |             27 |
| V for Vendetta         |             27 |
| Assassins              |             26 |
| ...                    |            ... |
+------------------------+----------------+