Galigator / openllet

Openllet is an OWL 2 reasoner in Java, build on top of Pellet.
https://www.w3.org/TR/owl2-primer/
Other
96 stars 26 forks source link

Deleting rules from the knowladgebase #34

Closed shadialian closed 4 years ago

shadialian commented 6 years ago

Hi all, I am trying to build a custom rule editor for a research project. I am using OWLApi with openllet reasoner. I have the rule in a seprate ontology as follows. The question is after I deleted a rule I couldn't get the reseanor to redo the reasoning without the rule. the code as follows and the ontologies are attached.

Thanks


package com.shadialian.owlapi;

import java.io.File;
import java.util.EnumSet;
import java.util.Set;
import openllet.core.KnowledgeBase;
import openllet.core.KnowledgeBaseImpl;
import openllet.core.rules.model.Rule;
import openllet.owlapi.OWL;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import openllet.owlapi.OpenlletReasoner;
import openllet.owlapi.OpenlletReasonerFactory;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.reasoner.Node;
import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration;
import org.semanticweb.owlapi.reasoner.SimpleConfiguration;

public class Main {
    private static final String familyOnt = "family.owl";
    private static final String familyRules = "family-rules.owl";
    private static final String familyIRI = "http://www.shadialian.com/ontologies/family#";
    public static void main(String args[]) throws OWLOntologyCreationException {
        owlApiTest();
    }
    public static void owlApiTest() throws OWLOntologyCreationException {
        File familyOntFile = new File(familyOnt);
        File familyRulesFile = new File(familyRules);
        final OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        final OWLOntology ontology = manager.loadOntologyFromOntologyDocument(familyOntFile);
        final OWLOntology rules = manager.loadOntologyFromOntologyDocument(familyRulesFile);
        OWLReasonerConfiguration config = new SimpleConfiguration();
        final OpenlletReasoner reasoner = OpenlletReasonerFactory.getInstance().createReasoner(rules, config);
        System.out.println("done.");
        reasoner.getKB().realize();
        reasoner.getKB().printClassTree();
        System.out.println(reasoner.getKB().getRules());
        Set<Rule> rulesSet = reasoner.getKB().getRules();
        Rule fRule = (Rule) rulesSet.toArray()[0];
        ((KnowledgeBaseImpl) reasoner.getKB()).setChanges(EnumSet.of(KnowledgeBase.ChangeType.ABOX_DEL));
        rulesSet.clear();
        reasoner.getKB().prepare();
        reasoner.getKB().realize();
        reasoner.flush();
        //reasoner.refresh();
        //reasoner.prepareReasoner();
        //reasoner.getKB().prepare();        
        //reasoner.getKB().realize();        
        //reasoner.getKB().printClassTree();
        final OWLClass Person = OWL.Class(familyIRI + "Person");
        System.out.println("\n-------------------------------------------------\nPerson Indivisuals");
        for (final Node<OWLNamedIndividual> ind : reasoner.getInstances(Person, false)) {
            System.out.println(ind);
        }
        final OWLClass Male = OWL.Class(familyIRI + "Male");
        System.out.println("\n-------------------------------------------------\nMale Indivisuals");
        for (final Node<OWLNamedIndividual> ind : reasoner.getInstances(Male, false)) {
            System.out.println(ind);
        }
        System.out.println(reasoner.getKB().getRules());
    }

}

owlapi.zip

ignazio1977 commented 6 years ago

You're trying to remove the rules by emptying an internal map in the reasoner, but I suspect that map is filled again when preparing the reasoner. I'd remove the rules from the root ontology and then flush the reasoner; that should be enough.

shadialian commented 6 years ago

Do you mean query the rules and delete them from the ontology. I was trying to clone the way rules are added.

Galigator commented 6 years ago

Hello Shadialian, I can confirm Ignazio affirmation, the only good way to add/del rules is to use the OWLOntology interface. You can look at example in the tests of the owlapi-module search for TestBasic.java .

In normal usages, you should only use interfaces from the owlapi. The KnowledgeBase.java may never be use.

Doing so will improve portability with other reasonner