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