通用SPARQL查询

时间:2014-03-18 09:15:14

标签: rdf sparql jena owl

我在为不同的猫头鹰创建通用查询时遇到问题。我有猫头鹰:

((ClassA MyProperty only (ClassX or ClassY)) AND
(ClassA MyProperty exactly 1 ClassX) AND 
(ClassA MyProperty exactly 1 ClassY))

有时类也有这个(它的组合,ClassA可以有XY或XZ)

OR
((ClassA MyProperty only (ClassX or ClassZ)) AND
(ClassA MyProperty exactly 1 ClassX) AND 
(ClassA MyProperty exactly 1 ClassZ))

全猫头鹰:

<owl:Class rdf:about="ClassA">
    <rdfs:subClassOf>
        <owl:Class>
            <owl:unionOf rdf:parseType="Collection">
                <owl:Class>
                    <owl:intersectionOf rdf:parseType="Collection">
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:allValuesFrom>
                                <owl:Class>
                                    <owl:unionOf rdf:parseType="Collection">
                                        <rdf:Description rdf:about="ClassX"/>
                                        <rdf:Description rdf:about="ClassY"/>
                                    </owl:unionOf>
                                </owl:Class>
                            </owl:allValuesFrom>
                        </owl:Restriction>
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:onClass rdf:resource="ClassX"/>
                            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
                        </owl:Restriction>
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:onClass rdf:resource="ClassY"/>
                            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
                        </owl:Restriction>
                    </owl:intersectionOf>
                </owl:Class>
                <owl:Class>
                    <owl:intersectionOf rdf:parseType="Collection">
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:allValuesFrom>
                                <owl:Class>
                                    <owl:unionOf rdf:parseType="Collection">
                                        <rdf:Description rdf:about="ClassX"/>
                                        <rdf:Description rdf:about="ClassZ"/>
                                    </owl:unionOf>
                                </owl:Class>
                            </owl:allValuesFrom>
                        </owl:Restriction>
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:onClass rdf:resource="ClassX"/>
                            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
                        </owl:Restriction>
                        <owl:Restriction>
                            <owl:onProperty rdf:resource="MyProperty"/>
                            <owl:onClass rdf:resource="ClassZ"/>
                            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
                        </owl:Restriction>
                    </owl:intersectionOf>
                </owl:Class>
            </owl:unionOf>
        </owl:Class>
    </rdfs:subClassOf>
</owl:Class>

问题是,我不知道哪个班级只有一个组合,哪个班级不止一个。

查询单一来源:

PREFIX my: <http://www.semanticweb.org/MyOnto#>

SELECT ?myClass ?prop (str(?numInt) as ?number) ?source
WHERE { 
?myClass rdfs:subClassOf my:Classes ;
         rdfs:subClassOf ?x .
?x owl:intersectionOf ?array.
?array rdf:rest*/rdf:first ?triples.
?triples owl:onProperty ?prop ;
        owl:onClass ?source;
        owl:qualifiedCardinality ?numInt.
?source rdfs:subClassOf my:Sources .
}

查询在顶部使用交集,但是如果我有2个或更多组合,它将包含在这个概念的联合中。所以它很容易包装查询,我得到组合的结果,但比我没有得到结果一次性使用。 所以问题是,如何编写适用于例如只有ClassA和MyProperty或...... THX

1 个答案:

答案 0 :(得分:2)

当您(i)提供我们可以使用的完整工作数据时,解决这些问题几乎总是更容易(在这种情况下,您还没有提供完整的本体,所以它&# 39;更难以使用),以及(ii)使用RDF的Turtle序列化,因为它更接近SPARQL语法。

你的本体

这里是完整的最小本体,包含您描述的结构:

@prefix :      <http://example.org/> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:universals  a  owl:Ontology .

:MyProperty  a  owl:ObjectProperty .

:ClassY  a      owl:Class .
:ClassX  a      owl:Class .
:ClassZ  a      owl:Class .

:ClassA  a               owl:Class ;
        rdfs:subClassOf  [ a            owl:Class ;
                           owl:unionOf  ( [ a                   owl:Class ;
                                            owl:intersectionOf  ( [ a                  owl:Restriction ;
                                                                    owl:allValuesFrom  [ a            owl:Class ;
                                                                                         owl:unionOf  ( :ClassX :ClassY )
                                                                                       ] ;
                                                                    owl:onProperty     :MyProperty
                                                                  ] [ a                         owl:Restriction ;
                                                                      owl:onClass               :ClassX ;
                                                                      owl:onProperty            :MyProperty ;
                                                                      owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                                                                    ] [ a                         owl:Restriction ;
                                                                        owl:onClass               :ClassY ;
                                                                        owl:onProperty            :MyProperty ;
                                                                        owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                                                                      ] )
                                          ] [ a                   owl:Class ;
                                              owl:intersectionOf  ( [ a                  owl:Restriction ;
                                                                      owl:allValuesFrom  [ a            owl:Class ;
                                                                                           owl:unionOf  ( :ClassX :ClassZ )
                                                                                         ] ;
                                                                      owl:onProperty     :MyProperty
                                                                    ] [ a                         owl:Restriction ;
                                                                        owl:onClass               :ClassX ;
                                                                        owl:onProperty            :MyProperty ;
                                                                        owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                                                                      ] [ a                         owl:Restriction ;
                                                                          owl:onClass               :ClassZ ;
                                                                          owl:onProperty            :MyProperty ;
                                                                          owl:qualifiedCardinality  "1"^^xsd:nonNegativeInteger
                                                                        ] )
                                            ] )
                         ] .

SPARQL查询

您提供的查询不合法,因为它没有定义您正在使用的所有前缀。如果我们认为他们已经获得了&#34;标准&#34;定义,它仍然不会返回您的数据上的任何内容,因为您要求my:Classes的子类,并且my:Classes没有出现在您的数据中我们向我们展示了同样,我们对my:Sources一无所知。我再次强调,提供完整的工作示例作为起点非常重要。否则,你只是要求人们不仅要弄清楚解决方案,还要弄清楚数据应该是什么。

从您的问题中我不清楚您希望获得什么样的结果,但是根据我提供的数据,您可以使用以下查询来获得结果组织每个联合类下面的交集类:

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

select ?class ?orClass ?andClass ?p ?n where { 
  ?class rdfs:subClassOf/owl:unionOf/rdf:rest*/rdf:first ?orClass .
  ?orClass owl:intersectionOf/rdf:rest*/rdf:first ?andClass .
  ?andClass owl:onProperty ?p ;
            owl:onClass ?onClass;
            owl:qualifiedCardinality ?n .
}
order by ?class ?orClass ?andClass
-----------------------------------------------------------------------------------------------------------
| class   | orClass | andClass | p           | n                                                          |
===========================================================================================================
| :ClassA | _:b0    | _:b1     | :MyProperty | "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> |
| :ClassA | _:b0    | _:b2     | :MyProperty | "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> |
| :ClassA | _:b3    | _:b4     | :MyProperty | "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> |
| :ClassA | _:b3    | _:b5     | :MyProperty | "1"^^<http://www.w3.org/2001/XMLSchema#nonNegativeInteger> |
-----------------------------------------------------------------------------------------------------------