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();
如何使用OntProperty和/或DatatypeProperty而不仅仅使用Property?
通过使用Property我能获得相同的表现力吗?
答案 0 :(得分:0)
要从本体模型中获取ObjectProperty
对象,请使用OntModel.getObjectProperty
()。同样,对于数据类型属性等,Ont
类更具表现力,因为它们包含便利API,例如,通过一个方法调用获取属性的超级属性。但是,由于便捷API仅访问图表中的基础三元组,因此严格来说,您无法使用ObjectProperty
对Property
进行操作。这只是更难的工作!
顺便提一下,Jena允许您使用.as()
方法访问基础RDF资源的其他方面。所以:
Property p = myModel.getProperty( "http://example.com/foo#p" );
OntProperty op = p.as( OntProperty.class );