Search code examples
javaowlowl-apiprotege4

OR some AND rules in OWL API?


I don’t seem to be able to figure out how to OR (ObjectUnionOf?) a set of AND (ObjectIntersectionOf) rules. What my code produces when the OWL file is opened in protégé is rules (has_difi_min some double[<= "184.84"^^double]) and (has_mean_ndvi some double[<= "0.3428"^^double]), etc. with lines separating the "rulesets" as shown below in the screenshot.

My OWLAPI code:

/* write rules */
// OWLObjectIntersectionOf intersection = null;
OWLClassExpression firstRuleSet = null;
OWLClass owlCls = null;
OWLObjectUnionOf union = null;
Iterator it = rules.map.entrySet().iterator();
Set<OWLClassExpression> unionSet = new HashSet<OWLClassExpression>();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry) it.next();
    String currCls = (String) pair.getKey();
    owlCls = factory.getOWLClass(IRI.create("#" + currCls));
    ArrayList<owlRuleSet> currRuleset = (ArrayList<owlRuleSet>) pair.getValue();
    for (int i = 0; i < currRuleset.size(); i++) {
        firstRuleSet = factory.getOWLObjectIntersectionOf(currRuleset.get(i).getRuleList(currCls))
        union = factory.getOWLObjectUnionOf(firstRuleSet);
        manager.addAxiom(ontology, factory.getOWLEquivalentClassesAxiom(owlCls, union));
    }
}
manager.saveOntology(ontology);

This is what is looks like: I want the lines to be ORs. What it looks like in protege

edit: Thanks Ignazio! My OWLAPI code now looks like so:

/* write rules */
OWLClass owlCls = null;
OWLObjectUnionOf totalUnion = null;
Iterator it = rules.map.entrySet().iterator();
Set<OWLClassExpression> unionSet = new HashSet<OWLClassExpression>();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry) it.next();
    String currCls = (String) pair.getKey();
    owlCls = factory.getOWLClass(IRI.create("#" + currCls));
    ArrayList<owlRuleSet> currRuleset = (ArrayList<owlRuleSet>) pair.getValue();
    for (int i = 0; i < currRuleset.size(); i++) {
        firstRuleSet = factory.getOWLObjectIntersectionOf(currRuleset.get(i).getRuleList(currCls))
        unionSet.add(firstRuleSet);
    }
    totalUnion = factory.getOWLObjectUnionOf(unionSet);
    unionSet.clear()
    manager.addAxiom(ontology, factory.getOWLEquivalentClassesAxiom(owlCls, totalunion));
}
manager.saveOntology(ontology);

Solution

  • You are creating unionSet but not using it. Instead of adding an axiom to the ontology, add firstRuleSet to unionSet, then create an equivalent class axiom outside the main loop, just before saving the ontology.