-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throws exceptions if ontology logically incoherent, fixes #40
Includes incoherent OPs in logical tests, fixes #104 Note these two tickets are coupled, so were tackled together. To handle validation checks a new ReasonerHelper class was created. This implements - inconsistency - incoherent TBox (ie unsat classes) - incoherent RBox (using probe classes) Exceptions are thrown on encountering any of these. Currently there is no way to allow the user to ignore exceptions; this could in theory be added but would likely be a very bady idea
- Loading branch information
Showing
13 changed files
with
359 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
robot-core/src/main/java/org/obolibrary/robot/ReasonerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.obolibrary.robot; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import org.obolibrary.robot.exceptions.IncoherentTBoxException; | ||
import org.obolibrary.robot.exceptions.IncoherentRBoxException; | ||
import org.obolibrary.robot.exceptions.InconsistentOntologyException; | ||
import org.semanticweb.owlapi.model.IRI; | ||
import org.semanticweb.owlapi.model.OWLAxiom; | ||
import org.semanticweb.owlapi.model.OWLClass; | ||
import org.semanticweb.owlapi.model.OWLDataFactory; | ||
import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
import org.semanticweb.owlapi.model.OWLOntology; | ||
import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
import org.semanticweb.owlapi.model.parameters.Imports; | ||
import org.semanticweb.owlapi.reasoner.InferenceType; | ||
import org.semanticweb.owlapi.reasoner.Node; | ||
import org.semanticweb.owlapi.reasoner.OWLReasoner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* | ||
* Provides convenience methods for working with OWL reasoning. | ||
* | ||
* @author cjm | ||
* | ||
*/ | ||
public class ReasonerHelper { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(ReasonerHelper.class); | ||
|
||
|
||
public static void validate(OWLReasoner reasoner) throws IncoherentTBoxException, InconsistentOntologyException, IncoherentRBoxException { | ||
|
||
OWLOntology ont = reasoner.getRootOntology(); | ||
OWLOntologyManager manager = ont.getOWLOntologyManager(); | ||
OWLDataFactory dataFactory = manager.getOWLDataFactory(); | ||
OWLClass nothing = dataFactory.getOWLNothing(); | ||
OWLClass thing = dataFactory.getOWLThing(); | ||
logger.info("Checking for inconsistencies"); | ||
if (!reasoner.isConsistent()) { | ||
throw new InconsistentOntologyException(); | ||
} | ||
|
||
logger.info("Checking for unsatisfiable classes..."); | ||
Set<OWLClass> unsatisfiableClasses = | ||
reasoner.getUnsatisfiableClasses().getEntitiesMinus(nothing); | ||
if (unsatisfiableClasses.size() > 0) { | ||
logger.error("There are {} unsatisfiable classes in the ontology.", | ||
unsatisfiableClasses.size()); | ||
for (OWLClass cls : unsatisfiableClasses) { | ||
logger.error(" unsatisfiable: " + cls.getIRI()); | ||
} | ||
throw new IncoherentTBoxException(unsatisfiableClasses); | ||
} | ||
|
||
logger.info("Checking for unsatisfiable object properties..."); | ||
|
||
Set<OWLAxiom>tempAxioms = new HashSet<>(); | ||
Map<OWLClass, OWLObjectProperty> probeFor = new HashMap<>(); | ||
for (OWLObjectProperty p : ont.getObjectPropertiesInSignature(Imports.INCLUDED)) { | ||
UUID uuid = UUID.randomUUID(); | ||
IRI probeIRI = IRI.create(p.getIRI().toString() + "-" + uuid.toString()); | ||
OWLClass probe = dataFactory.getOWLClass(probeIRI); | ||
probeFor.put(probe, p); | ||
tempAxioms.add(dataFactory.getOWLDeclarationAxiom(probe)); | ||
tempAxioms.add(dataFactory.getOWLSubClassOfAxiom(probe, | ||
dataFactory.getOWLObjectSomeValuesFrom(p, thing))); | ||
} | ||
manager.addAxioms(ont, tempAxioms); | ||
reasoner.flush(); | ||
|
||
Set<OWLClass> unsatisfiableProbeClasses = | ||
reasoner.getUnsatisfiableClasses().getEntitiesMinus(nothing); | ||
|
||
// leave no trace | ||
manager.removeAxioms(ont, tempAxioms); | ||
reasoner.flush(); | ||
|
||
if (unsatisfiableProbeClasses.size() > 0) { | ||
logger.error("There are {} unsatisfiable properties in the ontology.", | ||
unsatisfiableProbeClasses.size()); | ||
Set<OWLObjectProperty> unsatPs = new HashSet<>(); | ||
for (OWLClass cls : unsatisfiableProbeClasses) { | ||
OWLObjectProperty unsatP = probeFor.get(cls); | ||
unsatPs.add(unsatP); | ||
logger.error(" unsatisfiable property: " + unsatP.getIRI()); | ||
} | ||
throw new IncoherentRBoxException(unsatPs); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
} |
22 changes: 22 additions & 0 deletions
22
robot-core/src/main/java/org/obolibrary/robot/exceptions/IncoherentRBoxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.obolibrary.robot.exceptions; | ||
|
||
import java.util.Set; | ||
|
||
import org.semanticweb.owlapi.model.OWLClass; | ||
import org.semanticweb.owlapi.model.OWLEntity; | ||
import org.semanticweb.owlapi.model.OWLObject; | ||
import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
|
||
/** | ||
* Ontology contains unsatisfiable properties | ||
* | ||
* @author cjm | ||
* | ||
*/ | ||
public class IncoherentRBoxException extends OntologyLogicException { | ||
|
||
public IncoherentRBoxException(Set<OWLObjectProperty> unsatisfiableProperties) { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
robot-core/src/main/java/org/obolibrary/robot/exceptions/IncoherentTBoxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.obolibrary.robot.exceptions; | ||
|
||
import java.util.Set; | ||
|
||
import org.semanticweb.owlapi.model.OWLClass; | ||
import org.semanticweb.owlapi.model.OWLEntity; | ||
import org.semanticweb.owlapi.model.OWLObject; | ||
|
||
/** | ||
* Ontology contains unsatisfiable classes | ||
* | ||
* @author cjm | ||
* | ||
*/ | ||
public class IncoherentTBoxException extends OntologyLogicException { | ||
|
||
public IncoherentTBoxException(Set<OWLClass> unsatisfiableClasses) { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
robot-core/src/main/java/org/obolibrary/robot/exceptions/InconsistentOntologyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.obolibrary.robot.exceptions; | ||
|
||
/** | ||
* Ontology contains logical inconsistencies | ||
* | ||
* Note inconsistency is not the same as incoherency | ||
* | ||
* @author cjm | ||
* | ||
*/ | ||
public class InconsistentOntologyException extends OntologyLogicException { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
robot-core/src/main/java/org/obolibrary/robot/exceptions/OntologyLogicException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.obolibrary.robot.exceptions; | ||
|
||
/** | ||
* Ontology contains unsatisfiable classes, properties or inconsistencies | ||
* | ||
* @author cjm | ||
* | ||
*/ | ||
public class OntologyLogicException extends Exception { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
robot-core/src/main/java/org/obolibrary/robot/exceptions/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* | ||
*/ | ||
/** | ||
* @author cjm | ||
* | ||
*/ | ||
package org.obolibrary.robot.exceptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.