SPARQL - 基于某些属性的查询

时间:2017-07-12 23:57:27

标签: sparql jena owl protege

我想得到所有有奶酪配料的披萨名称,但结果显示(_:b0)这是一种猫头鹰限制以下是我的查询

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?X WHERE {
    ?X rdfs:subClassOf* [
        owl:onProperty pizza:hasTopping ;
        owl:someValuesFrom pizza:CheeseTopping
    ]
}

使用斯坦福的比萨本体

2 个答案:

答案 0 :(得分:2)

这有效(未启用推理)

 PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
 SELECT ?X ?topping WHERE {
     ?X rdfs:subClassOf ?Y .  
     ?Y owl:someValuesFrom ?topping .
     ?topping rdfs:subClassOf* pizza:CheeseTopping
 } 
 ORDER BY ?X

有些被列出不止一次,因为它们可能包含多个CheeseTopping。删除重复项:

 PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
 SELECT DISTINCT ?X WHERE {
        ?X rdfs:subClassOf ?Y .  
        ?Y owl:someValuesFrom ?topping .
        ?topping rdfs:subClassOf* pizza:CheeseTopping
 }
 ORDER BY ?X

如果启用推理器,则此方法有效:

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?X WHERE {
    ?X rdfs:subClassOf pizza:CheeseyPizza
}

价: 从这里使用披萨本体:http://protege.stanford.edu/ontologies/pizza/pizza.owl

答案 1 :(得分:1)

该查询有效,但非常复杂,可能不完整,因为有些比萨饼使用复杂的OWL构造:

PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?pizza WHERE {

  {
    ?pizza rdfs:subClassOf* pizza:Pizza .
    ?pizza owl:equivalentClass|rdfs:subClassOf [
      rdf:type owl:Restriction ;
      owl:onProperty pizza:hasTopping ;
               owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping 
      ]

   } UNION {
    ?pizza  owl:equivalentClass  _:b0 .
    _:b0    rdf:type             owl:Class ;
            owl:intersectionOf   _:b1 .
    _:b1 (rdf:rest)*/rdf:first ?otherClass.
     ?otherClass rdf:type owl:Restriction ;
               owl:onProperty pizza:hasTopping ;
               owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping 


   }
}