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