一种可能的解决方案如下所示。但是,我要提醒您,您并没有真正按照设计方式使用 OWL 和 RDF 的特性。如果您真的想利用 Jena 的功能,您应该阅读更多有关语义 Web 技术的信息并使用它来指导您的设计。否则,您还不如使用 Java 为您的应用程序设计一个自定义解决方案,而不是尝试将您的设计强制适应它并不真正适合的表示。
package test;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class User1374179_Test
{
public static String ns = "http://example.org/test#";
private OntClass livingBeing;
private OntClass animal;
private OntProperty primeFeatures;
private OntProperty speed;
private OntProperty nature;
public static void main( String[] args ) {
new User1374179_Test().run();
}
public void run() {
OntModel m = createSchema();
showClassProperties( livingBeing );
showClassProperties( animal );
Individual cat1 = createInstance( m, animal, "cat1" );
cat1.addProperty( speed, m.createTypedLiteral( 100 ));
cat1.addProperty( nature, "violent" );
showInstanceProperties( cat1, animal );
}
private OntModel createSchema() {
OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );
livingBeing = m.createClass( ns + "LivingBeing" );
primeFeatures = m.createOntProperty( ns + "primeFeatures" );
primeFeatures.addDomain( livingBeing );
animal = m.createClass( ns + "Animal" );
animal.addSuperClass( livingBeing );
speed = m.createOntProperty( ns + "speed" );
speed.addSuperProperty( primeFeatures );
speed.addDomain( animal );
nature = m.createOntProperty( ns + "nature" );
nature.addSuperProperty( primeFeatures );
nature.addDomain( animal );
return m;
}
private Individual createInstance( OntModel m, OntClass cls, String name ) {
return m.createIndividual( ns + name, cls );
}
private void showClassProperties( OntClass c ) {
System.out.println( "Class " + c.getURI() );
for (ExtendedIterator<OntProperty> i = c.listDeclaredProperties(); i.hasNext(); ) {
System.out.println( " .. has property " + i.next() );
}
}
private void showInstanceProperties( OntResource r, OntClass c ) {
System.out.println( "Instance " + r.getURI() + " has properties: " );
for (ExtendedIterator<OntProperty> i = c.listDeclaredProperties(true); i.hasNext(); ) {
OntProperty p = i.next();
System.out.println( p.getURI() + " ==> " + r.getPropertyValue( p ) );
}
}
}
输出:
Class http://example.org/test#LivingBeing
.. has property http://example.org/test#primeFeatures
Class http://example.org/test#Animal
.. has property http://example.org/test#speed
.. has property http://example.org/test#primeFeatures
.. has property http://example.org/test#nature
Instance http://example.org/test#cat1 has properties:
http://example.org/test#speed ==> 100^^http://www.w3.org/2001/XMLSchema#int
http://example.org/test#nature ==> violent