aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java176
1 files changed, 89 insertions, 87 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
index 1f435b7..dfbcb4d 100644
--- a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
+++ b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
@@ -37,21 +37,19 @@ public class MyQueryReasoner extends QueryReasoner {
37 BasicQueryEngine limitedSkolemUpperStore; 37 BasicQueryEngine limitedSkolemUpperStore;
38// boolean[] namedIndividuals_lazyUpper; 38// boolean[] namedIndividuals_lazyUpper;
39 39
40 OWLOntology elho_ontology; 40 OWLOntology elho_ontology;
41 KarmaQueryEngine elLowerStore = null; 41 KarmaQueryEngine elLowerStore = null;
42 42
43 BasicQueryEngine trackingStore = null; 43 BasicQueryEngine trackingStore = null;
44// boolean[] namedIndividuals_tracking; 44// boolean[] namedIndividuals_tracking;
45
46 boolean equalityTag;
47 boolean multiStageTag;
48 TrackingRuleEncoder encoder; 45 TrackingRuleEncoder encoder;
49 Timer t = new Timer(); 46 private boolean equalityTag;
47 private boolean multiStageTag;
48 private Timer t = new Timer();
50 private Collection<String> predicatesWithGap = null; 49 private Collection<String> predicatesWithGap = null;
51 private Boolean satisfiable; 50 private SatisfiabilityStatus satisfiable;
52 private ConsistencyManager consistency = new ConsistencyManager(this); 51 private ConsistencyManager consistency = new ConsistencyManager(this);
53 private boolean useUpperStores = false; 52 private boolean useUpperStores = false;
54
55 public MyQueryReasoner() { 53 public MyQueryReasoner() {
56 setup(true, true); 54 setup(true, true);
57 } 55 }
@@ -59,7 +57,7 @@ public class MyQueryReasoner extends QueryReasoner {
59 public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) { 57 public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) {
60 setup(multiStageTag, considerEqualities); 58 setup(multiStageTag, considerEqualities);
61 } 59 }
62 60
63 private BasicQueryEngine getUpperStore(String name, boolean checkValidity) { 61 private BasicQueryEngine getUpperStore(String name, boolean checkValidity) {
64 if (multiStageTag) 62 if (multiStageTag)
65 return new MultiStageQueryEngine(name, checkValidity); 63 return new MultiStageQueryEngine(name, checkValidity);
@@ -69,7 +67,7 @@ public class MyQueryReasoner extends QueryReasoner {
69 } 67 }
70 68
71 public void setup(boolean multiStageTag, boolean considerEqualities) { 69 public void setup(boolean multiStageTag, boolean considerEqualities) {
72 satisfiable = null; 70 satisfiable = SatisfiabilityStatus.UNCHECKED;
73 this.multiStageTag = multiStageTag; 71 this.multiStageTag = multiStageTag;
74 this.equalityTag = considerEqualities; 72 this.equalityTag = considerEqualities;
75 73
@@ -90,7 +88,7 @@ public class MyQueryReasoner extends QueryReasoner {
90 88
91 @Override 89 @Override
92 public void loadOntology(OWLOntology o) { 90 public void loadOntology(OWLOntology o) {
93 if (!equalityTag) { 91 if(!equalityTag) {
94 EqualitiesEliminator eliminator = new EqualitiesEliminator(o); 92 EqualitiesEliminator eliminator = new EqualitiesEliminator(o);
95 o = eliminator.getOutputOntology(); 93 o = eliminator.getOutputOntology();
96 eliminator.save(); 94 eliminator.save();
@@ -103,9 +101,9 @@ public class MyQueryReasoner extends QueryReasoner {
103// program.getGeneral().save(); 101// program.getGeneral().save();
104 102
105 useUpperStores = multiStageTag && !program.getGeneral().isHorn(); 103 useUpperStores = multiStageTag && !program.getGeneral().isHorn();
106 if (useUpperStores) { 104 if(useUpperStores) {
107 lazyUpperStore = getUpperStore("lazy-upper-bound", true); // new MultiStageQueryEngine("lazy-upper-bound", true); // 105 lazyUpperStore = getUpperStore("lazy-upper-bound", true);
108 limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true); 106 limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true);
109 } 107 }
110 108
111 importData(program.getAdditionalDataFile()); 109 importData(program.getAdditionalDataFile());
@@ -113,11 +111,11 @@ public class MyQueryReasoner extends QueryReasoner {
113 elho_ontology = new ELHOProfile().getFragment(ontology); 111 elho_ontology = new ELHOProfile().getFragment(ontology);
114 elLowerStore.processOntology(elho_ontology); 112 elLowerStore.processOntology(elho_ontology);
115 } 113 }
116 114
117 public Collection<String> getPredicatesWithGap() { 115 public Collection<String> getPredicatesWithGap() {
118 return predicatesWithGap; 116 return predicatesWithGap;
119 } 117 }
120 118
121 @Override 119 @Override
122 public boolean preprocess() { 120 public boolean preprocess() {
123 t.reset(); 121 t.reset();
@@ -127,7 +125,7 @@ public class MyQueryReasoner extends QueryReasoner {
127 rlLowerStore.importRDFData(name, datafile); 125 rlLowerStore.importRDFData(name, datafile);
128 rlLowerStore.materialise("lower program", program.getLower().toString()); 126 rlLowerStore.materialise("lower program", program.getLower().toString());
129// program.getLower().save(); 127// program.getLower().save();
130 if (!consistency.checkRLLowerBound()) return false; 128 if(!consistency.checkRLLowerBound()) return false;
131 Utility.logInfo("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber()); 129 Utility.logInfo("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber());
132 130
133 String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology); 131 String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology);
@@ -136,35 +134,35 @@ public class MyQueryReasoner extends QueryReasoner {
136 elLowerStore.materialise("saturate named individuals", originalMarkProgram); 134 elLowerStore.materialise("saturate named individuals", originalMarkProgram);
137 elLowerStore.materialise("lower program", program.getLower().toString()); 135 elLowerStore.materialise("lower program", program.getLower().toString());
138 elLowerStore.initialiseKarma(); 136 elLowerStore.initialiseKarma();
139 if (!consistency.checkELLowerBound()) return false; 137 if(!consistency.checkELLowerBound()) return false;
140 138
141 if (lazyUpperStore != null) { 139 if(lazyUpperStore != null) {
142 lazyUpperStore.importRDFData(name, datafile); 140 lazyUpperStore.importRDFData(name, datafile);
143 lazyUpperStore.materialise("saturate named individuals", originalMarkProgram); 141 lazyUpperStore.materialise("saturate named individuals", originalMarkProgram);
144 int tag = lazyUpperStore.materialiseRestrictedly(program, null); 142 int tag = lazyUpperStore.materialiseRestrictedly(program, null);
145 if (tag != 1) { 143 if(tag != 1) {
146 lazyUpperStore.dispose(); 144 lazyUpperStore.dispose();
147 lazyUpperStore = null; 145 lazyUpperStore = null;
148 } 146 }
149 if (tag == -1) return false; 147 if(tag == -1) return false;
150 } 148 }
151 if (consistency.checkUpper(lazyUpperStore)) { 149 if(consistency.checkUpper(lazyUpperStore)) {
152 satisfiable = true; 150 satisfiable = SatisfiabilityStatus.SATISFIABLE;
153 Utility.logInfo("time for satisfiability checking: " + t.duration()); 151 Utility.logInfo("time for satisfiability checking: " + t.duration());
154 } 152 }
155 153
156 if (limitedSkolemUpperStore != null) { 154 if(limitedSkolemUpperStore != null) {
157 limitedSkolemUpperStore.importRDFData(name, datafile); 155 limitedSkolemUpperStore.importRDFData(name, datafile);
158 limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); 156 limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram);
159 int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null); 157 int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null);
160 if (tag != 1) { 158 if(tag != 1) {
161 limitedSkolemUpperStore.dispose(); 159 limitedSkolemUpperStore.dispose();
162 limitedSkolemUpperStore = null; 160 limitedSkolemUpperStore = null;
163 } 161 }
164 if (tag == -1) return false; 162 if(tag == -1) return false;
165 } 163 }
166 if (consistency.checkUpper(limitedSkolemUpperStore)) { 164 if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) {
167 satisfiable = true; 165 satisfiable = SatisfiabilityStatus.SATISFIABLE;
168 Utility.logInfo("time for satisfiability checking: " + t.duration()); 166 Utility.logInfo("time for satisfiability checking: " + t.duration());
169 } 167 }
170 168
@@ -176,7 +174,7 @@ public class MyQueryReasoner extends QueryReasoner {
176 predicatesWithGap = gap.getPredicatesWithGap(); 174 predicatesWithGap = gap.getPredicatesWithGap();
177 gap.clear(); 175 gap.clear();
178 176
179 if (program.getGeneral().isHorn()) 177 if(program.getGeneral().isHorn())
180 encoder = new TrackingRuleEncoderWithGap(program.getUpper(), trackingStore); 178 encoder = new TrackingRuleEncoderWithGap(program.getUpper(), trackingStore);
181 else 179 else
182 encoder = new TrackingRuleEncoderDisjVar1(program.getUpper(), trackingStore); 180 encoder = new TrackingRuleEncoderDisjVar1(program.getUpper(), trackingStore);
@@ -186,21 +184,22 @@ public class MyQueryReasoner extends QueryReasoner {
186 184
187 program.deleteABoxTurtleFile(); 185 program.deleteABoxTurtleFile();
188 186
189 if (!isConsistent()) 187 if(!isConsistent())
190 return false; 188 return false;
191 189
192 consistency.extractBottomFragment(); 190 consistency.extractBottomFragment();
193 consistency.dispose(); 191 consistency.dispose();
192
194 return true; 193 return true;
195 } 194 }
196 195
197 @Override 196 @Override
198 public boolean isConsistent() { 197 public boolean isConsistent() {
199 if (satisfiable == null) { 198 if(satisfiable == SatisfiabilityStatus.UNCHECKED) {
200 satisfiable = consistency.check(); 199 satisfiable = consistency.check() ? SatisfiabilityStatus.SATISFIABLE : SatisfiabilityStatus.UNSATISFIABLE;
201 Utility.logInfo("time for satisfiability checking: " + t.duration()); 200 Utility.logInfo("time for satisfiability checking: " + t.duration());
202 } 201 }
203 return satisfiable; 202 return satisfiable == SatisfiabilityStatus.SATISFIABLE ? true : false;
204 } 203 }
205 204
206 /** 205 /**
@@ -208,8 +207,8 @@ public class MyQueryReasoner extends QueryReasoner {
208 * */ 207 * */
209 private OWLOntology relevantPart(QueryRecord queryRecord) { 208 private OWLOntology relevantPart(QueryRecord queryRecord) {
210 AnswerTuples rlAnswer = null, elAnswer = null; 209 AnswerTuples rlAnswer = null, elAnswer = null;
211 210
212 t.reset(); 211 t.reset();
213 try { 212 try {
214 rlAnswer = rlLowerStore.evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables()); 213 rlAnswer = rlLowerStore.evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables());
215 Utility.logDebug(t.duration()); 214 Utility.logDebug(t.duration());
@@ -218,11 +217,11 @@ public class MyQueryReasoner extends QueryReasoner {
218 if (rlAnswer != null) rlAnswer.dispose(); 217 if (rlAnswer != null) rlAnswer.dispose();
219 } 218 }
220 queryRecord.addProcessingTime(Step.LowerBound, t.duration()); 219 queryRecord.addProcessingTime(Step.LowerBound, t.duration());
221 220
222 t.reset(); 221 t.reset();
223 BasicQueryEngine upperStore = queryRecord.isBottom() || lazyUpperStore == null ? trackingStore : lazyUpperStore; 222 BasicQueryEngine upperStore = queryRecord.isBottom() || lazyUpperStore == null ? trackingStore : lazyUpperStore;
224 223
225 String[] extendedQuery = queryRecord.getExtendedQueryText(); 224 String[] extendedQuery = queryRecord.getExtendedQueryText();
226 225
227 // TODO why the following??? 226 // TODO why the following???
228 queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); 227 queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables());
@@ -246,10 +245,10 @@ public class MyQueryReasoner extends QueryReasoner {
246 245
247 queryRecord.addProcessingTime(Step.UpperBound, t.duration()); 246 queryRecord.addProcessingTime(Step.UpperBound, t.duration());
248 if (queryRecord.processed()) { 247 if (queryRecord.processed()) {
249 queryRecord.setDifficulty(Step.UpperBound); 248 queryRecord.setDifficulty(Step.UpperBound);
250 return null; 249 return null;
251 } 250 }
252 251
253 t.reset(); 252 t.reset();
254 try { 253 try {
255 elAnswer = elLowerStore.evaluate(extendedQuery[0], queryRecord.getAnswerVariables(), queryRecord.getLowerBoundAnswers()); 254 elAnswer = elLowerStore.evaluate(extendedQuery[0], queryRecord.getAnswerVariables(), queryRecord.getLowerBoundAnswers());
@@ -261,48 +260,46 @@ public class MyQueryReasoner extends QueryReasoner {
261 queryRecord.addProcessingTime(Step.ELLowerBound, t.duration()); 260 queryRecord.addProcessingTime(Step.ELLowerBound, t.duration());
262 261
263 if (queryRecord.processed()) { 262 if (queryRecord.processed()) {
264 queryRecord.setDifficulty(Step.ELLowerBound); 263 queryRecord.setDifficulty(Step.ELLowerBound);
265 return null; 264 return null;
266 } 265 }
267 266
268 t.reset(); 267 t.reset();
269 268
270 QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); 269 QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord);
271 270
272 OWLOntology knowledgebase; 271 OWLOntology knowledgebase;
273 t.reset(); 272 t.reset();
274// if (program.getGeneral().isHorn()) { 273// if (program.getGeneral().isHorn()) {
275// knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true); 274// knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true);
276// queryRecord.addProcessingTime(Step.Fragment, t.duration()); 275// queryRecord.addProcessingTime(Step.Fragment, t.duration());
277// return knowledgebase; 276// return knowledgebase;
278// } 277// }
279// else { 278// else {
280 knowledgebase = tracker.extract(trackingStore, consistency.getQueryRecords(), true); 279 knowledgebase = tracker.extract(trackingStore, consistency.getQueryRecords(), true);
281 queryRecord.addProcessingTime(Step.Fragment, t.duration()); 280 queryRecord.addProcessingTime(Step.Fragment, t.duration());
282// } 281// }
283 282
284 if (knowledgebase.isEmpty() || queryRecord.isBottom()) 283 if(knowledgebase.isEmpty() || queryRecord.isBottom())
285 return knowledgebase; 284 return knowledgebase;
286 285
287 if (program.getGeneral().isHorn()) return knowledgebase; 286 if(program.getGeneral().isHorn()) return knowledgebase;
288 287
289// t.reset(); 288// t.reset();
290// if (queryRecord.isHorn() && lazyUpperStore != null) { 289// if (queryRecord.isHorn() && lazyUpperStore != null) {
291//// knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true); 290//// knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true);
292// } else if (queryRecord.getArity() < 3) { 291// } else if (queryRecord.getArity() < 3) {
293// IterativeRefinement iterativeRefinement = new IterativeRefinement(queryRecord, tracker, trackingStore, consistency.getQueryRecords()); 292// IterativeRefinement iterativeRefinement = new IterativeRefinement(queryRecord, tracker, trackingStore, consistency.getQueryRecords());
294// knowledgebase = iterativeRefinement.extractWithFullABox(importedData.toString(), program.getUpperBottomStrategy()); 293// knowledgebase = iterativeRefinement.extractWithFullABox(importedData.toString(), program.getUpperBottomStrategy());
295// } 294// }
296// 295//
297// queryRecord.addProcessingTime(Step.FragmentRefinement, t.duration()); 296// queryRecord.addProcessingTime(Step.FragmentRefinement, t.duration());
298// 297//
299// if (knowledgebase == null) 298// if (knowledgebase == null)
300// queryRecord.setDifficulty(Step.FragmentRefinement); 299// queryRecord.setDifficulty(Step.FragmentRefinement);
301
302 return knowledgebase;
303 }
304 300
305// int counter = 0; 301 return knowledgebase;
302 }
306 303
307 private String toJsonKeyValuePair(String key, Object value) { 304 private String toJsonKeyValuePair(String key, Object value) {
308 HashMap<String, Object> map = new HashMap<>(); 305 HashMap<String, Object> map = new HashMap<>();
@@ -310,8 +307,10 @@ public class MyQueryReasoner extends QueryReasoner {
310 return QueryRecord.GsonCreator.getInstance().toJson(map); 307 return QueryRecord.GsonCreator.getInstance().toJson(map);
311 } 308 }
312 309
310// int counter = 0;
311
313 private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) { 312 private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) {
314 AnswerTuples rlAnswer = null; 313 AnswerTuples rlAnswer = null;
315 try { 314 try {
316 Utility.logDebug(queryText); 315 Utility.logDebug(queryText);
317 rlAnswer = upperStore.evaluate(queryText, answerVariables); 316 rlAnswer = upperStore.evaluate(queryText, answerVariables);
@@ -326,37 +325,38 @@ public class MyQueryReasoner extends QueryReasoner {
326 @Override 325 @Override
327 public void evaluate(QueryRecord queryRecord) { 326 public void evaluate(QueryRecord queryRecord) {
328 OWLOntology knowledgeBase = relevantPart(queryRecord); 327 OWLOntology knowledgeBase = relevantPart(queryRecord);
329 328
330 if (knowledgeBase == null) { 329 if(knowledgeBase == null) {
331 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); 330 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty());
332 return ; 331 return;
333 } 332 }
334 333
335 int aBoxCount = knowledgeBase.getABoxAxioms(true).size(); 334 int aBoxCount = knowledgeBase.getABoxAxioms(true).size();
336 Utility.logDebug("ABox axioms: " + aBoxCount + " TBox axioms: " + (knowledgeBase.getAxiomCount() - aBoxCount)); 335 Utility.logDebug("ABox axioms: " + aBoxCount + " TBox axioms: " + (knowledgeBase.getAxiomCount() - aBoxCount));
337// queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); 336// queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl");
338 337
339 Timer t = new Timer(); 338 Timer t = new Timer();
340 Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT()); 339 Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT());
341// int validNumber = 340// int validNumber =
342 summarisedChecker.check(queryRecord.getGapAnswers()); 341 summarisedChecker.check(queryRecord.getGapAnswers());
343 summarisedChecker.dispose(); 342 summarisedChecker.dispose();
344 Utility.logDebug("Total time for full reasoner: " + t.duration()); 343 Utility.logDebug("Total time for full reasoner: " + t.duration());
345// if (validNumber == 0) { 344// if (validNumber == 0) {
346 queryRecord.markAsProcessed(); 345 queryRecord.markAsProcessed();
347 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); 346 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty());
348// } 347// }
349 } 348 }
350 349
351 @Override 350 @Override
352 public void evaluateUpper(QueryRecord queryRecord) { 351 public void evaluateUpper(QueryRecord queryRecord) {
353 AnswerTuples rlAnswer = null; 352 AnswerTuples rlAnswer = null;
354 boolean useFull = queryRecord.isBottom() || lazyUpperStore == null; 353 boolean useFull = queryRecord.isBottom() || lazyUpperStore == null;
355 try { 354 try {
356 rlAnswer = (useFull ? trackingStore: lazyUpperStore).evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables()); 355 rlAnswer =
357 queryRecord.updateUpperBoundAnswers(rlAnswer, true); 356 (useFull ? trackingStore : lazyUpperStore).evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables());
357 queryRecord.updateUpperBoundAnswers(rlAnswer, true);
358 } finally { 358 } finally {
359 if (rlAnswer != null) rlAnswer.dispose(); 359 if(rlAnswer != null) rlAnswer.dispose();
360 } 360 }
361 } 361 }
362 362
@@ -370,4 +370,6 @@ public class MyQueryReasoner extends QueryReasoner {
370 super.dispose(); 370 super.dispose();
371 } 371 }
372 372
373 enum SatisfiabilityStatus {SATISFIABLE, UNSATISFIABLE, UNCHECKED}
374
373} 375}