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 | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java | 135 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java | 23 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java | 92 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapByStore4ID.java | 192 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapByTriple.java | 171 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/GapTupleIterator.java | 30 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/QueryManager.java | 123 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | 562 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java | 383 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/rollup/VariableConstant.java | 227 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/rollup/VariableIndividual.java | 416 |
11 files changed, 2354 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java new file mode 100644 index 0000000..8d7e0b1 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.Map; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.Constant; | ||
| 7 | import org.semanticweb.HermiT.model.Individual; | ||
| 8 | import org.semanticweb.HermiT.model.Term; | ||
| 9 | import org.semanticweb.HermiT.model.Variable; | ||
| 10 | |||
| 11 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 12 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 13 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 14 | import uk.ac.ox.cs.JRDFox.model.Literal; | ||
| 15 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 16 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 17 | |||
| 18 | public class AnswerTuple { | ||
| 19 | |||
| 20 | public static final String SEPARATOR = "\t"; | ||
| 21 | |||
| 22 | String m_str = null; | ||
| 23 | GroundTerm[] m_tuple; | ||
| 24 | |||
| 25 | public AnswerTuple(TupleIterator iter, int arity) { | ||
| 26 | m_tuple = new GroundTerm[arity]; | ||
| 27 | try { | ||
| 28 | for (int i = 0; i < arity; ++i) | ||
| 29 | m_tuple[i] = iter.getGroundTerm(i); | ||
| 30 | } catch (JRDFStoreException e) { | ||
| 31 | e.printStackTrace(); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | public AnswerTuple(GroundTerm[] terms) { | ||
| 36 | m_tuple = terms; | ||
| 37 | } | ||
| 38 | |||
| 39 | private AnswerTuple(AnswerTuple sup, int arity) { | ||
| 40 | m_tuple = new GroundTerm[arity]; | ||
| 41 | for (int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i]; | ||
| 42 | } | ||
| 43 | |||
| 44 | public int getArity() { | ||
| 45 | return m_tuple.length; | ||
| 46 | } | ||
| 47 | |||
| 48 | public int hashCode() { | ||
| 49 | // return toString().hashCode(); | ||
| 50 | int code = 0; | ||
| 51 | for (int i = 0; i < m_tuple.length; ++i) | ||
| 52 | code = code * 1997 + m_tuple[i].hashCode(); | ||
| 53 | return code; | ||
| 54 | } | ||
| 55 | |||
| 56 | public boolean equals(Object obj) { | ||
| 57 | if (!(obj instanceof AnswerTuple)) return false; | ||
| 58 | AnswerTuple that = (AnswerTuple) obj; | ||
| 59 | if (m_tuple.length != that.m_tuple.length) return false; | ||
| 60 | for (int i = 0; i < m_tuple.length; ++i) | ||
| 61 | if (!m_tuple[i].equals(that.m_tuple[i])) | ||
| 62 | return false; | ||
| 63 | return true; | ||
| 64 | // return toString().equals(obj.toString()); | ||
| 65 | } | ||
| 66 | |||
| 67 | public String toString() { | ||
| 68 | if (m_str != null) return m_str; | ||
| 69 | StringBuilder sb = new StringBuilder(); | ||
| 70 | for (int i = 0; i < m_tuple.length; ++i) { | ||
| 71 | if (sb.length() != 0) sb.append(SEPARATOR); | ||
| 72 | if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual) | ||
| 73 | sb.append("<").append(((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI()).append(">"); | ||
| 74 | else if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.BlankNode) { | ||
| 75 | sb.append(((uk.ac.ox.cs.JRDFox.model.BlankNode) m_tuple[i]).toString()); | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | Literal l = (Literal) m_tuple[i]; | ||
| 79 | sb.append('"').append(l.getLexicalForm()).append("\""); | ||
| 80 | if (!l.getDatatype().equals(Datatype.XSD_STRING) && !l.getDatatype().equals(Datatype.RDF_PLAIN_LITERAL)) | ||
| 81 | sb.append("^^<").append(l.getDatatype().getIRI()).append(">"); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | return m_str = sb.toString(); | ||
| 85 | } | ||
| 86 | |||
| 87 | public GroundTerm getGroundTerm(int i) { | ||
| 88 | return m_tuple[i]; | ||
| 89 | } | ||
| 90 | |||
| 91 | public Map<Variable, Term> getAssignment(String[] vars) { | ||
| 92 | Map<Variable, Term> map = new HashMap<Variable, Term>(); | ||
| 93 | int index = 0; | ||
| 94 | Term t; | ||
| 95 | for (String var: vars) { | ||
| 96 | if (m_tuple[index] instanceof uk.ac.ox.cs.JRDFox.model.Individual) | ||
| 97 | t = Individual.create((((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[index]).getIRI())); | ||
| 98 | else { | ||
| 99 | uk.ac.ox.cs.JRDFox.model.Literal l = (uk.ac.ox.cs.JRDFox.model.Literal) m_tuple[index]; | ||
| 100 | t = Constant.create(l.getLexicalForm(), l.getDatatype().getIRI()); | ||
| 101 | } | ||
| 102 | map.put(Variable.create(var), t); | ||
| 103 | ++index; | ||
| 104 | } | ||
| 105 | return map; | ||
| 106 | } | ||
| 107 | |||
| 108 | public boolean hasAuxPredicate() { | ||
| 109 | String iri; | ||
| 110 | for (int i = 0; i < m_tuple.length; ++i) | ||
| 111 | if ((m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)) { | ||
| 112 | iri = ((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI(); | ||
| 113 | if ( iri.startsWith(Namespace.PAGODA_AUX) || iri.contains("_AUX") || iri.contains("_neg") || iri.contains("internal:def")) | ||
| 114 | return true; | ||
| 115 | } | ||
| 116 | return false; | ||
| 117 | } | ||
| 118 | |||
| 119 | public boolean hasAnonyIndividual() { | ||
| 120 | String iri; | ||
| 121 | for (int i = 0; i < m_tuple.length; ++i) | ||
| 122 | if ((m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)) { | ||
| 123 | iri = ((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI(); | ||
| 124 | if (iri.startsWith(Namespace.PAGODA_ANONY) || iri.startsWith(Namespace.KARMA_ANONY)) | ||
| 125 | return true; | ||
| 126 | } | ||
| 127 | return false; | ||
| 128 | } | ||
| 129 | |||
| 130 | public static AnswerTuple create(AnswerTuple extendedTuple, int length) { | ||
| 131 | if (length == extendedTuple.getArity()) return extendedTuple; | ||
| 132 | else return new AnswerTuple(extendedTuple, length); | ||
| 133 | } | ||
| 134 | |||
| 135 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java new file mode 100644 index 0000000..e1e5302 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | public interface AnswerTuples { | ||
| 4 | |||
| 5 | public void reset(); | ||
| 6 | |||
| 7 | public boolean isValid(); | ||
| 8 | |||
| 9 | public int getArity(); | ||
| 10 | |||
| 11 | public String[] getAnswerVariables(); | ||
| 12 | |||
| 13 | public void moveNext(); | ||
| 14 | |||
| 15 | public void dispose(); | ||
| 16 | |||
| 17 | public AnswerTuple getTuple(); | ||
| 18 | |||
| 19 | public boolean contains(AnswerTuple t); | ||
| 20 | |||
| 21 | public void remove(); | ||
| 22 | |||
| 23 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java new file mode 100644 index 0000000..3e0f320 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.util.Iterator; | ||
| 4 | import java.util.Set; | ||
| 5 | |||
| 6 | public class AnswerTuplesImp implements AnswerTuples { | ||
| 7 | |||
| 8 | int m_index; | ||
| 9 | Iterator<AnswerTuple> m_iter; | ||
| 10 | Set<AnswerTuple> m_answers1, m_answers2; | ||
| 11 | String[] m_answerVars; | ||
| 12 | AnswerTuple m_tuple; | ||
| 13 | |||
| 14 | public AnswerTuplesImp(String[] answerVars, Set<AnswerTuple> answers) { | ||
| 15 | m_answers1 = answers; | ||
| 16 | m_answers2 = null; | ||
| 17 | m_answerVars = answerVars; | ||
| 18 | reset(); | ||
| 19 | } | ||
| 20 | |||
| 21 | public AnswerTuplesImp(String[] answerVars, Set<AnswerTuple> answers1, Set<AnswerTuple> answers2) { | ||
| 22 | m_answers1 = answers1; | ||
| 23 | m_answers2 = answers2; | ||
| 24 | m_answerVars = answerVars; | ||
| 25 | reset(); | ||
| 26 | } | ||
| 27 | |||
| 28 | @Override | ||
| 29 | public boolean isValid() { | ||
| 30 | return m_tuple != null; | ||
| 31 | } | ||
| 32 | |||
| 33 | @Override | ||
| 34 | public int getArity() { | ||
| 35 | return m_answerVars.length; | ||
| 36 | } | ||
| 37 | |||
| 38 | @Override | ||
| 39 | public void moveNext() { | ||
| 40 | if (m_iter != null && m_iter.hasNext()) { | ||
| 41 | m_tuple = m_iter.next(); | ||
| 42 | return ; | ||
| 43 | } | ||
| 44 | else if (m_answers2 != null && m_index == 1){ | ||
| 45 | ++m_index; | ||
| 46 | m_iter = m_answers2.iterator(); | ||
| 47 | if (m_iter.hasNext()) { | ||
| 48 | m_tuple = m_iter.next(); | ||
| 49 | return ; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | else | ||
| 53 | m_tuple = null; | ||
| 54 | } | ||
| 55 | |||
| 56 | @Override | ||
| 57 | public void dispose() {} | ||
| 58 | |||
| 59 | @Override | ||
| 60 | public void reset() { | ||
| 61 | if (m_answers1 == null || m_answers1.isEmpty()) { | ||
| 62 | m_index = 2; | ||
| 63 | m_iter = m_answers2 == null ? null : m_answers2.iterator(); | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | m_index = 1; | ||
| 67 | m_iter = m_answers1.iterator(); | ||
| 68 | } | ||
| 69 | moveNext(); | ||
| 70 | } | ||
| 71 | |||
| 72 | @Override | ||
| 73 | public boolean contains(AnswerTuple t) { | ||
| 74 | return m_answers1.contains(t) || (m_answers2 != null && m_answers2.contains(t)); | ||
| 75 | } | ||
| 76 | |||
| 77 | @Override | ||
| 78 | public AnswerTuple getTuple() { | ||
| 79 | return m_tuple; | ||
| 80 | } | ||
| 81 | |||
| 82 | @Override | ||
| 83 | public String[] getAnswerVariables() { | ||
| 84 | return m_answerVars; | ||
| 85 | } | ||
| 86 | |||
| 87 | @Override | ||
| 88 | public void remove() { | ||
| 89 | m_iter.remove(); | ||
| 90 | } | ||
| 91 | |||
| 92 | } | ||
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 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/GapByTriple.java b/src/uk/ac/ox/cs/pagoda/query/GapByTriple.java new file mode 100644 index 0000000..1a94f07 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/GapByTriple.java | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.FileOutputStream; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.io.OutputStreamWriter; | ||
| 7 | import java.util.Collection; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 12 | import org.semanticweb.HermiT.model.DLClause; | ||
| 13 | import org.semanticweb.HermiT.model.Individual; | ||
| 14 | |||
| 15 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 16 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 17 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 18 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 19 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 20 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 21 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 22 | import uk.ac.ox.cs.JRDFox.Prefixes; | ||
| 23 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 24 | import uk.ac.ox.cs.JRDFox.store.Parameters; | ||
| 25 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 26 | |||
| 27 | public class GapByTriple extends GapTupleIterator<String> { | ||
| 28 | |||
| 29 | private static final String RDF_TYPE = Namespace.RDF_NS + "type"; | ||
| 30 | private static final String BRIEF_RDF_TYPE = "rdf:type"; | ||
| 31 | |||
| 32 | static final String allTripleQuery = "SELECT ?X ?Y ?Z WHERE { ?X ?Y ?Z }"; | ||
| 33 | |||
| 34 | DataStore lowerStore, upperStore; | ||
| 35 | long multi; | ||
| 36 | TupleIterator iterator; | ||
| 37 | String sub, obj, predicate; | ||
| 38 | // GroundTerm subTerm, objTerm; | ||
| 39 | Prefixes prefixes; | ||
| 40 | Parameters parameters; | ||
| 41 | |||
| 42 | public GapByTriple(BasicQueryEngine lowerStore, BasicQueryEngine upperStore) { | ||
| 43 | this.lowerStore = lowerStore.getDataStore(); | ||
| 44 | this.upperStore = upperStore.getDataStore(); | ||
| 45 | prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes(); | ||
| 46 | parameters = new Parameters(); | ||
| 47 | } | ||
| 48 | |||
| 49 | public void compile(Collection<DLClause> clauses) throws JRDFStoreException { | ||
| 50 | iterator = this.upperStore.compileQuery(allTripleQuery, prefixes, parameters); | ||
| 51 | multi = iterator.open(); | ||
| 52 | } | ||
| 53 | |||
| 54 | @Override | ||
| 55 | public boolean hasNext() { | ||
| 56 | TupleIterator iter; | ||
| 57 | boolean inGap; | ||
| 58 | StringBuffer queryBuffer = new StringBuffer(); | ||
| 59 | try { | ||
| 60 | for (; multi != 0; multi = iterator.getNext()) { | ||
| 61 | // iterator.getRawGroundTerm(0); | ||
| 62 | // iterator.getRawGroundTerm(1); | ||
| 63 | // iterator.getRawGroundTerm(2); | ||
| 64 | |||
| 65 | sub = RDFoxTripleManager.getQuotedTerm(iterator.getResource(0)); | ||
| 66 | predicate = RDFoxTripleManager.getQuotedTerm(iterator.getResource(1)); | ||
| 67 | obj = RDFoxTripleManager.getQuotedTerm(iterator.getResource(2)); | ||
| 68 | |||
| 69 | if (!obj.startsWith("<")) { | ||
| 70 | // This fragment of code ignores data types assertions. | ||
| 71 | // Utility.LOGS.info(sub + " " + predicate + " " + obj); | ||
| 72 | continue; | ||
| 73 | } | ||
| 74 | |||
| 75 | queryBuffer.setLength(0); | ||
| 76 | queryBuffer.append("SELECT WHERE { ").append(sub).append(" ").append(predicate).append(" ").append(obj).append(" }"); | ||
| 77 | |||
| 78 | iter = lowerStore.compileQuery(queryBuffer.toString(), prefixes, parameters); | ||
| 79 | inGap = iter.open() != 0; | ||
| 80 | iter.dispose(); | ||
| 81 | if (inGap) | ||
| 82 | return true; | ||
| 83 | } | ||
| 84 | } catch (JRDFStoreException e) { | ||
| 85 | // TODO Auto-generated catch block | ||
| 86 | e.printStackTrace(); | ||
| 87 | } | ||
| 88 | return false; | ||
| 89 | } | ||
| 90 | |||
| 91 | @Override | ||
| 92 | public String next() { | ||
| 93 | try { | ||
| 94 | multi = iterator.getNext(); | ||
| 95 | } catch (JRDFStoreException e) { | ||
| 96 | e.printStackTrace(); | ||
| 97 | } | ||
| 98 | StringBuilder sb = new StringBuilder(); | ||
| 99 | if (isRDF_TYPE()) { | ||
| 100 | sb.append(sub).append(" ").append(predicate).append(" ").append(getGapPredicate(obj)).append("."); | ||
| 101 | } | ||
| 102 | else sb.append(sub).append(" ").append(getGapPredicate(predicate)).append(" ").append(obj).append("."); | ||
| 103 | return sb.toString(); | ||
| 104 | } | ||
| 105 | |||
| 106 | private boolean isRDF_TYPE() { | ||
| 107 | return predicate.equals(RDF_TYPE) || predicate.equals(BRIEF_RDF_TYPE); | ||
| 108 | } | ||
| 109 | |||
| 110 | @Override | ||
| 111 | public void remove() { | ||
| 112 | Utility.logError("Unsupported operation!"); | ||
| 113 | } | ||
| 114 | |||
| 115 | public void save(String file) { | ||
| 116 | int tupleCounter = 0; | ||
| 117 | try { | ||
| 118 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); | ||
| 119 | String tuple; | ||
| 120 | while (hasNext()) { | ||
| 121 | tuple = next(); | ||
| 122 | writer.write(tuple); | ||
| 123 | writer.newLine(); | ||
| 124 | ++tupleCounter; | ||
| 125 | } | ||
| 126 | writer.close(); | ||
| 127 | } catch (IOException e) { | ||
| 128 | e.printStackTrace(); | ||
| 129 | } | ||
| 130 | |||
| 131 | Utility.logError("There are " + tupleCounter + " tuples in the gap between lower and upper bound materialisation."); | ||
| 132 | } | ||
| 133 | |||
| 134 | public void addTo(DataStore store) throws JRDFStoreException { | ||
| 135 | int tupleCounter = 0; | ||
| 136 | RDFoxTripleManager tripleManager = new RDFoxTripleManager(store, false); | ||
| 137 | while (hasNext()) { | ||
| 138 | multi = iterator.getNext(); | ||
| 139 | ++tupleCounter; | ||
| 140 | if (isRDF_TYPE()) { | ||
| 141 | obj = OWLHelper.removeAngles(obj); | ||
| 142 | tripleManager.addTripleByTerm( | ||
| 143 | Atom.create(AtomicConcept.create(getGapPredicate(obj)), Individual.create(sub))); | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | predicate = OWLHelper.removeAngles(predicate); | ||
| 147 | tripleManager.addTripleByTerm( | ||
| 148 | Atom.create(AtomicRole.create(getGapPredicate(predicate)), Individual.create(sub), Individual.create(obj))); | ||
| 149 | } | ||
| 150 | if (tupleCounter % 10000 == 0) | ||
| 151 | Utility.logDebug(tupleCounter); | ||
| 152 | } | ||
| 153 | |||
| 154 | Utility.logDebug("There are " + tupleCounter + " tuples in the gap between lower and upper bound materialisation."); | ||
| 155 | } | ||
| 156 | |||
| 157 | @Override | ||
| 158 | public void addBackTo() throws JRDFStoreException { | ||
| 159 | addTo(upperStore); | ||
| 160 | } | ||
| 161 | |||
| 162 | @Override | ||
| 163 | public boolean isValid() { | ||
| 164 | return true; | ||
| 165 | } | ||
| 166 | |||
| 167 | @Override | ||
| 168 | public void clear() { | ||
| 169 | iterator.dispose(); | ||
| 170 | } | ||
| 171 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/GapTupleIterator.java b/src/uk/ac/ox/cs/pagoda/query/GapTupleIterator.java new file mode 100644 index 0000000..58303bb --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/GapTupleIterator.java | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.util.Iterator; | ||
| 4 | |||
| 5 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 6 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 7 | |||
| 8 | public abstract class GapTupleIterator<T> implements Iterator<T> { | ||
| 9 | |||
| 10 | public static final String gapPredicateSuffix = "_AUXg"; | ||
| 11 | |||
| 12 | public static final String getGapPredicate(String predicateIRI) { | ||
| 13 | if (predicateIRI.startsWith("<")) | ||
| 14 | return predicateIRI.replace(">", gapPredicateSuffix + ">"); | ||
| 15 | return predicateIRI + gapPredicateSuffix; | ||
| 16 | } | ||
| 17 | |||
| 18 | public void compile(String programText) throws JRDFStoreException {} | ||
| 19 | |||
| 20 | public abstract boolean isValid(); | ||
| 21 | |||
| 22 | public abstract void save(String file); | ||
| 23 | |||
| 24 | public abstract void addBackTo() throws JRDFStoreException; | ||
| 25 | |||
| 26 | public abstract void addTo(DataStore store) throws JRDFStoreException; | ||
| 27 | |||
| 28 | public abstract void clear(); | ||
| 29 | |||
| 30 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/QueryManager.java b/src/uk/ac/ox/cs/pagoda/query/QueryManager.java new file mode 100644 index 0000000..419cb97 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/QueryManager.java | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileNotFoundException; | ||
| 5 | import java.util.Collection; | ||
| 6 | import java.util.HashMap; | ||
| 7 | import java.util.LinkedList; | ||
| 8 | import java.util.Map; | ||
| 9 | import java.util.Scanner; | ||
| 10 | |||
| 11 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 12 | |||
| 13 | public class QueryManager { | ||
| 14 | |||
| 15 | public Collection<QueryRecord> collectQueryRecords(String queryfile) { | ||
| 16 | Collection<QueryRecord> ret = new LinkedList<QueryRecord>(); | ||
| 17 | for (String queryText: collectQueryTexts(queryfile)) | ||
| 18 | ret.add(create(queryText)); | ||
| 19 | return ret; | ||
| 20 | } | ||
| 21 | |||
| 22 | public static Collection<String> collectQueryTexts(String queryfile) { | ||
| 23 | Scanner scanner = null; | ||
| 24 | try { | ||
| 25 | scanner = new Scanner(new File(queryfile)); | ||
| 26 | } catch (FileNotFoundException e) { | ||
| 27 | e.printStackTrace(); | ||
| 28 | return null; | ||
| 29 | } | ||
| 30 | Collection<String> ret = new LinkedList<String>(); | ||
| 31 | |||
| 32 | StringBuilder sb = new StringBuilder(); | ||
| 33 | int leftToMatch; | ||
| 34 | String text; | ||
| 35 | while (scanner.hasNextLine()) { | ||
| 36 | leftToMatch = -1; | ||
| 37 | for (String line; scanner.hasNextLine(); ) { | ||
| 38 | line = scanner.nextLine(); | ||
| 39 | if (line.length() > 6 && line.substring(0, 6).equalsIgnoreCase("SELECT")) { | ||
| 40 | String next = line.split(" ")[1]; | ||
| 41 | if (!next.equalsIgnoreCase("distinct")) | ||
| 42 | line = line.substring(0, 6) + " distinct" + line.substring(6); | ||
| 43 | } | ||
| 44 | for (int i = 0; i < line.length(); ++i) | ||
| 45 | if (line.charAt(i) == '{') | ||
| 46 | if (leftToMatch == -1) leftToMatch = 1; | ||
| 47 | else ++leftToMatch; | ||
| 48 | else if (line.charAt(i) == '}') --leftToMatch; | ||
| 49 | |||
| 50 | // if (line.isEmpty()) break; | ||
| 51 | |||
| 52 | if (!line.isEmpty()) | ||
| 53 | sb.append(line).append(Utility.LINE_SEPARATOR); | ||
| 54 | |||
| 55 | if (leftToMatch == 0) break; | ||
| 56 | } | ||
| 57 | |||
| 58 | text = preprocess(sb.toString()); | ||
| 59 | if (!text.isEmpty()) | ||
| 60 | ret.add(text); | ||
| 61 | sb.setLength(0); | ||
| 62 | } | ||
| 63 | |||
| 64 | scanner.close(); | ||
| 65 | return ret; | ||
| 66 | } | ||
| 67 | |||
| 68 | private static String preprocess(String text) { | ||
| 69 | int index; | ||
| 70 | text = text.trim(); | ||
| 71 | while (text.startsWith("^") || text.startsWith("#") || text.startsWith("//") || text.startsWith("@")) | ||
| 72 | if ((index = text.indexOf("\n")) != -1) | ||
| 73 | text = text.substring(index + 1); | ||
| 74 | else { | ||
| 75 | text = ""; | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | return text; // text.replace(" a ", " <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "); | ||
| 79 | } | ||
| 80 | |||
| 81 | private Map<String, QueryRecord> allRecords = new HashMap<String, QueryRecord>(); | ||
| 82 | private int queryCounter = 0; | ||
| 83 | |||
| 84 | public QueryRecord create(String text, int i, int j) { | ||
| 85 | // StringBuilder queryText = new StringBuilder(); | ||
| 86 | // for (String seq : text.split("\s")) { | ||
| 87 | // if (seq.length() == 0) continue; | ||
| 88 | // if (queryText.length() != 0) queryText.append(" "); | ||
| 89 | // queryText.append(seq); | ||
| 90 | // } | ||
| 91 | // text = queryText.toString(); | ||
| 92 | text = text.replaceAll("\\s+", " ").trim(); | ||
| 93 | QueryRecord ret = allRecords.get(text); | ||
| 94 | if (ret != null) return ret; | ||
| 95 | else { | ||
| 96 | if (i == -1) { | ||
| 97 | i = ++queryCounter; | ||
| 98 | } | ||
| 99 | |||
| 100 | ret = new QueryRecord(this, text, i, j); | ||
| 101 | allRecords.put(text, ret); | ||
| 102 | return ret; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | public QueryRecord create(String text, int i) { | ||
| 107 | return create(text, i, 0); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | public void remove(String queryText) { | ||
| 112 | allRecords.remove(queryText); | ||
| 113 | } | ||
| 114 | |||
| 115 | public void put(String text, QueryRecord queryRecord) { | ||
| 116 | allRecords.put(text, queryRecord); | ||
| 117 | } | ||
| 118 | |||
| 119 | public QueryRecord create(String queryText) { | ||
| 120 | return create(queryText, -1, 0); | ||
| 121 | } | ||
| 122 | |||
| 123 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java new file mode 100644 index 0000000..ce92a67 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | |||
| @@ -0,0 +1,562 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.FileNotFoundException; | ||
| 5 | import java.io.FileOutputStream; | ||
| 6 | import java.io.IOException; | ||
| 7 | import java.io.OutputStreamWriter; | ||
| 8 | import java.util.Collection; | ||
| 9 | import java.util.HashMap; | ||
| 10 | import java.util.HashSet; | ||
| 11 | import java.util.Iterator; | ||
| 12 | import java.util.LinkedList; | ||
| 13 | import java.util.Map; | ||
| 14 | import java.util.Set; | ||
| 15 | |||
| 16 | import org.semanticweb.HermiT.model.Atom; | ||
| 17 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 18 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 19 | import org.semanticweb.HermiT.model.DLClause; | ||
| 20 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 21 | import org.semanticweb.HermiT.model.Variable; | ||
| 22 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 23 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 24 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 25 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 26 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 27 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 28 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 29 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 30 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 31 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 32 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 33 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 34 | |||
| 35 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 36 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxAnswerTuples; | ||
| 37 | import uk.ac.ox.cs.pagoda.rules.GeneralProgram; | ||
| 38 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | ||
| 39 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 40 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 41 | |||
| 42 | public class QueryRecord { | ||
| 43 | |||
| 44 | public static final String botQueryText = "SELECT ?X WHERE { ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Nothing> }"; | ||
| 45 | |||
| 46 | private Step diffculty; | ||
| 47 | |||
| 48 | private String queryText; | ||
| 49 | private int queryID = -1; | ||
| 50 | private String[][] answerVariables = null; | ||
| 51 | private Set<AnswerTuple> soundAnswerTuples = new HashSet<AnswerTuple>(); | ||
| 52 | private Set<AnswerTuple> gapAnswerTuples = null; | ||
| 53 | |||
| 54 | private QueryManager m_manager; | ||
| 55 | |||
| 56 | public QueryRecord(QueryManager manager, String text, int id, int subID) { | ||
| 57 | m_manager =manager; | ||
| 58 | resetInfo(text, id, subID); | ||
| 59 | resetTimer(); | ||
| 60 | } | ||
| 61 | |||
| 62 | public void resetInfo(String text, int id, int subid) { | ||
| 63 | queryID = id; | ||
| 64 | subID = subid; | ||
| 65 | stringQueryID = id + (subID == 0 ? "" : "_" + subID); | ||
| 66 | m_manager.remove(queryText); | ||
| 67 | m_manager.put(text, this); | ||
| 68 | queryClause = null; | ||
| 69 | answerVariables = ConjunctiveQueryHelper.getAnswerVariables(text); | ||
| 70 | queryText = text; // .replace("_:", "?"); | ||
| 71 | } | ||
| 72 | |||
| 73 | public void resetTimer() { | ||
| 74 | int length = Step.values().length; | ||
| 75 | timer = new double[length]; | ||
| 76 | for (int i = 0; i < length; ++i) timer[i] = 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | public AnswerTuples getAnswers() { | ||
| 80 | if (processed()) | ||
| 81 | return getLowerBoundAnswers(); | ||
| 82 | |||
| 83 | return getUpperBoundAnswers(); | ||
| 84 | } | ||
| 85 | |||
| 86 | public AnswerTuples getLowerBoundAnswers() { | ||
| 87 | return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples); | ||
| 88 | } | ||
| 89 | |||
| 90 | public AnswerTuples getUpperBoundAnswers() { | ||
| 91 | return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples, gapAnswerTuples); | ||
| 92 | } | ||
| 93 | |||
| 94 | public boolean updateLowerBoundAnswers(AnswerTuples answerTuples) { | ||
| 95 | if (answerTuples == null) return false; | ||
| 96 | boolean update = false; | ||
| 97 | for (AnswerTuple tuple; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 98 | tuple = answerTuples.getTuple(); | ||
| 99 | if (!soundAnswerTuples.contains(tuple) && (gapAnswerTuples == null || gapAnswerTuples.contains(tuple))) { | ||
| 100 | soundAnswerTuples.add(tuple); | ||
| 101 | if (gapAnswerTuples != null) | ||
| 102 | gapAnswerTuples.remove(tuple); | ||
| 103 | update = true; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | Utility.logInfo("The number of answers in the lower bound: " + soundAnswerTuples.size()); | ||
| 107 | |||
| 108 | return update; | ||
| 109 | } | ||
| 110 | |||
| 111 | public boolean updateUpperBoundAnswers(AnswerTuples answerTuples) { | ||
| 112 | return updateUpperBoundAnswers(answerTuples, false); | ||
| 113 | } | ||
| 114 | |||
| 115 | public boolean updateUpperBoundAnswers(AnswerTuples answerTuples, boolean toCheckAux) { | ||
| 116 | RDFoxAnswerTuples rdfAnswerTuples; | ||
| 117 | if (answerTuples instanceof RDFoxAnswerTuples) | ||
| 118 | rdfAnswerTuples = (RDFoxAnswerTuples) answerTuples; | ||
| 119 | else { | ||
| 120 | Utility.logError("The upper bound must be computed by RDFox!"); | ||
| 121 | return false; | ||
| 122 | } | ||
| 123 | |||
| 124 | if (soundAnswerTuples.size() > 0) { | ||
| 125 | int number = 0; | ||
| 126 | for (; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 127 | ++number; | ||
| 128 | } | ||
| 129 | Utility.logInfo("The number of answers returned by the upper bound: " + number); | ||
| 130 | if (number <= soundAnswerTuples.size()) { | ||
| 131 | if (gapAnswerTuples != null) gapAnswerTuples.clear(); | ||
| 132 | else gapAnswerTuples = new HashSet<AnswerTuple>(); | ||
| 133 | |||
| 134 | Utility.logInfo("The number of answers in the upper bound: " + (soundAnswerTuples.size() + gapAnswerTuples.size())); | ||
| 135 | return false; | ||
| 136 | } | ||
| 137 | answerTuples.reset(); | ||
| 138 | } | ||
| 139 | |||
| 140 | boolean justCheck = (answerTuples.getArity() != answerVariables[1].length); | ||
| 141 | |||
| 142 | Set<AnswerTuple> tupleSet = new HashSet<AnswerTuple>(); | ||
| 143 | AnswerTuple tuple, extendedTuple; | ||
| 144 | for (; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 145 | extendedTuple = rdfAnswerTuples.getTuple(); | ||
| 146 | if (isBottom() || !extendedTuple.hasAnonyIndividual()) { | ||
| 147 | tuple = AnswerTuple.create(extendedTuple, answerVariables[0].length); | ||
| 148 | if ((!toCheckAux || !tuple.hasAuxPredicate()) && !soundAnswerTuples.contains(tuple)) { | ||
| 149 | if (!toCheckAux && justCheck) return false; | ||
| 150 | tupleSet.add(extendedTuple); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if (gapAnswerTuples == null) { | ||
| 156 | gapAnswerTuples = tupleSet; | ||
| 157 | |||
| 158 | Utility.logInfo("The number of answers in the upper bound: " + (soundAnswerTuples.size() + gapAnswerTuples.size())); | ||
| 159 | return true; | ||
| 160 | } | ||
| 161 | |||
| 162 | boolean update = false; | ||
| 163 | for (Iterator<AnswerTuple> iter = gapAnswerTuples.iterator(); iter.hasNext(); ) { | ||
| 164 | tuple = iter.next(); | ||
| 165 | if (!tupleSet.contains(tuple)) { | ||
| 166 | iter.remove(); | ||
| 167 | update = true; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | Utility.logInfo("The number of answers in the upper bound: " + (soundAnswerTuples.size() + gapAnswerTuples.size())); | ||
| 172 | |||
| 173 | return update; | ||
| 174 | } | ||
| 175 | |||
| 176 | // private boolean containsAuxPredicate(String str) { | ||
| 177 | // return str.contains(Namespace.PAGODA_AUX) || str.contains("_AUX") || str.contains("owl#Nothing") || | ||
| 178 | // str.contains("internal:def"); | ||
| 179 | // } | ||
| 180 | |||
| 181 | boolean processed = false; | ||
| 182 | |||
| 183 | public void markAsProcessed() { | ||
| 184 | processed = true; | ||
| 185 | } | ||
| 186 | |||
| 187 | public boolean processed() { | ||
| 188 | if (gapAnswerTuples != null && gapAnswerTuples.isEmpty()) processed = true; | ||
| 189 | return processed; | ||
| 190 | } | ||
| 191 | |||
| 192 | public String[] getDistinguishedVariables() { | ||
| 193 | return answerVariables[1]; | ||
| 194 | } | ||
| 195 | |||
| 196 | public String[] getAnswerVariables() { | ||
| 197 | return answerVariables[0]; | ||
| 198 | } | ||
| 199 | |||
| 200 | public String[][] getVariables() { | ||
| 201 | return answerVariables; | ||
| 202 | } | ||
| 203 | |||
| 204 | public String getQueryText() { | ||
| 205 | return queryText; | ||
| 206 | } | ||
| 207 | |||
| 208 | String stringQueryID = null; | ||
| 209 | |||
| 210 | public String getQueryID() { | ||
| 211 | return stringQueryID; | ||
| 212 | } | ||
| 213 | |||
| 214 | public AnswerTuples getGapAnswers() { | ||
| 215 | return new AnswerTuplesImp(answerVariables[0], gapAnswerTuples); | ||
| 216 | } | ||
| 217 | |||
| 218 | public String toString() { | ||
| 219 | return queryText; | ||
| 220 | } | ||
| 221 | |||
| 222 | public static final String SEPARATOR = "----------------------------------------"; | ||
| 223 | |||
| 224 | public void outputAnswers(BufferedWriter writer) throws IOException { | ||
| 225 | |||
| 226 | int answerCounter = soundAnswerTuples.size(); | ||
| 227 | if (!processed()) answerCounter += gapAnswerTuples.size(); | ||
| 228 | |||
| 229 | Utility.logInfo("The number of answer tuples: " + answerCounter); | ||
| 230 | |||
| 231 | if (writer != null) { | ||
| 232 | writer.write("-------------- Query " + queryID + " ---------------------"); | ||
| 233 | writer.newLine(); | ||
| 234 | writer.write(queryText); | ||
| 235 | writer.newLine(); | ||
| 236 | StringBuilder space = new StringBuilder(); | ||
| 237 | int arity = getArity(), varSpace = 0; | ||
| 238 | for (int i = 0; i < arity; ++i) | ||
| 239 | varSpace += answerVariables[0][i].length(); | ||
| 240 | for (int i = 0; i < (SEPARATOR.length() - varSpace) / (arity + 1); ++i) | ||
| 241 | space.append(" "); | ||
| 242 | for (int i = 0; i < getArity(); ++i) { | ||
| 243 | writer.write(space.toString()); | ||
| 244 | writer.write(answerVariables[0][i]); | ||
| 245 | } | ||
| 246 | writer.newLine(); | ||
| 247 | writer.write(SEPARATOR); | ||
| 248 | writer.newLine(); | ||
| 249 | for (AnswerTuple tuple: soundAnswerTuples) { | ||
| 250 | writer.write(tuple.toString()); | ||
| 251 | writer.newLine(); | ||
| 252 | } | ||
| 253 | if (!processed()) | ||
| 254 | for (AnswerTuple tuple: gapAnswerTuples) { | ||
| 255 | writer.write("*"); | ||
| 256 | writer.write(tuple.toString()); | ||
| 257 | writer.newLine(); | ||
| 258 | } | ||
| 259 | // writer.write(SEPARATOR); | ||
| 260 | writer.newLine(); | ||
| 261 | } | ||
| 262 | |||
| 263 | } | ||
| 264 | |||
| 265 | public void outputTimes() { | ||
| 266 | for (Step step: Step.values()) { | ||
| 267 | Utility.logDebug("time for " + step + ": " + timer[step.ordinal()]); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | public String outputSoundAnswerTuple() { | ||
| 272 | StringBuilder builder = new StringBuilder(); | ||
| 273 | for (AnswerTuple tuple: soundAnswerTuples) | ||
| 274 | builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); | ||
| 275 | return builder.toString(); | ||
| 276 | } | ||
| 277 | |||
| 278 | public String outputGapAnswerTuple() { | ||
| 279 | StringBuilder builder = new StringBuilder(); | ||
| 280 | for (AnswerTuple tuple: gapAnswerTuples) | ||
| 281 | builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); | ||
| 282 | return builder.toString(); | ||
| 283 | } | ||
| 284 | |||
| 285 | public void setDifficulty(Step step) { | ||
| 286 | this.diffculty = step; | ||
| 287 | } | ||
| 288 | |||
| 289 | public Step getDifficulty() { | ||
| 290 | return diffculty; | ||
| 291 | } | ||
| 292 | |||
| 293 | OWLOntology relevantOntology = null; | ||
| 294 | Set<DLClause> relevantClauses = new HashSet<DLClause>(); | ||
| 295 | |||
| 296 | public void setRelevantOntology(OWLOntology knowledgebase) { | ||
| 297 | relevantOntology = knowledgebase; | ||
| 298 | } | ||
| 299 | |||
| 300 | public OWLOntology getRelevantOntology() { | ||
| 301 | return relevantOntology; | ||
| 302 | } | ||
| 303 | |||
| 304 | public void saveRelevantOntology(String filename) { | ||
| 305 | if (relevantOntology == null) return ; | ||
| 306 | OWLOntologyManager manager = relevantOntology.getOWLOntologyManager(); | ||
| 307 | try { | ||
| 308 | FileOutputStream outputStream = new FileOutputStream(filename); | ||
| 309 | manager.saveOntology(relevantOntology, outputStream); | ||
| 310 | outputStream.close(); | ||
| 311 | } catch (OWLOntologyStorageException e) { | ||
| 312 | e.printStackTrace(); | ||
| 313 | } catch (FileNotFoundException e) { | ||
| 314 | e.printStackTrace(); | ||
| 315 | } catch (IOException e) { | ||
| 316 | e.printStackTrace(); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | public void saveRelevantClause() { | ||
| 321 | if (relevantClauses == null) return ; | ||
| 322 | GeneralProgram p = new GeneralProgram(relevantClauses, relevantOntology); | ||
| 323 | p.save(); | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | public void removeUpperBoundAnswers(Collection<AnswerTuple> answers) { | ||
| 328 | for (AnswerTuple answer: answers) { | ||
| 329 | // if (soundAnswerTuples.contains(answer)) | ||
| 330 | // Utility.logError("The answer (" + answer + ") cannot be removed, because it is in the lower bound."); | ||
| 331 | if (!gapAnswerTuples.contains(answer)) | ||
| 332 | Utility.logError("The answer (" + answer + ") cannot be removed, because it is not in the upper bound."); | ||
| 333 | gapAnswerTuples.remove(answer); | ||
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | public void addLowerBoundAnswers(Collection<AnswerTuple> answers) { | ||
| 339 | for (AnswerTuple answer: answers) { | ||
| 340 | if (!gapAnswerTuples.contains(answer)) | ||
| 341 | Utility.logError("The answer (" + answer + ") cannot be added, because it is not in the upper bound."); | ||
| 342 | gapAnswerTuples.remove(answer); | ||
| 343 | |||
| 344 | answer = AnswerTuple.create(answer, answerVariables[0].length); | ||
| 345 | // if (soundAnswerTuples.contains(answer)) | ||
| 346 | // Utility.logError("The answer (" + answer + ") cannot be added, because it is in the lower bound."); | ||
| 347 | soundAnswerTuples.add(answer); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | public int getNoOfSoundAnswers() { | ||
| 352 | return soundAnswerTuples.size(); | ||
| 353 | } | ||
| 354 | |||
| 355 | public enum Step {LowerBound, UpperBound, ELLowerBound, | ||
| 356 | Fragment, FragmentRefinement, Summarisation, Dependency, FullReasoning}; | ||
| 357 | |||
| 358 | double[] timer; | ||
| 359 | |||
| 360 | public void addProcessingTime(Step step, double time) { | ||
| 361 | timer[step.ordinal()] += time; | ||
| 362 | } | ||
| 363 | |||
| 364 | public int getArity() { | ||
| 365 | return answerVariables[0].length; | ||
| 366 | } | ||
| 367 | |||
| 368 | public static Collection<String> collectQueryTexts(Collection<QueryRecord> queryRecords) { | ||
| 369 | Collection<String> texts = new LinkedList<String>(); | ||
| 370 | for (QueryRecord record: queryRecords) | ||
| 371 | texts.add(record.queryText); | ||
| 372 | return texts; | ||
| 373 | } | ||
| 374 | |||
| 375 | public void addRelevantClauses(DLClause clause) { | ||
| 376 | relevantClauses.add(clause); | ||
| 377 | } | ||
| 378 | |||
| 379 | public Set<DLClause> getRelevantClauses() { | ||
| 380 | return relevantClauses; | ||
| 381 | } | ||
| 382 | |||
| 383 | public void clearClauses() { | ||
| 384 | relevantClauses.clear(); | ||
| 385 | } | ||
| 386 | |||
| 387 | public boolean isHorn() { | ||
| 388 | for (DLClause clause: relevantClauses) | ||
| 389 | if (clause.getHeadLength() > 1) | ||
| 390 | return false; | ||
| 391 | return true; | ||
| 392 | } | ||
| 393 | |||
| 394 | public void saveABoxInTurtle(String filename) { | ||
| 395 | try { | ||
| 396 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))); | ||
| 397 | OWLIndividual a, b; | ||
| 398 | StringBuilder builder = new StringBuilder(); | ||
| 399 | for (OWLAxiom axiom: relevantOntology.getABoxAxioms(true)) { | ||
| 400 | if (axiom instanceof OWLClassAssertionAxiom) { | ||
| 401 | OWLClassAssertionAxiom classAssertion = (OWLClassAssertionAxiom) axiom; | ||
| 402 | OWLClass c = (OWLClass) classAssertion.getClassExpression(); | ||
| 403 | a = classAssertion.getIndividual(); | ||
| 404 | builder.append(a.toString()).append(" <").append(Namespace.RDF_TYPE).append("> ").append(c.toString()); | ||
| 405 | } | ||
| 406 | else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { | ||
| 407 | OWLObjectPropertyAssertionAxiom propertyAssertion = (OWLObjectPropertyAssertionAxiom) axiom; | ||
| 408 | OWLObjectProperty p = (OWLObjectProperty) propertyAssertion.getProperty(); | ||
| 409 | a = propertyAssertion.getSubject(); | ||
| 410 | b = propertyAssertion.getObject(); | ||
| 411 | builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(b.toString()); | ||
| 412 | } | ||
| 413 | else if (axiom instanceof OWLDataPropertyAssertionAxiom) { | ||
| 414 | OWLDataPropertyAssertionAxiom propertyAssertion = (OWLDataPropertyAssertionAxiom) axiom; | ||
| 415 | OWLDataProperty p = (OWLDataProperty) propertyAssertion.getProperty(); | ||
| 416 | a = propertyAssertion.getSubject(); | ||
| 417 | OWLLiteral l = propertyAssertion.getObject(); | ||
| 418 | builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(l.toString()); | ||
| 419 | } | ||
| 420 | |||
| 421 | writer.write(builder.toString()); | ||
| 422 | writer.write(" ."); | ||
| 423 | writer.newLine(); | ||
| 424 | builder.setLength(0); | ||
| 425 | } | ||
| 426 | writer.close(); | ||
| 427 | } catch (IOException e) { | ||
| 428 | e.printStackTrace(); | ||
| 429 | } finally { | ||
| 430 | |||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | int subID; | ||
| 435 | |||
| 436 | public void updateSubID() { | ||
| 437 | ++subID; | ||
| 438 | stringQueryID = String.valueOf(queryID) + "_" + subID; | ||
| 439 | } | ||
| 440 | |||
| 441 | DLClause queryClause = null; | ||
| 442 | |||
| 443 | public DLClause getClause() { | ||
| 444 | if (queryClause != null) | ||
| 445 | return queryClause; | ||
| 446 | return queryClause = DLClauseHelper.getQuery(queryText, null); | ||
| 447 | } | ||
| 448 | |||
| 449 | public boolean isBottom() { | ||
| 450 | return queryID == 0; | ||
| 451 | } | ||
| 452 | |||
| 453 | public int getNoOfCompleteAnswers() { | ||
| 454 | return soundAnswerTuples.size() + gapAnswerTuples.size(); | ||
| 455 | } | ||
| 456 | |||
| 457 | public int getSubID() { | ||
| 458 | return subID; | ||
| 459 | } | ||
| 460 | |||
| 461 | public boolean hasSameGapAnswers(QueryRecord that) { | ||
| 462 | return gapAnswerTuples.containsAll(that.gapAnswerTuples) && that.gapAnswerTuples.containsAll(gapAnswerTuples); | ||
| 463 | } | ||
| 464 | |||
| 465 | public void dispose() { | ||
| 466 | m_manager.remove(queryText); | ||
| 467 | if (gapAnswerTuples != null) gapAnswerTuples = null; | ||
| 468 | if (soundAnswerTuples != null) soundAnswerTuples = null; | ||
| 469 | if (relevantClauses != null) relevantClauses.clear(); | ||
| 470 | if (relevantOntology != null) | ||
| 471 | relevantOntology.getOWLOntologyManager().removeOntology(relevantOntology); | ||
| 472 | answerVariables = null; | ||
| 473 | } | ||
| 474 | |||
| 475 | public boolean canBeEncodedIntoAtom() { | ||
| 476 | // FIXME | ||
| 477 | return true; | ||
| 478 | // return false; | ||
| 479 | } | ||
| 480 | |||
| 481 | public boolean isPredicate(AnswerTuple a, int i) { | ||
| 482 | Atom[] atoms = getClause().getBodyAtoms(); | ||
| 483 | Variable v = Variable.create(answerVariables[1][i]); | ||
| 484 | String iri; | ||
| 485 | for (Atom atom: atoms) { | ||
| 486 | DLPredicate p = atom.getDLPredicate(); | ||
| 487 | if (p instanceof AtomicConcept) { | ||
| 488 | if (((AtomicConcept) p).getIRI().equals(v.toString())) return true; | ||
| 489 | } | ||
| 490 | else if (p instanceof AtomicRole) { | ||
| 491 | iri = ((AtomicRole) p).getIRI(); | ||
| 492 | if (iri.equals(v.toString())) return true; | ||
| 493 | if (iri.startsWith("?")) | ||
| 494 | iri = a.getGroundTerm(i).toString(); | ||
| 495 | if (iri.equals(Namespace.RDF_TYPE) && atom.getArgument(1).equals(v)) return true; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | return false; | ||
| 499 | } | ||
| 500 | |||
| 501 | public String[] getExtendedQueryText() { | ||
| 502 | String[] ret = new String[2]; | ||
| 503 | int index = queryText.toUpperCase().indexOf(" WHERE"); | ||
| 504 | String extendedSelect = queryText.substring(0, index); | ||
| 505 | String extendedWhere= queryText.substring(index + 1), fullyExtendedWhere = queryText.substring(index + 1); | ||
| 506 | |||
| 507 | String sub, obj; | ||
| 508 | Map<String, Set<String>> links = new HashMap<String, Set<String>>(); | ||
| 509 | Set<String> list; | ||
| 510 | for (Atom atom: getClause().getBodyAtoms()) | ||
| 511 | if (atom.getDLPredicate() instanceof AtomicRole && atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) { | ||
| 512 | sub = atom.getArgumentVariable(0).getName(); | ||
| 513 | obj = atom.getArgumentVariable(1).getName(); | ||
| 514 | if ((list = links.get(sub)) == null) | ||
| 515 | links.put(sub, list = new HashSet<String>()); | ||
| 516 | list.add(obj); | ||
| 517 | if ((list = links.get(obj)) == null) | ||
| 518 | links.put(obj, list = new HashSet<String>()); | ||
| 519 | list.add(sub); | ||
| 520 | } | ||
| 521 | |||
| 522 | StringBuilder extra = new StringBuilder(), fullyExtra = new StringBuilder(); | ||
| 523 | // if (answerVariables[0] != answerVariables[1]) { | ||
| 524 | for (int i = answerVariables[0].length; i < answerVariables[1].length; ++i) { | ||
| 525 | // for (int i = 0; i < answerVariables[1].length; ++i) { | ||
| 526 | fullyExtra.append(" . ?").append(answerVariables[1][i]).append(" a <").append(Namespace.PAGODA_ORIGINAL).append(">"); | ||
| 527 | if ((list = links.get(answerVariables[1][i])) == null || list.size() < 2) ; | ||
| 528 | else { | ||
| 529 | extra.append(" . ?").append(answerVariables[1][i]).append(" a <").append(Namespace.PAGODA_ORIGINAL).append(">"); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | if (extra.length() > 0) { | ||
| 534 | extra.append(" }"); | ||
| 535 | extendedWhere = extendedWhere.replace(" }", extendedWhere.contains(". }") ? extra.substring(2) : extra.toString()); | ||
| 536 | } | ||
| 537 | |||
| 538 | if (fullyExtra.length() > 0) { | ||
| 539 | fullyExtra.append(" }"); | ||
| 540 | fullyExtendedWhere = fullyExtendedWhere.replace(" }", fullyExtendedWhere.contains(". }") ? fullyExtra.substring(2) : fullyExtra.toString()); | ||
| 541 | } | ||
| 542 | // } | ||
| 543 | |||
| 544 | ret[0] = extendedSelect + " " + fullyExtendedWhere; | ||
| 545 | |||
| 546 | extra.setLength(0); | ||
| 547 | if (answerVariables[0] != answerVariables[1]) { | ||
| 548 | for (int i = answerVariables[0].length; i < answerVariables[1].length; ++i) | ||
| 549 | extra.append(" ?").append(answerVariables[1][i]); | ||
| 550 | extendedSelect = extendedSelect + extra.toString(); | ||
| 551 | } | ||
| 552 | ret[1] = extendedSelect + " " + extendedWhere; | ||
| 553 | |||
| 554 | return ret; | ||
| 555 | } | ||
| 556 | |||
| 557 | public boolean hasNonAnsDistinguishedVariables() { | ||
| 558 | return answerVariables[1].length > answerVariables[0].length; | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java b/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java new file mode 100644 index 0000000..11b0c75 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java | |||
| @@ -0,0 +1,383 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query.rollup; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.Iterator; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 12 | import org.semanticweb.HermiT.model.Constant; | ||
| 13 | import org.semanticweb.HermiT.model.Individual; | ||
| 14 | import org.semanticweb.HermiT.model.Term; | ||
| 15 | import org.semanticweb.HermiT.model.Variable; | ||
| 16 | import org.semanticweb.owlapi.model.IRI; | ||
| 17 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 19 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 20 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; | ||
| 22 | import org.semanticweb.owlapi.model.OWLDataExactCardinality; | ||
| 23 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 24 | import org.semanticweb.owlapi.model.OWLDataHasValue; | ||
| 25 | import org.semanticweb.owlapi.model.OWLDataMaxCardinality; | ||
| 26 | import org.semanticweb.owlapi.model.OWLDataMinCardinality; | ||
| 27 | import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; | ||
| 28 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 29 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 30 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 31 | import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; | ||
| 32 | import org.semanticweb.owlapi.model.OWLObjectComplementOf; | ||
| 33 | import org.semanticweb.owlapi.model.OWLObjectExactCardinality; | ||
| 34 | import org.semanticweb.owlapi.model.OWLObjectHasSelf; | ||
| 35 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 36 | import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; | ||
| 37 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 38 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 39 | import org.semanticweb.owlapi.model.OWLObjectOneOf; | ||
| 40 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 41 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 42 | import org.semanticweb.owlapi.model.OWLObjectUnionOf; | ||
| 43 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 44 | |||
| 45 | public class QueryGraph { | ||
| 46 | |||
| 47 | Set<Variable> freeVars = new HashSet<Variable>(); | ||
| 48 | Set<Variable> existVars = new HashSet<Variable>(); | ||
| 49 | Set<Individual> constants = new HashSet<Individual>(); | ||
| 50 | |||
| 51 | MultiMap<Term, OWLClassExpression> concepts = new MultiMap<Term, OWLClassExpression>(); | ||
| 52 | MultiMap<Term, ObjectEdge> rollable_edges = new MultiMap<Term, ObjectEdge>(); | ||
| 53 | MultiMap<Term, ObjectEdge> edges = new MultiMap<Term, ObjectEdge>(); | ||
| 54 | OWLOntology ontology; | ||
| 55 | OWLDataFactory factory; | ||
| 56 | |||
| 57 | public QueryGraph(Atom[] bodyAtoms, String[] distinguishedVars, OWLOntology onto) { | ||
| 58 | for (String vName: distinguishedVars) | ||
| 59 | freeVars.add(Variable.create(vName)); | ||
| 60 | |||
| 61 | ontology = onto; | ||
| 62 | factory = onto.getOWLOntologyManager().getOWLDataFactory(); | ||
| 63 | |||
| 64 | for (Atom atom: bodyAtoms) { | ||
| 65 | if (atom.getArity() == 1) { | ||
| 66 | updateExistentiallyVariables(atom.getArgumentVariable(0)); | ||
| 67 | IRI iri = IRI.create(((AtomicConcept) atom.getDLPredicate()).getIRI()); | ||
| 68 | if (ontology.containsClassInSignature(iri)) | ||
| 69 | concepts.add(atom.getArgument(0), factory.getOWLClass(IRI.create(((AtomicConcept) atom.getDLPredicate()).getIRI()))); | ||
| 70 | } | ||
| 71 | else if (atom.getArity() == 2) { | ||
| 72 | updateExistentiallyVariables(atom.getArgumentVariable(0)); | ||
| 73 | updateExistentiallyVariables(atom.getArgumentVariable(1)); | ||
| 74 | if (atom.getArgument(0).equals(atom.getArgument(1)) && atom.getArgument(0) instanceof Variable) { | ||
| 75 | concepts.add(atom.getArgument(0), factory.getOWLObjectHasSelf(factory.getOWLObjectProperty(IRI.create(((AtomicRole) atom.getDLPredicate()).getIRI())))); | ||
| 76 | } | ||
| 77 | else createEdges(atom.getArgument(0), (AtomicRole) atom.getDLPredicate(), atom.getArgument(1)); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | rollup(); | ||
| 82 | } | ||
| 83 | |||
| 84 | private void updateExistentiallyVariables(Variable argumentVariable) { | ||
| 85 | if (freeVars.contains(argumentVariable)) return ; | ||
| 86 | existVars.add(argumentVariable); | ||
| 87 | } | ||
| 88 | |||
| 89 | public void createEdges(Term u, AtomicRole r, Term v) { | ||
| 90 | if (ontology.containsDataPropertyInSignature(IRI.create(r.getIRI()))) { | ||
| 91 | // edges.add(u, new DataEdge(r, v)); | ||
| 92 | Constant c = (Constant) v; | ||
| 93 | OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI()); | ||
| 94 | concepts.add(u, factory.getOWLDataHasValue(factory.getOWLDataProperty(IRI.create(r.getIRI())), l)); | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | boolean rollable = existVars.contains(u) || existVars.contains(v); | ||
| 98 | |||
| 99 | ObjectEdge edge = new ObjectEdge(r, v, false); | ||
| 100 | if (rollable) { | ||
| 101 | rollable_edges.add(u, edge); | ||
| 102 | edge = new ObjectEdge(r, u, true); | ||
| 103 | rollable_edges.add(v, edge); | ||
| 104 | } | ||
| 105 | else edges.add(u, edge); | ||
| 106 | |||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | private void rollup() { | ||
| 111 | for (boolean updated = true; updated; ) { | ||
| 112 | updated = false; | ||
| 113 | |||
| 114 | Set<ObjectEdge> set; | ||
| 115 | for (Variable var: existVars) { | ||
| 116 | if ((set = rollable_edges.map.get(var)) != null && set.size() == 1) { | ||
| 117 | updated = true; | ||
| 118 | ObjectEdge edge = set.iterator().next(); | ||
| 119 | rollupEdge(edge.v, edge.p.getInverseProperty().getSimplified(), var, true); | ||
| 120 | set.clear(); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | if (updated) continue; | ||
| 124 | |||
| 125 | for (Variable var: existVars) { | ||
| 126 | set = rollable_edges.map.get(var); | ||
| 127 | if (set == null) continue; | ||
| 128 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { | ||
| 129 | ObjectEdge edge = iter.next(); | ||
| 130 | if (constants.contains(edge.v) || freeVars.contains(edge.v)) { | ||
| 131 | updated = true; | ||
| 132 | rollupEdge(var, edge.p, edge.v, false); | ||
| 133 | iter.remove(); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | private void rollupEdge(Term u, OWLObjectPropertyExpression op, Term v, boolean inverse) { | ||
| 142 | if (existVars.contains(v)) { | ||
| 143 | concepts.add(u, factory.getOWLObjectSomeValuesFrom(op, factory.getOWLObjectIntersectionOf(concepts.get(v)))); | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | OWLIndividual obj = getOWLIndividual(v); | ||
| 147 | concepts.add(u, factory.getOWLObjectHasValue(op, obj)); | ||
| 148 | } | ||
| 149 | |||
| 150 | if (inverse) | ||
| 151 | removeRollableEdge(u, op, v); | ||
| 152 | else | ||
| 153 | removeRollableEdge(v, op.getInverseProperty().getSimplified(), u); | ||
| 154 | } | ||
| 155 | |||
| 156 | private void removeRollableEdge(Term u, OWLObjectPropertyExpression op, Term v) { | ||
| 157 | Set<ObjectEdge> set = rollable_edges.get(u); | ||
| 158 | ObjectEdge edge; | ||
| 159 | if (set != null) | ||
| 160 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { | ||
| 161 | edge = iter.next(); | ||
| 162 | if (edge.p.equals(op) && edge.v.equals(v)) iter.remove(); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | OWLNamedIndividual getOWLIndividual(Term t) { | ||
| 167 | if (freeVars.contains(t)) | ||
| 168 | return new VariableIndividual((Variable) t); | ||
| 169 | else if (t instanceof Variable) | ||
| 170 | return null; | ||
| 171 | else | ||
| 172 | return factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI())); | ||
| 173 | } | ||
| 174 | |||
| 175 | class ObjectEdge { | ||
| 176 | OWLObjectPropertyExpression p; | ||
| 177 | Term v; | ||
| 178 | |||
| 179 | public ObjectEdge(AtomicRole r, Term t, boolean inverse) { | ||
| 180 | p = factory.getOWLObjectProperty(IRI.create(r.getIRI())); | ||
| 181 | if (inverse) p = p.getInverseProperty(); | ||
| 182 | v = t; | ||
| 183 | |||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | class MultiMap<K, V> { | ||
| 188 | |||
| 189 | HashMap<K, Set<V>> map = new HashMap<K, Set<V>>(); | ||
| 190 | |||
| 191 | void add(K key, V value) { | ||
| 192 | Set<V> list = map.get(key); | ||
| 193 | if (list == null) | ||
| 194 | map.put(key, list = new HashSet<V>()); | ||
| 195 | list.add(value); | ||
| 196 | } | ||
| 197 | |||
| 198 | public Set<V> get(K v) { | ||
| 199 | return map.get(v); | ||
| 200 | } | ||
| 201 | |||
| 202 | public boolean isEmpty() { | ||
| 203 | for (Map.Entry<K, Set<V>> entry: map.entrySet()) | ||
| 204 | if (!entry.getValue().isEmpty()) | ||
| 205 | return false; | ||
| 206 | return true; | ||
| 207 | } | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | public Set<OWLAxiom> getPropertyAssertions(Map<Variable, Term> assignment) { | ||
| 212 | OWLIndividual sub, obj; | ||
| 213 | Set<OWLAxiom> axioms = new HashSet<OWLAxiom>(); | ||
| 214 | for (Map.Entry<Term, Set<ObjectEdge>> entry: edges.map.entrySet()) { | ||
| 215 | sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI())); | ||
| 216 | for (ObjectEdge edge: entry.getValue()) { | ||
| 217 | obj = factory.getOWLNamedIndividual(IRI.create(getIndividual(edge.v, assignment).getIRI())); | ||
| 218 | axioms.add(factory.getOWLObjectPropertyAssertionAxiom(edge.p, sub, obj)); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return axioms; | ||
| 222 | } | ||
| 223 | |||
| 224 | public Set<OWLAxiom> getAssertions(Map<Variable, Term> assignment) { | ||
| 225 | if (!rollable_edges.isEmpty()) return null; | ||
| 226 | |||
| 227 | OWLIndividual sub; | ||
| 228 | Visitor visitor = new Visitor(factory, assignment); | ||
| 229 | Set<OWLAxiom> axioms = getPropertyAssertions(assignment); | ||
| 230 | for (Map.Entry<Term, Set<OWLClassExpression>> entry: concepts.map.entrySet()) { | ||
| 231 | if (existVars.contains(entry.getKey())) continue; | ||
| 232 | sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI())); | ||
| 233 | for (OWLClassExpression clsExp: entry.getValue()) { | ||
| 234 | axioms.add(factory.getOWLClassAssertionAxiom(clsExp.accept(visitor), sub)); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | return axioms; | ||
| 238 | } | ||
| 239 | |||
| 240 | private Individual getIndividual(Term key, Map<Variable, Term> assignment) { | ||
| 241 | if (key instanceof Individual) | ||
| 242 | return (Individual) key; | ||
| 243 | else | ||
| 244 | return (Individual) assignment.get(key); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | class Visitor implements OWLClassExpressionVisitorEx<OWLClassExpression> { | ||
| 249 | |||
| 250 | OWLDataFactory factory; | ||
| 251 | Map<Variable, Term> assignment; | ||
| 252 | |||
| 253 | public Visitor(OWLDataFactory factory, Map<Variable, Term> assignment) { | ||
| 254 | this.factory = factory; | ||
| 255 | this.assignment = assignment; | ||
| 256 | } | ||
| 257 | |||
| 258 | @Override | ||
| 259 | public OWLClassExpression visit(OWLClass ce) { | ||
| 260 | // TODO Auto-generated method stub | ||
| 261 | return ce; | ||
| 262 | } | ||
| 263 | |||
| 264 | @Override | ||
| 265 | public OWLClassExpression visit(OWLObjectIntersectionOf ce) { | ||
| 266 | Set<OWLClassExpression> clsExps = new HashSet<OWLClassExpression>(); | ||
| 267 | OWLClassExpression newExp; | ||
| 268 | boolean updated = false; | ||
| 269 | for (OWLClassExpression clsExp: ce.asConjunctSet()) { | ||
| 270 | clsExps.add(newExp = clsExp.accept(this)); | ||
| 271 | if (newExp != clsExp) updated = true; | ||
| 272 | } | ||
| 273 | |||
| 274 | if (updated) return factory.getOWLObjectIntersectionOf(clsExps); | ||
| 275 | else return ce; | ||
| 276 | } | ||
| 277 | |||
| 278 | @Override | ||
| 279 | public OWLClassExpression visit(OWLObjectUnionOf ce) { | ||
| 280 | // TODO Auto-generated method stub | ||
| 281 | return ce; | ||
| 282 | } | ||
| 283 | |||
| 284 | @Override | ||
| 285 | public OWLClassExpression visit(OWLObjectComplementOf ce) { | ||
| 286 | // TODO Auto-generated method stub | ||
| 287 | return ce; | ||
| 288 | } | ||
| 289 | |||
| 290 | @Override | ||
| 291 | public OWLClassExpression visit(OWLObjectSomeValuesFrom ce) { | ||
| 292 | OWLClassExpression newFiller = ce.getFiller().accept(this); | ||
| 293 | if (newFiller == ce.getFiller()) return ce; | ||
| 294 | return factory.getOWLObjectSomeValuesFrom(ce.getProperty(), newFiller); | ||
| 295 | } | ||
| 296 | |||
| 297 | @Override | ||
| 298 | public OWLClassExpression visit(OWLObjectAllValuesFrom ce) { | ||
| 299 | // TODO Auto-generated method stub | ||
| 300 | return ce; | ||
| 301 | } | ||
| 302 | |||
| 303 | @Override | ||
| 304 | public OWLClassExpression visit(OWLObjectHasValue ce) { | ||
| 305 | if (ce.getFiller() instanceof VariableIndividual) { | ||
| 306 | Individual c = (Individual) assignment.get(((VariableIndividual) ce.getFiller()).var); | ||
| 307 | OWLIndividual l = factory.getOWLNamedIndividual(IRI.create(c.getIRI())); | ||
| 308 | return factory.getOWLObjectHasValue(ce.getProperty(), l); | ||
| 309 | } | ||
| 310 | return ce; | ||
| 311 | } | ||
| 312 | |||
| 313 | @Override | ||
| 314 | public OWLClassExpression visit(OWLObjectMinCardinality ce) { | ||
| 315 | // TODO Auto-generated method stub | ||
| 316 | return ce; | ||
| 317 | } | ||
| 318 | |||
| 319 | @Override | ||
| 320 | public OWLClassExpression visit(OWLObjectExactCardinality ce) { | ||
| 321 | // TODO Auto-generated method stub | ||
| 322 | return ce; | ||
| 323 | } | ||
| 324 | |||
| 325 | @Override | ||
| 326 | public OWLClassExpression visit(OWLObjectMaxCardinality ce) { | ||
| 327 | // TODO Auto-generated method stub | ||
| 328 | return ce; | ||
| 329 | } | ||
| 330 | |||
| 331 | @Override | ||
| 332 | public OWLClassExpression visit(OWLObjectHasSelf ce) { | ||
| 333 | // TODO Auto-generated method stub | ||
| 334 | return ce; | ||
| 335 | } | ||
| 336 | |||
| 337 | @Override | ||
| 338 | public OWLClassExpression visit(OWLObjectOneOf ce) { | ||
| 339 | // TODO Auto-generated method stub | ||
| 340 | return ce; | ||
| 341 | } | ||
| 342 | |||
| 343 | @Override | ||
| 344 | public OWLClassExpression visit(OWLDataSomeValuesFrom ce) { | ||
| 345 | // TODO Auto-generated method stub | ||
| 346 | return ce; | ||
| 347 | } | ||
| 348 | |||
| 349 | @Override | ||
| 350 | public OWLClassExpression visit(OWLDataAllValuesFrom ce) { | ||
| 351 | // TODO Auto-generated method stub | ||
| 352 | return ce; | ||
| 353 | } | ||
| 354 | |||
| 355 | @Override | ||
| 356 | public OWLClassExpression visit(OWLDataHasValue ce) { | ||
| 357 | if (ce.getFiller() instanceof VariableConstant) { | ||
| 358 | Constant c = (Constant) assignment.get(((VariableConstant) ce.getFiller()).var); | ||
| 359 | OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI()); | ||
| 360 | return factory.getOWLDataHasValue(ce.getProperty(), l); | ||
| 361 | } | ||
| 362 | return ce; | ||
| 363 | } | ||
| 364 | |||
| 365 | @Override | ||
| 366 | public OWLClassExpression visit(OWLDataMinCardinality ce) { | ||
| 367 | // TODO Auto-generated method stub | ||
| 368 | return ce; | ||
| 369 | } | ||
| 370 | |||
| 371 | @Override | ||
| 372 | public OWLClassExpression visit(OWLDataExactCardinality ce) { | ||
| 373 | // TODO Auto-generated method stub | ||
| 374 | return ce; | ||
| 375 | } | ||
| 376 | |||
| 377 | @Override | ||
| 378 | public OWLClassExpression visit(OWLDataMaxCardinality ce) { | ||
| 379 | // TODO Auto-generated method stub | ||
| 380 | return ce; | ||
| 381 | } | ||
| 382 | |||
| 383 | } \ No newline at end of file | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/rollup/VariableConstant.java b/src/uk/ac/ox/cs/pagoda/query/rollup/VariableConstant.java new file mode 100644 index 0000000..3140fa4 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/rollup/VariableConstant.java | |||
| @@ -0,0 +1,227 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query.rollup; | ||
| 2 | |||
| 3 | import java.util.Set; | ||
| 4 | |||
| 5 | import org.semanticweb.HermiT.model.Variable; | ||
| 6 | import org.semanticweb.owlapi.model.OWLAnnotationValueVisitor; | ||
| 7 | import org.semanticweb.owlapi.model.OWLAnnotationValueVisitorEx; | ||
| 8 | import org.semanticweb.owlapi.model.OWLAnonymousIndividual; | ||
| 9 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 10 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 11 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 12 | import org.semanticweb.owlapi.model.OWLDataVisitor; | ||
| 13 | import org.semanticweb.owlapi.model.OWLDataVisitorEx; | ||
| 14 | import org.semanticweb.owlapi.model.OWLDatatype; | ||
| 15 | import org.semanticweb.owlapi.model.OWLEntity; | ||
| 16 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 17 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 18 | import org.semanticweb.owlapi.model.OWLObject; | ||
| 19 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 20 | import org.semanticweb.owlapi.model.OWLObjectVisitor; | ||
| 21 | import org.semanticweb.owlapi.model.OWLObjectVisitorEx; | ||
| 22 | |||
| 23 | class VariableConstant implements OWLLiteral { | ||
| 24 | |||
| 25 | /** | ||
| 26 | * | ||
| 27 | */ | ||
| 28 | private static final long serialVersionUID = 5089014375729171030L; | ||
| 29 | Variable var; | ||
| 30 | |||
| 31 | public VariableConstant(Variable v) { | ||
| 32 | var = v; | ||
| 33 | } | ||
| 34 | |||
| 35 | @Override | ||
| 36 | public Set<OWLEntity> getSignature() { | ||
| 37 | // TODO Auto-generated method stub | ||
| 38 | return null; | ||
| 39 | } | ||
| 40 | |||
| 41 | @Override | ||
| 42 | public Set<OWLAnonymousIndividual> getAnonymousIndividuals() { | ||
| 43 | // TODO Auto-generated method stub | ||
| 44 | return null; | ||
| 45 | } | ||
| 46 | |||
| 47 | @Override | ||
| 48 | public Set<OWLClass> getClassesInSignature() { | ||
| 49 | // TODO Auto-generated method stub | ||
| 50 | return null; | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | public Set<OWLDataProperty> getDataPropertiesInSignature() { | ||
| 55 | // TODO Auto-generated method stub | ||
| 56 | return null; | ||
| 57 | } | ||
| 58 | |||
| 59 | @Override | ||
| 60 | public Set<OWLObjectProperty> getObjectPropertiesInSignature() { | ||
| 61 | // TODO Auto-generated method stub | ||
| 62 | return null; | ||
| 63 | } | ||
| 64 | |||
| 65 | @Override | ||
| 66 | public Set<OWLNamedIndividual> getIndividualsInSignature() { | ||
| 67 | // TODO Auto-generated method stub | ||
| 68 | return null; | ||
| 69 | } | ||
| 70 | |||
| 71 | @Override | ||
| 72 | public Set<OWLDatatype> getDatatypesInSignature() { | ||
| 73 | // TODO Auto-generated method stub | ||
| 74 | return null; | ||
| 75 | } | ||
| 76 | |||
| 77 | @Override | ||
| 78 | public Set<OWLClassExpression> getNestedClassExpressions() { | ||
| 79 | // TODO Auto-generated method stub | ||
| 80 | return null; | ||
| 81 | } | ||
| 82 | |||
| 83 | @Override | ||
| 84 | public void accept(OWLObjectVisitor visitor) { | ||
| 85 | // TODO Auto-generated method stub | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public <O> O accept(OWLObjectVisitorEx<O> visitor) { | ||
| 91 | // TODO Auto-generated method stub | ||
| 92 | return null; | ||
| 93 | } | ||
| 94 | |||
| 95 | @Override | ||
| 96 | public boolean isTopEntity() { | ||
| 97 | // TODO Auto-generated method stub | ||
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | @Override | ||
| 102 | public boolean isBottomEntity() { | ||
| 103 | // TODO Auto-generated method stub | ||
| 104 | return false; | ||
| 105 | } | ||
| 106 | |||
| 107 | @Override | ||
| 108 | public int compareTo(OWLObject arg0) { | ||
| 109 | // TODO Auto-generated method stub | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | @Override | ||
| 114 | public boolean containsEntityInSignature(OWLEntity owlEntity) { | ||
| 115 | // TODO Auto-generated method stub | ||
| 116 | return false; | ||
| 117 | } | ||
| 118 | |||
| 119 | @Override | ||
| 120 | public void accept(OWLAnnotationValueVisitor visitor) { | ||
| 121 | // TODO Auto-generated method stub | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | @Override | ||
| 126 | public <O> O accept(OWLAnnotationValueVisitorEx<O> visitor) { | ||
| 127 | // TODO Auto-generated method stub | ||
| 128 | return null; | ||
| 129 | } | ||
| 130 | |||
| 131 | @Override | ||
| 132 | public boolean isRDFPlainLiteral() { | ||
| 133 | // TODO Auto-generated method stub | ||
| 134 | return false; | ||
| 135 | } | ||
| 136 | |||
| 137 | @Override | ||
| 138 | public String getLiteral() { | ||
| 139 | // TODO Auto-generated method stub | ||
| 140 | return null; | ||
| 141 | } | ||
| 142 | |||
| 143 | @Override | ||
| 144 | public OWLDatatype getDatatype() { | ||
| 145 | // TODO Auto-generated method stub | ||
| 146 | return null; | ||
| 147 | } | ||
| 148 | |||
| 149 | @Override | ||
| 150 | public boolean hasLang() { | ||
| 151 | // TODO Auto-generated method stub | ||
| 152 | return false; | ||
| 153 | } | ||
| 154 | |||
| 155 | @Override | ||
| 156 | public String getLang() { | ||
| 157 | // TODO Auto-generated method stub | ||
| 158 | return null; | ||
| 159 | } | ||
| 160 | |||
| 161 | @Override | ||
| 162 | public boolean hasLang(String lang) { | ||
| 163 | // TODO Auto-generated method stub | ||
| 164 | return false; | ||
| 165 | } | ||
| 166 | |||
| 167 | @Override | ||
| 168 | public boolean isInteger() { | ||
| 169 | // TODO Auto-generated method stub | ||
| 170 | return false; | ||
| 171 | } | ||
| 172 | |||
| 173 | @Override | ||
| 174 | public int parseInteger() throws NumberFormatException { | ||
| 175 | // TODO Auto-generated method stub | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | @Override | ||
| 180 | public boolean isBoolean() { | ||
| 181 | // TODO Auto-generated method stub | ||
| 182 | return false; | ||
| 183 | } | ||
| 184 | |||
| 185 | @Override | ||
| 186 | public boolean parseBoolean() throws NumberFormatException { | ||
| 187 | // TODO Auto-generated method stub | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | |||
| 191 | @Override | ||
| 192 | public boolean isDouble() { | ||
| 193 | // TODO Auto-generated method stub | ||
| 194 | return false; | ||
| 195 | } | ||
| 196 | |||
| 197 | @Override | ||
| 198 | public double parseDouble() throws NumberFormatException { | ||
| 199 | // TODO Auto-generated method stub | ||
| 200 | return 0; | ||
| 201 | } | ||
| 202 | |||
| 203 | @Override | ||
| 204 | public boolean isFloat() { | ||
| 205 | // TODO Auto-generated method stub | ||
| 206 | return false; | ||
| 207 | } | ||
| 208 | |||
| 209 | @Override | ||
| 210 | public float parseFloat() throws NumberFormatException { | ||
| 211 | // TODO Auto-generated method stub | ||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | @Override | ||
| 216 | public void accept(OWLDataVisitor visitor) { | ||
| 217 | // TODO Auto-generated method stub | ||
| 218 | |||
| 219 | } | ||
| 220 | |||
| 221 | @Override | ||
| 222 | public <O> O accept(OWLDataVisitorEx<O> visitor) { | ||
| 223 | // TODO Auto-generated method stub | ||
| 224 | return null; | ||
| 225 | } | ||
| 226 | |||
| 227 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/query/rollup/VariableIndividual.java b/src/uk/ac/ox/cs/pagoda/query/rollup/VariableIndividual.java new file mode 100644 index 0000000..53288e5 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/query/rollup/VariableIndividual.java | |||
| @@ -0,0 +1,416 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query.rollup; | ||
| 2 | |||
| 3 | import java.util.Map; | ||
| 4 | import java.util.Set; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.Variable; | ||
| 7 | import org.semanticweb.owlapi.model.EntityType; | ||
| 8 | import org.semanticweb.owlapi.model.IRI; | ||
| 9 | import org.semanticweb.owlapi.model.OWLAnnotation; | ||
| 10 | import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; | ||
| 11 | import org.semanticweb.owlapi.model.OWLAnnotationProperty; | ||
| 12 | import org.semanticweb.owlapi.model.OWLAnonymousIndividual; | ||
| 13 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 14 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 15 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 16 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 17 | import org.semanticweb.owlapi.model.OWLDataPropertyExpression; | ||
| 18 | import org.semanticweb.owlapi.model.OWLDatatype; | ||
| 19 | import org.semanticweb.owlapi.model.OWLEntity; | ||
| 20 | import org.semanticweb.owlapi.model.OWLEntityVisitor; | ||
| 21 | import org.semanticweb.owlapi.model.OWLEntityVisitorEx; | ||
| 22 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 23 | import org.semanticweb.owlapi.model.OWLIndividualVisitor; | ||
| 24 | import org.semanticweb.owlapi.model.OWLIndividualVisitorEx; | ||
| 25 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 26 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 27 | import org.semanticweb.owlapi.model.OWLNamedObjectVisitor; | ||
| 28 | import org.semanticweb.owlapi.model.OWLObject; | ||
| 29 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 30 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 31 | import org.semanticweb.owlapi.model.OWLObjectVisitor; | ||
| 32 | import org.semanticweb.owlapi.model.OWLObjectVisitorEx; | ||
| 33 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 34 | |||
| 35 | class VariableIndividual implements OWLNamedIndividual { | ||
| 36 | |||
| 37 | /** | ||
| 38 | * | ||
| 39 | */ | ||
| 40 | private static final long serialVersionUID = 3002966246639516395L; | ||
| 41 | Variable var; | ||
| 42 | |||
| 43 | public VariableIndividual(Variable v) { | ||
| 44 | var = v; | ||
| 45 | } | ||
| 46 | |||
| 47 | @Override | ||
| 48 | public boolean isNamed() { | ||
| 49 | // TODO Auto-generated method stub | ||
| 50 | return false; | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | public boolean isAnonymous() { | ||
| 55 | // TODO Auto-generated method stub | ||
| 56 | return false; | ||
| 57 | } | ||
| 58 | |||
| 59 | @Override | ||
| 60 | public OWLNamedIndividual asOWLNamedIndividual() { | ||
| 61 | // TODO Auto-generated method stub | ||
| 62 | return null; | ||
| 63 | } | ||
| 64 | |||
| 65 | @Override | ||
| 66 | public OWLAnonymousIndividual asOWLAnonymousIndividual() { | ||
| 67 | // TODO Auto-generated method stub | ||
| 68 | return null; | ||
| 69 | } | ||
| 70 | |||
| 71 | @Override | ||
| 72 | public Set<OWLClassExpression> getTypes(OWLOntology ontology) { | ||
| 73 | // TODO Auto-generated method stub | ||
| 74 | return null; | ||
| 75 | } | ||
| 76 | |||
| 77 | @Override | ||
| 78 | public Set<OWLClassExpression> getTypes(Set<OWLOntology> ontologies) { | ||
| 79 | // TODO Auto-generated method stub | ||
| 80 | return null; | ||
| 81 | } | ||
| 82 | |||
| 83 | @Override | ||
| 84 | public Map<OWLObjectPropertyExpression, Set<OWLIndividual>> getObjectPropertyValues( | ||
| 85 | OWLOntology ontology) { | ||
| 86 | // TODO Auto-generated method stub | ||
| 87 | return null; | ||
| 88 | } | ||
| 89 | |||
| 90 | @Override | ||
| 91 | public Set<OWLIndividual> getObjectPropertyValues( | ||
| 92 | OWLObjectPropertyExpression property, OWLOntology ontology) { | ||
| 93 | // TODO Auto-generated method stub | ||
| 94 | return null; | ||
| 95 | } | ||
| 96 | |||
| 97 | @Override | ||
| 98 | public boolean hasObjectPropertyValue(OWLObjectPropertyExpression property, | ||
| 99 | OWLIndividual individual, OWLOntology ontology) { | ||
| 100 | // TODO Auto-generated method stub | ||
| 101 | return false; | ||
| 102 | } | ||
| 103 | |||
| 104 | @Override | ||
| 105 | public boolean hasDataPropertyValue(OWLDataPropertyExpression property, | ||
| 106 | OWLLiteral value, OWLOntology ontology) { | ||
| 107 | // TODO Auto-generated method stub | ||
| 108 | return false; | ||
| 109 | } | ||
| 110 | |||
| 111 | @Override | ||
| 112 | public boolean hasNegativeObjectPropertyValue( | ||
| 113 | OWLObjectPropertyExpression property, OWLIndividual individual, | ||
| 114 | OWLOntology ontology) { | ||
| 115 | // TODO Auto-generated method stub | ||
| 116 | return false; | ||
| 117 | } | ||
| 118 | |||
| 119 | @Override | ||
| 120 | public Map<OWLObjectPropertyExpression, Set<OWLIndividual>> getNegativeObjectPropertyValues( | ||
| 121 | OWLOntology ontology) { | ||
| 122 | // TODO Auto-generated method stub | ||
| 123 | return null; | ||
| 124 | } | ||
| 125 | |||
| 126 | @Override | ||
| 127 | public Map<OWLDataPropertyExpression, Set<OWLLiteral>> getDataPropertyValues( | ||
| 128 | OWLOntology ontology) { | ||
| 129 | // TODO Auto-generated method stub | ||
| 130 | return null; | ||
| 131 | } | ||
| 132 | |||
| 133 | @Override | ||
| 134 | public Set<OWLLiteral> getDataPropertyValues( | ||
| 135 | OWLDataPropertyExpression property, OWLOntology ontology) { | ||
| 136 | // TODO Auto-generated method stub | ||
| 137 | return null; | ||
| 138 | } | ||
| 139 | |||
| 140 | @Override | ||
| 141 | public Map<OWLDataPropertyExpression, Set<OWLLiteral>> getNegativeDataPropertyValues( | ||
| 142 | OWLOntology ontology) { | ||
| 143 | // TODO Auto-generated method stub | ||
| 144 | return null; | ||
| 145 | } | ||
| 146 | |||
| 147 | @Override | ||
| 148 | public boolean hasNegativeDataPropertyValue( | ||
| 149 | OWLDataPropertyExpression property, OWLLiteral literal, | ||
| 150 | OWLOntology ontology) { | ||
| 151 | // TODO Auto-generated method stub | ||
| 152 | return false; | ||
| 153 | } | ||
| 154 | |||
| 155 | @Override | ||
| 156 | public Set<OWLIndividual> getSameIndividuals(OWLOntology ontology) { | ||
| 157 | // TODO Auto-generated method stub | ||
| 158 | return null; | ||
| 159 | } | ||
| 160 | |||
| 161 | @Override | ||
| 162 | public Set<OWLIndividual> getDifferentIndividuals(OWLOntology ontology) { | ||
| 163 | // TODO Auto-generated method stub | ||
| 164 | return null; | ||
| 165 | } | ||
| 166 | |||
| 167 | @Override | ||
| 168 | public String toStringID() { | ||
| 169 | // TODO Auto-generated method stub | ||
| 170 | return null; | ||
| 171 | } | ||
| 172 | |||
| 173 | @Override | ||
| 174 | public void accept(OWLIndividualVisitor visitor) { | ||
| 175 | // TODO Auto-generated method stub | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | @Override | ||
| 180 | public <O> O accept(OWLIndividualVisitorEx<O> visitor) { | ||
| 181 | // TODO Auto-generated method stub | ||
| 182 | return null; | ||
| 183 | } | ||
| 184 | |||
| 185 | @Override | ||
| 186 | public Set<OWLEntity> getSignature() { | ||
| 187 | // TODO Auto-generated method stub | ||
| 188 | return null; | ||
| 189 | } | ||
| 190 | |||
| 191 | @Override | ||
| 192 | public Set<OWLAnonymousIndividual> getAnonymousIndividuals() { | ||
| 193 | // TODO Auto-generated method stub | ||
| 194 | return null; | ||
| 195 | } | ||
| 196 | |||
| 197 | @Override | ||
| 198 | public Set<OWLClass> getClassesInSignature() { | ||
| 199 | // TODO Auto-generated method stub | ||
| 200 | return null; | ||
| 201 | } | ||
| 202 | |||
| 203 | @Override | ||
| 204 | public Set<OWLDataProperty> getDataPropertiesInSignature() { | ||
| 205 | // TODO Auto-generated method stub | ||
| 206 | return null; | ||
| 207 | } | ||
| 208 | |||
| 209 | @Override | ||
| 210 | public Set<OWLObjectProperty> getObjectPropertiesInSignature() { | ||
| 211 | // TODO Auto-generated method stub | ||
| 212 | return null; | ||
| 213 | } | ||
| 214 | |||
| 215 | @Override | ||
| 216 | public Set<OWLNamedIndividual> getIndividualsInSignature() { | ||
| 217 | // TODO Auto-generated method stub | ||
| 218 | return null; | ||
| 219 | } | ||
| 220 | |||
| 221 | @Override | ||
| 222 | public Set<OWLDatatype> getDatatypesInSignature() { | ||
| 223 | // TODO Auto-generated method stub | ||
| 224 | return null; | ||
| 225 | } | ||
| 226 | |||
| 227 | @Override | ||
| 228 | public Set<OWLClassExpression> getNestedClassExpressions() { | ||
| 229 | // TODO Auto-generated method stub | ||
| 230 | return null; | ||
| 231 | } | ||
| 232 | |||
| 233 | @Override | ||
| 234 | public void accept(OWLObjectVisitor visitor) { | ||
| 235 | // TODO Auto-generated method stub | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | @Override | ||
| 240 | public <O> O accept(OWLObjectVisitorEx<O> visitor) { | ||
| 241 | // TODO Auto-generated method stub | ||
| 242 | return null; | ||
| 243 | } | ||
| 244 | |||
| 245 | @Override | ||
| 246 | public boolean isTopEntity() { | ||
| 247 | // TODO Auto-generated method stub | ||
| 248 | return false; | ||
| 249 | } | ||
| 250 | |||
| 251 | @Override | ||
| 252 | public boolean isBottomEntity() { | ||
| 253 | // TODO Auto-generated method stub | ||
| 254 | return false; | ||
| 255 | } | ||
| 256 | |||
| 257 | @Override | ||
| 258 | public int compareTo(OWLObject arg0) { | ||
| 259 | // TODO Auto-generated method stub | ||
| 260 | return 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | @Override | ||
| 264 | public boolean containsEntityInSignature(OWLEntity owlEntity) { | ||
| 265 | // TODO Auto-generated method stub | ||
| 266 | return false; | ||
| 267 | } | ||
| 268 | |||
| 269 | @Override | ||
| 270 | public EntityType<?> getEntityType() { | ||
| 271 | // TODO Auto-generated method stub | ||
| 272 | return null; | ||
| 273 | } | ||
| 274 | |||
| 275 | @Override | ||
| 276 | public <E extends OWLEntity> E getOWLEntity(EntityType<E> entityType) { | ||
| 277 | // TODO Auto-generated method stub | ||
| 278 | return null; | ||
| 279 | } | ||
| 280 | |||
| 281 | @Override | ||
| 282 | public boolean isType(EntityType<?> entityType) { | ||
| 283 | // TODO Auto-generated method stub | ||
| 284 | return false; | ||
| 285 | } | ||
| 286 | |||
| 287 | @Override | ||
| 288 | public Set<OWLAnnotation> getAnnotations(OWLOntology ontology) { | ||
| 289 | // TODO Auto-generated method stub | ||
| 290 | return null; | ||
| 291 | } | ||
| 292 | |||
| 293 | @Override | ||
| 294 | public Set<OWLAnnotation> getAnnotations(OWLOntology ontology, | ||
| 295 | OWLAnnotationProperty annotationProperty) { | ||
| 296 | // TODO Auto-generated method stub | ||
| 297 | return null; | ||
| 298 | } | ||
| 299 | |||
| 300 | @Override | ||
| 301 | public Set<OWLAnnotationAssertionAxiom> getAnnotationAssertionAxioms( | ||
| 302 | OWLOntology ontology) { | ||
| 303 | // TODO Auto-generated method stub | ||
| 304 | return null; | ||
| 305 | } | ||
| 306 | |||
| 307 | @Override | ||
| 308 | public boolean isBuiltIn() { | ||
| 309 | // TODO Auto-generated method stub | ||
| 310 | return false; | ||
| 311 | } | ||
| 312 | |||
| 313 | @Override | ||
| 314 | public boolean isOWLClass() { | ||
| 315 | // TODO Auto-generated method stub | ||
| 316 | return false; | ||
| 317 | } | ||
| 318 | |||
| 319 | @Override | ||
| 320 | public OWLClass asOWLClass() { | ||
| 321 | // TODO Auto-generated method stub | ||
| 322 | return null; | ||
| 323 | } | ||
| 324 | |||
| 325 | @Override | ||
| 326 | public boolean isOWLObjectProperty() { | ||
| 327 | // TODO Auto-generated method stub | ||
| 328 | return false; | ||
| 329 | } | ||
| 330 | |||
| 331 | @Override | ||
| 332 | public OWLObjectProperty asOWLObjectProperty() { | ||
| 333 | // TODO Auto-generated method stub | ||
| 334 | return null; | ||
| 335 | } | ||
| 336 | |||
| 337 | @Override | ||
| 338 | public boolean isOWLDataProperty() { | ||
| 339 | // TODO Auto-generated method stub | ||
| 340 | return false; | ||
| 341 | } | ||
| 342 | |||
| 343 | @Override | ||
| 344 | public OWLDataProperty asOWLDataProperty() { | ||
| 345 | // TODO Auto-generated method stub | ||
| 346 | return null; | ||
| 347 | } | ||
| 348 | |||
| 349 | @Override | ||
| 350 | public boolean isOWLNamedIndividual() { | ||
| 351 | // TODO Auto-generated method stub | ||
| 352 | return false; | ||
| 353 | } | ||
| 354 | |||
| 355 | @Override | ||
| 356 | public boolean isOWLDatatype() { | ||
| 357 | // TODO Auto-generated method stub | ||
| 358 | return false; | ||
| 359 | } | ||
| 360 | |||
| 361 | @Override | ||
| 362 | public OWLDatatype asOWLDatatype() { | ||
| 363 | // TODO Auto-generated method stub | ||
| 364 | return null; | ||
| 365 | } | ||
| 366 | |||
| 367 | @Override | ||
| 368 | public boolean isOWLAnnotationProperty() { | ||
| 369 | // TODO Auto-generated method stub | ||
| 370 | return false; | ||
| 371 | } | ||
| 372 | |||
| 373 | @Override | ||
| 374 | public OWLAnnotationProperty asOWLAnnotationProperty() { | ||
| 375 | // TODO Auto-generated method stub | ||
| 376 | return null; | ||
| 377 | } | ||
| 378 | |||
| 379 | @Override | ||
| 380 | public Set<OWLAxiom> getReferencingAxioms(OWLOntology ontology) { | ||
| 381 | // TODO Auto-generated method stub | ||
| 382 | return null; | ||
| 383 | } | ||
| 384 | |||
| 385 | @Override | ||
| 386 | public Set<OWLAxiom> getReferencingAxioms(OWLOntology ontology, | ||
| 387 | boolean includeImports) { | ||
| 388 | // TODO Auto-generated method stub | ||
| 389 | return null; | ||
| 390 | } | ||
| 391 | |||
| 392 | @Override | ||
| 393 | public void accept(OWLEntityVisitor visitor) { | ||
| 394 | // TODO Auto-generated method stub | ||
| 395 | |||
| 396 | } | ||
| 397 | |||
| 398 | @Override | ||
| 399 | public <O> O accept(OWLEntityVisitorEx<O> visitor) { | ||
| 400 | // TODO Auto-generated method stub | ||
| 401 | return null; | ||
| 402 | } | ||
| 403 | |||
| 404 | @Override | ||
| 405 | public IRI getIRI() { | ||
| 406 | // TODO Auto-generated method stub | ||
| 407 | return null; | ||
| 408 | } | ||
| 409 | |||
| 410 | @Override | ||
| 411 | public void accept(OWLNamedObjectVisitor visitor) { | ||
| 412 | // TODO Auto-generated method stub | ||
| 413 | |||
| 414 | } | ||
| 415 | |||
| 416 | } | ||
