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:
create OntModel and read file.
OntModel ontModel = ModelFactory.createOntologyModel( OntModelSpec.XXX); ontModel.read(file:xxx);
get class and property:
OntClass iClass =ontModel.getOntClass(className); OntProperty iProperty= ontModel.getOntProperty(propertyName);
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()); }
Load your ontology into an
OntModel
. I assume you did that already.Use the
getProperty(String uri)
method of theOntModel
to retrieve the property.Use the
getOntClass(String uri)
method of theOntModel
to retrieve the class. It returns anOntClass
.Use the
getPropertyValue(Property property)
method of theOntClass
to get the value. It returns anRDFNode
.To turn the
RDFNode
into a string, either use simplytoString()
, or domyRDFNode.asLiteral().getString()
.
精彩评论