Search code examples
javasemanticsjenaontology

Using OntProperty and DatatypeProperty - Jena Ontology


    OntModel onto = ModelFactory.createOntologyModel(
            OntModelSpec.OWL_MEM_MICRO_RULE_INF, null );

    String inputFileName = "./src/test.xml";    

    InputStream in = FileManager.get().open(inputFileName);
    if (in == null) {
        throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }

    onto.read(new InputStreamReader(in), "");        

    //ns is the namespace...
    OntClass userClass = onto.getOntClass(ns+"User");

    Individual dada = onto.createIndividual(ns+"Daryl", userClass);

    Property prefBathtub = onto.getProperty(ns+"prefersBathtub");
    Property prefBathtubWt = onto.getProperty(ns+"prefersBathtubWeight");

    dada.addLiteral(prefBathtub, true);
    dada.addLiteral(prefBathtubWt, 0.30);

    OutputStream out = new FileOutputStream("./src/test2.xml");
    onto.write( out, "RDF/XML"); // readable rdf/xml
    out.close();

How do I use OntProperty and/or DatatypeProperty instead of just Property?

By using Property do I get the same amount of expressiveness?


Solution

  • To get an ObjectProperty object from an ontology model, use OntModel.getObjectProperty(). Likewise for datatype properties, etc. The Ont classes are more expressive in the sense that they contain convenience API for getting, for example, the super-properties of a property, with one method call. However, as the convenience API only accesses the underlying triples in the graph, there is strictly speaking nothing you can do with an ObjectProperty that you can't do with a Property. It's just harder work!

    Incidentally, Jena allows you to access other facets of an underlying RDF resource with the .as() method. So:

    Property p = myModel.getProperty( "http://example.com/foo#p" );
    OntProperty op = p.as( OntProperty.class );