LaknathR
LaknathR

Reputation: 195

Get string value of semantic property

In my ontology I have the class called "festival" and it has property value called "CRISTMAS". It's a string type value. How can I get this value using Jena OWL API?

Upvotes: 1

Views: 3538

Answers (2)

Tony
Tony

Reputation: 33

I guess you have a instance in the class and the value of a property for this instance is a String.

If so, do the following steps:

  1. create OntModel and read file.

    OntModel ontModel = ModelFactory.createOntologyModel( OntModelSpec.XXX);
    ontModel.read(file:xxx);
    
  2. get class and property:

    OntClass iClass =ontModel.getOntClass(className);
    OntProperty iProperty= ontModel.getOntProperty(propertyName);
    
  3. get instance and output the string:

    for (ExtendedIterator<? extends OntResource> it= iClass.listInstances(true);it.hasNext();) {
        Individual ins = (Individual) it.next();
    
        RDFNode iValue = ins.getPropertyValue(iProperty);
    
        System.out.println(iValue.toString());
    }   
    

Upvotes: 1

cygri
cygri

Reputation: 9492

  1. Load your ontology into an OntModel. I assume you did that already.

  2. Use the getProperty(String uri) method of the OntModel to retrieve the property.

  3. Use the getOntClass(String uri) method of the OntModel to retrieve the class. It returns an OntClass.

  4. Use the getPropertyValue(Property property) method of the OntClass to get the value. It returns an RDFNode.

  5. To turn the RDFNode into a string, either use simply toString(), or do myRDFNode.asLiteral().getString().

Upvotes: 0

Related Questions