diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java new file mode 100644 index 0000000..a222645 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java | |||
| @@ -0,0 +1,269 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner; | ||
| 2 | |||
| 3 | import java.util.LinkedList; | ||
| 4 | |||
| 5 | import org.semanticweb.HermiT.model.Atom; | ||
| 6 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 7 | import org.semanticweb.HermiT.model.DLClause; | ||
| 8 | import org.semanticweb.HermiT.model.Variable; | ||
| 9 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 10 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 11 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 12 | |||
| 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.rules.UpperDatalogProgram; | ||
| 19 | import uk.ac.ox.cs.pagoda.summary.HermitSummaryFilter; | ||
| 20 | import uk.ac.ox.cs.pagoda.tracking.QueryTracker; | ||
| 21 | import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoder; | ||
| 22 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 23 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 24 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 25 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 26 | |||
| 27 | public class ConsistencyManager { | ||
| 28 | |||
| 29 | protected MyQueryReasoner m_reasoner; | ||
| 30 | protected QueryManager m_queryManager; | ||
| 31 | |||
| 32 | Timer t = new Timer(); | ||
| 33 | |||
| 34 | public ConsistencyManager(MyQueryReasoner reasoner) { | ||
| 35 | m_reasoner = reasoner; | ||
| 36 | m_queryManager = reasoner.getQueryManager(); | ||
| 37 | } | ||
| 38 | |||
| 39 | QueryRecord fullQueryRecord; | ||
| 40 | QueryRecord[] botQueryRecords; | ||
| 41 | |||
| 42 | LinkedList<DLClause> toAddClauses; | ||
| 43 | |||
| 44 | boolean checkRLLowerBound() { | ||
| 45 | fullQueryRecord = m_queryManager.create(QueryRecord.botQueryText, 0); | ||
| 46 | fullQueryRecord.updateLowerBoundAnswers(m_reasoner.rlLowerStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables())); | ||
| 47 | if (fullQueryRecord.getNoOfSoundAnswers() > 0) { | ||
| 48 | Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); | ||
| 49 | return unsatisfiability(t.duration()); | ||
| 50 | } | ||
| 51 | return true; | ||
| 52 | } | ||
| 53 | |||
| 54 | boolean checkELLowerBound() { | ||
| 55 | fullQueryRecord.updateLowerBoundAnswers(m_reasoner.elLowerStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables())); | ||
| 56 | if (fullQueryRecord.getNoOfSoundAnswers() > 0) { | ||
| 57 | Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); | ||
| 58 | return unsatisfiability(t.duration()); | ||
| 59 | } | ||
| 60 | return true; | ||
| 61 | } | ||
| 62 | |||
| 63 | boolean checkLazyUpper() { | ||
| 64 | if (m_reasoner.lazyUpperStore != null) { | ||
| 65 | AnswerTuples tuples = null; | ||
| 66 | try { | ||
| 67 | tuples = m_reasoner.lazyUpperStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); | ||
| 68 | if (!tuples.isValid()) { | ||
| 69 | Utility.logInfo("There are no contradictions derived in the lazy upper bound materialisation."); | ||
| 70 | return satisfiability(t.duration()); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | finally { | ||
| 74 | if (tuples != null) tuples.dispose(); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | return false; | ||
| 78 | } | ||
| 79 | |||
| 80 | boolean check() { | ||
| 81 | // if (!checkRLLowerBound()) return false; | ||
| 82 | // if (!checkELLowerBound()) return false; | ||
| 83 | // if (checkLazyUpper()) return true; | ||
| 84 | |||
| 85 | fullQueryRecord.updateUpperBoundAnswers(m_reasoner.trackingStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables())); | ||
| 86 | if (fullQueryRecord.getNoOfCompleteAnswers() == 0) | ||
| 87 | return satisfiability(t.duration()); | ||
| 88 | |||
| 89 | extractBottomFragment(); | ||
| 90 | |||
| 91 | try { | ||
| 92 | extractAxioms4Full(); | ||
| 93 | } catch (OWLOntologyCreationException e) { | ||
| 94 | e.printStackTrace(); | ||
| 95 | } | ||
| 96 | fullQueryRecord.saveRelevantClause(); | ||
| 97 | |||
| 98 | boolean satisfiability; | ||
| 99 | |||
| 100 | Checker checker; | ||
| 101 | for (QueryRecord r: getQueryRecords()) { | ||
| 102 | // TODO to be removed ... | ||
| 103 | // r.saveRelevantOntology("bottom" + r.getQueryID() + ".owl"); | ||
| 104 | checker = new HermitSummaryFilter(r); // m_reasoner.factory.getSummarisedReasoner(r); | ||
| 105 | satisfiability = checker.isConsistent(); | ||
| 106 | checker.dispose(); | ||
| 107 | if (!satisfiability) return unsatisfiability(t.duration()); | ||
| 108 | } | ||
| 109 | |||
| 110 | // Checker checker = m_reasoner.factory.getSummarisedReasoner(fullQueryRecord); | ||
| 111 | // boolean satisfiable = checker.isConsistent(); | ||
| 112 | // checker.dispose(); | ||
| 113 | // if (!satisfiable) return unsatisfiability(t.duration()); | ||
| 114 | |||
| 115 | return satisfiability(t.duration()); | ||
| 116 | } | ||
| 117 | |||
| 118 | protected boolean unsatisfiability(double duration) { | ||
| 119 | fullQueryRecord.dispose(); | ||
| 120 | Utility.logDebug("The ontology and dataset is unsatisfiable."); | ||
| 121 | return false; | ||
| 122 | } | ||
| 123 | |||
| 124 | protected boolean satisfiability(double duration) { | ||
| 125 | fullQueryRecord.dispose(); | ||
| 126 | Utility.logDebug("The ontology and dataset is satisfiable."); | ||
| 127 | return true; | ||
| 128 | } | ||
| 129 | |||
| 130 | boolean fragmentExtracted = false; | ||
| 131 | |||
| 132 | public void extractBottomFragment() { | ||
| 133 | if (fragmentExtracted) return ; | ||
| 134 | fragmentExtracted = true; | ||
| 135 | |||
| 136 | UpperDatalogProgram upperProgram = m_reasoner.program.getUpper(); | ||
| 137 | int number = upperProgram.getBottomNumber(); | ||
| 138 | |||
| 139 | if (number <= 1) { | ||
| 140 | botQueryRecords = new QueryRecord[] { fullQueryRecord }; | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | QueryRecord[] tempQueryRecords = new QueryRecord[number - 1]; | ||
| 144 | QueryRecord record; | ||
| 145 | for (int i = 0; i < number - 1; ++i) { | ||
| 146 | tempQueryRecords[i] = record = m_queryManager.create(QueryRecord.botQueryText.replace("Nothing", "Nothing" + (i + 1)), 0, i + 1); | ||
| 147 | record.updateUpperBoundAnswers(m_reasoner.trackingStore.evaluate(record.getQueryText(), record.getAnswerVariables())); | ||
| 148 | } | ||
| 149 | |||
| 150 | int bottomNumber = 0; | ||
| 151 | int[] group = new int[number - 1]; | ||
| 152 | for (int i = 0; i < number - 1; ++i) group[i] = i; | ||
| 153 | for (int i = 0; i < number - 1; ++i) | ||
| 154 | if (tempQueryRecords[i].processed()) tempQueryRecords[i].dispose(); | ||
| 155 | else if (group[i] == i) { | ||
| 156 | ++bottomNumber; | ||
| 157 | record = tempQueryRecords[i]; | ||
| 158 | for (int j = i + 1; j < number - 1; ++j) | ||
| 159 | if (record.hasSameGapAnswers(tempQueryRecords[j])) | ||
| 160 | group[j] = i; | ||
| 161 | } | ||
| 162 | |||
| 163 | Utility.logInfo("There are " + bottomNumber + " different bottom fragments."); | ||
| 164 | toAddClauses = new LinkedList<DLClause>(); | ||
| 165 | int bottomCounter = 0; | ||
| 166 | botQueryRecords = new QueryRecord[bottomNumber]; | ||
| 167 | Variable X = Variable.create("X"); | ||
| 168 | for (int i = 0; i < number - 1; ++i) | ||
| 169 | if (!tempQueryRecords[i].processed()) | ||
| 170 | if (group[i] == i) { | ||
| 171 | botQueryRecords[bottomCounter] = record = tempQueryRecords[i]; | ||
| 172 | record.resetInfo(QueryRecord.botQueryText.replace("Nothing", "Nothing_final" + (++bottomCounter)), 0, group[i] = bottomCounter); | ||
| 173 | toAddClauses.add( | ||
| 174 | DLClause.create( | ||
| 175 | new Atom[] {Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + "_final" + bottomCounter), X)}, | ||
| 176 | new Atom[] {Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + (i + 1)), X)})); | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | toAddClauses.add( | ||
| 180 | DLClause.create( | ||
| 181 | new Atom[] {Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + "_final" + group[group[i]]), X)}, | ||
| 182 | new Atom[] {Atom.create(AtomicConcept.create(AtomicConcept.NOTHING.getIRI() + (i + 1)), X)})); | ||
| 183 | tempQueryRecords[i].dispose(); | ||
| 184 | } | ||
| 185 | |||
| 186 | upperProgram.updateDependencyGraph(toAddClauses); | ||
| 187 | } | ||
| 188 | |||
| 189 | String[] programs = collectTrackingProgramAndImport(); | ||
| 190 | if (programs.length == 0) | ||
| 191 | return ; | ||
| 192 | |||
| 193 | DataStore store = m_reasoner.trackingStore.getDataStore(); | ||
| 194 | long oldTripleCount, tripleCount; | ||
| 195 | try { | ||
| 196 | Timer t1 = new Timer(); | ||
| 197 | oldTripleCount = store.getTriplesCount(); | ||
| 198 | for (String program: programs) | ||
| 199 | store.importRules(program); | ||
| 200 | store.applyReasoning(true); | ||
| 201 | tripleCount = store.getTriplesCount(); | ||
| 202 | |||
| 203 | Utility.logInfo("tracking store after materialising tracking program: " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)", | ||
| 204 | "tracking store finished the materialisation of tracking program in " + t1.duration() + " seconds."); | ||
| 205 | |||
| 206 | extractAxioms(); | ||
| 207 | store.clearRulesAndMakeFactsExplicit(); | ||
| 208 | } catch (JRDFStoreException e) { | ||
| 209 | e.printStackTrace(); | ||
| 210 | } catch (OWLOntologyCreationException e) { | ||
| 211 | e.printStackTrace(); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | private void extractAxioms4Full() throws OWLOntologyCreationException { | ||
| 216 | OWLOntologyManager manager = m_reasoner.encoder.getProgram().getOntology().getOWLOntologyManager(); | ||
| 217 | OWLOntology fullOntology = manager.createOntology(); | ||
| 218 | for (QueryRecord record: botQueryRecords) { | ||
| 219 | for (DLClause clause: record.getRelevantClauses()) { | ||
| 220 | fullQueryRecord.addRelevantClauses(clause); | ||
| 221 | } | ||
| 222 | manager.addAxioms(fullOntology, record.getRelevantOntology().getAxioms()); | ||
| 223 | } | ||
| 224 | fullQueryRecord.setRelevantOntology(fullOntology); | ||
| 225 | } | ||
| 226 | |||
| 227 | private void extractAxioms() throws OWLOntologyCreationException { | ||
| 228 | OWLOntologyManager manager = m_reasoner.encoder.getProgram().getOntology().getOWLOntologyManager(); | ||
| 229 | for (QueryRecord record: botQueryRecords) { | ||
| 230 | record.setRelevantOntology(manager.createOntology()); | ||
| 231 | QueryTracker tracker = new QueryTracker(m_reasoner.encoder, m_reasoner.rlLowerStore, record); | ||
| 232 | m_reasoner.encoder.setCurrentQuery(record); | ||
| 233 | tracker.extractAxioms(m_reasoner.trackingStore); | ||
| 234 | // record.saveRelevantClause(); | ||
| 235 | // record.saveRelevantOntology("bottom" + record.getQueryID() + ".owl"); | ||
| 236 | Utility.logInfo("finish extracting axioms for bottom " + record.getQueryID()); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | private String[] collectTrackingProgramAndImport() { | ||
| 241 | String[] programs = new String[botQueryRecords.length]; | ||
| 242 | TrackingRuleEncoder encoder = m_reasoner.encoder; | ||
| 243 | |||
| 244 | StringBuilder builder; | ||
| 245 | LinkedList<DLClause> currentClauses = new LinkedList<DLClause>(); | ||
| 246 | |||
| 247 | for (int i = 0; i < botQueryRecords.length; ++i) { | ||
| 248 | encoder.setCurrentQuery(botQueryRecords[i]); | ||
| 249 | builder = new StringBuilder(encoder.getTrackingProgram()); | ||
| 250 | // encoder.saveTrackingRules("tracking_bottom" + (i + 1) + ".dlog"); | ||
| 251 | |||
| 252 | for (DLClause clause: toAddClauses) | ||
| 253 | if (clause.getHeadAtom(0).getDLPredicate().toString().contains("_final" + (i + 1))) | ||
| 254 | currentClauses.add(clause); | ||
| 255 | |||
| 256 | builder.append(DLClauseHelper.toString(currentClauses)); | ||
| 257 | programs[i] = builder.toString(); | ||
| 258 | |||
| 259 | currentClauses.clear(); | ||
| 260 | } | ||
| 261 | |||
| 262 | return programs; | ||
| 263 | } | ||
| 264 | |||
| 265 | public QueryRecord[] getQueryRecords() { | ||
| 266 | return botQueryRecords; | ||
| 267 | } | ||
| 268 | |||
| 269 | } | ||
