Search code examples
javardfsparqlowl

Querying all "AnnotationAssertions" for a class/individual


I am trying to query all AnnotationAssertion pertaining to an class/individual.

Below is a snippet of my source:

        <AnnotationAssertion>
            <AnnotationProperty abbreviatedIRI="rdfs:comment"/>
            <IRI>#Car</IRI>
            <Literal xml:lang="en" datatypeIRI="&rdf;PlainLiteral">A car has four tyres</Literal>
        </AnnotationAssertion>
        <AnnotationAssertion>
            <AnnotationProperty IRI="http://www.w3.org/2004/02/skos/core#definition"/>
            <IRI>#car</IRI>
            <Literal xml:lang="en" datatypeIRI="&rdf;PlainLiteral">a road vehicle, typically with four wheels, powered by an internal combustion engine and able to carry a small number of people.</Literal>
        </AnnotationAssertion>
        <AnnotationAssertion>
            <AnnotationProperty IRI="http://www.w3.org/2004/02/skos/core#example"/>
            <IRI>#car</IRI>
            <Literal xml:lang="en" datatypeIRI="&rdf;PlainLiteral">Audi</Literal>
        </AnnotationAssertion>
 etc..

I am trying to query AnnotationProperties associated with this class/individual and its list of AnnotationAssertions.

SPARQL-DL API provides an possibility to query annotationproperties via

Annotation(a, b, c)

Any pointers towards querying AnnotationAssertions would be really helpful.


Solution

  • You mention SPARQL-DL in the question, but this is also tagged with , and the answer in plain SPARQL isn't too complex. It would be something along the lines of:

    prefix  owl: <http://www.w3.org/2002/07/owl#>
    
    select ?s ?p ?o where { 
      ?s ?p ?o .
      ?p a owl:AnnotationProperty .
    }
    

    The difficulty that you might encounter here, though, is that it might not be the case that every annotation property is actually declared. That will make things harder, but you can take a stab at it by asking for all triples except those where the property is a known ObjectProperty or DatatypeProperty, and except those that are used in the OWL to RDF mapping (properties that start with rdf: or owl:, and certain rdfs: properties). E.g.,

    prefix owl: <http://www.w3.org/2002/07/owl#>
    prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    
    select ?s ?p ?o where {
      ?s ?p ?o .
    
      #-- don't include object or datatype properties    
      filter not exists {
        values ?type { owl:ObjectProperty owl:DatatypeProperty }
        ?p a ?type 
      }
    
      #-- don't include properties from rdf: or owl: namespaces  
      filter( !strstarts( str(?p), str(rdf:) ) )
      filter( !strstarts( str(?p), str(owl:) ) )
    
      #--  don't include rdfs: properties used in OWL to RDF mapping
      filter( ?p not in ( rdfs:range, rdfs:domain, rdfs:subClassOf, rdfs:subPropertyOf ) )
    }