Search code examples
javardfowljena

How to get in Jena the exact range of an ObjectProperty with restrictions?


I have an owl/rdf schema with object properties with range restrictions, and I'm unable to get with the Jena API the effective range class.

I would like to get the classes which are defined in the ontology as the range of a property. For example, with the following schema:

<rdf:RDF xmlns="http://localhost/Test"
     xml:base="http://localhost/TEST"
     xmlns:sf="http://www.opengis.net/ont/sf#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
  <owl:Ontology rdf:about="http://localhost/TEST"/>
  <owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
  <owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
   <owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
       <rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
       <rdfs:range>
           <owl:Restriction>
               <owl:onProperty rdf:resource="http://localhost/TEST#hasWaypoint"/>
               <owl:someValuesFrom rdf:resource="http://localhost/TEST#Waypoint"/>
           </owl:Restriction>
       </rdfs:range>
   </owl:ObjectProperty>  
 </rdf:RDF>

And I'm doing:

  model.read(...);
  OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
  ExtendedIterator properties = property.listDomain();
  OntClass thisClass = (OntClass) properties.next();
  String dom_localname = thisClass.getLocalName();
  if (thisClass.getLocalName() == null) {
     Restriction restriction = thisClass.asRestriction();
     OntResource resource = restriction.getOnProperty().getDomain();
     dom_localname = resource.getLocalName();
  }
  properties = property.listRange();
  thisClass = (OntClass) properties.next();
  String range_localname = thisClass.getLocalName();
  if (thisClass.getLocalName() == null) {
     Restriction restriction = thisClass.asRestriction();
     OntResource resource = restriction.getOnProperty().getRange();
     range_localname = resource.getLocalName();
  }      
  System.out.println("Domain localName: " + dom_localname);
  System.out.println("Range localName: " + range_localname);

I expected to get this result:

Domain localName: Aircraft
Range localName: Waypoint

But I get:

Domain localName: Aircraft
Range localName: null

This same code works without any problem if I don't use restrictions, for example, with the following schema:

<rdf:RDF xmlns="http://localhost/Test"
     xml:base="http://localhost/TEST"
     xmlns:sf="http://www.opengis.net/ont/sf#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
  <owl:Ontology rdf:about="http://localhost/TEST"/>
  <owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
  <owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
  <owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
   <owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
       <rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
       <rdfs:range rdf:resource="http://localhost/TEST#Waypoint"/>
   </owl:ObjectProperty>  
 </rdf:RDF>

I get the expected result:

Domain localName: Aircraft
Range localName: Waypoint

It looks that I am not handling correctly cases where a property use a restriction. What Am I doing wrong?


Solution

  • To complete the previous answer, if the restriction is qualified, Jena does not have a direct way to get the range. But you can do:

    model.read(...);
    OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");   
    ExtendedIterator properties = property.listRange();
    OntClass thisClass = (OntClass) properties.next();
    String range_localname = thisClass.getLocalName();
    if (range_localname == null) {
         Resource resource = null;
         Restriction restriction = thisClass.asRestriction();
         if (restriction.isAllValuesFromRestriction()) {
            AllValuesFromRestriction restriction1 = restriction.asAllValuesFromRestriction();
            resource = restriction1.getAllValuesFrom();
         } else if (restriction.isHasValueRestriction()) {
            HasValueRestriction restriction1 = restriction.asHasValueRestriction();
            resource = restriction1.getHasValue().asResource();
         } else if (restriction.isSomeValuesFromRestriction()) {
            SomeValuesFromRestriction restriction1 = restriction.asSomeValuesFromRestriction();
            resource = restriction1.getSomeValuesFrom();
         } else if (restriction.isMaxCardinalityRestriction()) {
            MaxCardinalityRestriction restriction1 = restriction.asMaxCardinalityRestriction();
            resource = restriction1.getIsDefinedBy();
         } else if (restriction.isMinCardinalityRestriction()) {
            MinCardinalityRestriction restriction1 = restriction.asMinCardinalityRestriction();
            resource = restriction1.getIsDefinedBy();
         } else if (restriction.isCardinalityRestriction()) {
            CardinalityRestriction restriction1 = restriction.asCardinalityRestriction();
            resource = restriction1.getIsDefinedBy();     
         } else {
            // qualified restrictions can be handled like that
            RDFNode node = restriction.getPropertyValue(OWL2.onClass);
            resource = node.asResource();
         }
         if (resource != null) {
            range_localname = resource.getLocalName();
         }
      }
      System.out.println("Range localName: " + range_localname);