开发者

Get string value of semantic property

In my ontology I have the class called "festival" and it has pr开发者_如何学JAVAoperty value called "CRISTMAS". It's a string type value. How can I get this value using Jena OWL API?


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());
    }   
    


  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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜