如何在OWL API中获取个人类

时间:2017-12-04 07:55:11

标签: java owl-api protege4 reasoner

如何用推理器获得猫头鹰中的个体类别

        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());
         }

我希望结果是每个人的阶级 但是推理者没有给出任何结果。为了保护它的工作做得很好但是当我接受我的本体并尝试使用owl api时,我没有得到任何结果

2 个答案:

答案 0 :(得分:1)

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

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

困扰我的是你说你有没有的结果。我希望你应该得到很多错误的结果(即Java类而不是OWL类)。好的,所以你在代码片段中创建了一个本体。你没有得到任何结果的原因是你没有为john添加一个类断言公理。您需要添加factory.getOWLClassAssertionAxiom(owlClassExpression, john) owlClassExpression,其中Person代表您的本体中的一个类,如$this->sold_item_by_gst_percentage($data['Sold_items'], $GST_Percentage)

答案 1 :(得分:0)

基于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));
}