From b3b822d187a6402a39d30e471fe90a5dfad64312 Mon Sep 17 00:00:00 2001 From: RncLsn Date: Wed, 3 Jun 2015 15:21:30 +0100 Subject: Before reintroducing extended queries. --- src/uk/ac/ox/cs/pagoda/reasoner/full/Checker.java | 12 +- .../ox/cs/pagoda/reasoner/full/HermitChecker.java | 330 +++++++++++---------- 2 files changed, 174 insertions(+), 168 deletions(-) (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/full') diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/full/Checker.java b/src/uk/ac/ox/cs/pagoda/reasoner/full/Checker.java index 849b971..fd620a5 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/full/Checker.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/full/Checker.java @@ -2,14 +2,14 @@ package uk.ac.ox.cs.pagoda.reasoner.full; import uk.ac.ox.cs.pagoda.query.AnswerTuple; import uk.ac.ox.cs.pagoda.query.AnswerTuples; +import uk.ac.ox.cs.pagoda.util.disposable.Disposable; -public interface Checker { +public abstract class Checker extends Disposable { - public int check(AnswerTuples answers); - - public boolean check(AnswerTuple answer); + public abstract int check(AnswerTuples answers); - public boolean isConsistent(); + public abstract boolean check(AnswerTuple answer); + + public abstract boolean isConsistent(); - public void dispose(); } diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java b/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java index 5dcf0f8..35db0f2 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java @@ -1,24 +1,10 @@ package uk.ac.ox.cs.pagoda.reasoner.full; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - import org.semanticweb.HermiT.Reasoner; import org.semanticweb.HermiT.model.DLClause; import org.semanticweb.HermiT.model.Term; import org.semanticweb.HermiT.model.Variable; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyManager; - +import org.semanticweb.owlapi.model.*; import uk.ac.ox.cs.pagoda.endomorph.Clique; import uk.ac.ox.cs.pagoda.endomorph.DependencyGraph; import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; @@ -30,216 +16,236 @@ import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; import uk.ac.ox.cs.pagoda.util.Namespace; import uk.ac.ox.cs.pagoda.util.Timer; import uk.ac.ox.cs.pagoda.util.Utility; +import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; -public class HermitChecker implements Checker { - - protected OWLDataFactory factory; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; - private String queryText; - private DLClause queryClause; +public class HermitChecker extends Checker { - private Reasoner hermit; + protected OWLDataFactory factory; protected String[][] answerVariable; protected OWLOntology ontology; - protected QueryRecord record; - protected QueryGraph qGraph = null; + protected QueryRecord record; + protected QueryGraph qGraph = null; boolean toCheck = true; - + AnswerTuple topAnswerTuple = null, botAnswerTuple = null; + private String queryText; + private DLClause queryClause; + private Reasoner hermit; + private int tag = 0; + private int counter = 0; + private DependencyGraph dGraph = null; public HermitChecker(Checker checker) { if (checker instanceof HermitChecker) { HermitChecker other = (HermitChecker) checker; - factory = other.factory; + factory = other.factory; queryText = other.queryText; - queryClause = other.queryClause; + queryClause = other.queryClause; answerVariable = other.answerVariable; ontology = other.ontology; -// record = other.record; +// record = other.record; } - + hermit = new Reasoner(ontology); } - + public HermitChecker(OWLOntology ontology, QueryRecord record, boolean toCheck) { this.ontology = ontology; - queryText = record.getQueryText(); + queryText = record.getQueryText(); answerVariable = record.getVariables(); - queryClause = record.getClause(); -// this.record = record; - this.toCheck = toCheck; + queryClause = record.getClause(); +// this.record = record; + this.toCheck = toCheck; } - + public HermitChecker(OWLOntology ontology, String queryText) { this.ontology = ontology; - this.queryText = queryText; + this.queryText = queryText; answerVariable = queryText == null ? null : ConjunctiveQueryHelper.getAnswerVariables(queryText); - queryClause = DLClauseHelper.getQuery(queryText, null); -// this.record = null; + queryClause = DLClauseHelper.getQuery(queryText, null); +// this.record = null; + } + + @Override + public int check(AnswerTuples answers) { + if(isDisposed()) throw new DisposedException(); + + if(hermit == null) initialiseReasoner(); + int answerCounter = 0, counter = 0; + for(; answers.isValid(); answers.moveNext()) { + ++counter; + if(check(answers.getTuple())) ++answerCounter; + } + answers.dispose(); + + Utility.logDebug("The number of individuals to be checked by HermiT: " + counter, + "The number of correct answers: " + answerCounter); + return answerCounter; + } + + @Override + public boolean check(AnswerTuple answerTuple) { + if(isDisposed()) throw new DisposedException(); + + if(!toCheck) return false; + + if(hermit == null) initialiseReasoner(); + if(tag != 0) return tag == 1; + ++counter; + Timer t = new Timer(); + Map sub = answerTuple.getAssignment(answerVariable[1]); + Set toCheckAxioms = qGraph.getAssertions(sub); + +// for (OWLAxiom axiom: toCheckAxioms) System.out.println(axiom.toString()); + + if(hermit.isEntailed(toCheckAxioms)) { + Utility.logDebug("@TIME to check one tuple: " + t.duration()); + return true; + } + Utility.logDebug("@TIME to check one tuple: " + t.duration()); + return false; } - private int tag = 0; - AnswerTuple topAnswerTuple = null, botAnswerTuple = null; - + @Override + public boolean isConsistent() { + if(isDisposed()) throw new DisposedException(); + + if(hermit == null) initialiseReasoner(); + return hermit.isConsistent(); + } + + public void dispose() { + super.dispose(); + + Utility.logInfo("Hermit was called " + counter + " times."); + if(hermit != null) hermit.dispose(); + hermit = null; + } + + public void setDependencyGraph(DependencyGraph dGraph) { + if(isDisposed()) throw new DisposedException(); + + this.dGraph = dGraph; + } + private void initialiseReasoner() { - qGraph = new QueryGraph(queryClause.getBodyAtoms(), answerVariable[1], ontology); - OWLOntologyManager manager = ontology.getOWLOntologyManager(); + qGraph = new QueryGraph(queryClause.getBodyAtoms(), answerVariable[1], ontology); + OWLOntologyManager manager = ontology.getOWLOntologyManager(); factory = manager.getOWLDataFactory(); - - if (hermit != null) hermit.dispose(); - - if (dGraph != null && answerVariable[1].length == 1 && (dGraph.getExits().size() > 1 || dGraph.getEntrances().size() > 1)) { - Set topAxioms = new HashSet(); - Set botAxioms = new HashSet(); + + if(hermit != null) hermit.dispose(); + + if(dGraph != null && answerVariable[1].length == 1 && (dGraph.getExits().size() > 1 || dGraph.getEntrances() + .size() > 1)) { + Set topAxioms = new HashSet(); + Set botAxioms = new HashSet(); addTopAndBotTuple(topAxioms, botAxioms); - manager.addAxioms(ontology, topAxioms); - manager.addAxioms(ontology, botAxioms); + manager.addAxioms(ontology, topAxioms); + manager.addAxioms(ontology, botAxioms); hermit = new Reasoner(ontology); - boolean topValid = true; - if (!hermit.isConsistent() || topAnswerTuple != null && (topValid = check(topAnswerTuple))) { + boolean topValid = true; + if(!hermit.isConsistent() || topAnswerTuple != null && (topValid = check(topAnswerTuple))) { hermit.dispose(); - manager.removeAxioms(ontology, topAxioms); - hermit = new Reasoner(ontology); - } else { - if (!topValid) tag = -1; - else - if (botAnswerTuple != null && check(botAnswerTuple)) tag = 1; + manager.removeAxioms(ontology, topAxioms); + hermit = new Reasoner(ontology); + } + else { + if(!topValid) tag = -1; + else if(botAnswerTuple != null && check(botAnswerTuple)) tag = 1; } } - else + else hermit = new Reasoner(ontology); } - + private void addTopAndBotTuple(Set topAxioms, Set botAxioms) { String top_str = Namespace.PAGODA_ANONY + "top", bot_str = Namespace.PAGODA_ANONY + "bot"; - topAnswerTuple = new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[] { uk.ac.ox.cs.JRDFox.model.Individual.create(top_str) } ); - botAnswerTuple = new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[] { uk.ac.ox.cs.JRDFox.model.Individual.create(bot_str) } ); - OWLIndividual top_ind = factory.getOWLNamedIndividual(IRI.create(top_str)), bot_ind = factory.getOWLNamedIndividual(IRI.create(bot_str)); - Map counter = new HashMap(); - + topAnswerTuple = + new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(top_str)}); + botAnswerTuple = + new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(bot_str)}); + OWLIndividual top_ind = factory.getOWLNamedIndividual(IRI.create(top_str)), bot_ind = + factory.getOWLNamedIndividual(IRI.create(bot_str)); + Map counter = new HashMap(); + Set topAnswers = new HashSet(), botAnswers = new HashSet(); - OWLIndividual sub, obj; - if (dGraph.getExits().size() > 1) { - for (Clique answerClique: dGraph.getExits()) - topAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative().getAnswerTuple().getGroundTerm(0)).getIRI()); + OWLIndividual sub, obj; + if(dGraph.getExits().size() > 1) { + for(Clique answerClique : dGraph.getExits()) + topAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative() + .getAnswerTuple() + .getGroundTerm(0)).getIRI()); } - else topAnswerTuple = null; - - if (dGraph.getEntrances().size() > 1) { - for (Clique answerClique: dGraph.getEntrances()) - botAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative().getAnswerTuple().getGroundTerm(0)).getIRI()); + else topAnswerTuple = null; + + if(dGraph.getEntrances().size() > 1) { + for(Clique answerClique : dGraph.getEntrances()) + botAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative() + .getAnswerTuple() + .getGroundTerm(0)).getIRI()); } - else botAnswerTuple = null; - - for (OWLAxiom axiom: ontology.getABoxAxioms(true)) - if (axiom instanceof OWLClassAssertionAxiom) { + else botAnswerTuple = null; + + for(OWLAxiom axiom : ontology.getABoxAxioms(true)) + if(axiom instanceof OWLClassAssertionAxiom) { OWLClassAssertionAxiom ca = (OWLClassAssertionAxiom) axiom; sub = ca.getIndividual(); - if (topAnswers.contains(sub.toStringID())) + if(topAnswers.contains(sub.toStringID())) topAxioms.add(factory.getOWLClassAssertionAxiom(ca.getClassExpression(), top_ind)); - if (botAnswers.contains(sub.toStringID())) + if(botAnswers.contains(sub.toStringID())) inc(counter, factory.getOWLClassAssertionAxiom(ca.getClassExpression(), bot_ind)); } - else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { - OWLObjectPropertyAssertionAxiom oa = (OWLObjectPropertyAssertionAxiom) axiom; - sub = oa.getSubject(); obj = oa.getObject(); - if (topAnswers.contains(sub.toStringID())) - if (topAnswers.contains(obj.toStringID())) + else if(axiom instanceof OWLObjectPropertyAssertionAxiom) { + OWLObjectPropertyAssertionAxiom oa = (OWLObjectPropertyAssertionAxiom) axiom; + sub = oa.getSubject(); + obj = oa.getObject(); + if(topAnswers.contains(sub.toStringID())) + if(topAnswers.contains(obj.toStringID())) topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, top_ind)); - else + else topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, obj)); else { - if (topAnswers.contains(obj.toStringID())) + if(topAnswers.contains(obj.toStringID())) topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, top_ind)); } - - if (botAnswers.contains(sub.toStringID())) - if (botAnswers.contains(obj.toStringID())) + + if(botAnswers.contains(sub.toStringID())) + if(botAnswers.contains(obj.toStringID())) inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, bot_ind)); - else + else inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, obj)); else { - if (botAnswers.contains(obj.toStringID())) + if(botAnswers.contains(obj.toStringID())) inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, bot_ind)); } - + } - else if (axiom instanceof OWLDataPropertyAssertionAxiom) { - OWLDataPropertyAssertionAxiom da = (OWLDataPropertyAssertionAxiom) axiom; - sub = da.getSubject(); - if (topAnswers.contains(sub.toStringID())) + else if(axiom instanceof OWLDataPropertyAssertionAxiom) { + OWLDataPropertyAssertionAxiom da = (OWLDataPropertyAssertionAxiom) axiom; + sub = da.getSubject(); + if(topAnswers.contains(sub.toStringID())) topAxioms.add(factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), top_ind, da.getObject())); - - if (botAnswers.contains(sub.toStringID())) + + if(botAnswers.contains(sub.toStringID())) inc(counter, factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), bot_ind, da.getObject())); } - - int number = botAnswers.size(); - for (Map.Entry entry: counter.entrySet()) { - if (entry.getValue() == number) + + int number = botAnswers.size(); + for(Map.Entry entry : counter.entrySet()) { + if(entry.getValue() == number) botAxioms.add(entry.getKey()); } } private void inc(Map counter, OWLAxiom newAxiom) { - Integer number = counter.get(newAxiom); - if (number == null) counter.put(newAxiom, 1); - else counter.put(newAxiom, number + 1); - } + if(isDisposed()) throw new DisposedException(); - @Override - public int check(AnswerTuples answers) { - if (hermit == null) initialiseReasoner(); - int answerCounter = 0, counter = 0; - for (; answers.isValid(); answers.moveNext()) { - ++counter; - if (check(answers.getTuple())) ++answerCounter; - } - answers.dispose(); - - Utility.logDebug("The number of individuals to be checked by HermiT: " + counter, - "The number of correct answers: " + answerCounter); - return answerCounter; - } - - private int counter = 0; - - @Override - public boolean check(AnswerTuple answerTuple) { - if (!toCheck) return false; - - if (hermit == null) initialiseReasoner(); - if (tag != 0) return tag == 1; - ++counter; - Timer t = new Timer(); - Map sub = answerTuple.getAssignment(answerVariable[1]); - Set toCheckAxioms = qGraph.getAssertions(sub); - -// for (OWLAxiom axiom: toCheckAxioms) System.out.println(axiom.toString()); - - if (hermit.isEntailed(toCheckAxioms)) { - Utility.logDebug("@TIME to check one tuple: " + t.duration()); - return true; - } - Utility.logDebug("@TIME to check one tuple: " + t.duration()); - return false; - } - - @Override - public boolean isConsistent() { - if (hermit == null) initialiseReasoner(); - return hermit.isConsistent(); - } - - - public void dispose() { - Utility.logInfo("Hermit was called " + counter + " times."); - if (hermit != null) hermit.dispose(); - hermit = null; - } - - private DependencyGraph dGraph = null; - - public void setDependencyGraph(DependencyGraph dGraph) { - this.dGraph = dGraph; + Integer number = counter.get(newAxiom); + if(number == null) counter.put(newAxiom, 1); + else counter.put(newAxiom, number + 1); } } -- cgit v1.2.3