diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java new file mode 100644 index 0000000..f8e1709 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 10 | import uk.ac.ox.cs.pagoda.util.UFS; | ||
| 11 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 12 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 13 | |||
| 14 | public class GapByStore4ID2 extends GapByStore4ID { | ||
| 15 | |||
| 16 | private BasicQueryEngine m_baseEngine; | ||
| 17 | private UFS<String> m_equality = null, m_baseEquality = null; | ||
| 18 | |||
| 19 | public GapByStore4ID2(BasicQueryEngine engine, BasicQueryEngine baseEngine) { | ||
| 20 | super(engine); | ||
| 21 | m_baseEngine = baseEngine; | ||
| 22 | } | ||
| 23 | |||
| 24 | @Override | ||
| 25 | public boolean hasNext() { | ||
| 26 | if (getNewGapTuple(iterator, -1)) return true; | ||
| 27 | if (iterator != null) { | ||
| 28 | iterator.dispose(); | ||
| 29 | iterator = null; | ||
| 30 | } | ||
| 31 | return getNextGapFactAboutEquality(); | ||
| 32 | } | ||
| 33 | |||
| 34 | private boolean getNewGapTuple(TupleIterator it, int firstElement) { | ||
| 35 | if (it == null) return false; | ||
| 36 | int firstIndex = 0; | ||
| 37 | tuple = new int[3]; | ||
| 38 | if (firstElement > 0) { | ||
| 39 | tuple[0] = firstElement; | ||
| 40 | firstIndex = 1; | ||
| 41 | } | ||
| 42 | Integer predicate; | ||
| 43 | try { | ||
| 44 | for (; multi != 0; multi = it.getNext()) { | ||
| 45 | for (int i = firstIndex; i < 3; ++i) | ||
| 46 | tuple[i] = (int) it.getResourceID(i - firstIndex); | ||
| 47 | |||
| 48 | if (isRDF_TYPE()) { | ||
| 49 | predicate = getGapPredicateID(tuple[2]); | ||
| 50 | if (predicate == null) continue; | ||
| 51 | tuple[2] = predicate; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | predicate = getGapPredicateID(tuple[1]); | ||
| 55 | if (predicate == null) continue; | ||
| 56 | tuple[1] = predicate; | ||
| 57 | } | ||
| 58 | return true; | ||
| 59 | } | ||
| 60 | } catch (JRDFStoreException e) { | ||
| 61 | e.printStackTrace(); | ||
| 62 | return false; | ||
| 63 | } | ||
| 64 | return false; | ||
| 65 | } | ||
| 66 | |||
| 67 | private LinkedList<String> toAddedIndividuals = null; | ||
| 68 | private TupleIterator iter_individual = null; | ||
| 69 | private int currentID = -1; | ||
| 70 | |||
| 71 | private boolean getNextGapFactAboutEquality() { | ||
| 72 | if (toAddedIndividuals == null) { | ||
| 73 | m_equality = m_engine.getEqualityGroups(false); | ||
| 74 | m_baseEquality = m_baseEngine.getEqualityGroups(false); | ||
| 75 | toAddedIndividuals = new LinkedList<String>(); | ||
| 76 | Map<String, Integer> rep2cnt = new HashMap<String, Integer>(); | ||
| 77 | Map<String, Integer> rep2cnt_base = new HashMap<String, Integer>(); | ||
| 78 | count(m_engine, m_equality, rep2cnt); | ||
| 79 | count(m_baseEngine, m_baseEquality, rep2cnt_base); | ||
| 80 | Set<String> visitedrep = new HashSet<String>(); | ||
| 81 | for (String individual : m_equality.keySet()) { | ||
| 82 | String rep = m_equality.find(individual); | ||
| 83 | if (visitedrep.contains(rep)) continue; | ||
| 84 | visitedrep.add(rep); | ||
| 85 | String rep_base = m_baseEquality.find(individual); | ||
| 86 | if (!rep2cnt.get(rep).equals(rep2cnt_base.get(rep_base))) { | ||
| 87 | toAddedIndividuals.add(rep); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | } | ||
| 92 | while (true) { | ||
| 93 | if (getNewGapTuple(iter_individual, currentID)) return true; | ||
| 94 | if (iter_individual != null) { | ||
| 95 | iter_individual.dispose(); | ||
| 96 | iter_individual = null; | ||
| 97 | } | ||
| 98 | if (toAddedIndividuals.isEmpty()) { | ||
| 99 | currentID = -1; | ||
| 100 | return false; | ||
| 101 | } | ||
| 102 | String individual = toAddedIndividuals.remove(); | ||
| 103 | currentID = tripleManager.getResourceID(individual); | ||
| 104 | try { | ||
| 105 | iter_individual = m_engine.internal_evaluateNotExpanded(String.format("select distinct ?y ?z where { <%s> ?y ?z }", individual)); | ||
| 106 | multi = iter_individual.open(); | ||
| 107 | } catch (JRDFStoreException e) { | ||
| 108 | e.printStackTrace(); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | private void count(BasicQueryEngine engine, UFS<String> equality, Map<String, Integer> map) { | ||
| 114 | for (String ind : equality.keySet()) { | ||
| 115 | Integer exist = map.get(ind); | ||
| 116 | if (exist == null) | ||
| 117 | map.put(equality.find(ind), 1); | ||
| 118 | else | ||
| 119 | map.put(equality.find(ind), ++exist); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | @Override | ||
| 124 | public int[] next() { | ||
| 125 | try { | ||
| 126 | if (iterator != null) | ||
| 127 | multi = iterator.getNext(); | ||
| 128 | else if (iter_individual != null) | ||
| 129 | multi = iter_individual.getNext(); | ||
| 130 | else | ||
| 131 | multi = 0; | ||
| 132 | } catch (JRDFStoreException e) { | ||
| 133 | e.printStackTrace(); | ||
| 134 | } | ||
| 135 | return tuple; | ||
| 136 | } | ||
| 137 | |||
| 138 | public void clear() { | ||
| 139 | super.clear(); | ||
| 140 | if (iter_individual != null) { | ||
| 141 | iter_individual.dispose(); | ||
| 142 | iter_individual = null; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | } | ||
