how to get individuals, object properties and data properties belonging to a class and also has a specific annotation in sparql [closed]

StackOverflow https://stackoverflow.com/questions/22654361

  •  21-06-2023
  •  | 
  •  

Question

I want to retrieve the individuals, object properties and data properties from my ontology that belongs to a class and also that has a given annotation.

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 }

Using the above code I can get all the individuals, object properties and data properties that belongs to class wel:Sentence1. But my target is to get the individuals, object properties and data properties that belongs to class wel:Sentence1 and that has the annotation

wel:belongs_to_story="SnowWhiteAndTheSevenDwarfs"

I followed the method given in Filter on annotation - SPARQL, but I don't get any answers. This is my query.

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"))
}

One portion of the data that includes the belongs_to_story property is the description of the SnowWhite individual:

<!-- 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>

Another is an axiom that has an annotation:

<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>

I want to connect those two queries. Please help.

Was it helpful?

Solution

Your terminology indicates that there might be some conceptual misunderstanding

SELECT *
WHERE {?s a wel:Sentence1 }

Using the above code I can get all the individuals, object properties and data properties that belongs to class wel:Sentence1.

If you're working in OWL, and wel:Sentence is an OWL class, then the only things that should be in triples of the form ?s a wel:Sentence1 are individuals. Only individuals belong to OWL classes; object properties and data properties don't belong to OWL classes, so there shouldn't be any object properties or data properties belonging to wel:Sentence1 to retrieve at all.

At any rate, a is just shorthand for the property rdf:type. It sounds like you're trying to retrieve individuals that have the string "SnowWhiteAndTheSevenDwarfs" as the value of the property wel:belongs_to_story, in which case you should be able to use the query

#-- ...prefixes...

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

However, your data actually looks more like this (from the link that you provided):

<!-- 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>

The value of the belongs_to_story property isn't a string; it's another resource, identified by an IRI that would be abbreviated as SnowWhiteAndTheSevenDwarfs. This is actually a bit clearer if you use the more human readable Turtle serialization, where your data would then be:

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 .

The Turtle serialization is actually very similar to the SPARQL syntax. Your query needs to be the following (with results generated by Jena's command line tools):

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 |
------------------

The blank nodes (indicated by _:b0 and _:b1 are the axioms that you've annotated:

[ 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
] .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top