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 | 132 |
1 files changed, 68 insertions, 64 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java index 8445713..acdb8a3 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java | |||
| @@ -31,42 +31,29 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 31 | OWLOntology ontology; | 31 | OWLOntology ontology; |
| 32 | DatalogProgram program; | 32 | DatalogProgram program; |
| 33 | 33 | ||
| 34 | // String additonalDataFile; | ||
| 35 | BasicQueryEngine rlLowerStore = null; | 34 | BasicQueryEngine rlLowerStore = null; |
| 36 | BasicQueryEngine lazyUpperStore = null; | 35 | BasicQueryEngine lazyUpperStore = null; |
| 37 | MultiStageQueryEngine limitedSkolemUpperStore; | 36 | // MultiStageQueryEngine limitedSkolemUpperStore; |
| 38 | OWLOntology elho_ontology; | 37 | OWLOntology elho_ontology; |
| 39 | // boolean[] namedIndividuals_lazyUpper; | ||
| 40 | KarmaQueryEngine elLowerStore = null; | 38 | KarmaQueryEngine elLowerStore = null; |
| 41 | BasicQueryEngine trackingStore = null; | 39 | BasicQueryEngine trackingStore = null; |
| 42 | // boolean[] namedIndividuals_tracking; | ||
| 43 | TrackingRuleEncoder encoder; | 40 | TrackingRuleEncoder encoder; |
| 44 | private boolean equalityTag; | 41 | private boolean equalityTag; |
| 45 | private boolean multiStageTag; | ||
| 46 | private Timer t = new Timer(); | 42 | private Timer t = new Timer(); |
| 47 | private Collection<String> predicatesWithGap = null; | 43 | private Collection<String> predicatesWithGap = null; |
| 48 | private SatisfiabilityStatus satisfiable; | 44 | private SatisfiabilityStatus satisfiable; |
| 49 | private ConsistencyManager consistency = new ConsistencyManager(this); | 45 | private ConsistencyManager consistency = new ConsistencyManager(this); |
| 50 | private boolean useUpperStores = false; | ||
| 51 | 46 | ||
| 52 | public MyQueryReasoner() { | 47 | public MyQueryReasoner() { |
| 53 | setup(true, true); | 48 | setup(true); |
| 54 | } | 49 | } |
| 55 | 50 | ||
| 56 | public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) { | 51 | public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) { |
| 57 | setup(multiStageTag, considerEqualities); | 52 | if(!multiStageTag) |
| 58 | } | 53 | throw new IllegalArgumentException( |
| 54 | "Value \"true\" for parameter \"multiStageTag\" is no longer supported"); | ||
| 59 | 55 | ||
| 60 | public void setup(boolean multiStageTag, boolean considerEqualities) { | 56 | setup(considerEqualities); |
| 61 | if(isDisposed()) throw new DisposedException(); | ||
| 62 | satisfiable = SatisfiabilityStatus.UNCHECKED; | ||
| 63 | this.multiStageTag = multiStageTag; | ||
| 64 | this.equalityTag = considerEqualities; | ||
| 65 | |||
| 66 | rlLowerStore = new BasicQueryEngine("rl-lower-bound"); | ||
| 67 | elLowerStore = new KarmaQueryEngine("elho-lower-bound"); | ||
| 68 | |||
| 69 | trackingStore = getUpperStore("tracking", false); | ||
| 70 | } | 57 | } |
| 71 | 58 | ||
| 72 | @Override | 59 | @Override |
| @@ -84,11 +71,8 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 84 | // program.getUpper().save(); | 71 | // program.getUpper().save(); |
| 85 | // program.getGeneral().save(); | 72 | // program.getGeneral().save(); |
| 86 | 73 | ||
| 87 | useUpperStores = multiStageTag && !program.getGeneral().isHorn(); | 74 | if(!program.getGeneral().isHorn()) |
| 88 | if(useUpperStores) { | 75 | lazyUpperStore = new MultiStageQueryEngine("lazy-upper-bound", true); |
| 89 | lazyUpperStore = getUpperStore("lazy-upper-bound", true); | ||
| 90 | limitedSkolemUpperStore = new MultiStageQueryEngine("limited-skolem-upper-bound", true); | ||
| 91 | } | ||
| 92 | 76 | ||
| 93 | importData(program.getAdditionalDataFile()); | 77 | importData(program.getAdditionalDataFile()); |
| 94 | 78 | ||
| @@ -104,15 +88,16 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 104 | @Override | 88 | @Override |
| 105 | public boolean preprocess() { | 89 | public boolean preprocess() { |
| 106 | if(isDisposed()) throw new DisposedException(); | 90 | if(isDisposed()) throw new DisposedException(); |
| 91 | |||
| 107 | t.reset(); | 92 | t.reset(); |
| 108 | Utility.logInfo("Preprocessing... checking satisfiability... "); | 93 | Utility.logInfo("Preprocessing (and checking satisfiability)..."); |
| 109 | 94 | ||
| 110 | String name = "data", datafile = importedData.toString(); | 95 | String name = "data", datafile = importedData.toString(); |
| 111 | rlLowerStore.importRDFData(name, datafile); | 96 | rlLowerStore.importRDFData(name, datafile); |
| 112 | rlLowerStore.materialise("lower program", program.getLower().toString()); | 97 | rlLowerStore.materialise("lower program", program.getLower().toString()); |
| 113 | // program.getLower().save(); | 98 | // program.getLower().save(); |
| 114 | if(!consistency.checkRLLowerBound()) return false; | 99 | if(!consistency.checkRLLowerBound()) return false; |
| 115 | Utility.logInfo("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber()); | 100 | Utility.logDebug("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber()); |
| 116 | 101 | ||
| 117 | String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology); | 102 | String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology); |
| 118 | 103 | ||
| @@ -134,22 +119,7 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 134 | } | 119 | } |
| 135 | if(consistency.checkUpper(lazyUpperStore)) { | 120 | if(consistency.checkUpper(lazyUpperStore)) { |
| 136 | satisfiable = SatisfiabilityStatus.SATISFIABLE; | 121 | satisfiable = SatisfiabilityStatus.SATISFIABLE; |
| 137 | Utility.logInfo("time for satisfiability checking: " + t.duration()); | 122 | Utility.logDebug("time for satisfiability checking: " + t.duration()); |
| 138 | } | ||
| 139 | |||
| 140 | if(limitedSkolemUpperStore != null) { | ||
| 141 | limitedSkolemUpperStore.importRDFData(name, datafile); | ||
| 142 | limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); | ||
| 143 | int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null); | ||
| 144 | if(tag != 1) { | ||
| 145 | limitedSkolemUpperStore.dispose(); | ||
| 146 | limitedSkolemUpperStore = null; | ||
| 147 | } | ||
| 148 | if(tag == -1) return false; | ||
| 149 | } | ||
| 150 | if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) { | ||
| 151 | satisfiable = SatisfiabilityStatus.SATISFIABLE; | ||
| 152 | Utility.logInfo("time for satisfiability checking: " + t.duration()); | ||
| 153 | } | 123 | } |
| 154 | 124 | ||
| 155 | trackingStore.importRDFData(name, datafile); | 125 | trackingStore.importRDFData(name, datafile); |
| @@ -194,14 +164,11 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 194 | @Override | 164 | @Override |
| 195 | public void evaluate(QueryRecord queryRecord) { | 165 | public void evaluate(QueryRecord queryRecord) { |
| 196 | if(isDisposed()) throw new DisposedException(); | 166 | if(isDisposed()) throw new DisposedException(); |
| 197 | if(queryBounds(queryRecord)) | 167 | |
| 168 | if(queryLowerAndUpperBounds(queryRecord)) | ||
| 198 | return; | 169 | return; |
| 199 | 170 | ||
| 200 | OWLOntology relevantOntologySubset = extractRelevantOntologySubset(queryRecord); | 171 | OWLOntology relevantOntologySubset = extractRelevantOntologySubset(queryRecord); |
| 201 | |||
| 202 | int aBoxCount = relevantOntologySubset.getABoxAxioms(true).size(); | ||
| 203 | Utility.logInfo("Relevant ontology subset: ABox_axioms=" + aBoxCount + " TBox_axioms=" + (relevantOntologySubset | ||
| 204 | .getAxiomCount() - aBoxCount)); | ||
| 205 | // queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); | 172 | // queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); |
| 206 | 173 | ||
| 207 | if(querySkolemisedRelevantSubset(relevantOntologySubset, queryRecord)) | 174 | if(querySkolemisedRelevantSubset(relevantOntologySubset, queryRecord)) |
| @@ -240,16 +207,18 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 240 | if(lazyUpperStore != null) lazyUpperStore.dispose(); | 207 | if(lazyUpperStore != null) lazyUpperStore.dispose(); |
| 241 | if(elLowerStore != null) elLowerStore.dispose(); | 208 | if(elLowerStore != null) elLowerStore.dispose(); |
| 242 | if(trackingStore != null) trackingStore.dispose(); | 209 | if(trackingStore != null) trackingStore.dispose(); |
| 243 | if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose(); | 210 | // if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose(); |
| 244 | |||
| 245 | } | 211 | } |
| 246 | 212 | ||
| 247 | private BasicQueryEngine getUpperStore(String name, boolean checkValidity) { | 213 | private void setup(boolean considerEqualities) { |
| 248 | if(multiStageTag) | 214 | if(isDisposed()) throw new DisposedException(); |
| 249 | return new MultiStageQueryEngine(name, checkValidity); | 215 | satisfiable = SatisfiabilityStatus.UNCHECKED; |
| 250 | // return new TwoStageQueryEngine(name, checkValidity); | 216 | this.equalityTag = considerEqualities; |
| 251 | else | 217 | |
| 252 | return new BasicQueryEngine(name); | 218 | rlLowerStore = new BasicQueryEngine("rl-lower-bound"); |
| 219 | elLowerStore = new KarmaQueryEngine("elho-lower-bound"); | ||
| 220 | |||
| 221 | trackingStore = new MultiStageQueryEngine("tracking", false); | ||
| 253 | } | 222 | } |
| 254 | 223 | ||
| 255 | protected void internal_importDataFile(String name, String datafile) { | 224 | protected void internal_importDataFile(String name, String datafile) { |
| @@ -284,10 +253,37 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 284 | return false; | 253 | return false; |
| 285 | } | 254 | } |
| 286 | 255 | ||
| 256 | private boolean checkGapAnswers(BasicQueryEngine relevantStore, QueryRecord queryRecord) { | ||
| 257 | Tuple<String> extendedQueries = queryRecord.getExtendedQueryText(); | ||
| 258 | if(queryRecord.hasNonAnsDistinguishedVariables()) | ||
| 259 | checkGapAnswers(relevantStore, queryRecord, extendedQueries.get(0), queryRecord.getAnswerVariables()); | ||
| 260 | else | ||
| 261 | checkGapAnswers(relevantStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); | ||
| 262 | |||
| 263 | queryRecord.addProcessingTime(Step.L_SKOLEM_UPPER_BOUND, t.duration()); | ||
| 264 | if(queryRecord.isProcessed()) { | ||
| 265 | queryRecord.setDifficulty(Step.L_SKOLEM_UPPER_BOUND); | ||
| 266 | return true; | ||
| 267 | } | ||
| 268 | return false; | ||
| 269 | } | ||
| 270 | |||
| 271 | private void checkGapAnswers(BasicQueryEngine relevantStore, QueryRecord queryRecord, String queryText, String[] answerVariables) { | ||
| 272 | AnswerTuples rlAnswer = null; | ||
| 273 | try { | ||
| 274 | Utility.logDebug(queryText); | ||
| 275 | rlAnswer = relevantStore.evaluate(queryText, answerVariables); | ||
| 276 | Utility.logDebug(t.duration()); | ||
| 277 | queryRecord.checkUpperBoundAnswers(rlAnswer); | ||
| 278 | } finally { | ||
| 279 | if(rlAnswer != null) rlAnswer.dispose(); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 287 | /** | 283 | /** |
| 288 | * Returns the part of the ontology relevant for Hermit, while computing the bound answers. | 284 | * Returns the part of the ontology relevant for Hermit, while computing the bound answers. |
| 289 | */ | 285 | */ |
| 290 | private boolean queryBounds(QueryRecord queryRecord) { | 286 | private boolean queryLowerAndUpperBounds(QueryRecord queryRecord) { |
| 291 | AnswerTuples rlAnswer = null, elAnswer = null; | 287 | AnswerTuples rlAnswer = null, elAnswer = null; |
| 292 | 288 | ||
| 293 | t.reset(); | 289 | t.reset(); |
| @@ -312,9 +308,6 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 312 | Utility.logDebug("Lazy store"); | 308 | Utility.logDebug("Lazy store"); |
| 313 | if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND)) | 309 | if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND)) |
| 314 | return true; | 310 | return true; |
| 315 | // Utility.logDebug("Skolem store"); | ||
| 316 | // if(limitedSkolemUpperStore != null && queryUpperStore(limitedSkolemUpperStore, queryRecord, extendedQueryTexts, Step.L_SKOLEM_UPPER_BOUND)) | ||
| 317 | // return null; | ||
| 318 | } | 311 | } |
| 319 | 312 | ||
| 320 | t.reset(); | 313 | t.reset(); |
| @@ -338,6 +331,8 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 338 | } | 331 | } |
| 339 | 332 | ||
| 340 | private OWLOntology extractRelevantOntologySubset(QueryRecord queryRecord) { | 333 | private OWLOntology extractRelevantOntologySubset(QueryRecord queryRecord) { |
| 334 | Utility.logInfo("Extracting relevant ontology-subset..."); | ||
| 335 | |||
| 341 | t.reset(); | 336 | t.reset(); |
| 342 | 337 | ||
| 343 | QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); | 338 | QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); |
| @@ -345,6 +340,14 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 345 | 340 | ||
| 346 | queryRecord.addProcessingTime(Step.FRAGMENT, t.duration()); | 341 | queryRecord.addProcessingTime(Step.FRAGMENT, t.duration()); |
| 347 | 342 | ||
| 343 | // just statistics | ||
| 344 | int numOfABoxAxioms = relevantOntologySubset.getABoxAxioms(true).size(); | ||
| 345 | int numOfTBoxAxioms = relevantOntologySubset.getAxiomCount() - numOfABoxAxioms; | ||
| 346 | int originalNumOfABoxAxioms = ontology.getABoxAxioms(true).size(); | ||
| 347 | int originalNumOfTBoxAxioms = ontology.getAxiomCount() - originalNumOfABoxAxioms; | ||
| 348 | Utility.logInfo("Relevant ontology-subset has been extracted: |ABox|=" | ||
| 349 | + numOfABoxAxioms + ", |TBox|=" + numOfTBoxAxioms); | ||
| 350 | |||
| 348 | return relevantOntologySubset; | 351 | return relevantOntologySubset; |
| 349 | } | 352 | } |
| 350 | 353 | ||
| @@ -361,22 +364,23 @@ class MyQueryReasoner extends QueryReasoner { | |||
| 361 | } | 364 | } |
| 362 | 365 | ||
| 363 | private boolean querySkolemisedRelevantSubset(OWLOntology relevantSubset, QueryRecord queryRecord) { | 366 | private boolean querySkolemisedRelevantSubset(OWLOntology relevantSubset, QueryRecord queryRecord) { |
| 367 | Utility.logInfo("Evaluating semi-Skolemised relevant upper store..."); | ||
| 368 | |||
| 364 | DatalogProgram relevantProgram = new DatalogProgram(relevantSubset, false); // toClassify is false | 369 | DatalogProgram relevantProgram = new DatalogProgram(relevantSubset, false); // toClassify is false |
| 365 | 370 | ||
| 366 | MultiStageQueryEngine relevantStore = | 371 | MultiStageQueryEngine relevantStore = |
| 367 | new MultiStageQueryEngine("Relevant-store", true); // checkValidity is true | 372 | new MultiStageQueryEngine("Relevant-store", true); // checkValidity is true |
| 368 | // relevantStore.importRDFData("data", relevantProgram.getAdditionalDataFile()); // tried, doesn't work | 373 | |
| 369 | relevantStore.importDataFromABoxOf(relevantSubset); | 374 | relevantStore.importDataFromABoxOf(relevantSubset); |
| 370 | 375 | ||
| 371 | int materialisationResult = relevantStore.materialiseSkolemly(relevantProgram, null); | 376 | int materialisationResult = relevantStore.materialiseSkolemly(relevantProgram, null); |
| 372 | // int materialisationResult = relevantStore.materialiseRestrictedly(relevantProgram, null); // DOESN'T WORK!!! | ||
| 373 | if(materialisationResult != 1) | 377 | if(materialisationResult != 1) |
| 374 | throw new RuntimeException("Skolemised materialisation error"); // TODO check consistency | 378 | throw new RuntimeException("Skolemised materialisation error"); // TODO check consistency |
| 375 | // relevantStore.materialiseRestrictedly(relevantProgram, null); // it has been tried | ||
| 376 | 379 | ||
| 377 | return queryUpperStore(relevantStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND); | 380 | boolean isFullyProcessed = checkGapAnswers(relevantStore, queryRecord); |
| 378 | 381 | ||
| 379 | // return queryUpperStore(limitedSkolemUpperStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND); | 382 | Utility.logInfo("Semi-Skolemised relevant upper store has been evaluated"); |
| 383 | return isFullyProcessed; | ||
| 380 | } | 384 | } |
| 381 | 385 | ||
| 382 | enum SatisfiabilityStatus {SATISFIABLE, UNSATISFIABLE, UNCHECKED} | 386 | enum SatisfiabilityStatus {SATISFIABLE, UNSATISFIABLE, UNCHECKED} |
