Tanny
Tanny

Reputation: 540

How to read nested OWL properties?

I have an OWL file, a sample snippet of which is as below.

<owl:NamedIndividual rdf:ID="001">
  <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">some literal 1</rdfs:label>
  <has_legislative_reference rdf:resource="#002"/>
  <has_legislative_reference rdf:resource="#003"/>
  <has_legislative_reference rdf:resource="#004"/>
  <has_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">some literal 2</has_name>
  <has_name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">some literal 3</has_name>
  <rdf:type rdf:resource="#some_class"/>
  <has_englishkeywords>
    <owl:NamedIndividual rdf:ID="005">
      <rdf:type rdf:resource="#006"/>
      <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">some literal 4</rdfs:label>
    </owl:NamedIndividual>
  </has_englishkeywords>
</owl:NamedIndividual>

Now, I have been able to fetch all the literals which are available on the first level of each statement, i.e. some literal 1, some literal 2 and some literal 3. However I am at a loss as to how to fetch some literal 4.

PS: For all the cases which are at the same level as the main OWL namespace, I am using the method given below and thereafter iterating over each of those NamedIndividuals.

ResIterator namedIndividuals = m.listResourcesWithProperty(RDF.type, OWL.NamedIndividual);

The OWL.NamedIndividual is defined as:

AnnotationProperty NamedIndividual = m_model.createAnnotationProperty("http://www.w3.org/2002/07/owl#NamedIndividual");

Now for the nested objects, do I need to create a temporary namespace to read the nested literals? If yes, how? Or is there any other efficient way to do the same? Any guidance is appreciated.

Upvotes: 0

Views: 310

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85873

This is the RDF/XML serialization of the RDF mapping of an OWL ontology. You can either query it as RDF, in which case you would want to use an RDF API (like Jena), or a SPARQL query (which you can also do with Jena). Alternatively, you can use an OWL based API and look for the particular types of axioms that you're interested in. Jena's OntModel API provides some OWL support, and that might be enough for you.

In this case, your data doesn't contain "nested properties", but rather there's an individual (001), that has another individual (005) as a value of the has_englishkeywords property, and that individual has a value of the rdfs:label property (some literal4).

What you're saying about "OWL namespaces" and "nested namespaces" doesn't really make sense. RDF is a graph-based representation. There are resources (URIs and blank nodes) that are linked to others (URIs, blank nodes, and literals) by properties (URIs). In this case, if you're just interested in finding literals reachable from some starting point, it's probably easiest to just use a SPARQL query:

prefix : <urn:ex:>

select ?literal {
  values ?start { <http://example.org/001> }  #-- value of ?start
  ?start (:|!:)* ?literal                     #-- property path with wildcard
  filter isLiteral(?literal)                  #-- make sure ?literal is a literal
}

There are lots of examples in other questions and answers of how to run SPARQL queries using Jena, as well as property paths, wildcards, etc.

Upvotes: 2

Related Questions