Skip to content

Instantly share code, notes, and snippets.

@vadimii
Created March 14, 2012 00:19
Show Gist options
  • Save vadimii/2032915 to your computer and use it in GitHub Desktop.
Save vadimii/2032915 to your computer and use it in GitHub Desktop.
Groovy OWLAPI explanations example
import java.io.PrintWriter
import org.semanticweb.owlapi.apibinding.OWLManager
import com.clarkparsia.owlapi.explanation.DefaultExplanationGenerator
import com.clarkparsia.owlapi.explanation.io.manchester.ManchesterSyntaxExplanationRenderer
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory
def I = { org.semanticweb.owlapi.model.IRI.create(it) }
def file = "http://owl.cs.manchester.ac.uk/repository/download?ontology=" +
"file:/Users/seanb/Desktop/Cercedilla2005/hands-on/people.owl" +
"&format=RDF/XML"
println "Reading file ${file}…"
def manager = OWLManager.createOWLOntologyManager()
def ontology = manager.loadOntology(I(file))
def reasoner = PelletReasonerFactory.instance.createReasoner(ontology)
reasoner.precomputeInferences()
def consistent = reasoner.consistent ? "Consistent!" : "Inconsistent!"
println "Loaded ${ontology.ontologyID}… ${consistent}"
def bottomNode = reasoner.unsatisfiableClasses
def unsatisfiable = bottomNode.entitiesMinusBottom
if (unsatisfiable.empty) {
println "Unsatisfiable classes don't exist"
return
}
println "Unsatisfiable classes found:\n${(unsatisfiable*.iri).join("\n")}"
def factory = PelletReasonerFactory.instance
def explanator = new DefaultExplanationGenerator(
manager, factory, ontology, null)
def explanations = explanator.getExplanations(
(unsatisfiable*.asOWLClass()).first())
def renderer = new ManchesterSyntaxExplanationRenderer()
def out = new PrintWriter(System.out)
renderer.startRendering(out)
renderer.render(explanations)
renderer.endRendering()
Reading file http://owl.cs.manchester.ac.uk/repository/download?ontology=file:/Users/seanb/Desktop/Cercedilla2005/hands-on/people.owl&format=RDF/XML…
Loaded <file:/Users/seanb/Desktop/Cercedilla2005/hands-on/people.owl>… Consistent!
Unsatisfiable classes found:
http://owl.man.ac.uk/2005/07/sssw/people#mad_cow
Explanation(s):
1) cow subClassOf vegetarian
sheep subClassOf animal
mad_cow equivalentTo cow
and eats some brain
and part_of some sheep
vegetarian equivalentTo animal
and eats only not animal
and eats only not part_of some animal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment