diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java | 145 |
1 files changed, 70 insertions, 75 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java index cc0e647..b5b9534 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java | |||
| @@ -23,7 +23,6 @@ import uk.ac.ox.cs.pagoda.util.Utility; | |||
| 23 | import uk.ac.ox.cs.pagoda.util.tuples.Tuple; | 23 | import uk.ac.ox.cs.pagoda.util.tuples.Tuple; |
| 24 | 24 | ||
| 25 | import java.util.Collection; | 25 | import java.util.Collection; |
| 26 | import java.util.HashMap; | ||
| 27 | 26 | ||
| 28 | class MyQueryReasoner extends QueryReasoner { | 27 | class MyQueryReasoner extends QueryReasoner { |
| 29 | 28 | ||
| @@ -33,7 +32,7 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 33 | // String additonalDataFile; | 32 | // String additonalDataFile; |
| 34 | BasicQueryEngine rlLowerStore = null; | 33 | BasicQueryEngine rlLowerStore = null; |
| 35 | BasicQueryEngine lazyUpperStore = null; | 34 | BasicQueryEngine lazyUpperStore = null; |
| 36 | BasicQueryEngine limitedSkolemUpperStore; | 35 | // BasicQueryEngine limitedSkolemUpperStore; |
| 37 | OWLOntology elho_ontology; | 36 | OWLOntology elho_ontology; |
| 38 | // boolean[] namedIndividuals_lazyUpper; | 37 | // boolean[] namedIndividuals_lazyUpper; |
| 39 | KarmaQueryEngine elLowerStore = null; | 38 | KarmaQueryEngine elLowerStore = null; |
| @@ -99,7 +98,7 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 99 | useUpperStores = multiStageTag && !program.getGeneral().isHorn(); | 98 | useUpperStores = multiStageTag && !program.getGeneral().isHorn(); |
| 100 | if(useUpperStores) { | 99 | if(useUpperStores) { |
| 101 | lazyUpperStore = getUpperStore("lazy-upper-bound", true); | 100 | lazyUpperStore = getUpperStore("lazy-upper-bound", true); |
| 102 | limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true); | 101 | // limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true); |
| 103 | } | 102 | } |
| 104 | 103 | ||
| 105 | importData(program.getAdditionalDataFile()); | 104 | importData(program.getAdditionalDataFile()); |
| @@ -147,20 +146,20 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 147 | Utility.logInfo("time for satisfiability checking: " + t.duration()); | 146 | Utility.logInfo("time for satisfiability checking: " + t.duration()); |
| 148 | } | 147 | } |
| 149 | 148 | ||
| 150 | if(limitedSkolemUpperStore != null) { | 149 | // if(limitedSkolemUpperStore != null) { |
| 151 | limitedSkolemUpperStore.importRDFData(name, datafile); | 150 | // limitedSkolemUpperStore.importRDFData(name, datafile); |
| 152 | limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); | 151 | // limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); |
| 153 | int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null); | 152 | // int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null); |
| 154 | if(tag != 1) { | 153 | // if(tag != 1) { |
| 155 | limitedSkolemUpperStore.dispose(); | 154 | // limitedSkolemUpperStore.dispose(); |
| 156 | limitedSkolemUpperStore = null; | 155 | // limitedSkolemUpperStore = null; |
| 157 | } | 156 | // } |
| 158 | if(tag == -1) return false; | 157 | // if(tag == -1) return false; |
| 159 | } | 158 | // } |
| 160 | if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) { | 159 | // if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) { |
| 161 | satisfiable = SatisfiabilityStatus.SATISFIABLE; | 160 | // satisfiable = SatisfiabilityStatus.SATISFIABLE; |
| 162 | Utility.logInfo("time for satisfiability checking: " + t.duration()); | 161 | // Utility.logInfo("time for satisfiability checking: " + t.duration()); |
| 163 | } | 162 | // } |
| 164 | 163 | ||
| 165 | trackingStore.importRDFData(name, datafile); | 164 | trackingStore.importRDFData(name, datafile); |
| 166 | trackingStore.materialise("saturate named individuals", originalMarkProgram); | 165 | trackingStore.materialise("saturate named individuals", originalMarkProgram); |
| @@ -214,7 +213,7 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 214 | queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); | 213 | queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); |
| 215 | 214 | ||
| 216 | queryRecord.addProcessingTime(step, t.duration()); | 215 | queryRecord.addProcessingTime(step, t.duration()); |
| 217 | if(queryRecord.processed()) { | 216 | if(queryRecord.isProcessed()) { |
| 218 | queryRecord.setDifficulty(step); | 217 | queryRecord.setDifficulty(step); |
| 219 | return true; | 218 | return true; |
| 220 | } | 219 | } |
| @@ -224,7 +223,7 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 224 | /** | 223 | /** |
| 225 | * Returns the part of the ontology relevant for Hermit, while computing the bound answers. | 224 | * Returns the part of the ontology relevant for Hermit, while computing the bound answers. |
| 226 | * */ | 225 | * */ |
| 227 | private OWLOntology relevantPart(QueryRecord queryRecord) { | 226 | private boolean queryBounds(QueryRecord queryRecord) { |
| 228 | AnswerTuples rlAnswer = null, elAnswer = null; | 227 | AnswerTuples rlAnswer = null, elAnswer = null; |
| 229 | 228 | ||
| 230 | t.reset(); | 229 | t.reset(); |
| @@ -243,15 +242,15 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 243 | 242 | ||
| 244 | Utility.logDebug("Tracking store"); | 243 | Utility.logDebug("Tracking store"); |
| 245 | if(queryUpperStore(trackingStore, queryRecord, extendedQueryTexts, Step.SIMPLE_UPPER_BOUND)) | 244 | if(queryUpperStore(trackingStore, queryRecord, extendedQueryTexts, Step.SIMPLE_UPPER_BOUND)) |
| 246 | return null; | 245 | return true; |
| 247 | 246 | ||
| 248 | if(!queryRecord.isBottom()) { | 247 | if(!queryRecord.isBottom()) { |
| 249 | Utility.logDebug("Lazy store"); | 248 | Utility.logDebug("Lazy store"); |
| 250 | if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND)) | 249 | if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND)) |
| 251 | return null; | 250 | return true; |
| 252 | Utility.logDebug("Skolem store"); | 251 | // Utility.logDebug("Skolem store"); |
| 253 | if(limitedSkolemUpperStore != null && queryUpperStore(limitedSkolemUpperStore, queryRecord, extendedQueryTexts, Step.L_SKOLEM_UPPER_BOUND)) | 252 | // if(limitedSkolemUpperStore != null && queryUpperStore(limitedSkolemUpperStore, queryRecord, extendedQueryTexts, Step.L_SKOLEM_UPPER_BOUND)) |
| 254 | return null; | 253 | // return null; |
| 255 | } | 254 | } |
| 256 | 255 | ||
| 257 | t.reset(); | 256 | t.reset(); |
| @@ -266,52 +265,23 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 266 | } | 265 | } |
| 267 | queryRecord.addProcessingTime(Step.EL_LOWER_BOUND, t.duration()); | 266 | queryRecord.addProcessingTime(Step.EL_LOWER_BOUND, t.duration()); |
| 268 | 267 | ||
| 269 | if (queryRecord.processed()) { | 268 | if(queryRecord.isProcessed()) { |
| 270 | queryRecord.setDifficulty(Step.EL_LOWER_BOUND); | 269 | queryRecord.setDifficulty(Step.EL_LOWER_BOUND); |
| 271 | return null; | 270 | return true; |
| 272 | } | 271 | } |
| 273 | 272 | ||
| 273 | return false; | ||
| 274 | } | ||
| 275 | |||
| 276 | private OWLOntology extractRelevantOntologySubset(QueryRecord queryRecord) { | ||
| 274 | t.reset(); | 277 | t.reset(); |
| 275 | 278 | ||
| 276 | QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); | 279 | QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); |
| 280 | OWLOntology relevantOntologySubset = tracker.extract(trackingStore, consistency.getQueryRecords(), true); | ||
| 277 | 281 | ||
| 278 | OWLOntology knowledgeBase; | ||
| 279 | t.reset(); | ||
| 280 | // if (program.getGeneral().isHorn()) { | ||
| 281 | // knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true); | ||
| 282 | // queryRecord.addProcessingTime(Step.Fragment, t.duration()); | ||
| 283 | // return knowledgebase; | ||
| 284 | // } | ||
| 285 | // else { | ||
| 286 | knowledgeBase = tracker.extract(trackingStore, consistency.getQueryRecords(), true); | ||
| 287 | queryRecord.addProcessingTime(Step.FRAGMENT, t.duration()); | 282 | queryRecord.addProcessingTime(Step.FRAGMENT, t.duration()); |
| 288 | // } | ||
| 289 | |||
| 290 | if(knowledgeBase.isEmpty() || queryRecord.isBottom()) | ||
| 291 | return knowledgeBase; | ||
| 292 | |||
| 293 | if(program.getGeneral().isHorn()) return knowledgeBase; | ||
| 294 | 283 | ||
| 295 | // t.reset(); | 284 | return relevantOntologySubset; |
| 296 | // if (queryRecord.isHorn() && lazyUpperStore != null) { | ||
| 297 | //// knowledgebase = tracker.extract(lazyUpperStore, consistency.getQueryRecords(), true); | ||
| 298 | // } else if (queryRecord.getArity() < 3) { | ||
| 299 | // IterativeRefinement iterativeRefinement = new IterativeRefinement(queryRecord, tracker, trackingStore, consistency.getQueryRecords()); | ||
| 300 | // knowledgebase = iterativeRefinement.extractWithFullABox(importedData.toString(), program.getUpperBottomStrategy()); | ||
| 301 | // } | ||
| 302 | // | ||
| 303 | // queryRecord.addProcessingTime(Step.FRAGMENT_REFINEMENT, t.duration()); | ||
| 304 | // | ||
| 305 | // if (knowledgebase == null) | ||
| 306 | // queryRecord.setDifficulty(Step.FRAGMENT_REFINEMENT); | ||
| 307 | |||
| 308 | return knowledgeBase; | ||
| 309 | } | ||
| 310 | |||
| 311 | private String toJsonKeyValuePair(String key, Object value) { | ||
| 312 | HashMap<String, Object> map = new HashMap<>(); | ||
| 313 | map.put(key, value); | ||
| 314 | return QueryRecord.GsonCreator.getInstance().toJson(map); | ||
| 315 | } | 285 | } |
| 316 | 286 | ||
| 317 | private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) { | 287 | private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) { |
| @@ -323,35 +293,59 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 323 | queryRecord.updateUpperBoundAnswers(rlAnswer); | 293 | queryRecord.updateUpperBoundAnswers(rlAnswer); |
| 324 | } finally { | 294 | } finally { |
| 325 | if(rlAnswer != null) rlAnswer.dispose(); | 295 | if(rlAnswer != null) rlAnswer.dispose(); |
| 326 | rlAnswer = null; | ||
| 327 | } | 296 | } |
| 328 | } | 297 | } |
| 329 | 298 | ||
| 330 | // int counter = 0; | ||
| 331 | |||
| 332 | @Override | 299 | @Override |
| 333 | public void evaluate(QueryRecord queryRecord) { | 300 | public void evaluate(QueryRecord queryRecord) { |
| 334 | OWLOntology knowledgeBase = relevantPart(queryRecord); | 301 | if(queryBounds(queryRecord)) |
| 335 | |||
| 336 | if(knowledgeBase == null) { | ||
| 337 | Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); | ||
| 338 | return; | 302 | return; |
| 339 | } | ||
| 340 | 303 | ||
| 341 | int aBoxCount = knowledgeBase.getABoxAxioms(true).size(); | 304 | OWLOntology relevantOntologySubset = extractRelevantOntologySubset(queryRecord); |
| 342 | Utility.logDebug("ABox axioms: " + aBoxCount + " TBox axioms: " + (knowledgeBase.getAxiomCount() - aBoxCount)); | 305 | |
| 306 | int aBoxCount = relevantOntologySubset.getABoxAxioms(true).size(); | ||
| 307 | Utility.logInfo("Relevant ontology subset: ABox_axioms=" + aBoxCount + " TBox_axioms=" + (relevantOntologySubset | ||
| 308 | .getAxiomCount() - aBoxCount)); | ||
| 343 | // queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); | 309 | // queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); |
| 344 | 310 | ||
| 311 | if(querySkolemisedRelevantSubset(relevantOntologySubset, queryRecord)) | ||
| 312 | return; | ||
| 313 | |||
| 345 | Timer t = new Timer(); | 314 | Timer t = new Timer(); |
| 346 | Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT()); | 315 | Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT()); |
| 347 | // int validNumber = | ||
| 348 | summarisedChecker.check(queryRecord.getGapAnswers()); | 316 | summarisedChecker.check(queryRecord.getGapAnswers()); |
| 349 | summarisedChecker.dispose(); | 317 | summarisedChecker.dispose(); |
| 350 | Utility.logDebug("Total time for full reasoner: " + t.duration()); | 318 | Utility.logDebug("Total time for full reasoner: " + t.duration()); |
| 351 | // if (validNumber == 0) { | ||
| 352 | queryRecord.markAsProcessed(); | 319 | queryRecord.markAsProcessed(); |
| 353 | Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); | 320 | Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); |
| 354 | // } | 321 | } |
| 322 | |||
| 323 | private boolean querySkolemisedRelevantSubset(OWLOntology relevantSubset, QueryRecord queryRecord) { | ||
| 324 | MultiStageQueryEngine relevantStore = | ||
| 325 | new MultiStageQueryEngine("Relevant-store", true); // checkValidity is true | ||
| 326 | DatalogProgram relevantProgram = new DatalogProgram(relevantSubset, false); // toClassify is false | ||
| 327 | |||
| 328 | // relevantStore.importRDFData("data", importedData.toString()); // 2 answers more | ||
| 329 | relevantStore.importDataFromABoxOf(relevantSubset); | ||
| 330 | |||
| 331 | int materialisationResult = relevantStore.materialiseSkolemly(relevantProgram, null); | ||
| 332 | if(materialisationResult != 1) | ||
| 333 | throw new RuntimeException("Skolemised materialisation error"); // TODO check consistency | ||
| 334 | // relevantStore.materialiseRestrictedly(relevantProgram, null); // it has been tried | ||
| 335 | |||
| 336 | return queryUpperStore(relevantStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND); | ||
| 337 | |||
| 338 | // the following has been tried | ||
| 339 | // Tuple<String> extendedQueryText = queryRecord.getExtendedQueryText(); | ||
| 340 | // if(queryRecord.hasNonAnsDistinguishedVariables()) { | ||
| 341 | // queryUpperBound(relevantStore, queryRecord, extendedQueryText.get(0), queryRecord.getAnswerVariables()); | ||
| 342 | // queryUpperBound(relevantStore, queryRecord, extendedQueryText.get(1), queryRecord.getDistinguishedVariables()); | ||
| 343 | // } | ||
| 344 | // else | ||
| 345 | // queryUpperBound(relevantStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); | ||
| 346 | // | ||
| 347 | // return queryRecord.isProcessed(); | ||
| 348 | |||
| 355 | } | 349 | } |
| 356 | 350 | ||
| 357 | @Override | 351 | @Override |
| @@ -375,7 +369,8 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 375 | if (lazyUpperStore != null) lazyUpperStore.dispose(); | 369 | if (lazyUpperStore != null) lazyUpperStore.dispose(); |
| 376 | if (elLowerStore != null) elLowerStore.dispose(); | 370 | if (elLowerStore != null) elLowerStore.dispose(); |
| 377 | if (trackingStore != null) trackingStore.dispose(); | 371 | if (trackingStore != null) trackingStore.dispose(); |
| 378 | if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose(); | 372 | |
| 373 | // if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose(); | ||
| 379 | super.dispose(); | 374 | super.dispose(); |
| 380 | } | 375 | } |
| 381 | 376 | ||
