0

我正在尝试从包含 onProperty 和 someValuesFrom 的 owl 文件中提取类的内容,其中 someValueFrom 由包含 unionOf(onProperty、someValueFrom 和等效类)的类组成,我创建了一个 SPARQL 查询来提取这些数据,但是每次它返回空白节点,例如“ :b0”和“ :b1”。有谁知道我应该如何处理我的查询以使其提供所需的结果。这是我的猫头鹰文件:

<?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>
 </rdf:RDF>

这是我创建的 SPARQL 查询:

    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 ?ObjectProperty ?someValuesFrom ?otherClass where { ?class a  owl:Class .


    OPTIONAL{
       ?class owl:equivalentClass ?e .
        ?e a owl:Restriction .
#       ?e owl:onProperty ?ObjectProperty .
        ?e owl:someValuesFrom [ a owl:Class; 
                                    owl:unionOf [ rdf:first ?    ObjectProperty; 
                                    rdf:rest ?someValuesFrom ;     rdf:rest*/rdf:first ?otherClass]] .  


      }
     FILTER( STRSTARTS(STR(?class),STR(owl:)) || STRSTARTS(STR(?class),STR(abc:)))  
    }group by ?class  ?ObjectProperty ?someValuesFrom ?otherClass
    order by ?class

这是我得到的结果:

-------------------------------------------------------------------------------
| class               | ObjectProperty | someValuesFrom | otherClass          |
===============================================================================
| abc:haulage_company |                |                |                     |
| abc:haulage_worker  | _:b0           | _:b1           | _:b0                |
| abc:haulage_worker  | _:b0           | _:b1           | abc:haulage_company |
| owl:Thing           |                |                |                     |
-------------------------------------------------------------------------------

但预期的结果是:

-----------------------------------------------------------------------------
| class               | ObjectProperty | someValuesFrom   | otherClass      |
=============================================================================
| abc:haulage_company |                |                  |                 |
| abc:haulage_worker  | abc:works_for  |                  |                 |
| abc:haulage_worker  | abc:part_of    | haulage_company  | haulage_company |
| owl:Thing           |                |                  |                 |
-----------------------------------------------------------------------------

为了返回这个结果,我应该如何处理我的 SPARQL 查询?

提前谢谢了 :)

4

1 回答 1

1

您的查询的问题:

如果您在 Turtle 序列化而不是 RDF/XML 序列化中查看数据,则可能更容易理解 SPARQL 查询的结果。您数据的相关部分是:

ns0:haulage_worker  a        owl:Class ;
        rdfs:comment         ""^^xsd:string ;
        rdfs:label           "haulage worker"^^xsd:string ;
        owl:equivalentClass  [ a                   owl:Restriction ;
                               owl:onProperty      ns0:works_for ;
                               owl:someValuesFrom  [ a            owl:Class ;
                                                     owl:unionOf  ( [ a                   owl:Restriction ;
                                                                      owl:onProperty      ns0:part_of ;
                                                                      owl:someValuesFrom  ns0:haulage_company
                                                                    ] ns0:haulage_company )
                                                   ]

考虑关于 的部分的匹配owl:unionOf。在您的查询中,它是

owl:unionOf  ( [ a                   owl:Restriction ;
                 owl:onProperty      ns0:part_of ;
                 owl:someValuesFrom  ns0:haulage_company ]
               ns0:haulage_company )

列表的元素是具有一些属性的空白节点,并且ns0:haulage_company. 应该匹配其中一些数据的查询是:

owl:unionOf [ rdf:first ?ObjectProperty; 
              rdf:rest ?someValuesFrom ;
              rdf:rest*/rdf:first ?otherClass ]] .  

将匹配?ObjectProperty的是列表的第一个元素,在这种情况下,它不是对象属性,而是一个空白节点。将匹配?someValuesFrom的是表示列表其余部分的列表节点。

修复您的查询:

我不太确定你想从这个查询中返回什么。根据您的预期结果,您似乎在说一个类?class可以与一个owl:someValuesFrom限制相关,在这种情况下,您想要绑定?ObjectProperty?someValuesFrom对象属性和类(如果它不是空白节点),并绑定?otherClass到其他否则相关(非空白)类。你说你期望这些结果:

-----------------------------------------------------------------------------
| class               | ObjectProperty | someValuesFrom   | otherClass      |
=============================================================================
| abc:haulage_company |                |                  |                 |
| abc:haulage_worker  | abc:works_for  |                  |                 |
| abc:haulage_worker  | abc:part_of    | haulage_company  | haulage_company |
| owl:Thing           |                |                  |                 |
-----------------------------------------------------------------------------

但如果我的理解是正确的,我认为得到类似的东西会更容易和更有用:

-----------------------------------------------------------------------------
| class               | ObjectProperty | someValuesFrom   | otherClass      |
=============================================================================
| abc:haulage_company |                |                  |                 |
| abc:haulage_worker  | abc:works_for  |                  |                 |
| abc:haulage_worker  | abc:part_of    | haulage_company  |                 |
| abc:haulage_worker  |                |                  | haulage_company |
| owl:Thing           |                |                  |                 |
-----------------------------------------------------------------------------

你可以用这样的查询来做到这一点:

prefix :      <http://owl.cs.manchester.ac.uk/2009/07/sssw/people#>
prefix owl:   <http://www.w3.org/2002/07/owl#>
prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

select distinct ?class ?onProperty ?someValuesFrom ?otherClass where {
  #-- Select each named class.
  ?class a owl:Class .
  filter( !isBlank( ?class ))

  optional {
    #-- Find "related" classes by following a somewhat complex
    #-- property path that will follow equivalent classes,
    #-- existential restrictions, and unionOf expressions.
    ?class (owl:equivalentClass
           |owl:someValuesFrom
           |(owl:unionOf/rdf:rest*/rdf:first))+ ?r .

    #-- Save non-blank related classes as ?otherClass.
    bind(if(isBlank(?r),?unbound,?r) as ?otherClass)

    #-- If the related class is a restriction, then we can 
    #-- take its owl:onProperty and owl:someValuesFrom.
    optional { 
      ?r owl:onProperty ?onProperty ;
         owl:someValuesFrom ?svf .
      bind( if(isBlank(?svf),?unbound,?svf) as ?someValuesFrom )
    }
  }
}
values ?unbound { UNDEF }

保存未绑定到空白节点的值的模式在 answers.semanticweb.com 问题的答案中描述,仅当另一个变量非空白时才绑定一个变量? 这个想法是用来values ?unbound { UNDEF }确保?unbound总是有一个未定义的值,然后使用bind并将一个或一些其他值if分配?unbound给一个投影变量。本质上是这样的:

bind(if(isBlank(...),?unbound,...) as ...)
values ?unbound { UNDEF }

结果是

-----------------------------------------------------------------------
| class            | onProperty | someValuesFrom   | otherClass       |
=======================================================================
| owl:Thing        |            |                  |                  |
| :haulage_company |            |                  |                  |
| :haulage_worker  |            |                  |                  |
| :haulage_worker  | :works_for |                  |                  |
| :haulage_worker  | :part_of   | :haulage_company |                  |
| :haulage_worker  |            |                  | :haulage_company |
-----------------------------------------------------------------------

这确实包括一个:haulage_worker没有与任何其他变量绑定的行,但我认为这没关系,因为您已经想要这样的行owl:Thing:haulage_company

于 2014-03-05T17:06:18.670 回答