diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/tracking')
11 files changed, 2403 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/AnswerTuplesWriter.java b/src/uk/ac/ox/cs/pagoda/tracking/AnswerTuplesWriter.java new file mode 100644 index 0000000..a83a4e9 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/AnswerTuplesWriter.java | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 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 | |||
| 10 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 11 | |||
| 12 | public class AnswerTuplesWriter { | ||
| 13 | |||
| 14 | private static final String QUERY_SEPARATOR = "---------------------------------------------"; | ||
| 15 | |||
| 16 | BufferedWriter writer = null; | ||
| 17 | |||
| 18 | public AnswerTuplesWriter(String fileName) { | ||
| 19 | try { | ||
| 20 | writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))); | ||
| 21 | } catch (FileNotFoundException e) { | ||
| 22 | e.printStackTrace(); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | public void write(String[] answerVariables, Collection<String> answerTuples)throws IOException { | ||
| 27 | for (int i = 0; i < answerVariables.length; ++i) | ||
| 28 | writer.write(answerVariables[i] + "\t"); | ||
| 29 | writer.newLine(); | ||
| 30 | writer.write(QUERY_SEPARATOR); | ||
| 31 | writer.newLine(); | ||
| 32 | |||
| 33 | if (answerTuples == null) { | ||
| 34 | writer.newLine(); | ||
| 35 | return ; | ||
| 36 | } | ||
| 37 | |||
| 38 | for (String answerTuple: answerTuples) { | ||
| 39 | writer.write(answerTuple); | ||
| 40 | writer.newLine(); | ||
| 41 | } | ||
| 42 | writer.newLine(); | ||
| 43 | } | ||
| 44 | |||
| 45 | public void close() { | ||
| 46 | if (writer != null) | ||
| 47 | try { | ||
| 48 | writer.close(); | ||
| 49 | } catch (IOException e) { | ||
| 50 | e.printStackTrace(); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | public void write(String[] answerVariables, AnswerTuples answerTuples) { | ||
| 55 | // TODO Auto-generated method stub | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/BottomFragmentManager.java b/src/uk/ac/ox/cs/pagoda/tracking/BottomFragmentManager.java new file mode 100644 index 0000000..09915e2 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/BottomFragmentManager.java | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.HashSet; | ||
| 4 | import java.util.Set; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 7 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 8 | import org.semanticweb.HermiT.model.DLClause; | ||
| 9 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 10 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 12 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 13 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 14 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 15 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 16 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 17 | |||
| 18 | import uk.ac.ox.cs.pagoda.constraints.PredicateDependency; | ||
| 19 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 20 | import uk.ac.ox.cs.pagoda.rules.ApproxProgram; | ||
| 21 | import uk.ac.ox.cs.pagoda.rules.GeneralProgram; | ||
| 22 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 23 | |||
| 24 | public class BottomFragmentManager { | ||
| 25 | |||
| 26 | QueryRecord m_record; | ||
| 27 | GeneralProgram m_program; | ||
| 28 | PredicateDependency m_graph; | ||
| 29 | |||
| 30 | public BottomFragmentManager(QueryRecord record) { | ||
| 31 | m_record = record; | ||
| 32 | m_program = new GeneralProgram(record.getRelevantClauses(), record.getRelevantOntology()); | ||
| 33 | m_graph = m_program.buildDependencyGraph(); | ||
| 34 | } | ||
| 35 | |||
| 36 | public Set<DLClause> relevantClauses(Set<DLClause> disjuntiveRules) { | ||
| 37 | Set<DLClause> relevant = new HashSet<DLClause>(); | ||
| 38 | Set<DLClause> now = new HashSet<DLClause>(); | ||
| 39 | Set<DLClause> last = new HashSet<DLClause>(); | ||
| 40 | |||
| 41 | for (DLClause rule: disjuntiveRules) | ||
| 42 | for (DLPredicate p: m_graph.collectPredicate(rule.getHeadAtoms())) | ||
| 43 | now.addAll(m_graph.pathToBottom(p)); | ||
| 44 | |||
| 45 | while (!relevant.containsAll(now)) { | ||
| 46 | relevant.addAll(now); | ||
| 47 | last.clear(); | ||
| 48 | last = now; | ||
| 49 | now = new HashSet<DLClause>(); | ||
| 50 | |||
| 51 | for (DLClause rule: last) { | ||
| 52 | for (DLPredicate p: m_graph.collectPredicate(rule.getHeadAtoms())) | ||
| 53 | now.addAll(m_graph.pathToBottom(p)); | ||
| 54 | for (DLPredicate p: m_graph.collectPredicate(rule.getBodyAtoms())) | ||
| 55 | now.addAll(m_graph.pathTo(p)); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | Utility.logDebug("There are " + relevant.size() + " clauses in the bottom fragment related to this query."); | ||
| 60 | return relevant; | ||
| 61 | } | ||
| 62 | |||
| 63 | public Set<OWLAxiom> relevantOntology(Set<DLClause> clauses, ApproxProgram upperProgram) { | ||
| 64 | Set<OWLAxiom> axioms = new HashSet<OWLAxiom>(); | ||
| 65 | Set<DLPredicate> predicates = new HashSet<DLPredicate>(); | ||
| 66 | for (DLClause clause: clauses) { | ||
| 67 | OWLAxiom axiom = upperProgram.getEquivalentAxiom(clause); | ||
| 68 | axioms.add(axiom); | ||
| 69 | predicates.addAll(m_graph.collectPredicate(clause.getHeadAtoms())); | ||
| 70 | predicates.addAll(m_graph.collectPredicate(clause.getBodyAtoms())); | ||
| 71 | } | ||
| 72 | |||
| 73 | int tboxCounter = axioms.size(); | ||
| 74 | Utility.logDebug("There are " + tboxCounter + " TBox axioms in the bottom fragment related to this query."); | ||
| 75 | String name; | ||
| 76 | for (OWLAxiom axiom: m_record.getRelevantOntology().getABoxAxioms(true)) { | ||
| 77 | if (axiom instanceof OWLClassAssertionAxiom) { | ||
| 78 | OWLClass cls = (OWLClass) ((OWLClassAssertionAxiom) axiom).getClassExpression(); | ||
| 79 | name = cls.getIRI().toString(); | ||
| 80 | if (predicates.contains(AtomicConcept.create(name))) | ||
| 81 | axioms.add(axiom); | ||
| 82 | } | ||
| 83 | else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { | ||
| 84 | OWLObjectProperty prop = (OWLObjectProperty) ((OWLObjectPropertyAssertionAxiom) axiom).getProperty(); | ||
| 85 | name = prop.getIRI().toString(); | ||
| 86 | if (predicates.contains(AtomicRole.create(name))) | ||
| 87 | axioms.add(axiom); | ||
| 88 | } | ||
| 89 | else if (axiom instanceof OWLDataPropertyAssertionAxiom) { | ||
| 90 | OWLDataProperty prop = (OWLDataProperty) ((OWLDataPropertyAssertionAxiom) axiom).getProperty(); | ||
| 91 | name = prop.getIRI().toString(); | ||
| 92 | if (predicates.contains(AtomicRole.create(name))) | ||
| 93 | axioms.add(axiom); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | Utility.logDebug("There are " + (axioms.size() - tboxCounter) + " ABox axioms in the bottom fragment related to this query."); | ||
| 98 | return axioms; | ||
| 99 | } | ||
| 100 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java b/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java new file mode 100644 index 0000000..0e19e30 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java | |||
| @@ -0,0 +1,472 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.HashSet; | ||
| 4 | import java.util.Iterator; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.model.DLClause; | ||
| 9 | import org.semanticweb.owlapi.model.IRI; | ||
| 10 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 12 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 13 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 14 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 15 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 16 | import org.semanticweb.owlapi.model.OWLObject; | ||
| 17 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 18 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 19 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 20 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 21 | |||
| 22 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 23 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 24 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 25 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 26 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 27 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 28 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 29 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 30 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 31 | import uk.ac.ox.cs.pagoda.util.UFS; | ||
| 32 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 33 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 34 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 35 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 36 | import uk.ac.ox.cs.JRDFox.store.Resource; | ||
| 37 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 38 | |||
| 39 | public class QueryTracker { | ||
| 40 | |||
| 41 | QueryRecord m_record; | ||
| 42 | BasicQueryEngine m_dataStore; | ||
| 43 | TrackingRuleEncoder m_encoder; | ||
| 44 | |||
| 45 | OWLOntologyManager m_manager; | ||
| 46 | Set<OWLAxiom> m_tBoxAxioms; | ||
| 47 | |||
| 48 | UFS<String> equalityGroups; | ||
| 49 | |||
| 50 | public QueryTracker(TrackingRuleEncoder encoder, BasicQueryEngine lowerStore, QueryRecord queryRecord) { | ||
| 51 | m_encoder = encoder; | ||
| 52 | m_dataStore = lowerStore; | ||
| 53 | m_record = queryRecord; | ||
| 54 | |||
| 55 | m_manager = m_encoder.getOntology().getOWLOntologyManager(); | ||
| 56 | equalityGroups = m_dataStore.getEqualityGroups(); | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | public OWLOntology extract(BasicQueryEngine trackingStore, QueryRecord[] botQuerRecords, boolean incrementally) { | ||
| 61 | try { | ||
| 62 | if (m_record.getRelevantOntology() != null) { | ||
| 63 | m_manager.removeOntology(m_record.getRelevantOntology()); | ||
| 64 | m_record.setRelevantOntology(null); | ||
| 65 | m_record.clearClauses(); | ||
| 66 | } | ||
| 67 | m_record.setRelevantOntology(m_manager.createOntology()); | ||
| 68 | } catch (OWLOntologyCreationException e) { | ||
| 69 | e.printStackTrace(); | ||
| 70 | } | ||
| 71 | |||
| 72 | m_encoder.setCurrentQuery(m_record); | ||
| 73 | |||
| 74 | // m_encoder.encodingQuery(botQuerRecords); | ||
| 75 | // m_encoder.saveTrackingRules("tracking_query" + m_record.getQueryID() + ".dlog"); | ||
| 76 | |||
| 77 | DataStore store = trackingStore.getDataStore(); | ||
| 78 | long oldTripleCount, tripleCount; | ||
| 79 | try { | ||
| 80 | Timer t1 = new Timer(); | ||
| 81 | oldTripleCount = store.getTriplesCount(); | ||
| 82 | // store.addRules(new String[] {m_encoder.getTrackingProgram()}); | ||
| 83 | store.importRules(m_encoder.getTrackingProgram()); | ||
| 84 | store.applyReasoning(incrementally); | ||
| 85 | tripleCount = store.getTriplesCount(); | ||
| 86 | |||
| 87 | Utility.logDebug("tracking store after materialising tracking program: " | ||
| 88 | + tripleCount | ||
| 89 | + " (" | ||
| 90 | + (tripleCount - oldTripleCount) | ||
| 91 | + " new)"); | ||
| 92 | Utility.logDebug("tracking store finished the materialisation of tracking program in " | ||
| 93 | + t1.duration() + " seconds."); | ||
| 94 | } catch (JRDFStoreException e) { | ||
| 95 | e.printStackTrace(); | ||
| 96 | } | ||
| 97 | |||
| 98 | extractAxioms(trackingStore); | ||
| 99 | |||
| 100 | trackingStore.clearRulesAndIDBFacts(m_encoder.getAddedData()); | ||
| 101 | |||
| 102 | if (!m_record.isBottom() | ||
| 103 | && !(m_encoder instanceof TrackingRuleEncoderDisj2)) | ||
| 104 | addRelatedAxiomsAndClauses(botQuerRecords); | ||
| 105 | |||
| 106 | return m_record.getRelevantOntology(); | ||
| 107 | } | ||
| 108 | |||
| 109 | public void extractAxioms(BasicQueryEngine trackingStore) { | ||
| 110 | Set<String> unaryPredicates = new HashSet<String>(); | ||
| 111 | Set<String> binaryPredicates = new HashSet<String>(); | ||
| 112 | |||
| 113 | getDerivedPredicates(trackingStore, unaryPredicates, binaryPredicates); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * To add all the relavant ABox assertions to the fragment | ||
| 117 | */ | ||
| 118 | |||
| 119 | int aboxAxiomCounter = extractUnaryTuples(trackingStore, | ||
| 120 | m_manager.getOWLDataFactory(), unaryPredicates) | ||
| 121 | + extractBinaryTuples(trackingStore, | ||
| 122 | m_manager.getOWLDataFactory(), binaryPredicates); | ||
| 123 | |||
| 124 | /** | ||
| 125 | * To all all the relavant TBox rules to the fragment | ||
| 126 | */ | ||
| 127 | String queryText = m_encoder.getSelectedSPARQLQuery(); | ||
| 128 | |||
| 129 | DLClause clause; | ||
| 130 | m_tBoxAxioms = new HashSet<OWLAxiom>(); | ||
| 131 | OWLAxiom tBoxAxiom; | ||
| 132 | TupleIterator answers = null; | ||
| 133 | String answer; | ||
| 134 | try { | ||
| 135 | answers = trackingStore.internal_evaluate(queryText); | ||
| 136 | for (long multi = answers.open(); multi != 0; multi = answers.getNext()) { | ||
| 137 | answer = answers.getResource(0).m_lexicalForm; | ||
| 138 | clause = m_encoder.getSelectedClause(MyPrefixes.PAGOdAPrefixes.expandIRI(answer)); | ||
| 139 | if (DLClauseHelper.isTautologyAboutDifferentFrom(clause)) | ||
| 140 | continue; | ||
| 141 | tBoxAxiom = m_encoder.getProgram().getEquivalentAxiom(clause); | ||
| 142 | m_record.addRelevantClauses(clause); | ||
| 143 | m_tBoxAxioms.add(tBoxAxiom); | ||
| 144 | } | ||
| 145 | } catch (JRDFStoreException e) { | ||
| 146 | e.printStackTrace(); | ||
| 147 | } finally { | ||
| 148 | if (answers != null) | ||
| 149 | answers.dispose(); | ||
| 150 | } | ||
| 151 | |||
| 152 | Utility.logTrace("Extracted TBox axioms: "); | ||
| 153 | for (OWLAxiom axiom : m_tBoxAxioms) { | ||
| 154 | Utility.logTrace(axiom); | ||
| 155 | m_manager.addAxiom(m_record.getRelevantOntology(), axiom); | ||
| 156 | } | ||
| 157 | Utility.logDebug("TBox extraction Done"); | ||
| 158 | |||
| 159 | Utility.logDebug("Before adding bottom fragment:\nABoxAxiomsCount = " | ||
| 160 | + aboxAxiomCounter + ", TBoxAxiomsCount = " | ||
| 161 | + m_tBoxAxioms.size()); | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | private int extractBinaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> binaryPredicates) { | ||
| 166 | OWLOntology fragment = m_record.getRelevantOntology(); | ||
| 167 | int count; | ||
| 168 | int aboxAxiomCounter = 0; | ||
| 169 | Resource sub, obj; | ||
| 170 | OWLAxiom aboxAxiom; | ||
| 171 | String trackingIRI; | ||
| 172 | Set<Integer> trackedIDEqualities = new HashSet<Integer>(); | ||
| 173 | Set<String> trackedEntityEqualities = new HashSet<String>(); | ||
| 174 | TupleIterator trackingAnswers, lowerAnswers; | ||
| 175 | |||
| 176 | for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) { | ||
| 177 | trackingIRI = iter.next(); | ||
| 178 | String propIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 179 | if (propIRI == null) continue; | ||
| 180 | if (!propIRI.equals(Namespace.EQUALITY_QUOTED)) continue; | ||
| 181 | trackingAnswers = null; | ||
| 182 | try { | ||
| 183 | trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI)); | ||
| 184 | for (long multi = trackingAnswers.open(); multi != 0; multi = trackingAnswers.getNext()) { | ||
| 185 | if (trackingAnswers.getResourceID(0) != trackingAnswers.getResourceID(1)) { | ||
| 186 | for (int i = 0; i < 2; ++i) | ||
| 187 | if (trackedIDEqualities.add(trackingAnswers.getResourceID(i))) { | ||
| 188 | trackedEntityEqualities.add(trackingAnswers.getResource(i).m_lexicalForm); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } catch (JRDFStoreException e) { | ||
| 193 | // TODO Auto-generated catch block | ||
| 194 | e.printStackTrace(); | ||
| 195 | } finally { | ||
| 196 | if (trackingAnswers != null) trackingAnswers.dispose(); | ||
| 197 | } | ||
| 198 | iter.remove(); | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | |||
| 202 | String sub_rep, obj_rep; | ||
| 203 | |||
| 204 | for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) { | ||
| 205 | trackingIRI = iter.next(); | ||
| 206 | count = 0; | ||
| 207 | String propIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 208 | if (propIRI == null) continue; | ||
| 209 | iter.remove(); | ||
| 210 | lowerAnswers = null; trackingAnswers = null; | ||
| 211 | Set<String> lower = new HashSet<String>(); | ||
| 212 | OWLObject prop = null; | ||
| 213 | try { | ||
| 214 | trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI)); | ||
| 215 | trackingAnswers.open(); | ||
| 216 | if (trackingAnswers.getMultiplicity() == 0) continue; | ||
| 217 | |||
| 218 | lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Binary(propIRI)); | ||
| 219 | lowerAnswers.open(); | ||
| 220 | if (lowerAnswers.getMultiplicity() == 0) continue; | ||
| 221 | |||
| 222 | StringBuilder builder = new StringBuilder(); | ||
| 223 | for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) { | ||
| 224 | sub = lowerAnswers.getResource(0); | ||
| 225 | obj = lowerAnswers.getResource(1); | ||
| 226 | builder.setLength(0); | ||
| 227 | builder.append(equalityGroups.find(sub.m_lexicalForm)).append(AnswerTuple.SEPARATOR).append(equalityGroups.find(obj.m_lexicalForm)); | ||
| 228 | lower.add(builder.toString()); | ||
| 229 | } | ||
| 230 | |||
| 231 | for (long multi = trackingAnswers.getMultiplicity(); multi != 0; multi = trackingAnswers.getNext()) { | ||
| 232 | sub = trackingAnswers.getResource(0); | ||
| 233 | obj = trackingAnswers.getResource(1); | ||
| 234 | builder.setLength(0); | ||
| 235 | sub_rep = equalityGroups.find(sub.m_lexicalForm); | ||
| 236 | obj_rep = equalityGroups.find(obj.m_lexicalForm); | ||
| 237 | if (!sub_rep.equals(sub.m_lexicalForm) || !obj_rep.equals(obj.m_lexicalForm)) continue; | ||
| 238 | |||
| 239 | builder.append(sub_rep).append(AnswerTuple.SEPARATOR).append(obj_rep); | ||
| 240 | if (lower.contains(builder.toString())) { | ||
| 241 | OWLObject owlObj = getOWLObject(obj, factory); | ||
| 242 | if (owlObj instanceof OWLIndividual) { | ||
| 243 | if (prop == null) | ||
| 244 | prop = factory.getOWLObjectProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI)); | ||
| 245 | aboxAxiom = factory.getOWLObjectPropertyAssertionAxiom( | ||
| 246 | (OWLObjectProperty) prop, | ||
| 247 | factory.getOWLNamedIndividual(IRI.create(sub_rep)), | ||
| 248 | factory.getOWLNamedIndividual(IRI.create(obj_rep))); | ||
| 249 | } | ||
| 250 | else if (owlObj instanceof OWLLiteral) { | ||
| 251 | if (prop == null) | ||
| 252 | prop = factory.getOWLDataProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI)); | ||
| 253 | aboxAxiom = factory.getOWLDataPropertyAssertionAxiom( | ||
| 254 | (OWLDataProperty) prop, | ||
| 255 | factory.getOWLNamedIndividual(IRI.create(sub_rep)), | ||
| 256 | (OWLLiteral) owlObj); | ||
| 257 | } | ||
| 258 | else { | ||
| 259 | Utility.logError("There might be an error here ... "); | ||
| 260 | continue; | ||
| 261 | } | ||
| 262 | if (!fragment.containsAxiom(aboxAxiom)) { | ||
| 263 | m_manager.addAxiom(fragment, aboxAxiom); | ||
| 264 | ++aboxAxiomCounter; | ||
| 265 | ++count; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } catch (JRDFStoreException e) { | ||
| 270 | e.printStackTrace(); | ||
| 271 | } finally { | ||
| 272 | if (trackingAnswers != null) trackingAnswers.dispose(); | ||
| 273 | if (lowerAnswers != null) lowerAnswers.dispose(); | ||
| 274 | lower.clear(); | ||
| 275 | } | ||
| 276 | Utility.logDebug("property: " + propIRI + " " + count); | ||
| 277 | } | ||
| 278 | |||
| 279 | count = 0; | ||
| 280 | String value; | ||
| 281 | OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY)); | ||
| 282 | for (String key: equalityGroups.keySet()) { | ||
| 283 | if (!trackedEntityEqualities.contains(key)) continue; | ||
| 284 | value = equalityGroups.find(key); | ||
| 285 | m_manager.addAxiom(fragment, factory.getOWLObjectPropertyAssertionAxiom( | ||
| 286 | sameAs, | ||
| 287 | factory.getOWLNamedIndividual(IRI.create(key)), | ||
| 288 | factory.getOWLNamedIndividual(IRI.create(value)))); | ||
| 289 | ++aboxAxiomCounter; | ||
| 290 | ++count; | ||
| 291 | } | ||
| 292 | Utility.logDebug("property: " + Namespace.EQUALITY_QUOTED + " " + count); | ||
| 293 | |||
| 294 | trackedEntityEqualities.clear(); | ||
| 295 | trackedIDEqualities.clear(); | ||
| 296 | Utility.logTrace(Namespace.EQUALITY_QUOTED + " " + count); | ||
| 297 | |||
| 298 | Utility.logDebug("ABox extraction Done"); | ||
| 299 | return aboxAxiomCounter; | ||
| 300 | } | ||
| 301 | |||
| 302 | private OWLObject getOWLObject(Resource rdfoxTerm, OWLDataFactory factory) { | ||
| 303 | if (rdfoxTerm.m_datatype.equals(Datatype.IRI_REFERENCE)) | ||
| 304 | return factory.getOWLNamedIndividual(IRI.create(rdfoxTerm.m_lexicalForm)); | ||
| 305 | // if (rdfoxTerm.m_datatype.equals(Datatype.OWL_REAL) || rdfoxTerm.m_datatype.equals(Datatype.OWL_RATIONAL) || | ||
| 306 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_DECIMAL) || rdfoxTerm.m_datatype.equals(Datatype.XSD_INTEGER) || | ||
| 307 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_NON_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_POSITIVE_INTEGER) || | ||
| 308 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_LONG) || | ||
| 309 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_SHORT) || | ||
| 310 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_BYTE) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_LONG) || | ||
| 311 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_SHORT) || | ||
| 312 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_BYTE)) | ||
| 313 | if (rdfoxTerm.m_datatype.equals(Datatype.XSD_DATE)) | ||
| 314 | return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(Namespace.XSD_STRING))); | ||
| 315 | |||
| 316 | else return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(rdfoxTerm.m_datatype.getIRI()))); | ||
| 317 | } | ||
| 318 | |||
| 319 | private int extractUnaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> unaryPredicates) { | ||
| 320 | OWLOntology fragment = m_record.getRelevantOntology(); | ||
| 321 | int count; | ||
| 322 | int aboxAxiomCounter = 0; | ||
| 323 | String answer; | ||
| 324 | OWLAxiom aboxAxiom; | ||
| 325 | for (String trackingIRI : unaryPredicates) { | ||
| 326 | count = 0; | ||
| 327 | String clsIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 328 | if (clsIRI == null) | ||
| 329 | continue; | ||
| 330 | TupleIterator answers = null, lowerAnswers = null; | ||
| 331 | Set<String> lower = new HashSet<String>(); | ||
| 332 | OWLClass cls = factory | ||
| 333 | .getOWLClass(IRI.create(clsIRI.startsWith("<") ? OWLHelper | ||
| 334 | .removeAngles(clsIRI) : clsIRI)); | ||
| 335 | try { | ||
| 336 | answers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Unary(trackingIRI)); | ||
| 337 | answers.open(); | ||
| 338 | if (answers.getMultiplicity() == 0) continue; | ||
| 339 | |||
| 340 | lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Unary(clsIRI)); | ||
| 341 | lowerAnswers.open(); | ||
| 342 | if (lowerAnswers.getMultiplicity() == 0) continue; | ||
| 343 | |||
| 344 | for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) | ||
| 345 | lower.add(equalityGroups.find(lowerAnswers.getResource(0).m_lexicalForm)); | ||
| 346 | |||
| 347 | for (long multi = answers.getMultiplicity(); multi != 0; multi = answers.getNext()) { | ||
| 348 | answer = equalityGroups.find(answers.getResource(0).m_lexicalForm); | ||
| 349 | if (lower.contains(answer)) { | ||
| 350 | OWLIndividual instance = factory.getOWLNamedIndividual(IRI.create(answer)); | ||
| 351 | aboxAxiom = factory.getOWLClassAssertionAxiom(cls,instance); | ||
| 352 | if (!fragment.containsAxiom(aboxAxiom)) { | ||
| 353 | m_manager.addAxiom(fragment, aboxAxiom); | ||
| 354 | ++aboxAxiomCounter; | ||
| 355 | ++count; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } catch (JRDFStoreException e) { | ||
| 360 | e.printStackTrace(); | ||
| 361 | } finally { | ||
| 362 | if (answers != null) | ||
| 363 | answers.dispose(); | ||
| 364 | if (lowerAnswers != null) | ||
| 365 | lowerAnswers.dispose(); | ||
| 366 | lower.clear(); | ||
| 367 | } | ||
| 368 | Utility.logDebug("class: " + clsIRI + " " + count); | ||
| 369 | } | ||
| 370 | return aboxAxiomCounter; | ||
| 371 | } | ||
| 372 | |||
| 373 | private void getDerivedPredicates(BasicQueryEngine trackingStore, | ||
| 374 | Set<String> unaryPredicates, Set<String> binaryPredicates) { | ||
| 375 | |||
| 376 | TupleIterator derivedTuples = null; | ||
| 377 | String selectedPredicate = OWLHelper.addAngles(m_encoder.getSelectedPredicate()); | ||
| 378 | try { | ||
| 379 | derivedTuples = trackingStore | ||
| 380 | .internal_evaluateAgainstIDBs("select distinct ?z where { ?x <" | ||
| 381 | + Namespace.RDF_TYPE + "> ?z . }"); | ||
| 382 | for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) { | ||
| 383 | String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0)); | ||
| 384 | if (p.equals(selectedPredicate)) | ||
| 385 | ; | ||
| 386 | else if (m_encoder.isAuxPredicate(p)) | ||
| 387 | ; | ||
| 388 | else | ||
| 389 | unaryPredicates.add(p); | ||
| 390 | } | ||
| 391 | } catch (JRDFStoreException e) { | ||
| 392 | e.printStackTrace(); | ||
| 393 | } finally { | ||
| 394 | if (derivedTuples != null) | ||
| 395 | derivedTuples.dispose(); | ||
| 396 | } | ||
| 397 | |||
| 398 | derivedTuples = null; | ||
| 399 | try { | ||
| 400 | derivedTuples = trackingStore | ||
| 401 | .internal_evaluateAgainstIDBs("select distinct ?y where { ?x ?y ?z . }"); | ||
| 402 | for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) { | ||
| 403 | String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0)); | ||
| 404 | if (p.equals(Namespace.RDF_TYPE_ABBR) | ||
| 405 | || p.equals(Namespace.RDF_TYPE_QUOTED)) | ||
| 406 | ; | ||
| 407 | else if (p.equals(Namespace.EQUALITY_ABBR) | ||
| 408 | || p.equals(Namespace.EQUALITY_QUOTED)) | ||
| 409 | ; | ||
| 410 | else if (m_encoder.isAuxPredicate(p)) | ||
| 411 | ; | ||
| 412 | else | ||
| 413 | binaryPredicates.add(p); | ||
| 414 | } | ||
| 415 | } catch (JRDFStoreException e) { | ||
| 416 | e.printStackTrace(); | ||
| 417 | } finally { | ||
| 418 | if (derivedTuples != null) | ||
| 419 | derivedTuples.dispose(); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | public void addRelatedAxiomsAndClauses(QueryRecord[] botQueryRecords) { | ||
| 424 | LinkedList<QueryRecord> toAddedRecords = new LinkedList<QueryRecord>(); | ||
| 425 | |||
| 426 | for (QueryRecord botQueryRecord : botQueryRecords) | ||
| 427 | if (overlappingDisjunctiveClauses(botQueryRecord) != null) | ||
| 428 | toAddedRecords.add(botQueryRecord); | ||
| 429 | |||
| 430 | for (QueryRecord botQueryRecord : toAddedRecords) { | ||
| 431 | m_manager.addAxioms(m_record.getRelevantOntology(), botQueryRecord | ||
| 432 | .getRelevantOntology().getAxioms()); | ||
| 433 | for (DLClause clause : botQueryRecord.getRelevantClauses()) | ||
| 434 | m_record.addRelevantClauses(clause); | ||
| 435 | } | ||
| 436 | |||
| 437 | if (!toAddedRecords.isEmpty()) | ||
| 438 | Utility.logDebug("Part of bottom fragments is added for this query."); | ||
| 439 | else | ||
| 440 | Utility.logDebug("None of bottom fragments is added for this query."); | ||
| 441 | } | ||
| 442 | |||
| 443 | private Set<DLClause> overlappingDisjunctiveClauses( | ||
| 444 | QueryRecord botQueryRecord) { | ||
| 445 | if (m_tBoxAxioms == null) | ||
| 446 | return null; | ||
| 447 | |||
| 448 | Set<DLClause> disjunctiveRules = new HashSet<DLClause>(); | ||
| 449 | Set<DLClause> clauses = m_record.getRelevantClauses(); | ||
| 450 | for (DLClause clause : botQueryRecord.getRelevantClauses()) | ||
| 451 | if (clause.getHeadLength() > 1 && clauses.contains(clause)) | ||
| 452 | disjunctiveRules.add(clause); | ||
| 453 | |||
| 454 | return disjunctiveRules.isEmpty() ? null : disjunctiveRules; | ||
| 455 | } | ||
| 456 | |||
| 457 | private String getSPARQLQuery4Unary(String p) { | ||
| 458 | StringBuilder builder = new StringBuilder(); | ||
| 459 | builder.append("select ?x where { ?x <") | ||
| 460 | .append(Namespace.RDF_TYPE).append("> "); | ||
| 461 | builder.append(p).append(" . }"); | ||
| 462 | return builder.toString(); | ||
| 463 | } | ||
| 464 | |||
| 465 | private String getSPARQLQuery4Binary(String p) { | ||
| 466 | StringBuilder builder = new StringBuilder(); | ||
| 467 | builder.append("select ?x ?y where { ?x ").append(p) | ||
| 468 | .append(" ?y . }"); | ||
| 469 | return builder.toString(); | ||
| 470 | } | ||
| 471 | |||
| 472 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java new file mode 100644 index 0000000..3a01e19 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java | |||
| @@ -0,0 +1,385 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 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 | import java.util.HashMap; | ||
| 9 | import java.util.HashSet; | ||
| 10 | import java.util.LinkedList; | ||
| 11 | import java.util.Map; | ||
| 12 | import java.util.Set; | ||
| 13 | |||
| 14 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 15 | import org.semanticweb.HermiT.model.Atom; | ||
| 16 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 17 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 18 | import org.semanticweb.HermiT.model.Constant; | ||
| 19 | import org.semanticweb.HermiT.model.DLClause; | ||
| 20 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 21 | import org.semanticweb.HermiT.model.DatatypeRestriction; | ||
| 22 | import org.semanticweb.HermiT.model.Equality; | ||
| 23 | import org.semanticweb.HermiT.model.Individual; | ||
| 24 | import org.semanticweb.HermiT.model.Inequality; | ||
| 25 | import org.semanticweb.HermiT.model.Variable; | ||
| 26 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 27 | |||
| 28 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 29 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 30 | import uk.ac.ox.cs.JRDFox.model.Literal; | ||
| 31 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 32 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 33 | import uk.ac.ox.cs.pagoda.query.*; | ||
| 34 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 35 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 36 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 37 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 38 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 39 | |||
| 40 | public abstract class TrackingRuleEncoder { | ||
| 41 | UpperDatalogProgram program; | ||
| 42 | Collection<DLClause> trackingClauses = new HashSet<DLClause>(); | ||
| 43 | Collection<DLClause> queryClauses = new LinkedList<DLClause>(); | ||
| 44 | |||
| 45 | Map<Integer, DLClause> index2clause = new HashMap<Integer, DLClause>(); | ||
| 46 | Map<DLClause, Integer> clause2index = new HashMap<DLClause, Integer>(); | ||
| 47 | |||
| 48 | String equalityRelatedRuleText = null; | ||
| 49 | protected BasicQueryEngine store; | ||
| 50 | |||
| 51 | public TrackingRuleEncoder(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 52 | this.program = program; | ||
| 53 | this.store = store; | ||
| 54 | } | ||
| 55 | |||
| 56 | protected abstract String getEqualityRelatedRuleText(); | ||
| 57 | |||
| 58 | boolean ruleEncoded = false; | ||
| 59 | |||
| 60 | public boolean encodingRules() { | ||
| 61 | if (ruleEncoded) return false; | ||
| 62 | ruleEncoded = true; | ||
| 63 | |||
| 64 | // for (DLClause clause: program.getClauses(currentQuery.getClause())) { | ||
| 65 | for (DLClause clause: program.getClauses()) { | ||
| 66 | encodingRule(clause); | ||
| 67 | } | ||
| 68 | return true; | ||
| 69 | } | ||
| 70 | |||
| 71 | protected String getIRI(String name) { | ||
| 72 | return program.getOntology().getOntologyID().getOntologyIRI().toString() + "#" + name; | ||
| 73 | } | ||
| 74 | |||
| 75 | protected abstract void encodingRule(DLClause clause); | ||
| 76 | |||
| 77 | protected Individual getIndividual4GeneralRule(DLClause clause) { | ||
| 78 | clause = program.getCorrespondingClause(clause); | ||
| 79 | // if (clause == null) | ||
| 80 | // return Individual.create(getIRI("_r0")); | ||
| 81 | |||
| 82 | int index; | ||
| 83 | if (clause2index.containsKey(clause)) | ||
| 84 | index = clause2index.get(clause); | ||
| 85 | else { | ||
| 86 | index = clause2index.size() + 1; | ||
| 87 | index2clause.put(index, clause); | ||
| 88 | clause2index.put(clause, index); | ||
| 89 | } | ||
| 90 | |||
| 91 | return Individual.create(getIRI("_r" + index)); | ||
| 92 | } | ||
| 93 | |||
| 94 | private boolean queryEncoded = false; | ||
| 95 | private LinkedList<int[]> addedData = new LinkedList<int[]>(); | ||
| 96 | |||
| 97 | public Collection<int[]> getAddedData() { | ||
| 98 | return addedData; | ||
| 99 | } | ||
| 100 | |||
| 101 | private void encodingQuery(QueryRecord[] botQuerRecords) { | ||
| 102 | if (queryEncoded) return ; | ||
| 103 | queryEncoded = true; | ||
| 104 | |||
| 105 | if (currentQuery.getArity() > 0 && currentQuery.getArity() < 3) { | ||
| 106 | encodingAtomicQuery(botQuerRecords); | ||
| 107 | |||
| 108 | } else { | ||
| 109 | DLClause queryClause = currentQuery.getClause(); | ||
| 110 | Atom[] bodyAtoms = queryClause.getBodyAtoms(); | ||
| 111 | for (Atom bodyAtom: bodyAtoms) | ||
| 112 | addQueryRule(bodyAtom, bodyAtoms); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | private void addQueryRule(Atom atom, Atom[] atoms) { | ||
| 117 | DLClause newClause; | ||
| 118 | Atom headAtom; | ||
| 119 | |||
| 120 | headAtom = Atom.create( | ||
| 121 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 122 | DLClauseHelper.getArguments(atom)); | ||
| 123 | newClause = DLClause.create(new Atom[] {headAtom}, atoms); | ||
| 124 | queryClauses.add(newClause); | ||
| 125 | } | ||
| 126 | |||
| 127 | public static final String trackingPredicateRelation = Namespace.PAGODA_AUX + "isTrackingPredicateFor"; | ||
| 128 | |||
| 129 | public static final String QueryPredicate = Namespace.PAGODA_AUX + "Query"; | ||
| 130 | |||
| 131 | protected String getCurrentQueryPredicate() { | ||
| 132 | return QueryPredicate + currentQuery.getQueryID(); | ||
| 133 | } | ||
| 134 | |||
| 135 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) { | ||
| 136 | encodingAtomicQuery(botQuerRecords, false); | ||
| 137 | } | ||
| 138 | |||
| 139 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords, boolean includingBottom) { | ||
| 140 | DLClause queryClause = currentQuery.getClause(); | ||
| 141 | AnswerTuples answerTuples = currentQuery.getGapAnswers(); | ||
| 142 | String[] answerVariables = currentQuery.getAnswerVariables(); | ||
| 143 | |||
| 144 | String currentQueryPredicate = getCurrentQueryPredicate(); | ||
| 145 | Atom newAtom; | ||
| 146 | if (answerVariables.length == 1) { | ||
| 147 | AtomicConcept queryConcept = AtomicConcept.create(currentQueryPredicate); | ||
| 148 | newAtom = Atom.create(queryConcept, Variable.create(answerVariables[0])); | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | AtomicRole queryRole = AtomicRole.create(currentQueryPredicate); | ||
| 152 | newAtom = Atom.create(queryRole, Variable.create(answerVariables[0]), Variable.create(answerVariables[1])); | ||
| 153 | } | ||
| 154 | |||
| 155 | Atom[] bodyAtoms = queryClause.getBodyAtoms(); | ||
| 156 | Atom[] newBodyAtoms = new Atom[queryClause.getBodyLength() + 1]; | ||
| 157 | for (int i = 0; i < bodyAtoms.length; ++i) | ||
| 158 | newBodyAtoms[i + 1] = bodyAtoms[i]; | ||
| 159 | newBodyAtoms[0] = newAtom; | ||
| 160 | |||
| 161 | for (Atom bodyAtom: bodyAtoms) | ||
| 162 | addQueryRule(bodyAtom, newBodyAtoms); | ||
| 163 | |||
| 164 | RDFoxTripleManager tripleManager = new RDFoxTripleManager(store.getDataStore(), true); | ||
| 165 | // MyPrefixes prefixes = MyPrefixes.PAGOdAPrefixes; | ||
| 166 | int[] triple; | ||
| 167 | int predicate = tripleManager.getResourceID(AtomicConcept.create(currentQueryPredicate)); | ||
| 168 | int rdftype = tripleManager.getResourceID(AtomicRole.create(Namespace.RDF_TYPE)); | ||
| 169 | if (answerVariables.length == 1) { | ||
| 170 | for (AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 171 | answer = answerTuples.getTuple(); | ||
| 172 | triple = new int[] { tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), rdftype, predicate }; | ||
| 173 | addedData.add(triple); | ||
| 174 | tripleManager.addTripleByID(triple); | ||
| 175 | // System.out.println("To be removed ... \n" + tripleManager.getRawTerm(tripleManager.getResourceID(prefixes.expandIRI(answer.getRawTerm(0)))) + " " + tripleManager.getRawTerm(rdftype) + " " + tripleManager.getRawTerm(predicate)); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | for (AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 180 | answer = answerTuples.getTuple(); | ||
| 181 | triple = new int[] { tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), predicate, tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(1))) }; | ||
| 182 | addedData.add(triple); | ||
| 183 | tripleManager.addTripleByID(triple); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | answerTuples.dispose(); | ||
| 187 | |||
| 188 | if (includingBottom && botQuerRecords != null) { | ||
| 189 | int index = 0; | ||
| 190 | GroundTerm t; | ||
| 191 | String raw; | ||
| 192 | for (QueryRecord botQueryRecord: botQuerRecords) { | ||
| 193 | answerTuples = botQueryRecord.getGapAnswers(); | ||
| 194 | int subID = 0;//botQueryRecord.getSubID(); | ||
| 195 | String p = subID == 0 ? AtomicConcept.NOTHING.getIRI() : Namespace.OWL_NS + "Nothing_final" + (++index); | ||
| 196 | predicate = tripleManager.getResourceID(AtomicConcept.create(p = getTrackingPredicate(p))); | ||
| 197 | for (AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 198 | answer = answerTuples.getTuple(); | ||
| 199 | // System.out.println("To be removed ... " + answer.getRawTerm(0)); | ||
| 200 | raw = ((t = answer.getGroundTerm(0)) instanceof uk.ac.ox.cs.JRDFox.model.Individual) ? ((uk.ac.ox.cs.JRDFox.model.Individual) t).getIRI() : t.toString(); | ||
| 201 | triple = new int[] { tripleManager.getResourceID(raw), rdftype, predicate }; | ||
| 202 | addedData.add(triple); | ||
| 203 | tripleManager.addTripleByID(triple); | ||
| 204 | } | ||
| 205 | answerTuples.dispose(); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | Utility.logInfo(addedData.size() + " triples are added into the store."); | ||
| 210 | } | ||
| 211 | |||
| 212 | public static String getRawTerm(GroundTerm r) { | ||
| 213 | if (r instanceof uk.ac.ox.cs.JRDFox.model.Individual) | ||
| 214 | return ((uk.ac.ox.cs.JRDFox.model.Individual) r).getIRI(); | ||
| 215 | else { | ||
| 216 | Literal l = (Literal) r; | ||
| 217 | if (l.getDatatype().equals(Datatype.XSD_STRING) && l.getDatatype().equals(Datatype.RDF_PLAIN_LITERAL)) | ||
| 218 | return "\"" + l.getLexicalForm() + "\""; | ||
| 219 | else | ||
| 220 | return "\"" + l.getLexicalForm() + "\"^^<" + l.getDatatype().getIRI() + ">"; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | protected DLPredicate getGapDLPredicate(DLPredicate dlPredicate) { | ||
| 225 | return getDLPredicate(dlPredicate, GapTupleIterator.gapPredicateSuffix); | ||
| 226 | } | ||
| 227 | |||
| 228 | DLPredicate getDLPredicate(DLPredicate p, String suffix) { | ||
| 229 | if (p instanceof AtomicConcept) | ||
| 230 | return AtomicConcept.create(((AtomicConcept) p).getIRI() + suffix); | ||
| 231 | else if (p instanceof DatatypeRestriction) { | ||
| 232 | DatatypeRestriction restriction = (DatatypeRestriction) p; | ||
| 233 | String newURI = restriction.getDatatypeURI() + suffix; | ||
| 234 | return getDatatypeRestriction(restriction, newURI); | ||
| 235 | } | ||
| 236 | else if (p instanceof AtomicRole) | ||
| 237 | return AtomicRole.create(((AtomicRole) p).getIRI() + suffix); | ||
| 238 | else if (p instanceof AnnotatedEquality || p instanceof Equality) | ||
| 239 | return AtomicRole.create(Namespace.EQUALITY + suffix); | ||
| 240 | else if (p instanceof Inequality) | ||
| 241 | return AtomicRole.create(Namespace.INEQUALITY + suffix); | ||
| 242 | else { | ||
| 243 | Utility.logDebug("strange DL predicate appeared ... " + p, | ||
| 244 | "the program paused here in TrackingRuleEncoderDisj.java"); | ||
| 245 | return null; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | protected DLPredicate getTrackingDLPredicate(DLPredicate dlPredicate) { | ||
| 250 | return getDLPredicate(dlPredicate, getTrackingSuffix(currentQuery.getQueryID())); | ||
| 251 | } | ||
| 252 | |||
| 253 | protected static String getTrackingSuffix(String queryID) { | ||
| 254 | return "_AUXt" + queryID; | ||
| 255 | } | ||
| 256 | |||
| 257 | public String getTrackingPredicate(String predicateIRI) { | ||
| 258 | if (predicateIRI.startsWith("<")) | ||
| 259 | return predicateIRI.replace(">", getTrackingSuffix(currentQuery.getQueryID()) + ">"); | ||
| 260 | else | ||
| 261 | return predicateIRI + getTrackingSuffix(currentQuery.getQueryID()); | ||
| 262 | } | ||
| 263 | |||
| 264 | protected DLPredicate getDatatypeRestriction(DatatypeRestriction restriction, String newName) { | ||
| 265 | int length = restriction.getNumberOfFacetRestrictions(); | ||
| 266 | String[] facets = new String[length]; | ||
| 267 | Constant[] values = new Constant[length]; | ||
| 268 | for (int i = 0; i < length; ++i) { | ||
| 269 | facets[i] = restriction.getFacetURI(i); | ||
| 270 | values[i] = restriction.getFacetValue(i); | ||
| 271 | } | ||
| 272 | return DatatypeRestriction.create(newName, facets, values); | ||
| 273 | } | ||
| 274 | |||
| 275 | protected QueryRecord currentQuery; | ||
| 276 | DLPredicate selected; | ||
| 277 | |||
| 278 | public void setCurrentQuery(QueryRecord record) { | ||
| 279 | deprecateTrackingAndQueryRules(); | ||
| 280 | currentQuery = record; | ||
| 281 | selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 282 | trackingSuffix = "_AUXt" + currentQuery.getQueryID(); | ||
| 283 | } | ||
| 284 | |||
| 285 | public void dispose() { | ||
| 286 | deprecateTrackingAndQueryRules(); | ||
| 287 | } | ||
| 288 | |||
| 289 | private String getTrackingRuleText() { | ||
| 290 | return DLClauseHelper.toString(trackingClauses); | ||
| 291 | } | ||
| 292 | |||
| 293 | private String getQueryRuleText() { | ||
| 294 | return DLClauseHelper.toString(queryClauses); | ||
| 295 | } | ||
| 296 | |||
| 297 | public String getTrackingProgram() { | ||
| 298 | StringBuilder sb = getTrackingProgramBody(); | ||
| 299 | sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText()); | ||
| 300 | return sb.toString(); | ||
| 301 | } | ||
| 302 | |||
| 303 | protected StringBuilder getTrackingProgramBody() { | ||
| 304 | encodingRules(); | ||
| 305 | encodingQuery(new QueryRecord[0]); | ||
| 306 | |||
| 307 | StringBuilder sb = new StringBuilder(); | ||
| 308 | sb.append(getTrackingRuleText()); | ||
| 309 | sb.append(getEqualityRelatedRuleText()); | ||
| 310 | sb.append(getQueryRuleText()); | ||
| 311 | return sb; | ||
| 312 | } | ||
| 313 | |||
| 314 | public void saveTrackingRules(String fileName) { | ||
| 315 | BufferedWriter writer = null; | ||
| 316 | try { | ||
| 317 | writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))); | ||
| 318 | writer.write(getTrackingProgram()); | ||
| 319 | writer.close(); | ||
| 320 | } catch (IOException e) { | ||
| 321 | e.printStackTrace(); | ||
| 322 | return ; | ||
| 323 | } | ||
| 324 | Utility.logDebug("The tracking rules are saved in " + fileName + "."); | ||
| 325 | } | ||
| 326 | |||
| 327 | private void deprecateTrackingAndQueryRules() { | ||
| 328 | trackingClauses.clear(); | ||
| 329 | queryClauses.clear(); | ||
| 330 | addedData.clear(); | ||
| 331 | ruleEncoded = false; | ||
| 332 | queryEncoded = false; | ||
| 333 | } | ||
| 334 | |||
| 335 | public String getSelectedPredicate() { | ||
| 336 | return getIRI("_selected" + currentQuery.getQueryID()); | ||
| 337 | } | ||
| 338 | |||
| 339 | public DLClause getSelectedClause(String iri) { | ||
| 340 | int index = iri.lastIndexOf("_r") + 2; | ||
| 341 | int ruleIndex = Integer.parseInt(iri.substring(index)); | ||
| 342 | return index2clause.get(ruleIndex); | ||
| 343 | } | ||
| 344 | |||
| 345 | /** | ||
| 346 | * SELECT ?X | ||
| 347 | * WHERE { | ||
| 348 | * ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#:type> :_selected? | ||
| 349 | * } | ||
| 350 | */ | ||
| 351 | public String getSelectedSPARQLQuery() { | ||
| 352 | StringBuilder builder = new StringBuilder(); | ||
| 353 | builder.append("SELECT ?X\nWHERE {\n?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "); | ||
| 354 | builder.append(selected.toString()).append("\n}"); | ||
| 355 | return builder.toString(); | ||
| 356 | } | ||
| 357 | |||
| 358 | public OWLOntology getOntology() { | ||
| 359 | return program.getOntology(); | ||
| 360 | } | ||
| 361 | |||
| 362 | public UpperDatalogProgram getProgram() { | ||
| 363 | return program; | ||
| 364 | } | ||
| 365 | |||
| 366 | private String trackingSuffix; | ||
| 367 | |||
| 368 | public String getOriginalPredicate(String p) { | ||
| 369 | if (p.startsWith("<")) { | ||
| 370 | if (!p.endsWith(trackingSuffix + ">")) return null; | ||
| 371 | } | ||
| 372 | else | ||
| 373 | if (!p.endsWith(trackingSuffix)) return null; | ||
| 374 | |||
| 375 | return p.replace(trackingSuffix, ""); | ||
| 376 | } | ||
| 377 | |||
| 378 | public boolean isAuxPredicate(String p) { | ||
| 379 | return false; | ||
| 380 | } | ||
| 381 | |||
| 382 | protected Set<String> unaryPredicates = new HashSet<String>(); | ||
| 383 | protected Set<String> binaryPredicates = new HashSet<String>(); | ||
| 384 | |||
| 385 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj.java new file mode 100644 index 0000000..cee829f --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj.java | |||
| @@ -0,0 +1,178 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.Collections; | ||
| 5 | import java.util.HashMap; | ||
| 6 | import java.util.HashSet; | ||
| 7 | import java.util.Iterator; | ||
| 8 | import java.util.LinkedList; | ||
| 9 | import java.util.Map; | ||
| 10 | |||
| 11 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 12 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 13 | import org.semanticweb.HermiT.model.Atom; | ||
| 14 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 15 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 16 | import org.semanticweb.HermiT.model.DLClause; | ||
| 17 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 18 | import org.semanticweb.HermiT.model.Equality; | ||
| 19 | import org.semanticweb.HermiT.model.Individual; | ||
| 20 | import org.semanticweb.HermiT.model.Inequality; | ||
| 21 | |||
| 22 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 23 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 24 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 25 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 26 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 27 | |||
| 28 | public abstract class TrackingRuleEncoderDisj extends TrackingRuleEncoderWithGap { | ||
| 29 | |||
| 30 | public TrackingRuleEncoderDisj(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 31 | super(program, store); | ||
| 32 | } | ||
| 33 | |||
| 34 | protected Map<DLClause, Collection<DLClause>> disjunctiveRules = new HashMap<DLClause, Collection<DLClause>>(); | ||
| 35 | |||
| 36 | /** | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | protected void processDisjunctiveRules() { | ||
| 40 | Map<Atom, Collection<DLClause>> auxiliaryAtoms = new HashMap<Atom, Collection<DLClause>>(); | ||
| 41 | Map<Individual, Collection<DLClause>> skolemisedAtoms = new HashMap<Individual, Collection<DLClause>>(); | ||
| 42 | |||
| 43 | for (Map.Entry<DLClause, Collection<DLClause>> entry: disjunctiveRules.entrySet()) { | ||
| 44 | DLClause original = entry.getKey(); | ||
| 45 | Collection<DLClause> overClauses = entry.getValue(); | ||
| 46 | |||
| 47 | int index = 0; | ||
| 48 | for (Iterator<DLClause> iter = overClauses.iterator(); iter.hasNext();) { | ||
| 49 | DLClause subClause = iter.next(); | ||
| 50 | if (DLClauseHelper.hasSubsetBodyAtoms(subClause, original)) { | ||
| 51 | Atom headAtom = subClause.getHeadAtom(0); | ||
| 52 | if ((index = OverApproxExist.indexOfSkolemisedIndividual(headAtom)) != -1) { | ||
| 53 | Individual i = (Individual) headAtom.getArgument(index); | ||
| 54 | Collection<DLClause> clauses = skolemisedAtoms.get(i); | ||
| 55 | if (clauses == null) { | ||
| 56 | clauses = new HashSet<DLClause>(); | ||
| 57 | skolemisedAtoms.put(i, clauses); | ||
| 58 | } | ||
| 59 | clauses.add(subClause); | ||
| 60 | } | ||
| 61 | else | ||
| 62 | auxiliaryAtoms.put(getAuxiliaryAtom(original, subClause.getHeadAtom(0)), Collections.singleton(subClause)); | ||
| 63 | } | ||
| 64 | else | ||
| 65 | super.encodingRule(subClause); | ||
| 66 | } | ||
| 67 | |||
| 68 | for (Atom headAtom: original.getHeadAtoms()) | ||
| 69 | if (headAtom.getDLPredicate() instanceof AtLeastConcept) { | ||
| 70 | AtLeastConcept alc = (AtLeastConcept) headAtom.getDLPredicate(); | ||
| 71 | Collection<DLClause> clauses = new HashSet<DLClause>(); | ||
| 72 | Individual[] individuals = new Individual[alc.getNumber()]; | ||
| 73 | for (int i = 0; i < alc.getNumber(); ++i) { | ||
| 74 | individuals[i] = OverApproxExist.getNewIndividual(original, i); | ||
| 75 | clauses.addAll(skolemisedAtoms.get(individuals[i])); | ||
| 76 | } | ||
| 77 | auxiliaryAtoms.put(getAuxiliaryAtom(original, headAtom, individuals), clauses); | ||
| 78 | } | ||
| 79 | |||
| 80 | index = 0; | ||
| 81 | Atom[] auxAtoms = auxiliaryAtoms.keySet().toArray(new Atom[0]); | ||
| 82 | for (Atom atom: auxAtoms) { | ||
| 83 | for (DLClause subClause: auxiliaryAtoms.get(atom)) | ||
| 84 | encodingDisjunctiveRule(subClause, index, auxAtoms); | ||
| 85 | index++; | ||
| 86 | } | ||
| 87 | |||
| 88 | auxiliaryAtoms.clear(); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | private Atom getAuxiliaryAtom(DLClause original, Atom headAtom, Individual... individuals) { | ||
| 93 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 94 | if (p instanceof AtLeastConcept) { | ||
| 95 | // AtLeastConcept alc = (AtLeastConcept) p; | ||
| 96 | // Individual[] individuals = new Individual[alc.getNumber()]; | ||
| 97 | // for (int i = 0; i < alc.getNumber(); ++i) | ||
| 98 | // individuals[i] = OverApproxExist.getNewIndividual(original, i); | ||
| 99 | return Atom.create(generateAuxiliaryRule((AtLeastConcept) p, original, individuals), headAtom.getArgument(0)); | ||
| 100 | } | ||
| 101 | if (p instanceof AtomicConcept) | ||
| 102 | return Atom.create(generateAuxiliaryRule((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 103 | if (p instanceof AtomicRole) | ||
| 104 | return Atom.create(generateAuxiliaryRule((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 105 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 106 | return Atom.create(generateAuxiliaryRule(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 107 | if (p instanceof Inequality) | ||
| 108 | return Atom.create(generateAuxiliaryRule((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 109 | |||
| 110 | return null; | ||
| 111 | } | ||
| 112 | |||
| 113 | private void encodingDisjunctiveRule(DLClause clause, int index, Atom[] auxAtoms) { | ||
| 114 | int validHeadLength = auxAtoms.length; | ||
| 115 | Atom headAtom = clause.getHeadAtom(0); | ||
| 116 | Atom[] bodyAtoms = clause.getBodyAtoms(); | ||
| 117 | |||
| 118 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 119 | DLPredicate selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 120 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 121 | |||
| 122 | for (Atom atom: bodyAtoms) { | ||
| 123 | Atom newAtom = Atom.create( | ||
| 124 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 125 | DLClauseHelper.getArguments(atom)); | ||
| 126 | newHeadAtoms.add(newAtom); | ||
| 127 | } | ||
| 128 | |||
| 129 | DLClause newClause; | ||
| 130 | Atom[] newBodyAtoms = new Atom[bodyAtoms.length + validHeadLength + 1]; | ||
| 131 | |||
| 132 | newBodyAtoms[0] = Atom.create( | ||
| 133 | getTrackingDLPredicate(headAtom.getDLPredicate()), | ||
| 134 | DLClauseHelper.getArguments(headAtom)); | ||
| 135 | |||
| 136 | newBodyAtoms[1] = Atom.create( | ||
| 137 | getGapDLPredicate(headAtom.getDLPredicate()), | ||
| 138 | DLClauseHelper.getArguments(headAtom)); | ||
| 139 | |||
| 140 | for (int i = 0; i < validHeadLength; ++i) | ||
| 141 | if (i != index) | ||
| 142 | newBodyAtoms[i + (i < index ? 2 : 1)] = auxAtoms[i]; | ||
| 143 | |||
| 144 | for (int i = 0; i < bodyAtoms.length; ++i) | ||
| 145 | newBodyAtoms[i + validHeadLength + 1] = bodyAtoms[i]; | ||
| 146 | |||
| 147 | for (Atom atom: newHeadAtoms) { | ||
| 148 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 149 | addTrackingClause(newClause); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | protected void addTrackingClause(DLClause clause) { | ||
| 154 | trackingClauses.add(clause); | ||
| 155 | } | ||
| 156 | |||
| 157 | protected void addDisjunctiveRule(DLClause key, DLClause clause) { | ||
| 158 | Collection<DLClause> value = disjunctiveRules.get(key); | ||
| 159 | if (value == null) { | ||
| 160 | value = new LinkedList<DLClause>(); | ||
| 161 | disjunctiveRules.put(key, value); | ||
| 162 | } | ||
| 163 | value.add(clause); | ||
| 164 | } | ||
| 165 | |||
| 166 | protected abstract DLPredicate generateAuxiliaryRule(AtLeastConcept p, DLClause original, Individual[] individuals); | ||
| 167 | |||
| 168 | protected abstract DLPredicate generateAuxiliaryRule(AtomicRole p); | ||
| 169 | |||
| 170 | protected abstract DLPredicate generateAuxiliaryRule(AtomicConcept p); | ||
| 171 | |||
| 172 | protected DLPredicate generateAuxiliaryRule(Equality instance) { | ||
| 173 | return generateAuxiliaryRule(AtomicRole.create(Namespace.EQUALITY)); | ||
| 174 | } | ||
| 175 | |||
| 176 | protected abstract DLPredicate generateAuxiliaryRule(Inequality instance); | ||
| 177 | |||
| 178 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj1.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj1.java new file mode 100644 index 0000000..e72ed96 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj1.java | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.LinkedList; | ||
| 4 | |||
| 5 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 6 | import org.semanticweb.HermiT.model.Atom; | ||
| 7 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 8 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 9 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 10 | import org.semanticweb.HermiT.model.DLClause; | ||
| 11 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 12 | import org.semanticweb.HermiT.model.Individual; | ||
| 13 | import org.semanticweb.HermiT.model.Inequality; | ||
| 14 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 15 | import org.semanticweb.HermiT.model.Term; | ||
| 16 | import org.semanticweb.HermiT.model.Variable; | ||
| 17 | |||
| 18 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 19 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 20 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 21 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 22 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 23 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 24 | |||
| 25 | public class TrackingRuleEncoderDisj1 extends TrackingRuleEncoderDisj { | ||
| 26 | |||
| 27 | public TrackingRuleEncoderDisj1(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 28 | super(program, store); | ||
| 29 | } | ||
| 30 | |||
| 31 | @Override | ||
| 32 | public boolean encodingRules() { | ||
| 33 | if (super.encodingRules()) { | ||
| 34 | processDisjunctiveRules(); | ||
| 35 | return true; | ||
| 36 | } | ||
| 37 | return false; | ||
| 38 | } | ||
| 39 | |||
| 40 | @Override | ||
| 41 | protected void encodingRule(DLClause clause) { | ||
| 42 | if (currentQuery.isBottom()) { | ||
| 43 | super.encodingRule(clause); | ||
| 44 | return ; | ||
| 45 | } | ||
| 46 | |||
| 47 | DLClause original = program.getCorrespondingClause(clause); | ||
| 48 | if (original.getHeadLength() <= 1) { | ||
| 49 | super.encodingRule(clause); | ||
| 50 | } | ||
| 51 | else addDisjunctiveRule(original, clause); | ||
| 52 | } | ||
| 53 | |||
| 54 | private DLPredicate getAuxPredicate(DLPredicate p, Individual... individuals) { | ||
| 55 | if (p instanceof AtLeastConcept) { | ||
| 56 | StringBuilder builder = new StringBuilder( | ||
| 57 | Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p, individuals)); | ||
| 58 | builder.append("_AUXa").append(currentQuery.getQueryID()); | ||
| 59 | return AtomicConcept.create(builder.toString()); | ||
| 60 | } | ||
| 61 | |||
| 62 | return getDLPredicate(p, "_AUXa" + currentQuery.getQueryID()); | ||
| 63 | } | ||
| 64 | |||
| 65 | private DLPredicate getTrackingBottomDLPredicate(DLPredicate p) { | ||
| 66 | return getDLPredicate(p, getTrackingSuffix("0")); | ||
| 67 | } | ||
| 68 | |||
| 69 | protected DLPredicate generateAuxiliaryRule(AtLeastConcept p, DLClause original, Individual[] individuals) { | ||
| 70 | DLPredicate ret = getAuxPredicate(p, individuals); | ||
| 71 | Atom[] headAtom = new Atom[] {Atom.create(ret, X)}; | ||
| 72 | |||
| 73 | AtomicRole role = p.getOnRole() instanceof AtomicRole ? | ||
| 74 | (AtomicRole) p.getOnRole(): | ||
| 75 | ((InverseRole) p.getOnRole()).getInverseOf(); | ||
| 76 | |||
| 77 | AtomicConcept concept = p.getToConcept() instanceof AtomicConcept ? | ||
| 78 | (AtomicConcept) p.getToConcept() : | ||
| 79 | OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept()); | ||
| 80 | |||
| 81 | Term[] roleArgs, conceptArg; | ||
| 82 | for (Individual i: individuals) { | ||
| 83 | // Variable i = Variable.create("Y"); | ||
| 84 | if (p.getOnRole() instanceof AtomicRole) { | ||
| 85 | roleArgs = new Term[] {X, i}; | ||
| 86 | conceptArg = new Term[] {i}; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | roleArgs = new Term[] {i, X}; | ||
| 90 | conceptArg = new Term[] {i}; | ||
| 91 | } | ||
| 92 | |||
| 93 | addTrackingClause( | ||
| 94 | DLClause.create(headAtom, | ||
| 95 | new Atom[] {Atom.create(getTrackingDLPredicate(role), roleArgs)})); | ||
| 96 | |||
| 97 | addTrackingClause( | ||
| 98 | DLClause.create(headAtom, | ||
| 99 | new Atom[] {Atom.create(getTrackingBottomDLPredicate(role), roleArgs)})); | ||
| 100 | |||
| 101 | Atom guard = Atom.create(role, roleArgs); | ||
| 102 | |||
| 103 | if (!concept.equals(AtomicConcept.THING)) { | ||
| 104 | addTrackingClause( | ||
| 105 | DLClause.create(headAtom, | ||
| 106 | new Atom[] {guard, Atom.create(getTrackingDLPredicate(concept), conceptArg)})); | ||
| 107 | |||
| 108 | addTrackingClause( | ||
| 109 | DLClause.create(headAtom, | ||
| 110 | new Atom[] {guard, Atom.create(getTrackingBottomDLPredicate(concept), conceptArg)})); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | return ret; | ||
| 115 | } | ||
| 116 | |||
| 117 | protected DLPredicate generateAuxiliaryRule(AtomicRole p) { | ||
| 118 | DLPredicate ret = getAuxPredicate(p); | ||
| 119 | Atom[] headAtom = new Atom[] {Atom.create(ret, X, Y)}; | ||
| 120 | |||
| 121 | addTrackingClause( | ||
| 122 | DLClause.create(headAtom, new Atom[] {Atom.create(getTrackingDLPredicate(p), X, Y)})); | ||
| 123 | addTrackingClause( | ||
| 124 | DLClause.create(headAtom, new Atom[] {Atom.create(getTrackingBottomDLPredicate(p), X, Y)})); | ||
| 125 | |||
| 126 | return ret; | ||
| 127 | } | ||
| 128 | |||
| 129 | private Variable X = Variable.create("X"), Y = Variable.create("Y"); | ||
| 130 | |||
| 131 | protected DLPredicate generateAuxiliaryRule(AtomicConcept p) { | ||
| 132 | DLPredicate ret = getAuxPredicate(p); | ||
| 133 | Atom[] headAtom = new Atom[] {Atom.create(ret, X)}; | ||
| 134 | addTrackingClause( | ||
| 135 | DLClause.create(headAtom, | ||
| 136 | new Atom[] { Atom.create(getTrackingDLPredicate(p), X)})); | ||
| 137 | addTrackingClause( | ||
| 138 | DLClause.create(headAtom, | ||
| 139 | new Atom[] { Atom.create(getTrackingBottomDLPredicate(p), X)})); | ||
| 140 | |||
| 141 | return ret; | ||
| 142 | } | ||
| 143 | |||
| 144 | protected DLPredicate generateAuxiliaryRule(Inequality instance) { | ||
| 145 | // TODO: | ||
| 146 | return null; | ||
| 147 | } | ||
| 148 | |||
| 149 | @Override | ||
| 150 | public boolean isAuxPredicate(String iri) { | ||
| 151 | return iri.contains("_AUXa"); | ||
| 152 | // if (iri.startsWith("<")) | ||
| 153 | // return iri.endsWith("_AUXa" + currentQuery.getQueryID() + ">"); | ||
| 154 | // return iri.endsWith("_AUXa" + currentQuery.getQueryID()); | ||
| 155 | } | ||
| 156 | |||
| 157 | @Override | ||
| 158 | public String getTrackingProgram() { | ||
| 159 | StringBuilder sb = getTrackingProgramBody(); | ||
| 160 | if (currentQuery.isBottom()) | ||
| 161 | sb.append(getBottomTrackingProgram()); | ||
| 162 | sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText()); | ||
| 163 | return sb.toString(); | ||
| 164 | } | ||
| 165 | |||
| 166 | private String bottomTrackingProgram = null; | ||
| 167 | |||
| 168 | private String getBottomTrackingProgram() { | ||
| 169 | if (bottomTrackingProgram != null) return bottomTrackingProgram.replace("_tn", getTrackingPredicate("")); | ||
| 170 | |||
| 171 | String bottomSuffix = getTrackingSuffix("0"); | ||
| 172 | LinkedList<DLClause> clauses = new LinkedList<DLClause>(); | ||
| 173 | Variable X = Variable.create("X"); | ||
| 174 | for (String concept: unaryPredicates) | ||
| 175 | clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicConcept.create(concept + bottomSuffix) , X)}, | ||
| 176 | new Atom[] {Atom.create(AtomicConcept.create(concept + "_tn"), X)})); | ||
| 177 | Variable Y = Variable.create("Y"); | ||
| 178 | for (String role: binaryPredicates) | ||
| 179 | clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicRole.create(role + bottomSuffix) , X, Y)}, | ||
| 180 | new Atom[] {Atom.create(AtomicRole.create(role + "_tn"), X, Y) })); | ||
| 181 | |||
| 182 | StringBuilder builder = new StringBuilder(DLClauseHelper.toString(clauses)); | ||
| 183 | bottomTrackingProgram = builder.toString(); | ||
| 184 | return bottomTrackingProgram.replace("_tn", getTrackingPredicate("")); | ||
| 185 | } | ||
| 186 | |||
| 187 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj2.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj2.java new file mode 100644 index 0000000..6cf239f --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisj2.java | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 4 | import org.semanticweb.HermiT.model.Atom; | ||
| 5 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 6 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 7 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 8 | import org.semanticweb.HermiT.model.DLClause; | ||
| 9 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 10 | import org.semanticweb.HermiT.model.Individual; | ||
| 11 | import org.semanticweb.HermiT.model.Inequality; | ||
| 12 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 13 | import org.semanticweb.HermiT.model.Term; | ||
| 14 | import org.semanticweb.HermiT.model.Variable; | ||
| 15 | |||
| 16 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 17 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 18 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 19 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 20 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 21 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 22 | |||
| 23 | public class TrackingRuleEncoderDisj2 extends TrackingRuleEncoderDisj { | ||
| 24 | |||
| 25 | public TrackingRuleEncoderDisj2(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 26 | super(program, store); | ||
| 27 | } | ||
| 28 | |||
| 29 | @Override | ||
| 30 | public boolean encodingRules() { | ||
| 31 | if (ruleEncoded) return false; | ||
| 32 | ruleEncoded = true; | ||
| 33 | |||
| 34 | for (DLClause clause: program.getClauses()) { | ||
| 35 | encodingRule(clause); | ||
| 36 | } | ||
| 37 | |||
| 38 | if (disjunctiveRules.isEmpty()) | ||
| 39 | return true; | ||
| 40 | |||
| 41 | processDisjunctiveRules(); | ||
| 42 | return false; | ||
| 43 | } | ||
| 44 | |||
| 45 | @Override | ||
| 46 | protected DLPredicate generateAuxiliaryRule(AtomicConcept p) { | ||
| 47 | return getTrackingDLPredicate(p); | ||
| 48 | } | ||
| 49 | |||
| 50 | @Override | ||
| 51 | protected DLPredicate generateAuxiliaryRule(AtomicRole p) { | ||
| 52 | return getTrackingDLPredicate(p); | ||
| 53 | } | ||
| 54 | |||
| 55 | private Variable X = Variable.create("X"); | ||
| 56 | |||
| 57 | @Override | ||
| 58 | protected DLPredicate generateAuxiliaryRule(AtLeastConcept p, DLClause original, Individual[] individuals) { | ||
| 59 | DLPredicate ret = AtomicConcept.create(getTrackingPredicate(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p, individuals))); | ||
| 60 | Atom[] headAtom = new Atom[] {Atom.create(ret, X)}; | ||
| 61 | |||
| 62 | AtomicRole role = p.getOnRole() instanceof AtomicRole ? | ||
| 63 | (AtomicRole) p.getOnRole(): | ||
| 64 | ((InverseRole) p.getOnRole()).getInverseOf(); | ||
| 65 | |||
| 66 | AtomicConcept concept = p.getToConcept() instanceof AtomicConcept ? | ||
| 67 | (AtomicConcept) p.getToConcept() : | ||
| 68 | OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept()); | ||
| 69 | |||
| 70 | Term[] roleArgs, conceptArg; | ||
| 71 | for (Individual i: individuals) { | ||
| 72 | // Variable i = Variable.create("Y"); | ||
| 73 | if (p.getOnRole() instanceof AtomicRole) { | ||
| 74 | roleArgs = new Term[] {X, i}; | ||
| 75 | conceptArg = new Term[] {i}; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | roleArgs = new Term[] {i, X}; | ||
| 79 | conceptArg = new Term[] {i}; | ||
| 80 | } | ||
| 81 | |||
| 82 | addTrackingClause( | ||
| 83 | DLClause.create(headAtom, | ||
| 84 | new Atom[] {Atom.create(getTrackingDLPredicate(role), roleArgs)})); | ||
| 85 | |||
| 86 | Atom guard = Atom.create(role, roleArgs); | ||
| 87 | |||
| 88 | if (!concept.equals(AtomicConcept.THING)) { | ||
| 89 | addTrackingClause( | ||
| 90 | DLClause.create(headAtom, | ||
| 91 | new Atom[] {guard, Atom.create(getTrackingDLPredicate(concept), conceptArg)})); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | return ret; | ||
| 96 | } | ||
| 97 | |||
| 98 | @Override | ||
| 99 | protected void encodingRule(DLClause clause) { | ||
| 100 | DLClause original = program.getCorrespondingClause(clause); | ||
| 101 | if (original.getHeadLength() <= 1) { | ||
| 102 | super.encodingRule(clause); | ||
| 103 | } | ||
| 104 | else addDisjunctiveRule(original, clause); | ||
| 105 | } | ||
| 106 | |||
| 107 | @Override | ||
| 108 | public String getTrackingProgram() { | ||
| 109 | StringBuilder sb = getTrackingProgramBody(); | ||
| 110 | sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText()); | ||
| 111 | return sb.toString(); | ||
| 112 | } | ||
| 113 | |||
| 114 | @Override | ||
| 115 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) { | ||
| 116 | super.encodingAtomicQuery(botQuerRecords, true); | ||
| 117 | } | ||
| 118 | |||
| 119 | @Override | ||
| 120 | protected DLPredicate generateAuxiliaryRule(Inequality instance) { | ||
| 121 | // TODO Auto-generated method stub | ||
| 122 | return null; | ||
| 123 | } | ||
| 124 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar1.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar1.java new file mode 100644 index 0000000..8998051 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar1.java | |||
| @@ -0,0 +1,436 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 9 | import org.semanticweb.HermiT.model.AtLeast; | ||
| 10 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 11 | import org.semanticweb.HermiT.model.Atom; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 13 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 14 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 15 | import org.semanticweb.HermiT.model.DLClause; | ||
| 16 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 17 | import org.semanticweb.HermiT.model.Equality; | ||
| 18 | import org.semanticweb.HermiT.model.Inequality; | ||
| 19 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 20 | import org.semanticweb.HermiT.model.Variable; | ||
| 21 | |||
| 22 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 23 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 24 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 25 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 26 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 27 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 28 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 29 | |||
| 30 | public class TrackingRuleEncoderDisjVar1 extends TrackingRuleEncoderWithGap { | ||
| 31 | |||
| 32 | public TrackingRuleEncoderDisjVar1(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 33 | super(program, store); | ||
| 34 | } | ||
| 35 | |||
| 36 | private Set<DLClause> disjunctiveRules = new HashSet<DLClause>(); | ||
| 37 | |||
| 38 | @Override | ||
| 39 | public boolean encodingRules() { | ||
| 40 | if (super.encodingRules()) { | ||
| 41 | processDisjunctiveRules(); | ||
| 42 | return true; | ||
| 43 | } | ||
| 44 | return false; | ||
| 45 | } | ||
| 46 | |||
| 47 | @Override | ||
| 48 | protected void encodingRule(DLClause clause) { | ||
| 49 | if (currentQuery.isBottom()) { | ||
| 50 | // super.encodingRule(clause); | ||
| 51 | encodingBottomQueryClause(clause); | ||
| 52 | return ; | ||
| 53 | } | ||
| 54 | |||
| 55 | DLClause original = program.getCorrespondingClause(clause); | ||
| 56 | if (original.getHeadLength() <= 1) { | ||
| 57 | super.encodingRule(clause); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | if (!DLClauseHelper.hasSubsetBodyAtoms(clause, original)) | ||
| 61 | super.encodingRule(clause); | ||
| 62 | addDisjunctiveRule(original); | ||
| 63 | } | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | private void processDisjunctiveRules() { | ||
| 69 | for (DLClause clause: disjunctiveRules) | ||
| 70 | encodingDisjunctiveRule(clause); | ||
| 71 | } | ||
| 72 | |||
| 73 | private Atom getAuxiliaryAtom(Atom headAtom) { | ||
| 74 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 75 | if (p instanceof AtLeast || p instanceof AtLeast) { | ||
| 76 | return Atom.create(generateAuxiliaryRule((AtLeast) p, true), headAtom.getArgument(0)); | ||
| 77 | } | ||
| 78 | if (p instanceof AtomicConcept) | ||
| 79 | return Atom.create(generateAuxiliaryRule((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 80 | if (p instanceof AtomicRole) | ||
| 81 | return Atom.create(generateAuxiliaryRule((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 82 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 83 | return Atom.create(generateAuxiliaryRule(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 84 | if (p instanceof Inequality) | ||
| 85 | return Atom.create(generateAuxiliaryRule((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 86 | |||
| 87 | return null; | ||
| 88 | } | ||
| 89 | |||
| 90 | private Atom getTrackingAtom(Atom headAtom) { | ||
| 91 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 92 | if (p instanceof AtLeast) { | ||
| 93 | p = Normalisation.toAtLeastConcept((AtLeast) p); | ||
| 94 | return Atom.create(getTrackingDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom.getArgument(0)); | ||
| 95 | } | ||
| 96 | if (p instanceof AtomicConcept) | ||
| 97 | return Atom.create(getTrackingDLPredicate((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 98 | if (p instanceof AtomicRole) | ||
| 99 | return Atom.create(getTrackingDLPredicate((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 100 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 101 | return Atom.create(getTrackingDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 102 | if (p instanceof Inequality) | ||
| 103 | return Atom.create(getTrackingDLPredicate((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 104 | |||
| 105 | return null; | ||
| 106 | } | ||
| 107 | |||
| 108 | private Atom getGapAtom(Atom headAtom) { | ||
| 109 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 110 | if (p instanceof AtLeast) { | ||
| 111 | p = Normalisation.toAtLeastConcept((AtLeast) p); | ||
| 112 | return Atom.create(getGapDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom.getArgument(0)); | ||
| 113 | } | ||
| 114 | if (p instanceof AtomicConcept) | ||
| 115 | return Atom.create(getGapDLPredicate((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 116 | if (p instanceof AtomicRole) | ||
| 117 | return Atom.create(getGapDLPredicate((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 118 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 119 | return Atom.create(getGapDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 120 | if (p instanceof Inequality) | ||
| 121 | return Atom.create(getGapDLPredicate((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 122 | |||
| 123 | return null; | ||
| 124 | } | ||
| 125 | |||
| 126 | private void encodingDisjunctiveRule(DLClause clause) { | ||
| 127 | int headLength = clause.getHeadLength(); | ||
| 128 | |||
| 129 | Atom[] auxAtoms = new Atom[headLength]; | ||
| 130 | for (int i = 0; i < headLength; ++i) | ||
| 131 | auxAtoms[i] = getAuxiliaryAtom(clause.getHeadAtom(i)); | ||
| 132 | |||
| 133 | Atom[] trackingAtoms = new Atom[headLength]; | ||
| 134 | for (int i = 0; i < headLength; ++i) | ||
| 135 | trackingAtoms[i] = getTrackingAtom(clause.getHeadAtom(i)); | ||
| 136 | |||
| 137 | Atom[] gapAtoms = new Atom[headLength]; | ||
| 138 | for (int i = 0; i < headLength; ++i) | ||
| 139 | gapAtoms[i] = getGapAtom(clause.getHeadAtom(i)); | ||
| 140 | |||
| 141 | Atom[] bodyAtoms = clause.getBodyAtoms(); | ||
| 142 | |||
| 143 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 144 | DLPredicate selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 145 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 146 | |||
| 147 | for (Atom atom: bodyAtoms) { | ||
| 148 | Atom newAtom = Atom.create( | ||
| 149 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 150 | DLClauseHelper.getArguments(atom)); | ||
| 151 | newHeadAtoms.add(newAtom); | ||
| 152 | } | ||
| 153 | |||
| 154 | DLClause newClause; | ||
| 155 | int index; | ||
| 156 | for (int j = 0; j < headLength; ++j) { | ||
| 157 | Atom[] newBodyAtoms = new Atom[headLength * 2 + bodyAtoms.length]; | ||
| 158 | index = 0; | ||
| 159 | for (int i = 0; i < headLength; ++i, ++index) | ||
| 160 | newBodyAtoms[index] = gapAtoms[i]; | ||
| 161 | for (int i = 0; i < headLength; ++i, ++index) | ||
| 162 | if (i != j) | ||
| 163 | newBodyAtoms[index] = auxAtoms[i]; | ||
| 164 | else | ||
| 165 | newBodyAtoms[index] = trackingAtoms[i]; | ||
| 166 | |||
| 167 | for (int i = 0; i < bodyAtoms.length; ++i, ++index) | ||
| 168 | newBodyAtoms[index] = bodyAtoms[i]; | ||
| 169 | |||
| 170 | for (Atom atom: newHeadAtoms) { | ||
| 171 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 172 | addTrackingClause(newClause); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | private void addTrackingClause(DLClause clause) { | ||
| 178 | trackingClauses.add(clause); | ||
| 179 | } | ||
| 180 | |||
| 181 | private void addDisjunctiveRule(DLClause clause) { | ||
| 182 | disjunctiveRules.add(clause); | ||
| 183 | } | ||
| 184 | |||
| 185 | private DLPredicate getAuxPredicate(DLPredicate p) { | ||
| 186 | if (p instanceof AtLeastConcept) { | ||
| 187 | StringBuilder builder = new StringBuilder( | ||
| 188 | Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p)); | ||
| 189 | builder.append("_AUXa").append(currentQuery.getQueryID()); | ||
| 190 | return AtomicConcept.create(builder.toString()); | ||
| 191 | } | ||
| 192 | |||
| 193 | return getDLPredicate(p, "_AUXa" + currentQuery.getQueryID()); | ||
| 194 | } | ||
| 195 | |||
| 196 | private DLPredicate getTrackingBottomDLPredicate(DLPredicate p) { | ||
| 197 | return getDLPredicate(p, getTrackingSuffix("0")); | ||
| 198 | } | ||
| 199 | |||
| 200 | private DLPredicate generateAuxiliaryRule(AtLeast p1, boolean withAux) { | ||
| 201 | AtLeastConcept p = Normalisation.toAtLeastConcept(p1); | ||
| 202 | |||
| 203 | int num = p.getNumber(); | ||
| 204 | Variable[] Ys = new Variable[num]; | ||
| 205 | if (num > 1) | ||
| 206 | for (int i = 0; i < num; ++i) | ||
| 207 | Ys[i] = Variable.create("Y" + (i + 1)); | ||
| 208 | else | ||
| 209 | Ys[0] = Y; | ||
| 210 | |||
| 211 | Collection<Atom> expandedAtom = new LinkedList<Atom>(); | ||
| 212 | Collection<Atom> representativeAtom = new LinkedList<Atom>(); | ||
| 213 | if (p.getOnRole() instanceof AtomicRole) { | ||
| 214 | AtomicRole r = (AtomicRole) p.getOnRole(); | ||
| 215 | for (int i = 0; i < num; ++i) | ||
| 216 | expandedAtom.add(Atom.create(r, X, Ys[i])); | ||
| 217 | representativeAtom.add(Atom.create(r, X, Ys[0])); | ||
| 218 | } | ||
| 219 | else { | ||
| 220 | AtomicRole r = ((InverseRole) p.getOnRole()).getInverseOf(); | ||
| 221 | for (int i = 0; i < num; ++i) | ||
| 222 | expandedAtom.add(Atom.create(r, Ys[i], X)); | ||
| 223 | representativeAtom.add(Atom.create(r, Ys[0], X)); | ||
| 224 | } | ||
| 225 | |||
| 226 | if (num > 1) { | ||
| 227 | representativeAtom.add(Atom.create(Inequality.INSTANCE, Ys[0], Ys[1])); | ||
| 228 | } | ||
| 229 | for (int i = 0; i < num; ++i) | ||
| 230 | for (int j = i + 1; j < num; ++j) | ||
| 231 | expandedAtom.add(Atom.create(Inequality.INSTANCE, Ys[i], Ys[j])); | ||
| 232 | |||
| 233 | if (!p.getToConcept().equals(AtomicConcept.THING)) { | ||
| 234 | AtomicConcept c; | ||
| 235 | if (p.getToConcept() instanceof AtomicConcept) | ||
| 236 | c = (AtomicConcept) p.getToConcept(); | ||
| 237 | else { | ||
| 238 | c = OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept()); | ||
| 239 | } | ||
| 240 | for (int i = 0; i < num; ++i) | ||
| 241 | expandedAtom.add(Atom.create(c, Ys[i])); | ||
| 242 | representativeAtom.add(Atom.create(c, Ys[0])); | ||
| 243 | } | ||
| 244 | |||
| 245 | AtomicConcept ac = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct(p)); | ||
| 246 | DLPredicate trackingPredicate = getTrackingDLPredicate(ac); | ||
| 247 | DLPredicate gapPredicate = getGapDLPredicate(ac); | ||
| 248 | DLPredicate auxPredicate = withAux ? getAuxPredicate(p) : null; | ||
| 249 | |||
| 250 | for (Atom atom: representativeAtom) { | ||
| 251 | Atom[] bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 252 | if (atom.getArity() == 1) | ||
| 253 | bodyAtoms[0] = Atom.create(getTrackingDLPredicate(atom.getDLPredicate()), atom.getArgument(0)); | ||
| 254 | else | ||
| 255 | bodyAtoms[0] = Atom.create(getTrackingDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1)); | ||
| 256 | int i = 0; | ||
| 257 | for (Atom bodyAtom: expandedAtom) | ||
| 258 | bodyAtoms[++i] = bodyAtom; | ||
| 259 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(trackingPredicate, X)}, bodyAtoms)); | ||
| 260 | |||
| 261 | bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 262 | if (atom.getArity() == 1) | ||
| 263 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0)); | ||
| 264 | else | ||
| 265 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1)); | ||
| 266 | i = 0; | ||
| 267 | for (Atom bodyAtom: expandedAtom) | ||
| 268 | bodyAtoms[++i] = bodyAtom; | ||
| 269 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(gapPredicate, X)}, bodyAtoms)); | ||
| 270 | |||
| 271 | if (withAux) { | ||
| 272 | bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 273 | bodyAtoms[0] = getAuxiliaryAtom(atom); | ||
| 274 | i = 0; | ||
| 275 | for (Atom bodyAtom: expandedAtom) | ||
| 276 | bodyAtoms[++i] = bodyAtom; | ||
| 277 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(auxPredicate, X)}, bodyAtoms)); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | return withAux ? auxPredicate : trackingPredicate; | ||
| 282 | } | ||
| 283 | |||
| 284 | private DLPredicate generateAuxiliaryRule(AtomicRole p) { | ||
| 285 | if (currentQuery.isBottom()) | ||
| 286 | return getTrackingDLPredicate(p); | ||
| 287 | |||
| 288 | DLPredicate ret = getAuxPredicate(p); | ||
| 289 | Atom[] headAtom = new Atom[] {Atom.create(ret, X, Y)}; | ||
| 290 | |||
| 291 | addTrackingClause( | ||
| 292 | DLClause.create(headAtom, new Atom[] {Atom.create(getTrackingDLPredicate(p), X, Y)})); | ||
| 293 | addTrackingClause( | ||
| 294 | DLClause.create(headAtom, new Atom[] {Atom.create(getTrackingBottomDLPredicate(p), X, Y)})); | ||
| 295 | |||
| 296 | return ret; | ||
| 297 | } | ||
| 298 | |||
| 299 | private Variable X = Variable.create("X"), Y = Variable.create("Y"); | ||
| 300 | |||
| 301 | private DLPredicate generateAuxiliaryRule(AtomicConcept p) { | ||
| 302 | if (currentQuery.isBottom()) | ||
| 303 | return getTrackingDLPredicate(p); | ||
| 304 | |||
| 305 | DLPredicate ret = getAuxPredicate(p); | ||
| 306 | Atom[] headAtom = new Atom[] {Atom.create(ret, X)}; | ||
| 307 | addTrackingClause( | ||
| 308 | DLClause.create(headAtom, | ||
| 309 | new Atom[] { Atom.create(getTrackingDLPredicate(p), X)})); | ||
| 310 | addTrackingClause( | ||
| 311 | DLClause.create(headAtom, | ||
| 312 | new Atom[] { Atom.create(getTrackingBottomDLPredicate(p), X)})); | ||
| 313 | |||
| 314 | return ret; | ||
| 315 | } | ||
| 316 | |||
| 317 | private DLPredicate generateAuxiliaryRule(Equality instance) { | ||
| 318 | return generateAuxiliaryRule(AtomicRole.create(Namespace.EQUALITY)); | ||
| 319 | } | ||
| 320 | |||
| 321 | private DLPredicate generateAuxiliaryRule(Inequality instance) { | ||
| 322 | return generateAuxiliaryRule(AtomicRole.create(Namespace.INEQUALITY)); | ||
| 323 | } | ||
| 324 | |||
| 325 | @Override | ||
| 326 | public String getTrackingProgram() { | ||
| 327 | StringBuilder sb = getTrackingProgramBody(); | ||
| 328 | if (currentQuery.isBottom()) | ||
| 329 | sb.append(getBottomTrackingProgram()); | ||
| 330 | sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText()); | ||
| 331 | return sb.toString(); | ||
| 332 | } | ||
| 333 | |||
| 334 | private String bottomTrackingProgram = null; | ||
| 335 | |||
| 336 | private String getBottomTrackingProgram() { | ||
| 337 | if (bottomTrackingProgram != null) return bottomTrackingProgram.replace("_tn", getTrackingPredicate("")); | ||
| 338 | |||
| 339 | String bottomSuffix = getTrackingSuffix("0"); | ||
| 340 | LinkedList<DLClause> clauses = new LinkedList<DLClause>(); | ||
| 341 | Variable X = Variable.create("X"); | ||
| 342 | for (String concept: unaryPredicates) | ||
| 343 | clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicConcept.create(concept + bottomSuffix) , X)}, | ||
| 344 | new Atom[] {Atom.create(AtomicConcept.create(concept + "_tn"), X)})); | ||
| 345 | Variable Y = Variable.create("Y"); | ||
| 346 | for (String role: binaryPredicates) | ||
| 347 | clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicRole.create(role + bottomSuffix) , X, Y)}, | ||
| 348 | new Atom[] {Atom.create(AtomicRole.create(role + "_tn"), X, Y) })); | ||
| 349 | |||
| 350 | StringBuilder builder = new StringBuilder(DLClauseHelper.toString(clauses)); | ||
| 351 | bottomTrackingProgram = builder.toString(); | ||
| 352 | return bottomTrackingProgram.replace("_tn", getTrackingPredicate("")); | ||
| 353 | } | ||
| 354 | |||
| 355 | private void encodingBottomQueryClause(DLClause clause) { | ||
| 356 | if (!clause.toString().contains("owl:Nothing")) | ||
| 357 | clause = program.getCorrespondingClause(clause); | ||
| 358 | |||
| 359 | // Term t; | ||
| 360 | // for (Atom tAtom: clause.getHeadAtoms()) { | ||
| 361 | // for (int i = 0; i < tAtom.getArity(); ++i) | ||
| 362 | // if ((t = tAtom.getArgument(i)) instanceof Individual) | ||
| 363 | // if (((Individual) t).getIRI().startsWith(OverApproxExist.skolemisedIndividualPrefix)) | ||
| 364 | // clause = program.getCorrespondingClause(clause); | ||
| 365 | // } | ||
| 366 | |||
| 367 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 368 | Atom selectAtom = Atom.create(selected, getIndividual4GeneralRule(program.getCorrespondingClause(clause))); | ||
| 369 | |||
| 370 | for (Atom atom: clause.getBodyAtoms()) { | ||
| 371 | atom = Atom.create( | ||
| 372 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 373 | DLClauseHelper.getArguments(atom)); | ||
| 374 | newHeadAtoms.add(atom); | ||
| 375 | } | ||
| 376 | |||
| 377 | DLClause newClause; | ||
| 378 | |||
| 379 | boolean botInHead = clause.getBodyLength() == 1 && clause.getBodyAtom(0).getDLPredicate().toString().contains("owl:Nothing"); | ||
| 380 | |||
| 381 | DLPredicate[] trackingPredicates = new DLPredicate[clause.getHeadLength()]; | ||
| 382 | DLPredicate[] predicates = new DLPredicate[clause.getHeadLength()]; | ||
| 383 | int headIndex = 0; | ||
| 384 | DLPredicate trackingPredicate, p; | ||
| 385 | for (Atom headAtom: clause.getHeadAtoms()) { | ||
| 386 | if ((p = headAtom.getDLPredicate()) instanceof AtLeastConcept) { | ||
| 387 | trackingPredicate = generateAuxiliaryRule((AtLeastConcept) p, false); | ||
| 388 | p = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p)); | ||
| 389 | trackingClauses.add(DLClause.create( | ||
| 390 | new Atom[] { Atom.create(getDLPredicate(p, getTrackingSuffix("0")), X) }, | ||
| 391 | new Atom[] { Atom.create(trackingPredicate, X) })); | ||
| 392 | } | ||
| 393 | else | ||
| 394 | trackingPredicate = getTrackingDLPredicate(p); | ||
| 395 | |||
| 396 | trackingPredicates[headIndex] = trackingPredicate; | ||
| 397 | predicates[headIndex] = p; | ||
| 398 | ++headIndex; | ||
| 399 | } | ||
| 400 | |||
| 401 | headIndex = 0; | ||
| 402 | int headLength = clause.getHeadLength(); | ||
| 403 | Atom[] gapAtoms = new Atom[headLength]; | ||
| 404 | for (int i = 0; i < headLength; ++i) | ||
| 405 | gapAtoms[i] = getGapAtom(clause.getHeadAtom(i)); | ||
| 406 | // Atom.create(getGapDLPredicate(predicates[headIndex]), DLClauseHelper.getArguments(clause.getHeadAtom(i))); | ||
| 407 | int index, selectIndex; | ||
| 408 | for (Atom headAtom: clause.getHeadAtoms()) { | ||
| 409 | index = 0; selectIndex = 0; | ||
| 410 | Atom[] newBodyAtoms = new Atom[clause.getBodyLength() + 1 + headLength - 1 + (botInHead ? 0 : headLength)]; | ||
| 411 | Atom[] selectBodyAtoms = new Atom[clause.getBodyLength() + 1 + (botInHead ? 0 : headLength)]; | ||
| 412 | newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = Atom.create(trackingPredicates[headIndex], DLClauseHelper.getArguments(headAtom)); | ||
| 413 | |||
| 414 | if (!botInHead) { | ||
| 415 | for (int i = 0; i < headLength; ++i) | ||
| 416 | newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = gapAtoms[i]; | ||
| 417 | } | ||
| 418 | |||
| 419 | for (int i = 0; i < headLength; ++i) | ||
| 420 | if (i != headIndex) { | ||
| 421 | newBodyAtoms[index++] = Atom.create(getDLPredicate(predicates[i], getTrackingSuffix("0")), DLClauseHelper.getArguments(clause.getHeadAtom(i))); | ||
| 422 | } | ||
| 423 | |||
| 424 | for (int i = 0; i < clause.getBodyLength(); ++i) | ||
| 425 | newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = clause.getBodyAtom(i); | ||
| 426 | |||
| 427 | for (Atom atom: newHeadAtoms) { | ||
| 428 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 429 | trackingClauses.add(newClause); | ||
| 430 | } | ||
| 431 | trackingClauses.add(DLClause.create(new Atom[] {selectAtom}, selectBodyAtoms)); | ||
| 432 | ++headIndex; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java new file mode 100644 index 0000000..d257de3 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar2.java | |||
| @@ -0,0 +1,242 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 9 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 10 | import org.semanticweb.HermiT.model.Atom; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 13 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 14 | import org.semanticweb.HermiT.model.DLClause; | ||
| 15 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 16 | import org.semanticweb.HermiT.model.Equality; | ||
| 17 | import org.semanticweb.HermiT.model.Inequality; | ||
| 18 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 19 | import org.semanticweb.HermiT.model.Variable; | ||
| 20 | |||
| 21 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 22 | import uk.ac.ox.cs.pagoda.multistage.Normalisation; | ||
| 23 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 24 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 25 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 26 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 27 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 28 | |||
| 29 | public class TrackingRuleEncoderDisjVar2 extends TrackingRuleEncoderWithGap { | ||
| 30 | |||
| 31 | public TrackingRuleEncoderDisjVar2(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 32 | super(program, store); | ||
| 33 | } | ||
| 34 | |||
| 35 | private Set<DLClause> disjunctiveRules = new HashSet<DLClause>(); | ||
| 36 | |||
| 37 | @Override | ||
| 38 | public boolean encodingRules() { | ||
| 39 | if (ruleEncoded) return false; | ||
| 40 | ruleEncoded = true; | ||
| 41 | |||
| 42 | for (DLClause clause: program.getClauses()) { | ||
| 43 | encodingRule(clause); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (disjunctiveRules.isEmpty()) | ||
| 47 | return true; | ||
| 48 | |||
| 49 | processDisjunctiveRules(); | ||
| 50 | return false; | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | protected void encodingRule(DLClause clause) { | ||
| 55 | DLClause original = program.getCorrespondingClause(clause); | ||
| 56 | if (original.getHeadLength() <= 1) { | ||
| 57 | super.encodingRule(clause); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | if (!DLClauseHelper.hasSubsetBodyAtoms(clause, original)) | ||
| 61 | super.encodingRule(clause); | ||
| 62 | addDisjunctiveRule(original); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | private void processDisjunctiveRules() { | ||
| 67 | for (DLClause clause: disjunctiveRules) | ||
| 68 | encodingDisjunctiveRule(clause); | ||
| 69 | } | ||
| 70 | |||
| 71 | private Atom getAuxiliaryAtom(Atom headAtom) { | ||
| 72 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 73 | if (p instanceof AtLeastConcept) { | ||
| 74 | return Atom.create(generateAuxiliaryRule((AtLeastConcept) p), headAtom.getArgument(0)); | ||
| 75 | } | ||
| 76 | if (p instanceof AtomicConcept) | ||
| 77 | return Atom.create(generateAuxiliaryRule((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 78 | if (p instanceof AtomicRole) | ||
| 79 | return Atom.create(generateAuxiliaryRule((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 80 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 81 | return Atom.create(generateAuxiliaryRule(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 82 | if (p instanceof Inequality) | ||
| 83 | return Atom.create(generateAuxiliaryRule((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 84 | |||
| 85 | return null; | ||
| 86 | } | ||
| 87 | |||
| 88 | private Atom getGapAtom(Atom headAtom) { | ||
| 89 | DLPredicate p = headAtom.getDLPredicate(); | ||
| 90 | if (p instanceof AtLeastConcept) { | ||
| 91 | return Atom.create(getGapDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom.getArgument(0)); | ||
| 92 | } | ||
| 93 | if (p instanceof AtomicConcept) | ||
| 94 | return Atom.create(getGapDLPredicate((AtomicConcept) p), headAtom.getArgument(0)); | ||
| 95 | if (p instanceof AtomicRole) | ||
| 96 | return Atom.create(getGapDLPredicate((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 97 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 98 | return Atom.create(getGapDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 99 | if (p instanceof Inequality) | ||
| 100 | return Atom.create(getGapDLPredicate((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1)); | ||
| 101 | |||
| 102 | return null; | ||
| 103 | } | ||
| 104 | |||
| 105 | private void encodingDisjunctiveRule(DLClause clause) { | ||
| 106 | int headLength = clause.getHeadLength(); | ||
| 107 | |||
| 108 | Atom[] auxAtoms = new Atom[headLength]; | ||
| 109 | for (int i = 0; i < headLength; ++i) | ||
| 110 | auxAtoms[i] = getAuxiliaryAtom(clause.getHeadAtom(i)); | ||
| 111 | |||
| 112 | Atom[] gapAtoms = new Atom[headLength]; | ||
| 113 | for (int i = 0; i < headLength; ++i) | ||
| 114 | gapAtoms[i] = getGapAtom(clause.getHeadAtom(i)); | ||
| 115 | |||
| 116 | Atom[] bodyAtoms = clause.getBodyAtoms(); | ||
| 117 | |||
| 118 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 119 | DLPredicate selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 120 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 121 | |||
| 122 | for (Atom atom: bodyAtoms) { | ||
| 123 | Atom newAtom = Atom.create( | ||
| 124 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 125 | DLClauseHelper.getArguments(atom)); | ||
| 126 | newHeadAtoms.add(newAtom); | ||
| 127 | } | ||
| 128 | |||
| 129 | DLClause newClause; | ||
| 130 | for (int j = 0; j < headLength; ++j) { | ||
| 131 | Atom[] newBodyAtoms = new Atom[headLength + bodyAtoms.length + 1]; | ||
| 132 | newBodyAtoms[0] = gapAtoms[j]; | ||
| 133 | |||
| 134 | for (int i = 0; i < headLength; ++i) | ||
| 135 | // newBodyAtoms[i] = auxAtoms[i]; | ||
| 136 | newBodyAtoms[i + 1] = auxAtoms[i]; | ||
| 137 | |||
| 138 | for (int i = 0; i < bodyAtoms.length; ++i) | ||
| 139 | // newBodyAtoms[i + headLength] = bodyAtoms[i]; | ||
| 140 | newBodyAtoms[i + headLength + 1] = bodyAtoms[i]; | ||
| 141 | |||
| 142 | for (Atom atom: newHeadAtoms) { | ||
| 143 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 144 | addTrackingClause(newClause); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | private void addTrackingClause(DLClause clause) { | ||
| 150 | trackingClauses.add(clause); | ||
| 151 | } | ||
| 152 | |||
| 153 | private void addDisjunctiveRule(DLClause clause) { | ||
| 154 | disjunctiveRules.add(clause); | ||
| 155 | } | ||
| 156 | |||
| 157 | protected DLPredicate generateAuxiliaryRule(AtLeastConcept p) { | ||
| 158 | AtomicConcept ac = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct(p)); | ||
| 159 | int num = p.getNumber(); | ||
| 160 | Variable X = Variable.create("X"); | ||
| 161 | Variable[] Ys = new Variable[num]; | ||
| 162 | for (int i = 0; i < num; ++i) Ys[i] = Variable.create("Y" + (i + 1)); | ||
| 163 | Collection<Atom> expandedAtom = new LinkedList<Atom>(); | ||
| 164 | Collection<Atom> representativeAtom = new LinkedList<Atom>(); | ||
| 165 | if (p.getOnRole() instanceof AtomicRole) { | ||
| 166 | AtomicRole r = (AtomicRole) p.getOnRole(); | ||
| 167 | for (int i = 0; i < num; ++i) | ||
| 168 | expandedAtom.add(Atom.create(r, X, Ys[i])); | ||
| 169 | representativeAtom.add(Atom.create(r, X, Ys[0])); | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | AtomicRole r = ((InverseRole) p.getOnRole()).getInverseOf(); | ||
| 173 | for (int i = 0; i < num; ++i) | ||
| 174 | expandedAtom.add(Atom.create(r, Ys[i], X)); | ||
| 175 | representativeAtom.add(Atom.create(r, Ys[0], X)); | ||
| 176 | |||
| 177 | } | ||
| 178 | |||
| 179 | if (num > 1) { | ||
| 180 | representativeAtom.add(Atom.create(Inequality.INSTANCE, Ys[0], Ys[1])); | ||
| 181 | } | ||
| 182 | for (int i = 0; i < num; ++i) | ||
| 183 | for (int j = i + 1; j < num; ++i) | ||
| 184 | expandedAtom.add(Atom.create(Inequality.INSTANCE, Ys[i], Ys[j])); | ||
| 185 | |||
| 186 | if (!p.getToConcept().equals(AtomicConcept.THING)) { | ||
| 187 | AtomicConcept c; | ||
| 188 | if (p.getToConcept() instanceof AtomicConcept) | ||
| 189 | c = (AtomicConcept) p.getToConcept(); | ||
| 190 | else | ||
| 191 | c = OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept()); | ||
| 192 | for (int i = 0; i < num; ++i) | ||
| 193 | expandedAtom.add(Atom.create(c, Ys[i])); | ||
| 194 | representativeAtom.add(Atom.create(c, Ys[0])); | ||
| 195 | } | ||
| 196 | |||
| 197 | DLPredicate auxPredicate = getTrackingDLPredicate(ac); | ||
| 198 | DLPredicate gapPredicate = getGapDLPredicate(ac); | ||
| 199 | for (Atom atom: representativeAtom) { | ||
| 200 | Atom[] bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 201 | bodyAtoms[0] = getAuxiliaryAtom(atom); | ||
| 202 | int i = 0; | ||
| 203 | for (Atom bodyAtom: expandedAtom) | ||
| 204 | bodyAtoms[++i] = bodyAtom; | ||
| 205 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(auxPredicate, X)}, bodyAtoms)); | ||
| 206 | |||
| 207 | bodyAtoms = new Atom[expandedAtom.size() + 1]; | ||
| 208 | if (atom.getArity() == 1) | ||
| 209 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0)); | ||
| 210 | else | ||
| 211 | bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1)); | ||
| 212 | i = 0; | ||
| 213 | for (Atom bodyAtom: expandedAtom) | ||
| 214 | bodyAtoms[++i] = bodyAtom; | ||
| 215 | addTrackingClause(DLClause.create(new Atom[] {Atom.create(gapPredicate, X)}, bodyAtoms)); | ||
| 216 | } | ||
| 217 | |||
| 218 | return auxPredicate; | ||
| 219 | } | ||
| 220 | |||
| 221 | private DLPredicate generateAuxiliaryRule(AtomicConcept p) { | ||
| 222 | return getTrackingDLPredicate(p); | ||
| 223 | } | ||
| 224 | |||
| 225 | private DLPredicate generateAuxiliaryRule(AtomicRole p) { | ||
| 226 | return getTrackingDLPredicate(p); | ||
| 227 | } | ||
| 228 | |||
| 229 | protected DLPredicate generateAuxiliaryRule(Equality instance) { | ||
| 230 | return getTrackingDLPredicate(AtomicRole.create(Namespace.EQUALITY)); | ||
| 231 | } | ||
| 232 | |||
| 233 | protected DLPredicate generateAuxiliaryRule(Inequality instance) { | ||
| 234 | return getTrackingDLPredicate(AtomicRole.create(Namespace.INEQUALITY)); | ||
| 235 | } | ||
| 236 | |||
| 237 | @Override | ||
| 238 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) { | ||
| 239 | encodingAtomicQuery(botQuerRecords, true); | ||
| 240 | } | ||
| 241 | |||
| 242 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithGap.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithGap.java new file mode 100644 index 0000000..e7bd188 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithGap.java | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.LinkedList; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.Atom; | ||
| 7 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 8 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 9 | import org.semanticweb.HermiT.model.DLClause; | ||
| 10 | import org.semanticweb.HermiT.model.Variable; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 12 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 13 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 14 | |||
| 15 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 16 | import uk.ac.ox.cs.pagoda.query.*; | ||
| 17 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 18 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 19 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 20 | |||
| 21 | public class TrackingRuleEncoderWithGap extends TrackingRuleEncoder { | ||
| 22 | |||
| 23 | public TrackingRuleEncoderWithGap(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 24 | super(program, store); | ||
| 25 | } | ||
| 26 | |||
| 27 | @Override | ||
| 28 | protected String getEqualityRelatedRuleText() { | ||
| 29 | if (equalityRelatedRuleText != null) return equalityRelatedRuleText.replace("_tn", getTrackingPredicate("")); | ||
| 30 | |||
| 31 | Collection<DLClause> equalityRelatedClauses = new LinkedList<DLClause>(); | ||
| 32 | Variable X = Variable.create("X"); | ||
| 33 | AtomicRole trackingSameAs = AtomicRole.create(Namespace.EQUALITY + "_tn"); | ||
| 34 | OWLOntology onto = program.getOntology(); | ||
| 35 | Atom[] headAtom = new Atom[] {Atom.create(trackingSameAs, X, X)}, bodyAtom; | ||
| 36 | for (OWLClass cls: onto.getClassesInSignature(true)) { | ||
| 37 | String clsIRI = cls.getIRI().toString(); | ||
| 38 | unaryPredicates.add(clsIRI); | ||
| 39 | bodyAtom = new Atom[] { | ||
| 40 | Atom.create(AtomicConcept.create(clsIRI + "_tn"), X), | ||
| 41 | Atom.create(AtomicConcept.create(GapTupleIterator.getGapPredicate(clsIRI)), X)}; | ||
| 42 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 43 | } | ||
| 44 | |||
| 45 | Variable Y = Variable.create("Y"); | ||
| 46 | for (OWLObjectProperty prop: onto.getObjectPropertiesInSignature(true)) { | ||
| 47 | String propIRI = prop.getIRI().toString(); | ||
| 48 | binaryPredicates.add(propIRI); | ||
| 49 | AtomicRole trackingRole = AtomicRole.create(propIRI + "_tn"); | ||
| 50 | AtomicRole gapRole = AtomicRole.create(GapTupleIterator.getGapPredicate(propIRI)); | ||
| 51 | // AtomicRole role = AtomicRole.create(propIRI); | ||
| 52 | bodyAtom = new Atom[] { | ||
| 53 | Atom.create(trackingRole, X, Y), | ||
| 54 | Atom.create(gapRole, X, Y)}; | ||
| 55 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 56 | |||
| 57 | bodyAtom = new Atom[] { | ||
| 58 | Atom.create(trackingRole, Y, X), | ||
| 59 | Atom.create(gapRole, Y, X)}; | ||
| 60 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 61 | } | ||
| 62 | |||
| 63 | equalityRelatedClauses.add( | ||
| 64 | DLClause.create( | ||
| 65 | new Atom[] {Atom.create(trackingSameAs, Y, X)}, | ||
| 66 | new Atom[] {Atom.create(trackingSameAs, X, Y)})); | ||
| 67 | |||
| 68 | equalityRelatedRuleText = DLClauseHelper.toString(equalityRelatedClauses).toString(); | ||
| 69 | return equalityRelatedRuleText.replace("_tn", getTrackingPredicate("")); | ||
| 70 | } | ||
| 71 | |||
| 72 | @Override | ||
| 73 | protected void encodingRule(DLClause clause) { | ||
| 74 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 75 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 76 | |||
| 77 | Atom headAtom; | ||
| 78 | for (Atom atom: clause.getBodyAtoms()) { | ||
| 79 | headAtom = Atom.create( | ||
| 80 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 81 | DLClauseHelper.getArguments(atom)); | ||
| 82 | newHeadAtoms.add(headAtom); | ||
| 83 | } | ||
| 84 | |||
| 85 | DLClause newClause; | ||
| 86 | |||
| 87 | int offset = (clause.getBodyLength() == 1 && clause.getBodyAtom(0).getDLPredicate().toString().contains("owl:Nothing")) ? 1 : 2; | ||
| 88 | |||
| 89 | Atom[] newBodyAtoms = new Atom[clause.getBodyLength() + offset]; | ||
| 90 | headAtom = clause.getHeadAtom(0); | ||
| 91 | newBodyAtoms[0] = Atom.create( | ||
| 92 | getTrackingDLPredicate(headAtom.getDLPredicate()), | ||
| 93 | DLClauseHelper.getArguments(headAtom)); | ||
| 94 | |||
| 95 | if (offset == 2) | ||
| 96 | newBodyAtoms[1] = Atom.create( | ||
| 97 | getGapDLPredicate(headAtom.getDLPredicate()), | ||
| 98 | DLClauseHelper.getArguments(headAtom)); | ||
| 99 | |||
| 100 | for (int i = 0; i < clause.getBodyLength(); ++i) | ||
| 101 | newBodyAtoms[i + offset] = clause.getBodyAtom(i); | ||
| 102 | |||
| 103 | for (Atom atom: newHeadAtoms) { | ||
| 104 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 105 | trackingClauses.add(newClause); | ||
| 106 | } | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithoutGap.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithoutGap.java new file mode 100644 index 0000000..be9e45c --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderWithoutGap.java | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.LinkedList; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.Atom; | ||
| 7 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 8 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 9 | import org.semanticweb.HermiT.model.DLClause; | ||
| 10 | import org.semanticweb.HermiT.model.Variable; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 12 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 13 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 14 | |||
| 15 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 16 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 17 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 18 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 19 | |||
| 20 | public class TrackingRuleEncoderWithoutGap extends TrackingRuleEncoder { | ||
| 21 | |||
| 22 | public TrackingRuleEncoderWithoutGap(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 23 | super(program, store); | ||
| 24 | } | ||
| 25 | |||
| 26 | @Override | ||
| 27 | protected String getEqualityRelatedRuleText() { | ||
| 28 | if (equalityRelatedRuleText != null) return equalityRelatedRuleText.replace("_tn", getTrackingPredicate("")); | ||
| 29 | |||
| 30 | Collection<DLClause> equalityRelatedClauses = new LinkedList<DLClause>(); | ||
| 31 | Variable X = Variable.create("X"); | ||
| 32 | AtomicRole trackingSameAs = AtomicRole.create(Namespace.EQUALITY + "_tn"); | ||
| 33 | OWLOntology onto = program.getOntology(); | ||
| 34 | Atom[] headAtom, bodyAtom; | ||
| 35 | for (OWLClass cls: onto.getClassesInSignature(true)) { | ||
| 36 | String clsIRI = cls.getIRI().toString(); | ||
| 37 | unaryPredicates.add(clsIRI); | ||
| 38 | headAtom = new Atom[] {Atom.create(trackingSameAs, X, X)}; | ||
| 39 | bodyAtom = new Atom[] { | ||
| 40 | Atom.create(AtomicConcept.create(clsIRI + "_tn"), X), | ||
| 41 | // Atom.create(AtomicConcept.create(GapTupleIterator.getGapPredicate(clsIRI)), X1), | ||
| 42 | Atom.create(AtomicConcept.create(clsIRI), X)}; | ||
| 43 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 44 | } | ||
| 45 | Variable Y = Variable.create("Y"); | ||
| 46 | for (OWLObjectProperty prop: onto.getObjectPropertiesInSignature(true)) { | ||
| 47 | String propIRI = prop.getIRI().toString(); | ||
| 48 | binaryPredicates.add(propIRI); | ||
| 49 | AtomicRole trackingRole = AtomicRole.create(propIRI + "_tn"); | ||
| 50 | // AtomicRole gapRole = AtomicRole.create(GapTupleIterator.getGapPredicate(propIRI)); | ||
| 51 | AtomicRole role = AtomicRole.create(propIRI); | ||
| 52 | headAtom = new Atom[] {Atom.create(trackingSameAs, X, X)}; | ||
| 53 | bodyAtom = new Atom[] { | ||
| 54 | Atom.create(trackingRole, X, Y), | ||
| 55 | // Atom.create(gapRole, X1, Y), | ||
| 56 | Atom.create(role, X, Y)}; | ||
| 57 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 58 | |||
| 59 | bodyAtom = new Atom[] { | ||
| 60 | Atom.create(trackingRole, Y, X), | ||
| 61 | // Atom.create(gapRole, Y, X1), | ||
| 62 | Atom.create(role, Y, X)}; | ||
| 63 | equalityRelatedClauses.add(DLClause.create(headAtom, bodyAtom)); | ||
| 64 | } | ||
| 65 | |||
| 66 | equalityRelatedClauses.add( | ||
| 67 | DLClause.create( | ||
| 68 | new Atom[] {Atom.create(trackingSameAs, Y, X)}, | ||
| 69 | new Atom[] {Atom.create(trackingSameAs, X, Y)})); | ||
| 70 | |||
| 71 | equalityRelatedRuleText = DLClauseHelper.toString(equalityRelatedClauses).toString(); | ||
| 72 | return equalityRelatedRuleText.replace("_tn", getTrackingPredicate("")); | ||
| 73 | } | ||
| 74 | |||
| 75 | @Override | ||
| 76 | protected void encodingRule(DLClause clause) { | ||
| 77 | LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>(); | ||
| 78 | newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause))); | ||
| 79 | |||
| 80 | Atom headAtom; | ||
| 81 | for (Atom atom: clause.getBodyAtoms()) { | ||
| 82 | headAtom = Atom.create( | ||
| 83 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 84 | DLClauseHelper.getArguments(atom)); | ||
| 85 | newHeadAtoms.add(headAtom); | ||
| 86 | } | ||
| 87 | |||
| 88 | DLClause newClause; | ||
| 89 | Atom[] newBodyAtoms = new Atom[clause.getBodyLength() + 1]; | ||
| 90 | headAtom = clause.getHeadAtom(0); | ||
| 91 | newBodyAtoms[0] = Atom.create( | ||
| 92 | getTrackingDLPredicate(headAtom.getDLPredicate()), | ||
| 93 | DLClauseHelper.getArguments(headAtom)); | ||
| 94 | |||
| 95 | // newBodyAtoms[1] = Atom.create( | ||
| 96 | // getGapDLPredicate(headAtom.getDLPredicate()), | ||
| 97 | // DLClauseHelper.getArguments(headAtom)); | ||
| 98 | |||
| 99 | for (int i = 0; i < clause.getBodyLength(); ++i) | ||
| 100 | newBodyAtoms[i + 1] = clause.getBodyAtom(i); | ||
| 101 | |||
| 102 | for (Atom atom: newHeadAtoms) { | ||
| 103 | newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); | ||
| 104 | trackingClauses.add(newClause); | ||
| 105 | } | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | } | ||
