如何获取属于某个类的个体,对象属性和数据属性,并在sparql中具有特定的注释

时间:2014-03-26 07:36:48

标签: rdf sparql ontology

我希望从属于某个类的本体中检索个体,对象属性和数据属性,并且还具有给定的注释。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wel: <http://www.semanticweb.org/dell/ontologies/2014/2/untitled-ontology-11#>

SELECT *
WHERE {?s a wel:Sentence1 }

使用上面的代码,我可以获得属于类wel:Sentence1的所有个体,对象属性和数据属性。但我的目标是获取属于类wel:Sentence1且具有注释的个人,对象属性和数据属性

wel:belongs_to_story="SnowWhiteAndTheSevenDwarfs"

我按照Filter on annotation - SPARQL中给出的方法,但我没有得到任何答案。这是我的疑问。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wel: <http://www.semanticweb.org/dell/ontologies/2014/2/untitled-ontology-11#>

SELECT *
WHERE {
  ?s wel:belongs_to_story ?label  .
  FILTER(CONTAINS(?label, "SnowWhiteAndTheSevenDwarfs"))
}

包含belongs_to_story属性的数据的一部分是SnowWhite个人的描述:

<!-- http://www.semanticweb.org/dell/ontologies/2014/2/untitled-ontology-11#SnowWhite -->

<NamedIndividual rdf:about="&untitled-ontology-11;SnowWhite">
    <rdf:type rdf:resource="&untitled-ontology-11;Heroien"/>
    <untitled-ontology-11:appearance>beatiful</untitled-ontology-11:appearance>
    <untitled-ontology-11:quality>kind</untitled-ontology-11:quality>
    <untitled-ontology-11:appearance>loving</untitled-ontology-11:appearance>
    <untitled-ontology-11:appearance>fairest</untitled-ontology-11:appearance>
    <untitled-ontology-11:ate rdf:resource="&untitled-ontology-11;Apple"/>
    <untitled-ontology-11:loves rdf:resource="&untitled-ontology-11;Prince"/>
    <untitled-ontology-11:belongs_to_story rdf:resource="&untitled-ontology-11;SnowWhiteAndTheSevenDwarfs"/>
    <untitled-ontology-11:heroienOf rdf:resource="&untitled-ontology-11;SnowWhiteAndTheSevenDwarfs"/>
    <untitled-ontology-11:livedWith rdf:resource="&untitled-ontology-11;StepMother"/>
    <untitled-ontology-11:sleptOn rdf:resource="&untitled-ontology-11;bed"/>
    <untitled-ontology-11:saw rdf:resource="&untitled-ontology-11;beds"/>
    <untitled-ontology-11:livedIn rdf:resource="&untitled-ontology-11;castle"/>
    <untitled-ontology-11:saw rdf:resource="&untitled-ontology-11;chairs"/>
    <untitled-ontology-11:saw rdf:resource="&untitled-ontology-11;cottage"/>
    <untitled-ontology-11:wentIn rdf:resource="&untitled-ontology-11;cottage"/>
    <untitled-ontology-11:saw rdf:resource="&untitled-ontology-11;cups"/>
    <untitled-ontology-11:fellOn rdf:resource="&untitled-ontology-11;dead"/>
    <untitled-ontology-11:fellOn rdf:resource="&untitled-ontology-11;ground"/>
    <untitled-ontology-11:combed rdf:resource="&untitled-ontology-11;hair"/>
    <untitled-ontology-11:ranInto rdf:resource="&untitled-ontology-11;jungle"/>
    <untitled-ontology-11:felt rdf:resource="&untitled-ontology-11;tired"/>
</NamedIndividual>

另一个是带有注释的公理:

<Axiom>
    <untitled-ontology-11:seq rdf:resource="&untitled-ontology-11;Sentence2"/>
    <annotatedSource rdf:resource="&untitled-ontology-11;SnowWhite"/>
    <untitled-ontology-11:belongs_to_story rdf:resource="&untitled-ontology-11;SnowWhiteAndTheSevenDwarfs"/>
    <annotatedTarget rdf:resource="&untitled-ontology-11;StepMother"/>
    <annotatedProperty rdf:resource="&untitled-ontology-11;livedWith"/>
</Axiom>

我想连接这两个查询。请帮忙。

1 个答案:

答案 0 :(得分:1)

您的术语表明可能存在一些概念上的误解

  
SELECT *
WHERE {?s a wel:Sentence1 }
     

使用上面的代码我可以得到所有个体,对象属性   和属于类wel:Sentence1的数据属性。

如果您在OWL中工作,并且wel:Sentence是一个OWL课程,那么?s a wel:Sentence1形式的三元组中唯一应该是个人。只有个人属于OWL班级;对象属性和数据属性不属于OWL类,因此不应该是属于wel:Sentence1的任何对象属性或数据属性。

无论如何,a只是属性rdf:type的简写。听起来您正在尝试检索具有字符"SnowWhiteAndTheSevenDwarfs"作为属性wel:belongs_to_story的值的个人,在这种情况下您应该能够使用查询

#-- ...prefixes...

select * where {
  ?s wel:belongs_to_story "SnowWhiteAndTheSevenDwarfs"
}

但是,your data实际上看起来更像是这样(来自您提供的链接):

<!-- http://www.semanticweb.org/dell/ontologies/2014/2/untitled-ontology-11#SnowWhite -->

<NamedIndividual rdf:about="&untitled-ontology-11;SnowWhite">
    <rdf:type rdf:resource="&untitled-ontology-11;Heroien"/>
    <!-- … -->
    <untitled-ontology-11:belongs_to_story rdf:resource="&untitled-ontology-11;SnowWhiteAndTheSevenDwarfs"/>
    <!-- … -->
</NamedIndividual>

belongs_to_story属性的值不是字符串;它是另一种资源,由IRI标识,缩写为SnowWhiteAndTheSevenDwarfs。如果您使用更易读的Turtle序列化,那么这实际上会更清楚一些,那么您的数据就是:

wel:SnowWhite  a              wel:Heroien , owl:NamedIndividual ;
        # …
        wel:belongs_to_story  wel:SnowWhiteAndTheSevenDwarfs ;
        # …
        wel:saw               wel:beds , wel:cups , wel:cottage , wel:chairs ;
        wel:sleptOn           wel:bed ;
        wel:wentIn            wel:cottage .

Turtle序列化实际上与SPARQL语法非常相似。您的查询需要如下(使用Jena的命令行工具生成结果):

prefix wel: <http://www.semanticweb.org/dell/ontologies/2014/2/untitled-ontology-11#>

select * where {
  ?s wel:belongs_to_story wel:SnowWhiteAndTheSevenDwarfs
}
------------------
| s              |
==================
| _:b0           |
| wel:castle     |
| _:b1           |
| wel:SnowWhite  |
| wel:StepMother |
------------------

空白节点(由_:b0_:b1表示是您已注释的公理:

[ a                         owl:Axiom ;
  wel:belongs_to_story
          wel:SnowWhiteAndTheSevenDwarfs ;
  wel:seq  wel:Sentence1 ;
  owl:annotatedProperty     wel:livedIn ;
  owl:annotatedSource       wel:SnowWhite ;
  owl:annotatedTarget       wel:castle
] .

[ a                         owl:Axiom ;
  wel:belongs_to_story
          wel:SnowWhiteAndTheSevenDwarfs ;
  wel:seq  wel:Sentence2 ;
  owl:annotatedProperty     wel:livedWith ;
  owl:annotatedSource       wel:SnowWhite ;
  owl:annotatedTarget       wel:StepMother
] .