diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java | 422 |
1 files changed, 0 insertions, 422 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java deleted file mode 100644 index 034827e..0000000 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java +++ /dev/null | |||
| @@ -1,422 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.DLClause; | ||
| 4 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 5 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 6 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 7 | import uk.ac.ox.cs.JRDFox.store.Parameters; | ||
| 8 | import uk.ac.ox.cs.JRDFox.store.TripleStatus; | ||
| 9 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 10 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 11 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 12 | import uk.ac.ox.cs.pagoda.query.GapByStore4ID; | ||
| 13 | import uk.ac.ox.cs.pagoda.rules.DatalogProgram; | ||
| 14 | import uk.ac.ox.cs.pagoda.rules.Program; | ||
| 15 | import uk.ac.ox.cs.pagoda.util.*; | ||
| 16 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 17 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 18 | |||
| 19 | import java.util.*; | ||
| 20 | |||
| 21 | public class BasicQueryEngine extends RDFoxQueryEngine { | ||
| 22 | |||
| 23 | protected DataStore store; | ||
| 24 | protected Parameters parameters = new Parameters(); | ||
| 25 | Set<DLClause> materialisedRules = new HashSet<DLClause>(); | ||
| 26 | private UFS<String> equalityGroups = null; | ||
| 27 | |||
| 28 | public BasicQueryEngine(String name) { | ||
| 29 | super(name); | ||
| 30 | store = RDFoxQueryEngine.createDataStore(); | ||
| 31 | parameters.m_allAnswersInRoot = true; | ||
| 32 | parameters.m_useBushy = true; | ||
| 33 | } | ||
| 34 | |||
| 35 | /*** | ||
| 36 | * @return Overall number of triples. | ||
| 37 | */ | ||
| 38 | public long getStoreSize() throws JRDFStoreException { | ||
| 39 | return store.getTriplesCount(); | ||
| 40 | } | ||
| 41 | |||
| 42 | public void materialiseFoldedly(DatalogProgram dProgram, GapByStore4ID gap) { | ||
| 43 | if(isDisposed()) throw new DisposedException(); | ||
| 44 | if(gap != null) { | ||
| 45 | materialise("lower program", dProgram.getLower().toString()); | ||
| 46 | String program = dProgram.getUpper().toString(); | ||
| 47 | try { | ||
| 48 | gap.compile(program); | ||
| 49 | gap.addBackTo(); | ||
| 50 | getDataStore().clearRulesAndMakeFactsExplicit(); | ||
| 51 | } catch(JRDFStoreException e) { | ||
| 52 | e.printStackTrace(); | ||
| 53 | } finally { | ||
| 54 | gap.clear(); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | else | ||
| 58 | materialise("upper program", dProgram.getUpper().toString()); | ||
| 59 | } | ||
| 60 | |||
| 61 | public int materialiseRestrictedly(DatalogProgram dProgram, GapByStore4ID gap) { | ||
| 62 | if(isDisposed()) throw new DisposedException(); | ||
| 63 | if(gap != null) { | ||
| 64 | materialise("lower program", dProgram.getLower().toString()); | ||
| 65 | String program = dProgram.getUpper().toString(); | ||
| 66 | try { | ||
| 67 | gap.compile(program); | ||
| 68 | gap.addBackTo(); | ||
| 69 | getDataStore().clearRulesAndMakeFactsExplicit(); | ||
| 70 | } catch(JRDFStoreException e) { | ||
| 71 | e.printStackTrace(); | ||
| 72 | } finally { | ||
| 73 | gap.clear(); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | else | ||
| 77 | materialise("upper program", dProgram.getUpper().toString()); | ||
| 78 | |||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | @Override | ||
| 83 | public AnswerTuples evaluate(String queryText) { | ||
| 84 | if(isDisposed()) throw new DisposedException(); | ||
| 85 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0]); | ||
| 86 | } | ||
| 87 | |||
| 88 | @Override | ||
| 89 | public AnswerTuples evaluate(String queryText, String[] answerVars) { | ||
| 90 | if(isDisposed()) throw new DisposedException(); | ||
| 91 | TupleIterator tupleIterator; | ||
| 92 | try { | ||
| 93 | tupleIterator = store.compileQuery(queryText.replace("_:", "?"), prefixes, parameters); | ||
| 94 | } catch(JRDFStoreException e) { | ||
| 95 | e.printStackTrace(); | ||
| 96 | return null; | ||
| 97 | } | ||
| 98 | return new RDFoxAnswerTuples(answerVars, tupleIterator); | ||
| 99 | } | ||
| 100 | |||
| 101 | @Override | ||
| 102 | public DataStore getDataStore() { | ||
| 103 | if(isDisposed()) throw new DisposedException(); | ||
| 104 | return store; | ||
| 105 | } | ||
| 106 | |||
| 107 | @Override | ||
| 108 | public void dispose() { | ||
| 109 | super.dispose(); | ||
| 110 | store.dispose(); | ||
| 111 | } | ||
| 112 | |||
| 113 | public void outputInstance4BinaryPredicate(String iri, String filename) { | ||
| 114 | if(isDisposed()) throw new DisposedException(); | ||
| 115 | |||
| 116 | Utility.redirectCurrentOut(filename); | ||
| 117 | outputInstance4BinaryPredicate(iri); | ||
| 118 | Utility.closeCurrentOut(); | ||
| 119 | } | ||
| 120 | |||
| 121 | public void outputInstance4BinaryPredicate(String iri) { | ||
| 122 | if(isDisposed()) throw new DisposedException(); | ||
| 123 | |||
| 124 | outputAnswers("select ?x ?y where { ?x <" + iri + "> ?y . }"); | ||
| 125 | } | ||
| 126 | |||
| 127 | public void outputInstanceNumbers(String filename) { | ||
| 128 | if(isDisposed()) throw new DisposedException(); | ||
| 129 | |||
| 130 | TupleIterator predicateTuples = null; | ||
| 131 | TupleIterator instanceTuples; | ||
| 132 | Set<String> number = new HashSet<String>(); | ||
| 133 | String predicate; | ||
| 134 | try { | ||
| 135 | predicateTuples = | ||
| 136 | getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Y }", prefixes, parameters); | ||
| 137 | for(long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { | ||
| 138 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); | ||
| 139 | instanceTuples = null; | ||
| 140 | try { | ||
| 141 | instanceTuples = | ||
| 142 | getDataStore().compileQuery("SELECT ?X WHERE { ?X <" + Namespace.RDF_TYPE + "> " + predicate + " }", prefixes, parameters); | ||
| 143 | long totalCount = 0; | ||
| 144 | for(long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) { | ||
| 145 | totalCount += instanceTuples.getMultiplicity(); | ||
| 146 | } | ||
| 147 | number.add(predicate + " * " + totalCount); | ||
| 148 | } finally { | ||
| 149 | if(instanceTuples != null) instanceTuples.dispose(); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } catch(JRDFStoreException e) { | ||
| 153 | e.printStackTrace(); | ||
| 154 | } finally { | ||
| 155 | if(predicateTuples != null) predicateTuples.dispose(); | ||
| 156 | predicateTuples = null; | ||
| 157 | } | ||
| 158 | |||
| 159 | try { | ||
| 160 | predicateTuples = | ||
| 161 | getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X ?Y ?Z }", prefixes, parameters); | ||
| 162 | for(long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { | ||
| 163 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); | ||
| 164 | instanceTuples = null; | ||
| 165 | try { | ||
| 166 | instanceTuples = | ||
| 167 | getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X " + predicate + " ?Z }", prefixes, parameters); | ||
| 168 | long totalCount = 0; | ||
| 169 | for(long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) | ||
| 170 | totalCount += instanceTuples.getMultiplicity(); | ||
| 171 | number.add(predicate + " * " + totalCount); | ||
| 172 | } finally { | ||
| 173 | if(instanceTuples != null) instanceTuples.dispose(); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | } catch(JRDFStoreException e) { | ||
| 178 | e.printStackTrace(); | ||
| 179 | } finally { | ||
| 180 | if(predicateTuples != null) predicateTuples.dispose(); | ||
| 181 | predicateTuples = null; | ||
| 182 | } | ||
| 183 | |||
| 184 | Utility.redirectCurrentOut(filename); | ||
| 185 | String[] ordered = number.toArray(new String[0]); | ||
| 186 | Arrays.sort(ordered, new DLPredicateComparator()); | ||
| 187 | for(String line : ordered) System.out.println(line); | ||
| 188 | Utility.closeCurrentOut(); | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | public TupleIterator internal_evaluateAgainstIDBs(String queryText) throws JRDFStoreException { | ||
| 193 | if(isDisposed()) throw new DisposedException(); | ||
| 194 | |||
| 195 | TupleIterator iter = | ||
| 196 | store.compileQuery(queryText, prefixes, parameters, TripleStatus.TUPLE_STATUS_IDB.union(TripleStatus.TUPLE_STATUS_EDB), TripleStatus.TUPLE_STATUS_IDB); | ||
| 197 | // iter.open(); | ||
| 198 | return iter; | ||
| 199 | } | ||
| 200 | |||
| 201 | public TupleIterator internal_evaluate(String queryText) throws JRDFStoreException { | ||
| 202 | if(isDisposed()) throw new DisposedException(); | ||
| 203 | |||
| 204 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | ||
| 205 | // iter.open(); | ||
| 206 | return iter; | ||
| 207 | } | ||
| 208 | |||
| 209 | public void setExpandEquality(boolean flag) { | ||
| 210 | if(isDisposed()) throw new DisposedException(); | ||
| 211 | |||
| 212 | parameters.m_expandEquality = flag; | ||
| 213 | } | ||
| 214 | |||
| 215 | public TupleIterator internal_evaluateNotExpanded(String queryText) throws JRDFStoreException { | ||
| 216 | if(isDisposed()) throw new DisposedException(); | ||
| 217 | |||
| 218 | parameters.m_expandEquality = false; | ||
| 219 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | ||
| 220 | // iter.open(); | ||
| 221 | parameters.m_expandEquality = true; | ||
| 222 | return iter; | ||
| 223 | } | ||
| 224 | |||
| 225 | public TupleIterator internal_evaluate(String queryText, boolean incrementally) throws JRDFStoreException { | ||
| 226 | if(isDisposed()) throw new DisposedException(); | ||
| 227 | |||
| 228 | return incrementally ? internal_evaluateAgainstIDBs(queryText) : internal_evaluate(queryText); | ||
| 229 | } | ||
| 230 | |||
| 231 | public String getUnusedRules(Collection<DLClause> clauses, boolean toUpdate) { | ||
| 232 | if(isDisposed()) throw new DisposedException(); | ||
| 233 | |||
| 234 | DLClause clause; | ||
| 235 | for(Iterator<DLClause> iter = clauses.iterator(); iter.hasNext(); ) { | ||
| 236 | if(materialisedRules.contains(clause = iter.next())) | ||
| 237 | iter.remove(); | ||
| 238 | else if(toUpdate) materialisedRules.add(clause); | ||
| 239 | } | ||
| 240 | |||
| 241 | if(clauses.isEmpty()) return null; | ||
| 242 | |||
| 243 | return Program.toString(clauses); | ||
| 244 | } | ||
| 245 | |||
| 246 | public void outputMaterialisedRules() { | ||
| 247 | if(isDisposed()) throw new DisposedException(); | ||
| 248 | |||
| 249 | System.out.println(DLClauseHelper.toString(materialisedRules)); | ||
| 250 | } | ||
| 251 | |||
| 252 | public void outputAnswers(String query) { | ||
| 253 | if(isDisposed()) throw new DisposedException(); | ||
| 254 | |||
| 255 | TupleIterator iter = null; | ||
| 256 | try { | ||
| 257 | iter = internal_evaluate(query); | ||
| 258 | System.out.println(query); | ||
| 259 | int arity = iter.getArity(); | ||
| 260 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { | ||
| 261 | for(int i = 0; i < arity; ++i) | ||
| 262 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); | ||
| 263 | System.out.println(); | ||
| 264 | } | ||
| 265 | } catch(JRDFStoreException e) { | ||
| 266 | e.printStackTrace(); | ||
| 267 | } finally { | ||
| 268 | if(iter != null) iter.dispose(); | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | public void outputInstance4UnaryPredicate(String iri) { | ||
| 273 | if(isDisposed()) throw new DisposedException(); | ||
| 274 | |||
| 275 | outputAnswers("select ?x where { ?x " | ||
| 276 | + "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" | ||
| 277 | + iri | ||
| 278 | + "> .}"); | ||
| 279 | } | ||
| 280 | |||
| 281 | public void outputSubjects(String p, String o) { | ||
| 282 | if(isDisposed()) throw new DisposedException(); | ||
| 283 | |||
| 284 | outputAnswers("select x where { ?x <" + p + "> <" + o + "> . }"); | ||
| 285 | } | ||
| 286 | |||
| 287 | public void outputObjects(String s, String p) { | ||
| 288 | if(isDisposed()) throw new DisposedException(); | ||
| 289 | |||
| 290 | outputAnswers("select ?x where { <" + s + "> <" + p + "> ?x . }"); | ||
| 291 | } | ||
| 292 | |||
| 293 | public void outputIDBFacts() { | ||
| 294 | if(isDisposed()) throw new DisposedException(); | ||
| 295 | |||
| 296 | TupleIterator iter = null; | ||
| 297 | try { | ||
| 298 | iter = internal_evaluateAgainstIDBs("select distict ?x ?y ?z where { ?x ?y ?z }"); | ||
| 299 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { | ||
| 300 | for(int i = 0; i < 3; ++i) | ||
| 301 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); | ||
| 302 | System.out.println(); | ||
| 303 | } | ||
| 304 | } catch(JRDFStoreException e) { | ||
| 305 | // TODO Auto-generated catch block | ||
| 306 | e.printStackTrace(); | ||
| 307 | } finally { | ||
| 308 | if(iter != null) iter.dispose(); | ||
| 309 | } | ||
| 310 | |||
| 311 | } | ||
| 312 | |||
| 313 | public void outputType4Individual(String iri) { | ||
| 314 | if(isDisposed()) throw new DisposedException(); | ||
| 315 | |||
| 316 | outputAnswers("select ?z where { <" + iri + "> " + Namespace.RDF_TYPE_QUOTED + " ?z }"); | ||
| 317 | } | ||
| 318 | |||
| 319 | public int getSameAsNumber() { | ||
| 320 | if(isDisposed()) throw new DisposedException(); | ||
| 321 | |||
| 322 | TupleIterator iter = null; | ||
| 323 | int counter = 0; | ||
| 324 | try { | ||
| 325 | iter = internal_evaluate("select ?x ?y where {?x " + Namespace.EQUALITY_QUOTED + " ?y . }"); | ||
| 326 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) | ||
| 327 | if(iter.getResourceID(0) != iter.getResourceID(1)) | ||
| 328 | ++counter; | ||
| 329 | } catch(JRDFStoreException e) { | ||
| 330 | e.printStackTrace(); | ||
| 331 | } finally { | ||
| 332 | if(iter != null) iter.dispose(); | ||
| 333 | } | ||
| 334 | return counter; | ||
| 335 | } | ||
| 336 | |||
| 337 | public UFS<String> getEqualityGroups(boolean reuse) { | ||
| 338 | if(isDisposed()) throw new DisposedException(); | ||
| 339 | |||
| 340 | if(reuse && equalityGroups != null) return equalityGroups; | ||
| 341 | |||
| 342 | equalityGroups = new UFS<String>(); | ||
| 343 | |||
| 344 | TupleIterator answers = null; | ||
| 345 | try { | ||
| 346 | Timer t = new Timer(); | ||
| 347 | answers = internal_evaluate("select ?x ?z where {?x " + Namespace.EQUALITY_QUOTED + "?z . }"); | ||
| 348 | for(long multi = answers.open(); multi != 0; multi = answers.getNext()) { | ||
| 349 | if(answers.getResourceID(0) != answers.getResourceID(1)) | ||
| 350 | equalityGroups.merge(answers.getResource(0).m_lexicalForm, answers.getResource(1).m_lexicalForm); | ||
| 351 | } | ||
| 352 | Utility.logInfo("@Time to group individuals by equality: " + t.duration()); | ||
| 353 | } catch(JRDFStoreException e) { | ||
| 354 | e.printStackTrace(); | ||
| 355 | } finally { | ||
| 356 | if(answers != null) answers.dispose(); | ||
| 357 | } | ||
| 358 | |||
| 359 | return equalityGroups; | ||
| 360 | } | ||
| 361 | |||
| 362 | public void clearRulesAndIDBFacts(Collection<int[]> collection) { | ||
| 363 | if(isDisposed()) throw new DisposedException(); | ||
| 364 | |||
| 365 | // performDeletion(collection); | ||
| 366 | collection.clear(); | ||
| 367 | try { | ||
| 368 | store.clearRulesAndMakeFactsExplicit(); | ||
| 369 | } catch(JRDFStoreException e) { | ||
| 370 | e.printStackTrace(); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | protected void outputClassAssertions(String filename) { | ||
| 375 | TupleIterator allTuples = null; | ||
| 376 | boolean redirect = false; | ||
| 377 | try { | ||
| 378 | allTuples = | ||
| 379 | getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Z }", prefixes, parameters); | ||
| 380 | redirect = Utility.redirectCurrentOut(filename); | ||
| 381 | for(long multi = allTuples.open(); multi != 0; multi = allTuples.getNext()) | ||
| 382 | System.out.println(RDFoxTripleManager.getQuotedTerm(allTuples.getResource(0)) + " " + RDFoxTripleManager | ||
| 383 | .getQuotedTerm(allTuples.getResource(1))); | ||
| 384 | } catch(JRDFStoreException e) { | ||
| 385 | e.printStackTrace(); | ||
| 386 | } finally { | ||
| 387 | if(redirect) Utility.closeCurrentOut(); | ||
| 388 | if(allTuples != null) allTuples.dispose(); | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | @SuppressWarnings("unused") | ||
| 393 | private void performDeletion(Collection<int[]> collection) { | ||
| 394 | Utility.logInfo("Remove all rules, idb facts and added staff..."); | ||
| 395 | Timer timer = new Timer(); | ||
| 396 | TupleIterator iter = null; | ||
| 397 | try { | ||
| 398 | UpdateType ut = UpdateType.ScheduleForDeletion; | ||
| 399 | for(int[] t : collection) | ||
| 400 | store.addTriplesByResourceIDs(t, ut); | ||
| 401 | |||
| 402 | try { | ||
| 403 | iter = internal_evaluateAgainstIDBs("select ?x ?y ?z where { ?x ?y ?z . }"); | ||
| 404 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { | ||
| 405 | int[] triple = new int[3]; | ||
| 406 | for(int i = 0; i < 3; ++i) | ||
| 407 | triple[i] = iter.getResourceID(i); | ||
| 408 | store.addTriplesByResourceIDs(triple, ut); | ||
| 409 | } | ||
| 410 | } finally { | ||
| 411 | if(iter != null) iter.dispose(); | ||
| 412 | iter = null; | ||
| 413 | } | ||
| 414 | store.applyReasoning(true); | ||
| 415 | } catch(JRDFStoreException e) { | ||
| 416 | e.printStackTrace(); | ||
| 417 | } | ||
| 418 | Utility.logInfo("Time for deletion: " + timer.duration()); | ||
| 419 | } | ||
| 420 | |||
| 421 | |||
| 422 | } | ||
