1

我想将一个人添加到一个类中,我在 OWL API 官方站点中引用了该文档。

这是我的代码。

public void addIndividualsToClass(String className, String indName) throws OWLOntologyStorageException{
    /*
     * Add an individual to input class
     */
    OWLClass tClass = fac.getOWLClass(IRI.create(NS + className));
    OWLNamedIndividual tIndividual = fac.getOWLNamedIndividual(IRI.create(NS + indName));
    OWLClassAssertionAxiom classAssertion = fac.getOWLClassAssertionAxiom(tClass, tIndividual);
    manager.addAxiom(ont, classAssertion);
    manager.saveOntology(ont, new StreamDocumentTarget(new ByteArrayOutputStream()));
}

然后,eclipse 抛出这个异常。

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at org.coode.owlapi.owlxml.renderer.OWLXMLObjectRenderer.visit(OWLXMLObjectRenderer.java:184)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyImpl.accept(OWLOntologyImpl.java:1630)
at org.coode.owlapi.owlxml.renderer.OWLXMLRenderer.render(OWLXMLRenderer.java:106)
at org.coode.owlapi.owlxml.renderer.OWLXMLOntologyStorer.storeOntology(OWLXMLOntologyStorer.java:73)
at org.semanticweb.owlapi.util.AbstractOWLOntologyStorer.storeOntology(AbstractOWLOntologyStorer.java:174)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:870)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:861)
at Test.addIndividualsToClass(Test.java:146)
at Test.main(Test.java:155)

有人能帮我吗?

4

3 回答 3

1

这应该有效。您应该查看http://owlapi.sourceforge.net/index.html的 OWL-API 页面上的 Examples.java

public static void createNewOnto() throws OWLOntologyCreationException,
        OWLOntologyStorageException {
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    IRI ontologyIRI = IRI.create("http://example.com/owlapi/families");
    OWLOntology ont = manager.createOntology(ontologyIRI);
    OWLDataFactory factory = manager.getOWLDataFactory();

    OWLIndividual john = factory.getOWLNamedIndividual(IRI
            .create(ontologyIRI + "#John"));
    OWLIndividual mary = factory.getOWLNamedIndividual(IRI
            .create(ontologyIRI + "#Mary"));
    OWLIndividual susan = factory.getOWLNamedIndividual(IRI
            .create(ontologyIRI + "#Susan"));
    OWLIndividual bill = factory.getOWLNamedIndividual(IRI
            .create(ontologyIRI + "#Bill"));

    OWLObjectProperty hasWife = factory.getOWLObjectProperty(IRI
            .create(ontologyIRI + "#hasWife"));

    OWLObjectPropertyAssertionAxiom axiom1 = factory
            .getOWLObjectPropertyAssertionAxiom(hasWife, john, mary);

    AddAxiom addAxiom1 = new AddAxiom(ont, axiom1);
    // Now we apply the change using the manager.
    manager.applyChange(addAxiom1);

    System.out.println("RDF/XML: ");
    manager.saveOntology(ont, new StreamDocumentTarget(System.out));

}
于 2013-03-06T13:46:21.667 回答
0

以下适用于 OWL-API 版本 5。它展示了如何向本体添加类断言公理,即断言个体是类的实例。

public void addIndividualsToClass(String className, String indName) throws Exception{
    
    PrefixManager pm = new DefaultPrefixManager(NS); // assuming NS is string and is your namespace
    OWLClass tClass = fac.getOWLClass("#" + className, pm); //assuming '#' is your delimiter
    OWLIndividual tIndividual = fac.getOWLNamedIndividual("#" + indName, pm);
    OWLClassAssertionAxiom classAssertion = fac.getOWLClassAssertionAxiom(tClass, tIndividual);
    manager.addAxiom(ont, classAssertion);
    manager.saveOntology(ont);
}
于 2021-08-14T16:41:36.380 回答
0

只是提到一些示例和方法已被弃用

于 2019-04-06T15:51:39.970 回答