0

I have created an ontology in protege and want to display the values of object properties and save the values in an array so that i can use the value to perform reasoning .The problem is i am not able to retrieve the datatype values only the domain and range are being displayed ,but no errors are there please help to find the solution

      import java.io.IOException;
 import com.hp.hpl.jena.ontology.DatatypeProperty;
 import com.hp.hpl.jena.ontology.EnumeratedClass;
 import com.hp.hpl.jena.ontology.Individual;
 import com.hp.hpl.jena.ontology.OntClass;
 import com.hp.hpl.jena.ontology.OntModel;
 import com.hp.hpl.jena.ontology.OntResource;
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.RDFNode;
 import com.hp.hpl.jena.util.FileManager;
 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
 public class GetStartedSemWeb {
 static String defaultNameSpace ="http://semanticweb.org/ontologies#";
 Model schema = null;
  public static void main(String[] args) throws IOException 
  {
    GetStartedSemWeb getsemweb = new GetStartedSemWeb();
    System.out.println(" Adding  student ontology ");
    getsemweb.loadontology();
    }
   private void loadontology() throws IOException
    {
   schema = ModelFactory.createOntologyModel();
      java.io.InputStream inschema =                    FileManager.get().open("C:/Users/Desktop/Documents/Extracting knowledge from ontology               using jena/getstarted.owl");
   schema.read(inschema,defaultNameSpace);
    System.out.println("new  ontology added");

   ExtendedIterator it = ((OntModel) schema).listClasses();
   while(it.hasNext()) 
     {
           OntClass cls= (OntClass)it.next();
            System.out.println("URI of classes of Merged University Ontology                are "+cls.getURI());
            ExtendedIterator pinstance = ((OntClass)cls).listInstances();
            while(pinstance.hasNext()) 
            {
                Individual pinstance1= (Individual)pinstance.next();
                        System.out.println("Individual of          "                        +cls.getLocalName()  +      pinstance1.getLocalName());
            ExtendedIterator dp = ((OntModel)                                                                                 schema).listDatatypeProperties();
                         while(dp.hasNext())
                        {
                            DatatypeProperty p = (DatatypeProperty) dp.next();
                            if (p.isDatatypeProperty() && p.getDomain()!=null &&                        p.getRange()!=null)
                            {
                                System.out.println("Data Property Name: "+                          p.getLocalName());
                                System.out.println("Domain: "+                                  p.getDomain().getLocalName());
                                EnumeratedClass e = null;
                                ExtendedIterator i = null;
                                if(p.getRange().asClass().isEnumeratedClass())
                                {
                                    e = p.getRange().asClass().asEnumeratedClass();
                                    i = e.getOneOf().iterator();

                                   RDFNode prop = null;
                                     String s=null;
                                    System.out.println("Range: ");
                                    while(i.hasNext())
                                    {
                                        prop = (RDFNode) i.next();
             s=((Object)   prop).getClass().toString().split("\\^\\^")[0];

             RDFNode propvalue = ((OntResource) prop).getPropertyValue(p);                                  
        System.out.println(" Property value is"         +propvalue);
                                        System.out.println(s);
                                    }
                                }
                                else
                    System.out.println("Range: "+ p.getRange().getLocalName());
                                }

                                System.out.println("\n");
                            }

                        }

            }

}               
schema.close();

} }

        OWL file content

          // OWL file content
                      <rdf:RDFxml:base="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl"><owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl"><owl:versionIRI rdf:resource="http://www.semanticweb.org/ontologies/2013/9/Ontology1382504980350.owl"/></owl:Ontology><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Datatypes
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired -   -><owl:ObjectProperty    rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired"> <rdfs:range rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:ObjectProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#isAcquiredBy --><owl:ObjectProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#isAcquiredBy"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdfs:range rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><owl:inverseOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasaAcquired"/></owl:ObjectProperty><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAge --> <owl:DatatypeProperty r df:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAge"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2002/07/owl#real"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAggregate --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasAggregate"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasEmailaddress --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasEmailaddress"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasGender --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasGender"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasName --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasName"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasPostaladress --><owl:DatatypeProperty rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#hasPostaladress"><rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/></owl:DatatypeProperty><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal"><rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/></owl:Class><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student --><owl:Class rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Student"/><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
 --><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jack --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jack"><hasGender>male</hasGender><hasAggregate>50</hasAggregate><hasAge>20</hasAge><hasPostaladress>illonious chicago</hasPostaladress><hasName>jack</hasName><hasEmailaddress>jackid</hasEmailaddress></owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jill_ --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jill_"><hasGender>male</hasGender><hasEmailaddress>jillid</hasEmailaddress><hasName>jill</hasName><hasAggregate>34</hasAggregate><hasPostaladress>chicago</hasPostaladress><hasAge>21</hasAge></owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jim --><owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#jim"><hasEmailaddress>jimid</hasEmailaddress><hasAggregate>70</hasAggregate><hasName>jim</hasName><hasGender>male</hasGender><hasAge>23</hasAge><hasPostaladress>chicago</hasPostaladress></owl:NamedIndividual><!-- 
///////////////////////////////////////////////////////////////////////////////////////
//
// General axioms
//
///////////////////////////////////////////////////////////////////////////////////////
 --><rdf:Description><rdf:type rdf:resource="http://www.w3.org/2002/07/owl#AllDisjointClasses"/><owl:members rdf:parseType="Collection"><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Academic"/><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#General"/><rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/9/23/Ontology1382505604507.owl#Personal"/></owl:members></rdf:Description></rdf:RDF>
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • 1
    Your code is formatted rather unreadably, and your OWL snippet doesn't appear to show any triples using the properties, so we have no way knowing what you expect your code to produce. – Joshua Taylor Sep 18 '14 at 12:41

1 Answers1

0

Note that the range of a DatatypeProperty cannot logically be an EnumeratedClass. An enumerated class is an owl class defined to be a fixed collection of individuals. Those individuals cannot be literals. The values within the range of a DatatypeProperty can only be a Literal.

You want to use DataRange, which represents an enumeration of Literal values. Replace your tests of .asClass().isEnumeratedClass() with .isDataRange()

In your data, you want the range for your property to be crafted using owl:DataRange and owl:oneOf as demonstrated here.

This applies only if you wish to explore the theoretical space of allowed values for an owl:DatatypeProperty whose range is enumerated as a owl:DataRange. While your code implies that is your goal, the wording of your question suggests an alternative goal: you have some owl:DatatypeProperty, ex:p, and you wish to identify all of the objects of that property as they exist within your model.

If that is your goal, then you can simply ask the model for the literal values:

final ExtendedIterator<Literal> observedRange =
        new NiceIterator<RDFNode>()
        .andThen(Iter.distinct(model.listObjectsOfProperty(p)))
        .mapWith(new Map1<RDFNode,Literal>(){
            @Override
            public Literal map1(final RDFNode o) {
                return o.asLiteral();
            }});
Rob Hall
  • 2,693
  • 16
  • 22