diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-28 17:24:00 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-28 17:24:00 +0100 |
| commit | 1055e67727b1aca80ae7ffaceabce3aacb00b6d2 (patch) | |
| tree | 71632dfc0fdf596d0286a4912245cacedfd2b534 /src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java | |
| parent | de3749532d060f26c966a81c03f9a5d846c33d06 (diff) | |
| parent | 4f98cb7df7f2921808d825cdcd82f95a0899640e (diff) | |
| download | ACQuA-1055e67727b1aca80ae7ffaceabce3aacb00b6d2.tar.gz ACQuA-1055e67727b1aca80ae7ffaceabce3aacb00b6d2.zip | |
Merge branch 'upstream' into Query-dependent-skolemisation
Conflicts:
src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java
src/uk/ac/ox/cs/pagoda/endomorph/plan/OpenEndMultiThreadPlan.java
src/uk/ac/ox/cs/pagoda/endomorph/plan/OpenEndPlan.java
src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java
src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java
src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java
src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithGap.java
src/uk/ac/ox/cs/pagoda/util/Utility.java
test/uk/ac/ox/cs/pagoda/junit/ClauseTester.java
test/uk/ac/ox/cs/pagoda/junit/TestGapMappedToLower.java
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java | 265 |
1 files changed, 135 insertions, 130 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java index 19efc58..f8e1709 100644 --- a/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java +++ b/src/uk/ac/ox/cs/pagoda/query/GapByStore4ID2.java | |||
| @@ -1,141 +1,146 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | 1 | package uk.ac.ox.cs.pagoda.query; |
| 2 | 2 | ||
| 3 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | 3 | import java.util.HashMap; |
| 4 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | 4 | import java.util.HashSet; |
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 5 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | 9 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; |
| 6 | import uk.ac.ox.cs.pagoda.util.UFS; | 10 | import uk.ac.ox.cs.pagoda.util.UFS; |
| 7 | 11 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | |
| 8 | import java.util.*; | 12 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; |
| 9 | 13 | ||
| 10 | public class GapByStore4ID2 extends GapByStore4ID { | 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 | } | ||
| 11 | 66 | ||
| 12 | private BasicQueryEngine m_baseEngine; | 67 | private LinkedList<String> toAddedIndividuals = null; |
| 13 | private UFS<String> m_equality = null, m_baseEquality = null; | 68 | private TupleIterator iter_individual = null; |
| 14 | private LinkedList<String> toAddedIndividuals = null; | 69 | private int currentID = -1; |
| 15 | private TupleIterator iter_individual = null; | 70 | |
| 16 | private int currentID = -1; | 71 | private boolean getNextGapFactAboutEquality() { |
| 17 | 72 | if (toAddedIndividuals == null) { | |
| 18 | public GapByStore4ID2(BasicQueryEngine engine, BasicQueryEngine baseEngine) { | 73 | m_equality = m_engine.getEqualityGroups(false); |
| 19 | super(engine); | 74 | m_baseEquality = m_baseEngine.getEqualityGroups(false); |
| 20 | m_baseEngine = baseEngine; | 75 | toAddedIndividuals = new LinkedList<String>(); |
| 21 | } | 76 | Map<String, Integer> rep2cnt = new HashMap<String, Integer>(); |
| 22 | 77 | Map<String, Integer> rep2cnt_base = new HashMap<String, Integer>(); | |
| 23 | @Override | 78 | count(m_engine, m_equality, rep2cnt); |
| 24 | public boolean hasNext() { | 79 | count(m_baseEngine, m_baseEquality, rep2cnt_base); |
| 25 | if(getNewGapTuple(iterator, -1)) return true; | 80 | Set<String> visitedrep = new HashSet<String>(); |
| 26 | if(iterator != null) { | 81 | for (String individual : m_equality.keySet()) { |
| 27 | iterator.dispose(); | 82 | String rep = m_equality.find(individual); |
| 28 | iterator = null; | 83 | if (visitedrep.contains(rep)) continue; |
| 29 | } | 84 | visitedrep.add(rep); |
| 30 | return getNextGapFactAboutEquality(); | 85 | String rep_base = m_baseEquality.find(individual); |
| 31 | } | 86 | if (!rep2cnt.get(rep).equals(rep2cnt_base.get(rep_base))) { |
| 32 | 87 | toAddedIndividuals.add(rep); | |
| 33 | private boolean getNewGapTuple(TupleIterator it, int firstElement) { | 88 | } |
| 34 | if(it == null) return false; | 89 | } |
| 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 | 90 | ||
| 118 | @Override | 91 | } |
| 119 | public int[] next() { | 92 | while (true) { |
| 120 | try { | 93 | if (getNewGapTuple(iter_individual, currentID)) return true; |
| 121 | if(iterator != null) | 94 | if (iter_individual != null) { |
| 122 | multi = iterator.getNext(); | 95 | iter_individual.dispose(); |
| 123 | else if(iter_individual != null) | 96 | iter_individual = null; |
| 124 | multi = iter_individual.getNext(); | 97 | } |
| 125 | else | 98 | if (toAddedIndividuals.isEmpty()) { |
| 126 | multi = 0; | 99 | currentID = -1; |
| 127 | } catch(JRDFStoreException e) { | 100 | return false; |
| 128 | e.printStackTrace(); | 101 | } |
| 129 | } | 102 | String individual = toAddedIndividuals.remove(); |
| 130 | return tuple; | 103 | currentID = tripleManager.getResourceID(individual); |
| 131 | } | 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 | } | ||
| 132 | 112 | ||
| 133 | public void clear() { | 113 | private void count(BasicQueryEngine engine, UFS<String> equality, Map<String, Integer> map) { |
| 134 | super.clear(); | 114 | for (String ind : equality.keySet()) { |
| 135 | if(iter_individual != null) { | 115 | Integer exist = map.get(ind); |
| 136 | iter_individual.dispose(); | 116 | if (exist == null) |
| 137 | iter_individual = null; | 117 | map.put(equality.find(ind), 1); |
| 138 | } | 118 | else |
| 139 | } | 119 | map.put(equality.find(ind), ++exist); |
| 120 | } | ||
| 121 | } | ||
| 140 | 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 | |||
| 141 | } | 146 | } |
