package uk.ac.ox.cs.pagoda.ore; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLClass; import org.semanticweb.owlapi.model.OWLClassExpression; import org.semanticweb.owlapi.model.OWLDataFactory; import org.semanticweb.owlapi.model.OWLDataProperty; import org.semanticweb.owlapi.model.OWLDataPropertyExpression; import org.semanticweb.owlapi.model.OWLLiteral; import org.semanticweb.owlapi.model.OWLNamedIndividual; import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyChange; import org.semanticweb.owlapi.reasoner.AxiomNotInProfileException; import org.semanticweb.owlapi.reasoner.BufferingMode; import org.semanticweb.owlapi.reasoner.ClassExpressionNotInProfileException; import org.semanticweb.owlapi.reasoner.FreshEntitiesException; import org.semanticweb.owlapi.reasoner.FreshEntityPolicy; import org.semanticweb.owlapi.reasoner.InconsistentOntologyException; import org.semanticweb.owlapi.reasoner.IndividualNodeSetPolicy; import org.semanticweb.owlapi.reasoner.InferenceType; import org.semanticweb.owlapi.reasoner.Node; import org.semanticweb.owlapi.reasoner.NodeSet; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.ReasonerInterruptedException; import org.semanticweb.owlapi.reasoner.TimeOutException; import org.semanticweb.owlapi.reasoner.UnsupportedEntailmentTypeException; import org.semanticweb.owlapi.reasoner.impl.OWLClassNodeSet; import org.semanticweb.owlapi.util.Version; import uk.ac.ox.cs.JRDFox.model.GroundTerm; import uk.ac.ox.cs.JRDFox.model.Individual; import uk.ac.ox.cs.pagoda.query.AnswerTuples; import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; public class PagodaOWLReasoner implements OWLReasoner { QueryReasoner reasoner; OWLOntology ontology; OWLDataFactory factory; boolean sat; public PagodaOWLReasoner(OWLOntology ontology) { this.ontology = ontology; factory = ontology.getOWLOntologyManager().getOWLDataFactory(); reasoner = QueryReasoner.getInstance(ontology); reasoner.setToClassify(false); reasoner.loadOntology(ontology); sat = reasoner.preprocess(); thing = new OWLClassNodeSet(factory.getOWLThing()); } @Override public String getReasonerName() { return "PAGOdA"; } @Override public Version getReasonerVersion() { return null; } @Override public BufferingMode getBufferingMode() { // TODO Auto-generated method stub return null; } @Override public void flush() { // TODO Auto-generated method stub } @Override public List getPendingChanges() { // TODO Auto-generated method stub return null; } @Override public Set getPendingAxiomAdditions() { // TODO Auto-generated method stub return null; } @Override public Set getPendingAxiomRemovals() { // TODO Auto-generated method stub return null; } @Override public OWLOntology getRootOntology() { return ontology; } @Override public void interrupt() { // TODO Auto-generated method stub } @Override public void precomputeInferences(InferenceType... inferenceTypes) throws ReasonerInterruptedException, TimeOutException, InconsistentOntologyException { if (!sat) { throw new InconsistentOntologyException(); } if (inferenceTypes.length == 1 && inferenceTypes[0].equals(InferenceType.CLASS_ASSERTIONS) && types.isEmpty()) { Set queriedClasses = new HashSet(); for (OWLOntology onto: ontology.getImportsClosure()) for (OWLClass cls: onto.getClassesInSignature(true)) { // if (cls.toStringID().equals("http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#Meritage")); // else continue; if (!cls.equals(factory.getOWLThing()) && !queriedClasses.contains(cls)) { queriedClasses.add(cls); AnswerTuples tuples = null; try { tuples = reasoner.evaluate(String.format("select distinct ?x where { ?x <%s> .}", cls.toStringID())); int cnt = 0; for (GroundTerm t; tuples.isValid(); tuples.moveNext()) { t = tuples.getTuple().getGroundTerm(0); if (t instanceof Individual) { addType(((Individual) t).getIRI(), cls); ++cnt; } } System.out.println("The number of answers: " + cnt); } finally { if (tuples != null) tuples.dispose(); } } } } } private void addType(String iri, OWLClass cls) { OWLNamedIndividual ind = factory.getOWLNamedIndividual(IRI.create(iri)); OWLClassNodeSet set; if (types.containsKey(ind)) { set = types.get(ind); } else { set = new OWLClassNodeSet(factory.getOWLThing()); types.put(ind, set); } set.addEntity(cls); } @Override public boolean isPrecomputed(InferenceType inferenceType) { // TODO Auto-generated method stub return false; } @Override public Set getPrecomputableInferenceTypes() { return java.util.Collections.singleton(InferenceType.CLASS_ASSERTIONS); } @Override public boolean isConsistent() throws ReasonerInterruptedException, TimeOutException { return sat; } @Override public boolean isSatisfiable(OWLClassExpression classExpression) throws ReasonerInterruptedException, TimeOutException, ClassExpressionNotInProfileException, FreshEntitiesException, InconsistentOntologyException { // TODO Auto-generated method stub return false; } @Override public Node getUnsatisfiableClasses() throws ReasonerInterruptedException, TimeOutException, InconsistentOntologyException { // TODO Auto-generated method stub return null; } @Override public boolean isEntailed(OWLAxiom axiom) throws ReasonerInterruptedException, UnsupportedEntailmentTypeException, TimeOutException, AxiomNotInProfileException, FreshEntitiesException, InconsistentOntologyException { // TODO Auto-generated method stub return false; } @Override public boolean isEntailed(Set axioms) throws ReasonerInterruptedException, UnsupportedEntailmentTypeException, TimeOutException, AxiomNotInProfileException, FreshEntitiesException, InconsistentOntologyException { // TODO Auto-generated method stub return false; } @Override public boolean isEntailmentCheckingSupported(AxiomType axiomType) { // TODO Auto-generated method stub return false; } @Override public Node getTopClassNode() { // TODO Auto-generated method stub return null; } @Override public Node getBottomClassNode() { // TODO Auto-generated method stub return null; } @Override public NodeSet getSubClasses(OWLClassExpression ce, boolean direct) throws ReasonerInterruptedException, TimeOutException, FreshEntitiesException, InconsistentOntologyException, ClassExpressionNotInProfileException { // TODO Auto-generated method stub return null; } @Override public NodeSet getSuperClasses(OWLClassExpression ce, boolean direct) throws InconsistentOntologyException, ClassExpressionNotInProfileException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getEquivalentClasses(OWLClassExpression ce) throws InconsistentOntologyException, ClassExpressionNotInProfileException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getDisjointClasses(OWLClassExpression ce) throws ReasonerInterruptedException, TimeOutException, FreshEntitiesException, InconsistentOntologyException { // TODO Auto-generated method stub return null; } @Override public Node getTopObjectPropertyNode() { // TODO Auto-generated method stub return null; } @Override public Node getBottomObjectPropertyNode() { // TODO Auto-generated method stub return null; } @Override public NodeSet getSubObjectProperties( OWLObjectPropertyExpression pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getSuperObjectProperties( OWLObjectPropertyExpression pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getEquivalentObjectProperties( OWLObjectPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getDisjointObjectProperties( OWLObjectPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getInverseObjectProperties( OWLObjectPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getObjectPropertyDomains( OWLObjectPropertyExpression pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getObjectPropertyRanges( OWLObjectPropertyExpression pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getTopDataPropertyNode() { // TODO Auto-generated method stub return null; } @Override public Node getBottomDataPropertyNode() { // TODO Auto-generated method stub return null; } @Override public NodeSet getSubDataProperties(OWLDataProperty pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getSuperDataProperties(OWLDataProperty pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getEquivalentDataProperties(OWLDataProperty pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getDisjointDataProperties( OWLDataPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getDataPropertyDomains(OWLDataProperty pe, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } private Map types = new HashMap(); private OWLClassNodeSet thing; @Override public NodeSet getTypes(OWLNamedIndividual ind, boolean direct) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { if (!sat) { throw new InconsistentOntologyException(); } if (types.containsKey(ind)) return types.get(ind); return thing; } @Override public NodeSet getInstances(OWLClassExpression ce, boolean direct) throws InconsistentOntologyException, ClassExpressionNotInProfileException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getObjectPropertyValues( OWLNamedIndividual ind, OWLObjectPropertyExpression pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Set getDataPropertyValues(OWLNamedIndividual ind, OWLDataProperty pe) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public Node getSameIndividuals(OWLNamedIndividual ind) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public NodeSet getDifferentIndividuals( OWLNamedIndividual ind) throws InconsistentOntologyException, FreshEntitiesException, ReasonerInterruptedException, TimeOutException { // TODO Auto-generated method stub return null; } @Override public long getTimeOut() { // TODO Auto-generated method stub return 0; } @Override public FreshEntityPolicy getFreshEntityPolicy() { // TODO Auto-generated method stub return null; } @Override public IndividualNodeSetPolicy getIndividualNodeSetPolicy() { // TODO Auto-generated method stub return null; } @Override public void dispose() { reasoner.dispose(); } }