aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java281
1 files changed, 0 insertions, 281 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java b/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
deleted file mode 100644
index 0362fc2..0000000
--- a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
+++ /dev/null
@@ -1,281 +0,0 @@
1package uk.ac.ox.cs.pagoda.reasoner.full;
2
3import org.semanticweb.HermiT.Reasoner;
4import org.semanticweb.HermiT.model.DLClause;
5import org.semanticweb.HermiT.model.Term;
6import org.semanticweb.HermiT.model.Variable;
7import org.semanticweb.owlapi.model.*;
8import uk.ac.ox.cs.pagoda.endomorph.Clique;
9import uk.ac.ox.cs.pagoda.endomorph.DependencyGraph;
10import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
11import uk.ac.ox.cs.pagoda.query.AnswerTuple;
12import uk.ac.ox.cs.pagoda.query.AnswerTuples;
13import uk.ac.ox.cs.pagoda.query.QueryRecord;
14import uk.ac.ox.cs.pagoda.query.rollup.QueryGraph;
15import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper;
16import uk.ac.ox.cs.pagoda.util.Namespace;
17import uk.ac.ox.cs.pagoda.util.Timer;
18import uk.ac.ox.cs.pagoda.util.Utility;
19import uk.ac.ox.cs.pagoda.util.disposable.DisposedException;
20
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.Map;
24import java.util.Set;
25
26public class HermitChecker extends Checker {
27
28 protected OWLDataFactory factory;
29 protected String[][] answerVariable;
30 protected OWLOntology ontology;
31 protected QueryRecord record;
32 protected QueryGraph qGraph = null;
33 boolean toCheck = true;
34 AnswerTuple topAnswerTuple = null, botAnswerTuple = null;
35 private String queryText;
36 private DLClause queryClause;
37 private Reasoner hermit;
38 private int tag = 0;
39
40 public int getNoOfCalls() {
41 if(isDisposed()) throw new DisposedException();
42 return noOfCalls;
43 }
44
45 private int noOfCalls = 0;
46 private DependencyGraph dGraph = null;
47
48 public HermitChecker(Checker checker) {
49 if(checker instanceof HermitChecker) {
50 HermitChecker other = (HermitChecker) checker;
51 factory = other.factory;
52 queryText = other.queryText;
53 queryClause = other.queryClause;
54 answerVariable = other.answerVariable;
55 ontology = other.ontology;
56// record = other.record;
57 }
58
59 hermit = new Reasoner(ontology);
60 }
61
62 public HermitChecker(OWLOntology ontology, QueryRecord record, boolean toCheck) {
63 this.ontology = ontology;
64 queryText = record.getQueryText();
65 answerVariable = record.getVariables();
66 queryClause = record.getClause();
67// this.record = record;
68 this.toCheck = toCheck;
69 }
70
71 public HermitChecker(OWLOntology ontology, String queryText) {
72 this.ontology = ontology;
73 this.queryText = queryText;
74 answerVariable = queryText == null ? null : ConjunctiveQueryHelper.getAnswerVariables(queryText);
75 queryClause = DLClauseHelper.getQuery(queryText, null);
76// this.record = null;
77 }
78
79 @Override
80 public int check(AnswerTuples answers) {
81 if(isDisposed()) throw new DisposedException();
82
83 if(hermit == null) initialiseReasoner();
84 int answerCounter = 0, counter = 0;
85 for(; answers.isValid(); answers.moveNext()) {
86 ++counter;
87 if(check(answers.getTuple())) ++answerCounter;
88 }
89 answers.dispose();
90
91 Utility.logDebug("The number of individuals to be checked by HermiT: " + counter,
92 "The number of correct answers: " + answerCounter);
93 return answerCounter;
94 }
95
96 @Override
97 public boolean check(AnswerTuple answerTuple) {
98 if(isDisposed()) throw new DisposedException();
99
100 if(!toCheck) return false;
101 ++noOfCalls;
102 if(tag != 0) return tag == 1;
103 if(hermit == null) initialiseReasoner();
104
105 Timer t = new Timer();
106 Map<Variable, Term> sub = answerTuple.getAssignment(answerVariable[1]);
107 Set<OWLAxiom> toCheckAxioms = qGraph.getAssertions(sub);
108
109 // TODO complete
110 Set<OWLAxiom> toCheckExistentialAxioms = qGraph.getExistentialAxioms(sub);
111
112 // TODO possibly inefficient
113 for(OWLAxiom subclassAxiom : toCheckExistentialAxioms) {
114 Utility.logDebug("Checking consistency of ontology union " + subclassAxiom);
115 ontology.getOWLOntologyManager().addAxiom(ontology, subclassAxiom);
116 hermit.flush();
117 if(hermit.isConsistent()) {
118 ontology.getOWLOntologyManager().removeAxiom(ontology, subclassAxiom);
119 hermit.flush();
120 Utility.logDebug("@TIME to check one tuple: " + t.duration());
121 return false;
122 }
123 ontology.getOWLOntologyManager().removeAxiom(ontology, subclassAxiom);
124 hermit.flush();
125 }
126
127
128// for (OWLAxiom axiom: toCheckAxioms) System.out.println(axiom.toString());
129
130// Utility.logInfo(toCheckAxioms);
131
132 if(hermit.isEntailed(toCheckAxioms)) {
133 Utility.logDebug("@TIME to check one tuple: " + t.duration());
134 return true;
135 }
136 Utility.logDebug("@TIME to check one tuple: " + t.duration());
137 return false;
138 }
139
140 @Override
141 public boolean isConsistent() {
142 if(isDisposed()) throw new DisposedException();
143
144 if(hermit == null) initialiseReasoner();
145 return hermit.isConsistent();
146 }
147
148 public void dispose() {
149 super.dispose();
150
151 Utility.logDebug("Disposing of an instance of Hermit after " + noOfCalls + " calls");
152 if(hermit != null) hermit.dispose();
153 hermit = null;
154 }
155
156 public void setDependencyGraph(DependencyGraph dGraph) {
157 if(isDisposed()) throw new DisposedException();
158
159 this.dGraph = dGraph;
160 }
161
162 private void initialiseReasoner() {
163 qGraph = new QueryGraph(queryClause.getBodyAtoms(), answerVariable[1], ontology);
164 OWLOntologyManager manager = ontology.getOWLOntologyManager();
165 factory = manager.getOWLDataFactory();
166
167 if(hermit != null) hermit.dispose();
168
169 if(dGraph != null && answerVariable[1].length == 1 && (dGraph.getExits().size() > 1 || dGraph.getEntrances()
170 .size() > 1)) {
171 Set<OWLAxiom> topAxioms = new HashSet<OWLAxiom>();
172 Set<OWLAxiom> botAxioms = new HashSet<OWLAxiom>();
173 addTopAndBotTuple(topAxioms, botAxioms);
174 manager.addAxioms(ontology, topAxioms);
175 manager.addAxioms(ontology, botAxioms);
176 hermit = new Reasoner(ontology);
177 boolean topValid = true;
178 if(!hermit.isConsistent() || topAnswerTuple != null && (topValid = check(topAnswerTuple))) {
179 hermit.dispose();
180 manager.removeAxioms(ontology, topAxioms);
181 hermit = new Reasoner(ontology);
182 }
183 else {
184 if(!topValid) tag = -1;
185 else if(botAnswerTuple != null && check(botAnswerTuple)) tag = 1;
186 }
187 }
188 else
189 hermit = new Reasoner(ontology);
190 }
191
192 private void addTopAndBotTuple(Set<OWLAxiom> topAxioms, Set<OWLAxiom> botAxioms) {
193 String top_str = Namespace.PAGODA_ANONY + "top", bot_str = Namespace.PAGODA_ANONY + "bot";
194 topAnswerTuple =
195 new AnswerTuple(
196 new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(top_str)});
197 botAnswerTuple =
198 new AnswerTuple(
199 new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(bot_str)});
200 OWLIndividual top_ind = factory.getOWLNamedIndividual(IRI.create(top_str)), bot_ind =
201 factory.getOWLNamedIndividual(IRI.create(bot_str));
202 Map<OWLAxiom, Integer> counter = new HashMap<OWLAxiom, Integer>();
203
204 Set<String> topAnswers = new HashSet<String>(), botAnswers = new HashSet<String>();
205 OWLIndividual sub, obj;
206 if(dGraph.getExits().size() > 1) {
207 for(Clique answerClique : dGraph.getExits())
208 topAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative()
209 .getAnswerTuple()
210 .getGroundTerm(0)).getIRI());
211 }
212 else topAnswerTuple = null;
213
214 if(dGraph.getEntrances().size() > 1) {
215 for(Clique answerClique : dGraph.getEntrances())
216 botAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative()
217 .getAnswerTuple()
218 .getGroundTerm(0)).getIRI());
219 }
220 else botAnswerTuple = null;
221
222 for(OWLAxiom axiom : ontology.getABoxAxioms(true))
223 if(axiom instanceof OWLClassAssertionAxiom) {
224 OWLClassAssertionAxiom ca = (OWLClassAssertionAxiom) axiom;
225 sub = ca.getIndividual();
226 if(topAnswers.contains(sub.toStringID()))
227 topAxioms.add(factory.getOWLClassAssertionAxiom(ca.getClassExpression(), top_ind));
228 if(botAnswers.contains(sub.toStringID()))
229 inc(counter, factory.getOWLClassAssertionAxiom(ca.getClassExpression(), bot_ind));
230 }
231 else if(axiom instanceof OWLObjectPropertyAssertionAxiom) {
232 OWLObjectPropertyAssertionAxiom oa = (OWLObjectPropertyAssertionAxiom) axiom;
233 sub = oa.getSubject();
234 obj = oa.getObject();
235 if(topAnswers.contains(sub.toStringID()))
236 if(topAnswers.contains(obj.toStringID()))
237 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, top_ind));
238 else
239 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, obj));
240 else {
241 if(topAnswers.contains(obj.toStringID()))
242 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, top_ind));
243 }
244
245 if(botAnswers.contains(sub.toStringID()))
246 if(botAnswers.contains(obj.toStringID()))
247 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, bot_ind));
248 else
249 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, obj));
250 else {
251 if(botAnswers.contains(obj.toStringID()))
252 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, bot_ind));
253 }
254
255 }
256 else if(axiom instanceof OWLDataPropertyAssertionAxiom) {
257 OWLDataPropertyAssertionAxiom da = (OWLDataPropertyAssertionAxiom) axiom;
258 sub = da.getSubject();
259 if(topAnswers.contains(sub.toStringID()))
260 topAxioms.add(factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), top_ind, da.getObject()));
261
262 if(botAnswers.contains(sub.toStringID()))
263 inc(counter, factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), bot_ind, da.getObject()));
264 }
265
266 int number = botAnswers.size();
267 for(Map.Entry<OWLAxiom, Integer> entry : counter.entrySet()) {
268 if(entry.getValue() == number)
269 botAxioms.add(entry.getKey());
270 }
271 }
272
273 private void inc(Map<OWLAxiom, Integer> counter, OWLAxiom newAxiom) {
274 if(isDisposed()) throw new DisposedException();
275
276 Integer number = counter.get(newAxiom);
277 if(number == null) counter.put(newAxiom, 1);
278 else counter.put(newAxiom, number + 1);
279 }
280
281}