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/query/GapByStore4ID.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java new file mode 100644 index 0000000..1c0eb48 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.LinkedList; | ||
| 5 | import java.util.Map; | ||
| 6 | |||
| 7 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 8 | //import uk.ac.ox.cs.pagoda.multistage.AnswerTupleID; | ||
| 9 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 10 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 11 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 12 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 13 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 14 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 15 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 16 | |||
| 17 | //public class GapByStore4ID extends GapTupleIterator<AnswerTupleID> { | ||
| 18 | public class GapByStore4ID extends GapTupleIterator<int[]> { | ||
| 19 | |||
| 20 | private MyPrefixes prefixes = MyPrefixes.PAGOdAPrefixes; | ||
| 21 | private TupleIterator iterator = null; | ||
| 22 | |||
| 23 | // AnswerTupleID tuple; | ||
| 24 | int[] tuple; | ||
| 25 | private BasicQueryEngine m_engine; | ||
| 26 | private DataStore m_store; | ||
| 27 | private RDFoxTripleManager tripleManager; | ||
| 28 | |||
| 29 | public GapByStore4ID(BasicQueryEngine engine) { | ||
| 30 | m_engine = engine; | ||
| 31 | m_store = engine.getDataStore(); | ||
| 32 | tripleManager = new RDFoxTripleManager(m_store, false); | ||
| 33 | } | ||
| 34 | |||
| 35 | long multi; | ||
| 36 | |||
| 37 | @Override | ||
| 38 | public void compile(String program) throws JRDFStoreException { | ||
| 39 | clear(); | ||
| 40 | |||
| 41 | boolean incrementally = true; | ||
| 42 | Timer t = new Timer(); | ||
| 43 | long oldTripleCount = m_store.getTriplesCount(); | ||
| 44 | |||
| 45 | if (program != null) { | ||
| 46 | // m_store.addRules(new String[] {program}); | ||
| 47 | m_store.importRules(program); | ||
| 48 | incrementally = false; | ||
| 49 | } | ||
| 50 | |||
| 51 | m_store.applyReasoning(incrementally); | ||
| 52 | |||
| 53 | long tripleCount = m_store.getTriplesCount(); | ||
| 54 | |||
| 55 | Utility.logDebug("current store after materialising upper related rules: " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)", | ||
| 56 | "current store finished the materialisation of upper related rules in " + t.duration() + " seconds."); | ||
| 57 | |||
| 58 | m_engine.setExpandEquality(false); | ||
| 59 | iterator = m_engine.internal_evaluateAgainstIDBs("select ?x ?y ?z where { ?x ?y ?z . }"); | ||
| 60 | m_engine.setExpandEquality(true); | ||
| 61 | |||
| 62 | multi = iterator.open(); | ||
| 63 | Utility.logDebug("gap query evaluted ..."); | ||
| 64 | } | ||
| 65 | |||
| 66 | @Override | ||
| 67 | public boolean hasNext() { | ||
| 68 | if (iterator == null) return false; | ||
| 69 | try { | ||
| 70 | // tuple = new AnswerTupleID(3); | ||
| 71 | tuple = new int[3]; | ||
| 72 | Integer predicate; | ||
| 73 | for (; multi != 0; multi = iterator.getNext()) { | ||
| 74 | for (int i = 0; i < 3; ++i) | ||
| 75 | // tuple.setTerm(i, (int) iterator.getResourceID(i)); | ||
| 76 | tuple[i] = (int) iterator.getResourceID(i); | ||
| 77 | |||
| 78 | if (isRDF_TYPE()) { | ||
| 79 | // predicate = getGapPredicateID(tuple.getTerm(2)); | ||
| 80 | predicate = getGapPredicateID(tuple[2]); | ||
| 81 | if (predicate == null) continue; | ||
| 82 | // tuple.setTerm(2, predicate); | ||
| 83 | tuple[2] = predicate; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | // predicate = getGapPredicateID(tuple.getTerm(1)); | ||
| 87 | predicate = getGapPredicateID(tuple[1]); | ||
| 88 | if (predicate == null) continue; | ||
| 89 | // tuple.setTerm(1, predicate); | ||
| 90 | tuple[1] = predicate; | ||
| 91 | } | ||
| 92 | return true; | ||
| 93 | } | ||
| 94 | } catch (JRDFStoreException e) { | ||
| 95 | e.printStackTrace(); | ||
| 96 | return false; | ||
| 97 | } | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | @Override | ||
| 102 | // public AnswerTupleID next() { | ||
| 103 | public int[] next() { | ||
| 104 | try { | ||
| 105 | multi = iterator.getNext(); | ||
| 106 | } catch (JRDFStoreException e) { | ||
| 107 | e.printStackTrace(); | ||
| 108 | } | ||
| 109 | |||
| 110 | return tuple; | ||
| 111 | } | ||
| 112 | |||
| 113 | Map<Integer, Integer> original2gap = new HashMap<Integer, Integer>(); | ||
| 114 | LinkedList<String> predicatesWithGap = new LinkedList<String>(); | ||
| 115 | |||
| 116 | public LinkedList<String> getPredicatesWithGap() { | ||
| 117 | return predicatesWithGap; | ||
| 118 | } | ||
| 119 | |||
| 120 | private Integer getGapPredicateID(int originalID) { | ||
| 121 | Integer gapID; | ||
| 122 | if ((gapID = original2gap.get(originalID)) != null) | ||
| 123 | return gapID; | ||
| 124 | |||
| 125 | String originalPredicate = tripleManager.getRawTerm(originalID); | ||
| 126 | if (isAuxPredicate(originalPredicate)) { | ||
| 127 | // Utility.LOGS.info(originalPredicate); | ||
| 128 | return null; | ||
| 129 | } | ||
| 130 | |||
| 131 | predicatesWithGap.add(originalPredicate); | ||
| 132 | String gapPredicate = prefixes.expandIRI(getGapPredicate(originalPredicate)); | ||
| 133 | gapID = tripleManager.getResourceID(gapPredicate); | ||
| 134 | original2gap.put(originalID, gapID); | ||
| 135 | |||
| 136 | return gapID; | ||
| 137 | } | ||
| 138 | |||
| 139 | private boolean isAuxPredicate(String originalPredicate) { | ||
| 140 | return originalPredicate.contains("_AUX"); | ||
| 141 | } | ||
| 142 | |||
| 143 | private boolean isRDF_TYPE() { | ||
| 144 | // return tripleManager.isRdfTypeID(tuple.getTerm(1)); | ||
| 145 | return tripleManager.isRdfTypeID(tuple[1]); | ||
| 146 | } | ||
| 147 | |||
| 148 | @Override | ||
| 149 | public void remove() { | ||
| 150 | Utility.logError("Unsupported operation!"); | ||
| 151 | } | ||
| 152 | |||
| 153 | private boolean valid = false; | ||
| 154 | |||
| 155 | @Override | ||
| 156 | public void save(String file) { | ||
| 157 | Utility.logError("Unsupported Operation..."); | ||
| 158 | } | ||
| 159 | |||
| 160 | @Override | ||
| 161 | public void addBackTo() throws JRDFStoreException { | ||
| 162 | int tupleCounter = 0; | ||
| 163 | Timer t = new Timer(); | ||
| 164 | long oldTripleCounter; | ||
| 165 | Utility.logDebug("current store before importing gap tuples: " + (oldTripleCounter = m_store.getTriplesCount())); | ||
| 166 | while (hasNext()) { | ||
| 167 | next(); | ||
| 168 | ++tupleCounter; | ||
| 169 | tripleManager.addTripleByID(tuple); | ||
| 170 | } | ||
| 171 | valid = true; | ||
| 172 | |||
| 173 | long tripleCounter = m_store.getTriplesCount(); | ||
| 174 | Utility.logDebug("There are " + tupleCounter + " tuples in the gap between lower and upper bound materialisation.", | ||
| 175 | "current store after importing gap tuples: " + tripleCounter + " (" + (tripleCounter - oldTripleCounter) + ").", | ||
| 176 | "current store finished importing gap tuples: " + tripleCounter + " in " + t.duration() + "."); | ||
| 177 | } | ||
| 178 | |||
| 179 | public void clear() { | ||
| 180 | if (iterator != null) { | ||
| 181 | iterator.dispose(); | ||
| 182 | iterator = null; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | public boolean isValid() {return valid; } | ||
| 187 | |||
| 188 | @Override | ||
| 189 | public void addTo(DataStore store) throws JRDFStoreException { | ||
| 190 | Utility.logError("Unsupported Operation..."); | ||
| 191 | } | ||
| 192 | } | ||
