protegeowl-apireasoningswrlpellet

How to generate specific inference using owlapi?


I am working on this project which requires to perform ontology reasoning in code, so far I have written this rule. I am implementing this in java with the help of owlapi, and clarkparsia.pellet.owlapiv3.PelletReasoner.

//dexpi:Pump(?pump) ^ swrlb:greaterThan(?value, "10.0"^^xsd:double) ^ rdl:datumValue(?pump, ?value) -> dexpi:EjectorPump(?pump)
// Rule created using SWRLClassAtoms and DataPropertyAtoms
ontologyManager.applyChange(new AddAxiom(ontology, rule1));

So this section of code adds the rule to the ontology. I know the rules are defined correctly, since I can see this rule in protege, but How do I update the ontology according to the rule, i.e. change class type of all Pump instances whose datumValue is greaterThan 10 to EjectorPump.

I have tried these approaches so far.

Approach-1

// These lines uses pellet reasoner to perform reasoning
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(ontology);     
InferredOntologyGenerator iog = new  InferredOntologyGenerator(reasoner);
iog.fillOntology(factory, ontology);        

Approach-2

List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<>();
gens.add(new InferredClassAssertionAxiomGenerator());
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
iog.fillOntology(factory, ontology);

Both of the approaches works perfectly fine but the problem is when they generate inference there is too much information I just want reclassification to be done like this if possible (This one is generated by protege. desired inference (generated by protege)

but instead, these codes reclassify like this code generated inference

Additionally, if you can direct me on how I can generate and show an explanation of reasoning, it will be a huge help. Thank you.


Solution

  • Extending the discussion from the comments above:

    Openllet and OWLAPI versions: 2.6.5 of Openllet was released three years back, I have just tried it with OWLAPI 5.1.20 (released a year and a half ago) and got no errors. However, I do not have your ontology, so this might not be a sufficient test.

    With either setup, you can be more selective about which axioms to generate by writing your own generator. This example introduces a generator that creates assertion axioms for only one class, i.e., it will not add any other type to individuals:

        List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<>();
        gens.add(new InferredEntityAxiomGenerator<OWLClass, OWLClassAssertionAxiom>() {
            @Override
            protected Stream<OWLClass> getEntities(OWLOntology ont) {
                return Stream.of(factory.getOWLClass(IRI.create("urn:test:onto#A")));
            }
    
            @Override
            protected void addAxioms(OWLClass entity, OWLReasoner reasoner, OWLDataFactory dataFactory,
                    Set<OWLClassAssertionAxiom> result) {
                reasoner.getInstances(entity).entities().map(x -> factory.getOWLClassAssertionAxiom(entity, x))
                        .forEach(result::add);
            }
    
            @Override
            public String getLabel() {
                // TODO Auto-generated method stub
                return "Instances of A";
            }
    
        });
        InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
        iog.fillOntology(factory, ontology);
    

    The approach can be followed for older OWLAPI versions, but you will have to adjust it to compile correctly with whichever version you're using.