提取类内容,例如(onProperty,somevalueFrom,unioinOf(包含集合)和equivalentClass)

时间:2013-10-25 11:42:29

标签: rdf sparql jena owl reasoning

我正在尝试从owl文件中提取类的内容,该文件由onProperty和someValuesFrom组成,其中someValueFrom由包含unionOf(onProperty,someValueFrom和equivalentClass)的类组成,我创建了一个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查询才能返回此结果?

非常感谢提前:)

1 个答案:

答案 0 :(得分: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和{{1} }对象属性和类(如果它不是空节点),否则绑定?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           |                |                  |                 |
-----------------------------------------------------------------------------

在answer.semanticweb.com问题的答案中描述了保存未绑定到空白节点的值的模式。Binding a variable only when another is non-blank?我们的想法是使用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 } 来确保{{1}始终具有未定义的值,然后使用values ?unbound { UNDEF }?unboundbind或其他值分配给投影变量。从本质上讲,就是这样:

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 | ----------------------------------------------------------------------- 和{{1}这样的行}。