0

如何使用推理器获取 owl 中的个体类别

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(KOALA));


        IRI ontologyIRI = IRI.create("http://www.semanticweb.org/xxxxx/ontologies/2017/10/ontology");

        OWLDataFactory factory = manager.getOWLDataFactory();

        OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));

        OWLDataPropertyExpression hasConnexion= factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasConnexion"));

        OWLDataPropertyAssertionAxiom axiom = factory.getOWLDataPropertyAssertionAxiom(hasConnexion, john, 3);

        AddAxiom addAxiom = new AddAxiom(ontology, axiom);

        manager.applyChange(addAxiom);

        manager.saveOntology(ontology, new StreamDocumentTarget(System.out));


        //reasoner
        OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
        OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);


        OWLClass myClass= fac.getOWLClass(IRI.create("http://www.semanticweb.org/xxxxx/ontologies/2017/10/ontology#hasConnexion"));


        NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(myClass,
                false);
         for (Node<OWLNamedIndividual> i : individuals)
         {
             System.out.println(i.getClass());
         }

我希望结果是每个人的类别,但推理器没有给出结果。在 protege 中它运行良好,但是当我使用我的本体并尝试使用 owl api 制作它时,我没有得到任何结果

4

2 回答 2

1

基于OWL Api Examples,这里有两个例子:

获取 OWLClass 样式的子类:

OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();

IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Style");

OWLClass style = owlManager.getOWLDataFactory().getOWLClass(iri);

OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologia);

NodeSet<OWLClass> subClasses = reasoner.getSubClasses(style, true);
Set<OWLClass> clses = subClasses.getFlattened();

Log.d(TAG, "Subclasses of Style: ");

for (OWLClass cls : clses) {
    String s = cls.toString();
    Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}

获取 OWLClass Rock 的个人:

OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();

IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Rock");

OWLClass rock = owlManager.getOWLDataFactory().getOWLClass(iri);

OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontologia);

NodeSet<OWLNamedIndividual> individualsNodeSet = reasoner.getInstances(rock, true);
Set<OWLNamedIndividual> individuals = individualsNodeSet.getFlattened();

Log.d(TAG, "Instances of Rock: ");

for (OWLNamedIndividual ind : individuals) {
    String s = ind.toString();
    Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}
于 2018-05-01T17:25:19.833 回答
1

是的,那是行不通的。i.getClass()将为您提供 Java 类而不是个人的断言类型。要获得您需要调用的个人的断言类型ontology.axioms(i).collect(Collectors.toSet())。这只会返回已添加到本体的断言。

要获得推断类型,您需要调用reasoner.types(i).collect(Collectors.toSet()).

困扰我的是你说你没有结果。我希望你会得到很多错误的结果(即 Java 类而不是 OWL 类)。好的,所以您正在代码片段中创建一个本体。您没有得到任何结果的原因是您没有为john. 您需要 在您的本体中添加一个代表类的factory.getOWLClassAssertionAxiom(owlClassExpression, john)位置,例如.owlClassExpressionPerson

于 2017-12-04T10:30:03.913 回答