1
    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?

通过使用属性,我可以获得相同数量的表现力吗?

4

1 回答 1

0

ObjectProperty要从本体模型中获取对象,请使用OntModel.getObjectProperty()。对于数据类型属性等Ont也是如此。从某种意义上说,这些类更具表现力,因为它们包含方便的 API,例如,通过一个方法调用来获取属性的超属性。但是,由于便捷 API 仅访问图中的底层三元组,严格来说ObjectProperty,您无法对 a 进行任何操作,而对Property. 这只是更难的工作!

顺便说一句,Jena 允许您使用该方法访问底层 RDF 资源的其他方面.as()。所以:

Property p = myModel.getProperty( "http://example.com/foo#p" );
OntProperty op = p.as( OntProperty.class );
于 2012-09-25T20:53:50.877 回答