2

我正在尝试在 OWL 文件中提取intersectionOf和,该文件由类集合或/和. 我创建了一个 SPARQL 查询,它为 提取“集合” ,但问题是某些检索到的数据与该类无关。unionOfinteresctionOfunionOfsomeValuesFromonPropertyintersectionOf

例如,我有一个名为man. 这个类有一个等价类,它是三个intersectionOf类,即 、adultperson。我的maleSPARQL 查询返回一些不正确的结果:它返回类adultperson和也与我的 OWL 文件中的所有其他类等效的类,例如,这是不正确的。这是我的 SPARQL 查询:malemanhaulage_worker

PREFIX abc: <http://owl.cs.manchester.ac.uk/2009/07/sssw/people#>
PREFIX ghi: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX mno: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX list: <http://jena.hpl.hp.com/ARQ/list#>

SELECT  Distinct ?class ?equivalentClass  
WHERE{ ?class a owl:Class .
FILTER( STRSTARTS(STR(?class),"http://www.w3.org/2002/07/owl#") || STRSTARTS(STR(?class),"http://owl.cs.manchester.ac.uk/2009/07/sssw/people#") 

)

     ?x a owl:Class ; owl:intersectionOf ?list .
     ?list rdf:rest*/rdf:first ?equivalentClass .


   } GROUP BY ?class ?equivalentClass ORDER BY ?no

这是我的 OWL 文件:

<?xml version="1.0"?>
<rdf:RDF
    xmlns="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:ns0="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#"
  xml:base="http://owl.cs.manchester.ac.uk/2009/07/sssw/people">
  <owl:Ontology rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people"/>
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
  <owl:Class rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#haulage_worker">
    <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
   ></rdfs:comment>
    <owl:equivalentClass>
      <owl:Restriction>
        <owl:onProperty>
          <owl:ObjectProperty      rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#works_for"/>
        </owl:onProperty>
        <owl:someValuesFrom>
        <owl:Class>
            <owl:unionOf rdf:parseType="Collection">
              <owl:Restriction>
               <owl:onProperty>
                <owl:ObjectProperty    rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#part_of"/>
               </owl:onProperty>
               <owl:someValuesFrom>
                <owl:Class   rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#haulage_company"/>
               </owl:someValuesFrom>
              </owl:Restriction>
              <owl:Class   rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#haulage_company"/>
            </owl:unionOf>
          </owl:Class>
         </owl:someValuesFrom>
        </owl:Restriction>
     </owl:equivalentClass>
    <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >haulage worker</rdfs:label>
  </owl:Class>
  <owl:Class rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#man">
    <owl:equivalentClass>
      <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
          <owl:Class rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#adult"/>
          <owl:Class rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#person"/>
          <owl:Class rdf:about="http://owl.cs.manchester.ac.uk/2009/07/sssw/people#male"/>
        </owl:intersectionOf>
      </owl:Class>
    </owl:equivalentClass>
  </owl:Class>
</rdf:RDF>

这是我得到的输出(它们不是正确的输出):

-----------------------------------------
    | class               | equivalentClass |
    =========================================
    | abc:adult           | abc:adult       |
    | abc:adult           | abc:male        |
    | abc:adult           | abc:person      |
    | abc:haulage_company | abc:adult       |
    | abc:haulage_company | abc:male        |
    | abc:haulage_company | abc:person      |
    | abc:haulage_worker  | abc:adult       |
    | abc:haulage_worker  | abc:male        |
    | abc:haulage_worker  | abc:person      |
    | abc:male            | abc:adult       |
    | abc:male            | abc:male        |
    | abc:male            | abc:person      |
    | abc:man             | abc:adult       |
    | abc:man             | abc:male        |
    | abc:man             | abc:person      |
    | abc:person          | abc:adult       |
    | abc:person          | abc:male        |
    | abc:person          | abc:person      |
    | owl:Thing           | abc:adult       |
    | owl:Thing           | abc:male        |
    | owl:Thing           | abc:person      |
    -----------------------------------------

预期的输出将是这样的:

-----------------------------------------
    | class               | equivalentClass |
    =========================================
    | abc:adult           | abc:adult       |
    | abc:adult           | abc:male        |
    | abc:adult           | abc:person      |
    | abc:haulage_company |                 |
    | abc:haulage_company |                 |
    | abc:haulage_company |                 |
    | abc:haulage_worker  |                 |
    | abc:haulage_worker  |                 |
    | abc:haulage_worker  |                 |
    | abc:male            | abc:adult       |
    | abc:male            | abc:male        |
    | abc:male            | abc:person      |
    | abc:man             | abc:adult       |
    | abc:man             | abc:male        |
    | abc:man             | abc:person      |
    | abc:person          | abc:adult       |
    | abc:person          | abc:male        |
    | abc:person          | abc:person      |
    | owl:Thing           |                 |
    | owl:Thing           |                 |
    | owl:Thing           |                 |
    -----------------------------------------

我应该在我的 SPARQL 查询中进行哪些更改以使我的输出像上一个表一样?

4

1 回答 1

2

稍微清理一下您的查询,我们有:

prefix abc: <http://owl.cs.manchester.ac.uk/2009/07/sssw/people#>
prefix ghi: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix mno: <http://www.w3.org/2001/XMLSchema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix list: <http://jena.hpl.hp.com/ARQ/list#>

select distinct ?class ?equivalentClass where {
  ?class a owl:Class .
  filter( strstarts(str(?class),str(owl:)) ||     # since "owl:" is an IRI, you can 
          strstarts(str(?class),str(abc:))    )   # use str(owl:) and str(:abc)

  ?x a owl:Class ; 
     owl:intersectionOf ?list .
     ?list rdf:rest*/rdf:first ?equivalentClass .
}
group by ?class ?equivalentClass
order by ?class                                   # ?class, not ?no

您的问题在于您正在选择?class,它可以是owl:Class本体中的每个(只要它以适当的前缀开头),然后?equivalentClass从 的交集类列表中选择?x,并且与?no ?class?x没有任何联系`。)?class. (You were also sorting by, but I think you meant to sort by

如果我们以更易于阅读的格式(例如 Turtle)查看数据,找出要编写的正确查询会更容易。在 Turtle 中,man类是:

ns0:man  a                   owl:Class ;
        owl:equivalentClass  [ a                   owl:Class ;
                               owl:intersectionOf  ( ns0:adult ns0:person ns0:male )
                             ] .

您正在寻找的东西是owl:Classes,与owl:equivalentClass其他东西相关,它是owl:Class,并且具有 的列表值owl:intersectionOf。这在 SPARQL 中并不太难,而且查询实际上与这个 Turtle 文本具有相同的结构:

prefix abc: <http://owl.cs.manchester.ac.uk/2009/07/sssw/people#>
prefix ghi: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix mno: <http://www.w3.org/2001/XMLSchema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix list: <http://jena.hpl.hp.com/ARQ/list#>

select distinct ?class ?otherClass where {
  ?class a owl:Class ;
         owl:equivalentClass [ a owl:Class ;
                               owl:intersectionOf [ rdf:rest*/rdf:first ?otherClass ] ] .
  filter( strstarts(str(?class),str(owl:)) ||
          strstarts(str(?class),str(abc:))    )
}
group by ?class ?otherClass
order by ?class

我将变量名从 更改equivalentClassotherClass,因为adult,maleperson等同man. 他们的交点是。使用 Jena 的命令行sparql工具,您将获得如下结果:

$ sparql --data data.rdf --query query.rq
------------------------
| class   | otherClass |
========================
| abc:man | abc:adult  |
| abc:man | abc:male   |
| abc:man | abc:person |
------------------------

此查询仅检索与某个交集等效的类。您的预期结果显示了 IRI 以abc:or开头的所有类owl:,这意味着额外的结构实际上是可选的,因此我们通过将可选部分包装在 中相应地调整查询optional { … },我们得到了我们正在寻找的结果:

prefix abc: <http://owl.cs.manchester.ac.uk/2009/07/sssw/people#>
prefix ghi: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix mno: <http://www.w3.org/2001/XMLSchema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix list: <http://jena.hpl.hp.com/ARQ/list#>

select distinct ?class ?otherClass where {
  ?class a owl:Class .
  optional { 
    ?class owl:equivalentClass [ a owl:Class ;
                                 owl:intersectionOf [ rdf:rest*/rdf:first ?otherClass ] ] .
  }
  filter( strstarts(str(?class),str(owl:)) ||
          strstarts(str(?class),str(abc:))    )
}
group by ?class ?otherClass
order by ?class
$ sparql --data data.rdf --query query.rq
------------------------------------
| class               | otherClass |
====================================
| abc:adult           |            |
| abc:haulage_company |            |
| abc:haulage_worker  |            |
| abc:male            |            |
| abc:man             | abc:adult  |
| abc:man             | abc:male   |
| abc:man             | abc:person |
| abc:person          |            |
| owl:Thing           |            |
------------------------------------
于 2013-10-22T18:20:27.350 回答