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