diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip | |
[pagoda] Move project to Scala
This commit includes a few changes:
- The repository still uses Maven to manage dependency but it is now a
Scala project.
- The code has been ported from OWLAPI 3.4.10 to 5.1.20
- A proof of concept program using both RSAComb and PAGOdA has been
added.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java | 314 |
1 files changed, 0 insertions, 314 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java deleted file mode 100644 index 3fd2fbd..0000000 --- a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java +++ /dev/null | |||
| @@ -1,314 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.Atom; | ||
| 4 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 5 | import org.semanticweb.HermiT.model.DLClause; | ||
| 6 | import org.semanticweb.HermiT.model.Variable; | ||
| 7 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 8 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 9 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 10 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 11 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 12 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 13 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 14 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 15 | import uk.ac.ox.cs.pagoda.query.QueryManager; | ||
| 16 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 17 | import uk.ac.ox.cs.pagoda.reasoner.full.Checker; | ||
| 18 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 19 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 20 | import uk.ac.ox.cs.pagoda.summary.HermitSummaryFilter; | ||
| 21 | import uk.ac.ox.cs.pagoda.tracking.QueryTracker; | ||
| 22 | import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoder; | ||
| 23 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 24 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 25 | import uk.ac.ox.cs.pagoda.util.disposable.Disposable; | ||
| 26 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 27 | |||
| 28 | import java.util.LinkedList; | ||
| 29 | |||
| 30 | public class ConsistencyManager extends Disposable { | ||
| 31 | |||
| 32 | protected MyQueryReasoner m_reasoner; | ||
| 33 | protected QueryManager m_queryManager; | ||
| 34 | |||
| 35 | Timer t = new Timer(); | ||
| 36 | QueryRecord fullQueryRecord; | ||
| 37 | QueryRecord[] botQueryRecords; | ||
| 38 | LinkedList<DLClause> toAddClauses; | ||
| 39 | boolean fragmentExtracted = false; | ||
| 40 | |||
| 41 | public ConsistencyManager(MyQueryReasoner reasoner) { | ||
| 42 | m_reasoner = reasoner; | ||
| 43 | m_queryManager = reasoner.getQueryManager(); | ||
| 44 | } | ||
| 45 | |||
| 46 | @Override | ||
| 47 | public void dispose() { | ||
| 48 | super.dispose(); | ||
| 49 | fullQueryRecord.dispose(); | ||
| 50 | } | ||
| 51 | |||
| 52 | public void extractBottomFragment() { | ||
| 53 | if(isDisposed()) throw new DisposedException(); | ||
| 54 | |||
| 55 | if(fragmentExtracted) return; | ||
| 56 | fragmentExtracted = true; | ||
| 57 | |||
| 58 | UpperDatalogProgram upperProgram = m_reasoner.program.getUpper(); | ||
| 59 | int number = upperProgram.getBottomNumber(); | ||
| 60 | |||
| 61 | if(number <= 1) { | ||
| 62 | botQueryRecords = new QueryRecord[]{fullQueryRecord}; | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | QueryRecord[] tempQueryRecords = new QueryRecord[number - 1]; | ||
| 66 | QueryRecord record; | ||
| 67 | for(int i = 0; i < number - 1; ++i) { | ||
| 68 | tempQueryRecords[i] = record = | ||
| 69 | m_queryManager.create(QueryRecord.botQueryText.replace("Nothing", "Nothing" + (i + 1)), 0, i + 1); | ||
| 70 | AnswerTuples iter = null; | ||
| 71 | try { | ||
| 72 | iter = m_reasoner.trackingStore.evaluate(record.getQueryText(), record.getAnswerVariables()); | ||
| 73 | record.updateUpperBoundAnswers(iter); | ||
| 74 | } finally { | ||
| 75 | if(iter != null) iter.dispose(); | ||
| 76 | iter = null; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | int bottomNumber = 0; | ||
| 81 | int[] group = new int[number - 1]; | ||
| 82 | for(int i = 0; i < number - 1; ++i) group[i] = i; | ||
| 83 | for(int i = 0; i < number - 1; ++i) | ||
| 84 | if(tempQueryRecords[i].isProcessed()) tempQueryRecords[i].dispose(); | ||
| 85 | else if(group[i] == i) { | ||
| 86 | ++bottomNumber; | ||
| 87 | record = tempQueryRecords[i]; | ||
| 88 | for(int j = i + 1; j < number - 1; ++j) | ||
| 89 | if(record.hasSameGapAnswers(tempQueryRecords[j])) | ||
| 90 | group[j] = i; | ||
| 91 | } | ||
| 92 | |||
| 93 | Utility.logInfo("There are " + bottomNumber + " different bottom fragments."); | ||
| 94 | toAddClauses = new LinkedList<DLClause>(); | ||
| 95 | int bottomCounter = 0; | ||
| 96 | botQueryRecords = new QueryRecord[bottomNumber]; | ||
| 97 | Variable X = Variable.create("X"); | ||
| 98 | for(int i = 0; i < number - 1; ++i) | ||
| 99 | if(!tempQueryRecords[i].isDisposed() && !tempQueryRecords[i].isProcessed()) | ||
| 100 | if(group[i] == i) { | ||
| 101 | botQueryRecords[bottomCounter] = record = tempQueryRecords[i]; | ||
| 102 | record.resetInfo(QueryRecord.botQueryText.replace("Nothing", "Nothing_final" + (++bottomCounter)), 0, | ||
| 103 | group[i] = bottomCounter); | ||
| 104 | toAddClauses.add( | ||
| 105 | DLClause.create( | ||
| 106 | new Atom[]{Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + "_final" + bottomCounter), X)}, | ||
| 107 | new Atom[]{Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + (i + 1)), X)})); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | toAddClauses.add( | ||
| 111 | DLClause.create( | ||
| 112 | new Atom[]{Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + "_final" + group[group[i]]), X)}, | ||
| 113 | new Atom[]{Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + (i + 1)), X)})); | ||
| 114 | tempQueryRecords[i].dispose(); | ||
| 115 | } | ||
| 116 | |||
| 117 | upperProgram.updateDependencyGraph(toAddClauses); | ||
| 118 | } | ||
| 119 | |||
| 120 | String[] programs = collectTrackingProgramAndImport(); | ||
| 121 | if(programs.length == 0) | ||
| 122 | return; | ||
| 123 | |||
| 124 | DataStore store = m_reasoner.trackingStore.getDataStore(); | ||
| 125 | long oldTripleCount, tripleCount; | ||
| 126 | try { | ||
| 127 | Timer t1 = new Timer(); | ||
| 128 | oldTripleCount = store.getTriplesCount(); | ||
| 129 | for(String program : programs) | ||
| 130 | store.importRules(program, UpdateType.ScheduleForAddition); | ||
| 131 | store.applyReasoning(true); | ||
| 132 | tripleCount = store.getTriplesCount(); | ||
| 133 | |||
| 134 | Utility.logDebug("tracking store after materialising tracking program: " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)", | ||
| 135 | "tracking store finished the materialisation of tracking program in " + t1.duration() + " seconds."); | ||
| 136 | |||
| 137 | extractAxioms(); | ||
| 138 | store.clearRulesAndMakeFactsExplicit(); | ||
| 139 | } catch(JRDFStoreException e) { | ||
| 140 | e.printStackTrace(); | ||
| 141 | } catch(OWLOntologyCreationException e) { | ||
| 142 | e.printStackTrace(); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | public QueryRecord[] getQueryRecords() { | ||
| 147 | if(isDisposed()) throw new DisposedException(); | ||
| 148 | |||
| 149 | return botQueryRecords; | ||
| 150 | } | ||
| 151 | |||
| 152 | boolean checkRLLowerBound() { | ||
| 153 | if(isDisposed()) throw new DisposedException(); | ||
| 154 | |||
| 155 | fullQueryRecord = m_queryManager.create(QueryRecord.botQueryText, 0); | ||
| 156 | AnswerTuples iter = null; | ||
| 157 | |||
| 158 | try { | ||
| 159 | iter = m_reasoner.rlLowerStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); | ||
| 160 | fullQueryRecord.updateLowerBoundAnswers(iter); | ||
| 161 | } finally { | ||
| 162 | iter.dispose(); | ||
| 163 | } | ||
| 164 | |||
| 165 | if (fullQueryRecord.getNoOfSoundAnswers() > 0) { | ||
| 166 | Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); | ||
| 167 | return false; | ||
| 168 | } | ||
| 169 | return true; | ||
| 170 | } | ||
| 171 | |||
| 172 | // protected boolean unsatisfiability(double duration) { | ||
| 173 | // fullQueryRecord.dispose(); | ||
| 174 | // Utility.logDebug("The ontology and dataset is unsatisfiable."); | ||
| 175 | // return false; | ||
| 176 | // } | ||
| 177 | |||
| 178 | // protected boolean satisfiability(double duration) { | ||
| 179 | // fullQueryRecord.dispose(); | ||
| 180 | // Utility.logDebug("The ontology and dataset is satisfiable."); | ||
| 181 | // return true; | ||
| 182 | // } | ||
| 183 | |||
| 184 | boolean checkELLowerBound() { | ||
| 185 | if(isDisposed()) throw new DisposedException(); | ||
| 186 | |||
| 187 | fullQueryRecord.updateLowerBoundAnswers(m_reasoner.elLowerStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord | ||
| 188 | .getAnswerVariables())); | ||
| 189 | if(fullQueryRecord.getNoOfSoundAnswers() > 0) { | ||
| 190 | Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); | ||
| 191 | return true; | ||
| 192 | } | ||
| 193 | return true; | ||
| 194 | } | ||
| 195 | |||
| 196 | boolean checkUpper(BasicQueryEngine upperStore) { | ||
| 197 | if(isDisposed()) throw new DisposedException(); | ||
| 198 | |||
| 199 | if(upperStore != null) { | ||
| 200 | AnswerTuples tuples = null; | ||
| 201 | try { | ||
| 202 | tuples = upperStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); | ||
| 203 | if(!tuples.isValid()) { | ||
| 204 | Utility.logInfo("There are no contradictions derived in " + upperStore.getName() + " materialisation."); | ||
| 205 | Utility.logDebug("The ontology and dataset is satisfiable."); | ||
| 206 | return true; | ||
| 207 | } | ||
| 208 | } finally { | ||
| 209 | if(tuples != null) tuples.dispose(); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | return false; | ||
| 213 | } | ||
| 214 | |||
| 215 | boolean check() { | ||
| 216 | if(isDisposed()) throw new DisposedException(); | ||
| 217 | |||
| 218 | // if (!checkRLLowerBound()) return false; | ||
| 219 | // if (!checkELLowerBound()) return false; | ||
| 220 | // if (checkLazyUpper()) return true; | ||
| 221 | AnswerTuples iter = null; | ||
| 222 | |||
| 223 | try { | ||
| 224 | iter = | ||
| 225 | m_reasoner.trackingStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); | ||
| 226 | fullQueryRecord.updateUpperBoundAnswers(iter); | ||
| 227 | } finally { | ||
| 228 | if(iter != null) iter.dispose(); | ||
| 229 | } | ||
| 230 | |||
| 231 | if(fullQueryRecord.getNoOfCompleteAnswers() == 0) | ||
| 232 | return true; | ||
| 233 | |||
| 234 | extractBottomFragment(); | ||
| 235 | |||
| 236 | try { | ||
| 237 | extractAxioms4Full(); | ||
| 238 | } catch(OWLOntologyCreationException e) { | ||
| 239 | e.printStackTrace(); | ||
| 240 | } | ||
| 241 | // fullQueryRecord.saveRelevantClause(); | ||
| 242 | |||
| 243 | boolean satisfiability; | ||
| 244 | |||
| 245 | Checker checker; | ||
| 246 | for(QueryRecord r : getQueryRecords()) { | ||
| 247 | // TODO to be removed ... | ||
| 248 | // r.saveRelevantOntology("bottom" + r.getQueryID() + ".owl"); | ||
| 249 | checker = new HermitSummaryFilter(r, true); // m_reasoner.factory.getSummarisedReasoner(r); | ||
| 250 | satisfiability = checker.isConsistent(); | ||
| 251 | checker.dispose(); | ||
| 252 | if(!satisfiability) return false; | ||
| 253 | } | ||
| 254 | |||
| 255 | // Checker checker = m_reasoner.factory.getSummarisedReasoner(fullQueryRecord); | ||
| 256 | // boolean satisfiable = checker.isConsistent(); | ||
| 257 | // checker.dispose(); | ||
| 258 | // if (!satisfiable) return unsatisfiability(t.duration()); | ||
| 259 | |||
| 260 | return true; | ||
| 261 | } | ||
| 262 | |||
| 263 | private void extractAxioms4Full() throws OWLOntologyCreationException { | ||
| 264 | OWLOntologyManager manager = m_reasoner.encoder.getProgram().getOntology().getOWLOntologyManager(); | ||
| 265 | OWLOntology fullOntology = manager.createOntology(); | ||
| 266 | for (QueryRecord record: botQueryRecords) { | ||
| 267 | for (DLClause clause: record.getRelevantClauses()) { | ||
| 268 | fullQueryRecord.addRelevantClauses(clause); | ||
| 269 | } | ||
| 270 | manager.addAxioms(fullOntology, record.getRelevantOntology().getAxioms()); | ||
| 271 | } | ||
| 272 | fullQueryRecord.setRelevantOntology(fullOntology); | ||
| 273 | } | ||
| 274 | |||
| 275 | private void extractAxioms() throws OWLOntologyCreationException { | ||
| 276 | OWLOntologyManager manager = m_reasoner.encoder.getProgram().getOntology().getOWLOntologyManager(); | ||
| 277 | for (QueryRecord record: botQueryRecords) { | ||
| 278 | record.setRelevantOntology(manager.createOntology()); | ||
| 279 | QueryTracker tracker = new QueryTracker(m_reasoner.encoder, m_reasoner.rlLowerStore, record); | ||
| 280 | m_reasoner.encoder.setCurrentQuery(record); | ||
| 281 | tracker.extractAxioms(m_reasoner.trackingStore); | ||
| 282 | // record.saveRelevantClause(); | ||
| 283 | // record.saveRelevantOntology("bottom" + record.getQueryID() + ".owl"); | ||
| 284 | Utility.logInfo("finish extracting axioms for bottom " + record.getQueryID()); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | private String[] collectTrackingProgramAndImport() { | ||
| 289 | String[] programs = new String[botQueryRecords.length]; | ||
| 290 | TrackingRuleEncoder encoder = m_reasoner.encoder; | ||
| 291 | |||
| 292 | StringBuilder builder; | ||
| 293 | LinkedList<DLClause> currentClauses = new LinkedList<DLClause>(); | ||
| 294 | |||
| 295 | for (int i = 0; i < botQueryRecords.length; ++i) { | ||
| 296 | encoder.setCurrentQuery(botQueryRecords[i]); | ||
| 297 | builder = new StringBuilder(encoder.getTrackingProgram()); | ||
| 298 | // encoder.saveTrackingRules("tracking_bottom" + (i + 1) + ".dlog"); | ||
| 299 | |||
| 300 | for (DLClause clause: toAddClauses) | ||
| 301 | if (clause.getHeadAtom(0).getDLPredicate().toString().contains("_final" + (i + 1))) | ||
| 302 | currentClauses.add(clause); | ||
| 303 | |||
| 304 | builder.append(DLClauseHelper.toString(currentClauses)); | ||
| 305 | programs[i] = builder.toString(); | ||
| 306 | |||
| 307 | currentClauses.clear(); | ||
| 308 | } | ||
| 309 | |||
| 310 | return programs; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | } | ||
