aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
diff options
context:
space:
mode:
authorRncLsn <rnc.lsn@gmail.com>2015-05-29 18:35:51 +0100
committerRncLsn <rnc.lsn@gmail.com>2015-05-29 18:35:51 +0100
commit4b7253559c290b6fdd1c4122830f153fda85dd62 (patch)
tree37c1487e4682be719dec532ca3d7e1af353fb9a1 /src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
parent6a559a415b3bdb3048021839e5bcf5bbf5aa4bbd (diff)
downloadACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.tar.gz
ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.zip
Disposable.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java662
1 files changed, 332 insertions, 330 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
index 618fb70..8445713 100644
--- a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
+++ b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java
@@ -21,362 +21,364 @@ import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoderDisjVar1;
21import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoderWithGap; 21import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoderWithGap;
22import uk.ac.ox.cs.pagoda.util.Timer; 22import uk.ac.ox.cs.pagoda.util.Timer;
23import uk.ac.ox.cs.pagoda.util.Utility; 23import uk.ac.ox.cs.pagoda.util.Utility;
24import uk.ac.ox.cs.pagoda.util.disposable.DisposedException;
24import uk.ac.ox.cs.pagoda.util.tuples.Tuple; 25import uk.ac.ox.cs.pagoda.util.tuples.Tuple;
25 26
26import java.util.Collection; 27import java.util.Collection;
27 28
28class MyQueryReasoner extends QueryReasoner { 29class MyQueryReasoner extends QueryReasoner {
29 30
30 OWLOntology ontology; 31 OWLOntology ontology;
31 DatalogProgram program; 32 DatalogProgram program;
32 33
33// String additonalDataFile; 34 // String additonalDataFile;
34 BasicQueryEngine rlLowerStore = null; 35 BasicQueryEngine rlLowerStore = null;
35 BasicQueryEngine lazyUpperStore = null; 36 BasicQueryEngine lazyUpperStore = null;
36 // BasicQueryEngine limitedSkolemUpperStore; 37 MultiStageQueryEngine limitedSkolemUpperStore;
37 OWLOntology elho_ontology; 38 OWLOntology elho_ontology;
38// boolean[] namedIndividuals_lazyUpper; 39 // boolean[] namedIndividuals_lazyUpper;
39 KarmaQueryEngine elLowerStore = null; 40 KarmaQueryEngine elLowerStore = null;
40 BasicQueryEngine trackingStore = null; 41 BasicQueryEngine trackingStore = null;
41 // boolean[] namedIndividuals_tracking; 42 // boolean[] namedIndividuals_tracking;
42 TrackingRuleEncoder encoder; 43 TrackingRuleEncoder encoder;
43 private boolean equalityTag; 44 private boolean equalityTag;
44 private boolean multiStageTag; 45 private boolean multiStageTag;
45 private Timer t = new Timer(); 46 private Timer t = new Timer();
46 private Collection<String> predicatesWithGap = null; 47 private Collection<String> predicatesWithGap = null;
47 private SatisfiabilityStatus satisfiable; 48 private SatisfiabilityStatus satisfiable;
48 private ConsistencyManager consistency = new ConsistencyManager(this); 49 private ConsistencyManager consistency = new ConsistencyManager(this);
49 private boolean useUpperStores = false; 50 private boolean useUpperStores = false;
50 public MyQueryReasoner() { 51
51 setup(true, true); 52 public MyQueryReasoner() {
52 } 53 setup(true, true);
53 public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) { 54 }
54 setup(multiStageTag, considerEqualities); 55
55 } 56 public MyQueryReasoner(boolean multiStageTag, boolean considerEqualities) {
56 57 setup(multiStageTag, considerEqualities);
57 private BasicQueryEngine getUpperStore(String name, boolean checkValidity) { 58 }
58 if (multiStageTag) 59
59 return new MultiStageQueryEngine(name, checkValidity); 60 public void setup(boolean multiStageTag, boolean considerEqualities) {
60// return new TwoStageQueryEngine(name, checkValidity); 61 if(isDisposed()) throw new DisposedException();
61 else 62 satisfiable = SatisfiabilityStatus.UNCHECKED;
62 return new BasicQueryEngine(name); 63 this.multiStageTag = multiStageTag;
63 } 64 this.equalityTag = considerEqualities;
64 65
65 public void setup(boolean multiStageTag, boolean considerEqualities) { 66 rlLowerStore = new BasicQueryEngine("rl-lower-bound");
66 satisfiable = SatisfiabilityStatus.UNCHECKED; 67 elLowerStore = new KarmaQueryEngine("elho-lower-bound");
67 this.multiStageTag = multiStageTag; 68
68 this.equalityTag = considerEqualities; 69 trackingStore = getUpperStore("tracking", false);
69 70 }
70 rlLowerStore = new BasicQueryEngine("rl-lower-bound"); 71
71 elLowerStore = new KarmaQueryEngine("elho-lower-bound"); 72 @Override
72 73 public void loadOntology(OWLOntology o) {
73 trackingStore = getUpperStore("tracking", false); 74 if(isDisposed()) throw new DisposedException();
74 } 75 if(!equalityTag) {
75 76 EqualitiesEliminator eliminator = new EqualitiesEliminator(o);
76 protected void internal_importDataFile(String name, String datafile) { 77 o = eliminator.getOutputOntology();
77// addDataFile(datafile); 78 eliminator.save();
78 rlLowerStore.importRDFData(name, datafile); 79 }
79 if (lazyUpperStore != null) 80
80 lazyUpperStore.importRDFData(name, datafile); 81 ontology = o;
81 elLowerStore.importRDFData(name, datafile); 82 program = new DatalogProgram(ontology, properties.getToClassify());
82 trackingStore.importRDFData(name, datafile);
83 }
84
85 @Override
86 public void loadOntology(OWLOntology o) {
87 if(!equalityTag) {
88 EqualitiesEliminator eliminator = new EqualitiesEliminator(o);
89 o = eliminator.getOutputOntology();
90 eliminator.save();
91 }
92
93 ontology = o;
94 program = new DatalogProgram(ontology, properties.getToClassify());
95// program.getLower().save(); 83// program.getLower().save();
96// program.getUpper().save(); 84// program.getUpper().save();
97// program.getGeneral().save(); 85// program.getGeneral().save();
98 86
99 useUpperStores = multiStageTag && !program.getGeneral().isHorn(); 87 useUpperStores = multiStageTag && !program.getGeneral().isHorn();
100 if(useUpperStores) { 88 if(useUpperStores) {
101 lazyUpperStore = getUpperStore("lazy-upper-bound", true); 89 lazyUpperStore = getUpperStore("lazy-upper-bound", true);
102// limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true); 90 limitedSkolemUpperStore = new MultiStageQueryEngine("limited-skolem-upper-bound", true);
103 } 91 }
104 92
105 importData(program.getAdditionalDataFile()); 93 importData(program.getAdditionalDataFile());
106 94
107 elho_ontology = new ELHOProfile().getFragment(ontology); 95 elho_ontology = new ELHOProfile().getFragment(ontology);
108 elLowerStore.processOntology(elho_ontology); 96 elLowerStore.processOntology(elho_ontology);
109 } 97 }
110 98
111 public Collection<String> getPredicatesWithGap() { 99 public Collection<String> getPredicatesWithGap() {
112 return predicatesWithGap; 100 if(isDisposed()) throw new DisposedException();
113 } 101 return predicatesWithGap;
102 }
114 103
115 @Override 104 @Override
116 public boolean preprocess() { 105 public boolean preprocess() {
117 t.reset(); 106 if(isDisposed()) throw new DisposedException();
118 Utility.logInfo("Preprocessing... checking satisfiability... "); 107 t.reset();
108 Utility.logInfo("Preprocessing... checking satisfiability... ");
119 109
120 String name = "data", datafile = importedData.toString(); 110 String name = "data", datafile = importedData.toString();
121 rlLowerStore.importRDFData(name, datafile); 111 rlLowerStore.importRDFData(name, datafile);
122 rlLowerStore.materialise("lower program", program.getLower().toString()); 112 rlLowerStore.materialise("lower program", program.getLower().toString());
123// program.getLower().save(); 113// program.getLower().save();
124 if(!consistency.checkRLLowerBound()) return false; 114 if(!consistency.checkRLLowerBound()) return false;
125 Utility.logInfo("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber()); 115 Utility.logInfo("The number of sameAs assertions in RL lower store: " + rlLowerStore.getSameAsNumber());
126 116
127 String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology); 117 String originalMarkProgram = OWLHelper.getOriginalMarkProgram(ontology);
128 118
129 elLowerStore.importRDFData(name, datafile); 119 elLowerStore.importRDFData(name, datafile);
130 elLowerStore.materialise("saturate named individuals", originalMarkProgram); 120 elLowerStore.materialise("saturate named individuals", originalMarkProgram);
131 elLowerStore.materialise("lower program", program.getLower().toString()); 121 elLowerStore.materialise("lower program", program.getLower().toString());
132 elLowerStore.initialiseKarma(); 122 elLowerStore.initialiseKarma();
133 if(!consistency.checkELLowerBound()) return false; 123 if(!consistency.checkELLowerBound()) return false;
134 124
135 if(lazyUpperStore != null) { 125 if(lazyUpperStore != null) {
136 lazyUpperStore.importRDFData(name, datafile); 126 lazyUpperStore.importRDFData(name, datafile);
137 lazyUpperStore.materialise("saturate named individuals", originalMarkProgram); 127 lazyUpperStore.materialise("saturate named individuals", originalMarkProgram);
138 int tag = lazyUpperStore.materialiseRestrictedly(program, null); 128 int tag = lazyUpperStore.materialiseRestrictedly(program, null);
139 if(tag != 1) { 129 if(tag != 1) {
140 lazyUpperStore.dispose(); 130 lazyUpperStore.dispose();
141 lazyUpperStore = null; 131 lazyUpperStore = null;
142 } 132 }
143 if(tag == -1) return false; 133 if(tag == -1) return false;
144 } 134 }
145 if(consistency.checkUpper(lazyUpperStore)) { 135 if(consistency.checkUpper(lazyUpperStore)) {
146 satisfiable = SatisfiabilityStatus.SATISFIABLE; 136 satisfiable = SatisfiabilityStatus.SATISFIABLE;
147 Utility.logInfo("time for satisfiability checking: " + t.duration()); 137 Utility.logInfo("time for satisfiability checking: " + t.duration());
148 } 138 }
149 139
150// if(limitedSkolemUpperStore != null) { 140 if(limitedSkolemUpperStore != null) {
151// limitedSkolemUpperStore.importRDFData(name, datafile); 141 limitedSkolemUpperStore.importRDFData(name, datafile);
152// limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); 142 limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram);
153// int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null); 143 int tag = limitedSkolemUpperStore.materialiseSkolemly(program, null);
154// if(tag != 1) { 144 if(tag != 1) {
155// limitedSkolemUpperStore.dispose(); 145 limitedSkolemUpperStore.dispose();
156// limitedSkolemUpperStore = null; 146 limitedSkolemUpperStore = null;
157// } 147 }
158// if(tag == -1) return false; 148 if(tag == -1) return false;
159// } 149 }
160// if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) { 150 if(satisfiable == SatisfiabilityStatus.UNCHECKED && consistency.checkUpper(limitedSkolemUpperStore)) {
161// satisfiable = SatisfiabilityStatus.SATISFIABLE; 151 satisfiable = SatisfiabilityStatus.SATISFIABLE;
162// Utility.logInfo("time for satisfiability checking: " + t.duration()); 152 Utility.logInfo("time for satisfiability checking: " + t.duration());
163// } 153 }
164 154
165 trackingStore.importRDFData(name, datafile); 155 trackingStore.importRDFData(name, datafile);
166 trackingStore.materialise("saturate named individuals", originalMarkProgram); 156 trackingStore.materialise("saturate named individuals", originalMarkProgram);
167 157
168// materialiseFullUpper(); 158// materialiseFullUpper();
169// GapByStore4ID gap = new GapByStore4ID(trackingStore); 159// GapByStore4ID gap = new GapByStore4ID(trackingStore);
170 GapByStore4ID gap = new GapByStore4ID2(trackingStore, rlLowerStore); 160 GapByStore4ID gap = new GapByStore4ID2(trackingStore, rlLowerStore);
171 trackingStore.materialiseFoldedly(program, gap); 161 trackingStore.materialiseFoldedly(program, gap);
172 predicatesWithGap = gap.getPredicatesWithGap(); 162 predicatesWithGap = gap.getPredicatesWithGap();
173 gap.clear(); 163 gap.clear();
174 164
175 if(program.getGeneral().isHorn()) 165 if(program.getGeneral().isHorn())
176 encoder = new TrackingRuleEncoderWithGap(program.getUpper(), trackingStore); 166 encoder = new TrackingRuleEncoderWithGap(program.getUpper(), trackingStore);
177 else 167 else
178 encoder = new TrackingRuleEncoderDisjVar1(program.getUpper(), trackingStore); 168 encoder = new TrackingRuleEncoderDisjVar1(program.getUpper(), trackingStore);
179// encoder = new TrackingRuleEncoderDisj1(program.getUpper(), trackingStore); 169// encoder = new TrackingRuleEncoderDisj1(program.getUpper(), trackingStore);
180// encoder = new TrackingRuleEncoderDisjVar2(program.getUpper(), trackingStore); 170// encoder = new TrackingRuleEncoderDisjVar2(program.getUpper(), trackingStore);
181// encoder = new TrackingRuleEncoderDisj2(program.getUpper(), trackingStore); 171// encoder = new TrackingRuleEncoderDisj2(program.getUpper(), trackingStore);
182 172
183 program.deleteABoxTurtleFile(); 173 if(!isConsistent())
184 174 return false;
185 if(!isConsistent()) 175
186 return false; 176 consistency.extractBottomFragment();
187 177 consistency.dispose();
188 consistency.extractBottomFragment(); 178
189 consistency.dispose(); 179 program.dispose();
190 180
191 return true; 181 return true;
192 } 182 }
193 183
194 @Override 184 @Override
195 public boolean isConsistent() { 185 public boolean isConsistent() {
196 if(satisfiable == SatisfiabilityStatus.UNCHECKED) { 186 if(isDisposed()) throw new DisposedException();
197 satisfiable = consistency.check() ? SatisfiabilityStatus.SATISFIABLE : SatisfiabilityStatus.UNSATISFIABLE; 187 if(satisfiable == SatisfiabilityStatus.UNCHECKED) {
198 Utility.logInfo("time for satisfiability checking: " + t.duration()); 188 satisfiable = consistency.check() ? SatisfiabilityStatus.SATISFIABLE : SatisfiabilityStatus.UNSATISFIABLE;
199 } 189 Utility.logInfo("time for satisfiability checking: " + t.duration());
200 return satisfiable == SatisfiabilityStatus.SATISFIABLE; 190 }
201 } 191 return satisfiable == SatisfiabilityStatus.SATISFIABLE;
202 192 }
203 /** 193
204 * It deals with blanks nodes differently from variables 194 @Override
205 * according to SPARQL semantics for OWL2 Entailment Regime. 195 public void evaluate(QueryRecord queryRecord) {
206 * <p> 196 if(isDisposed()) throw new DisposedException();
207 * In particular variables are matched only against named individuals, 197 if(queryBounds(queryRecord))
208 * and blank nodes against named and anonymous individuals. 198 return;
209 */ 199
210 private boolean queryUpperStore(BasicQueryEngine upperStore, QueryRecord queryRecord, 200 OWLOntology relevantOntologySubset = extractRelevantOntologySubset(queryRecord);
211 Tuple<String> extendedQuery, Step step) { 201
212 202 int aBoxCount = relevantOntologySubset.getABoxAxioms(true).size();
213 if(queryRecord.hasNonAnsDistinguishedVariables()) 203 Utility.logInfo("Relevant ontology subset: ABox_axioms=" + aBoxCount + " TBox_axioms=" + (relevantOntologySubset
214 queryUpperBound(upperStore, queryRecord, extendedQuery.get(0), queryRecord.getAnswerVariables()); 204 .getAxiomCount() - aBoxCount));
215 else 205// queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl");
216 queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); 206
217 207 if(querySkolemisedRelevantSubset(relevantOntologySubset, queryRecord))
218 queryRecord.addProcessingTime(step, t.duration()); 208 return;
219 if(queryRecord.isProcessed()) { 209
220 queryRecord.setDifficulty(step); 210 Timer t = new Timer();
221 return true; 211 Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT());
222 } 212 summarisedChecker.check(queryRecord.getGapAnswers());
223 return false; 213 summarisedChecker.dispose();
224 } 214 Utility.logDebug("Total time for full reasoner: " + t.duration());
225 215 queryRecord.markAsProcessed();
226 /** 216 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty());
227 * Returns the part of the ontology relevant for Hermit, while computing the bound answers. 217 }
228 * */ 218
229 private boolean queryBounds(QueryRecord queryRecord) { 219 @Override
230 AnswerTuples rlAnswer = null, elAnswer = null; 220 public void evaluateUpper(QueryRecord queryRecord) {
231 221 if(isDisposed()) throw new DisposedException();
232 t.reset(); 222 // TODO add new upper store
233 try { 223 AnswerTuples rlAnswer = null;
234 rlAnswer = rlLowerStore.evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables()); 224 boolean useFull = queryRecord.isBottom() || lazyUpperStore == null;
235 Utility.logDebug(t.duration()); 225 try {
236 queryRecord.updateLowerBoundAnswers(rlAnswer); 226 rlAnswer =
237 } finally { 227 (useFull ? trackingStore : lazyUpperStore).evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables());
238 if (rlAnswer != null) rlAnswer.dispose(); 228 queryRecord.updateUpperBoundAnswers(rlAnswer, true);
239 } 229 } finally {
240 queryRecord.addProcessingTime(Step.LOWER_BOUND, t.duration()); 230 if(rlAnswer != null) rlAnswer.dispose();
241 231 }
242 t.reset(); 232 }
243 233
244 Tuple<String> extendedQueryTexts = queryRecord.getExtendedQueryText(); 234 @Override
245 235 public void dispose() {
246 Utility.logDebug("Tracking store"); 236 super.dispose();
247 if(queryUpperStore(trackingStore, queryRecord, extendedQueryTexts, Step.SIMPLE_UPPER_BOUND)) 237
248 return true; 238 if(encoder != null) encoder.dispose();
249 239 if(rlLowerStore != null) rlLowerStore.dispose();
250 if(!queryRecord.isBottom()) { 240 if(lazyUpperStore != null) lazyUpperStore.dispose();
251 Utility.logDebug("Lazy store"); 241 if(elLowerStore != null) elLowerStore.dispose();
252 if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND)) 242 if(trackingStore != null) trackingStore.dispose();
253 return true; 243 if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose();
244
245 }
246
247 private BasicQueryEngine getUpperStore(String name, boolean checkValidity) {
248 if(multiStageTag)
249 return new MultiStageQueryEngine(name, checkValidity);
250// return new TwoStageQueryEngine(name, checkValidity);
251 else
252 return new BasicQueryEngine(name);
253 }
254
255 protected void internal_importDataFile(String name, String datafile) {
256// addDataFile(datafile);
257 rlLowerStore.importRDFData(name, datafile);
258 if(lazyUpperStore != null)
259 lazyUpperStore.importRDFData(name, datafile);
260 elLowerStore.importRDFData(name, datafile);
261 trackingStore.importRDFData(name, datafile);
262 }
263
264 /**
265 * It deals with blanks nodes differently from variables
266 * according to SPARQL semantics for OWL2 Entailment Regime.
267 * <p>
268 * In particular variables are matched only against named individuals,
269 * and blank nodes against named and anonymous individuals.
270 */
271 private boolean queryUpperStore(BasicQueryEngine upperStore, QueryRecord queryRecord,
272 Tuple<String> extendedQuery, Step step) {
273
274 if(queryRecord.hasNonAnsDistinguishedVariables())
275 queryUpperBound(upperStore, queryRecord, extendedQuery.get(0), queryRecord.getAnswerVariables());
276 else
277 queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables());
278
279 queryRecord.addProcessingTime(step, t.duration());
280 if(queryRecord.isProcessed()) {
281 queryRecord.setDifficulty(step);
282 return true;
283 }
284 return false;
285 }
286
287 /**
288 * Returns the part of the ontology relevant for Hermit, while computing the bound answers.
289 */
290 private boolean queryBounds(QueryRecord queryRecord) {
291 AnswerTuples rlAnswer = null, elAnswer = null;
292
293 t.reset();
294 try {
295 rlAnswer = rlLowerStore.evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables());
296 Utility.logDebug(t.duration());
297 queryRecord.updateLowerBoundAnswers(rlAnswer);
298 } finally {
299 if(rlAnswer != null) rlAnswer.dispose();
300 }
301 queryRecord.addProcessingTime(Step.LOWER_BOUND, t.duration());
302
303 t.reset();
304
305 Tuple<String> extendedQueryTexts = queryRecord.getExtendedQueryText();
306
307 Utility.logDebug("Tracking store");
308 if(queryUpperStore(trackingStore, queryRecord, extendedQueryTexts, Step.SIMPLE_UPPER_BOUND))
309 return true;
310
311 if(!queryRecord.isBottom()) {
312 Utility.logDebug("Lazy store");
313 if(lazyUpperStore != null && queryUpperStore(lazyUpperStore, queryRecord, extendedQueryTexts, Step.LAZY_UPPER_BOUND))
314 return true;
254// Utility.logDebug("Skolem store"); 315// Utility.logDebug("Skolem store");
255// if(limitedSkolemUpperStore != null && queryUpperStore(limitedSkolemUpperStore, queryRecord, extendedQueryTexts, Step.L_SKOLEM_UPPER_BOUND)) 316// if(limitedSkolemUpperStore != null && queryUpperStore(limitedSkolemUpperStore, queryRecord, extendedQueryTexts, Step.L_SKOLEM_UPPER_BOUND))
256// return null; 317// return null;
257 } 318 }
258 319
259 t.reset(); 320 t.reset();
260 try { 321 try {
261 elAnswer = elLowerStore.evaluate(extendedQueryTexts.get(0), 322 elAnswer = elLowerStore.evaluate(extendedQueryTexts.get(0),
262 queryRecord.getAnswerVariables(), 323 queryRecord.getAnswerVariables(),
263 queryRecord.getLowerBoundAnswers()); 324 queryRecord.getLowerBoundAnswers());
264 Utility.logDebug(t.duration()); 325 Utility.logDebug(t.duration());
265 queryRecord.updateLowerBoundAnswers(elAnswer); 326 queryRecord.updateLowerBoundAnswers(elAnswer);
266 } finally { 327 } finally {
267 if (elAnswer != null) elAnswer.dispose(); 328 if(elAnswer != null) elAnswer.dispose();
268 } 329 }
269 queryRecord.addProcessingTime(Step.EL_LOWER_BOUND, t.duration()); 330 queryRecord.addProcessingTime(Step.EL_LOWER_BOUND, t.duration());
270 331
271 if(queryRecord.isProcessed()) { 332 if(queryRecord.isProcessed()) {
272 queryRecord.setDifficulty(Step.EL_LOWER_BOUND); 333 queryRecord.setDifficulty(Step.EL_LOWER_BOUND);
273 return true; 334 return true;
274 } 335 }
275 336
276 return false; 337 return false;
277 } 338 }
278 339
279 private OWLOntology extractRelevantOntologySubset(QueryRecord queryRecord) { 340 private OWLOntology extractRelevantOntologySubset(QueryRecord queryRecord) {
280 t.reset(); 341 t.reset();
281 342
282 QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord); 343 QueryTracker tracker = new QueryTracker(encoder, rlLowerStore, queryRecord);
283 OWLOntology relevantOntologySubset = tracker.extract(trackingStore, consistency.getQueryRecords(), true); 344 OWLOntology relevantOntologySubset = tracker.extract(trackingStore, consistency.getQueryRecords(), true);
284 345
285 queryRecord.addProcessingTime(Step.FRAGMENT, t.duration()); 346 queryRecord.addProcessingTime(Step.FRAGMENT, t.duration());
286 347
287 return relevantOntologySubset; 348 return relevantOntologySubset;
288 } 349 }
289 350
290 private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) { 351 private void queryUpperBound(BasicQueryEngine upperStore, QueryRecord queryRecord, String queryText, String[] answerVariables) {
291 AnswerTuples rlAnswer = null; 352 AnswerTuples rlAnswer = null;
292 try { 353 try {
293 Utility.logDebug(queryText); 354 Utility.logDebug(queryText);
294 rlAnswer = upperStore.evaluate(queryText, answerVariables); 355 rlAnswer = upperStore.evaluate(queryText, answerVariables);
295 Utility.logDebug(t.duration()); 356 Utility.logDebug(t.duration());
296 queryRecord.updateUpperBoundAnswers(rlAnswer); 357 queryRecord.updateUpperBoundAnswers(rlAnswer);
297 } finally { 358 } finally {
298 if(rlAnswer != null) rlAnswer.dispose(); 359 if(rlAnswer != null) rlAnswer.dispose();
299 } 360 }
300 } 361 }
301 362
302 @Override 363 private boolean querySkolemisedRelevantSubset(OWLOntology relevantSubset, QueryRecord queryRecord) {
303 public void evaluate(QueryRecord queryRecord) { 364 DatalogProgram relevantProgram = new DatalogProgram(relevantSubset, false); // toClassify is false
304 if(queryBounds(queryRecord)) 365
305 return; 366 MultiStageQueryEngine relevantStore =
306 367 new MultiStageQueryEngine("Relevant-store", true); // checkValidity is true
307 OWLOntology relevantOntologySubset = extractRelevantOntologySubset(queryRecord); 368// relevantStore.importRDFData("data", relevantProgram.getAdditionalDataFile()); // tried, doesn't work
308 369 relevantStore.importDataFromABoxOf(relevantSubset);
309 int aBoxCount = relevantOntologySubset.getABoxAxioms(true).size(); 370
310 Utility.logInfo("Relevant ontology subset: ABox_axioms=" + aBoxCount + " TBox_axioms=" + (relevantOntologySubset 371 int materialisationResult = relevantStore.materialiseSkolemly(relevantProgram, null);
311 .getAxiomCount() - aBoxCount)); 372// int materialisationResult = relevantStore.materialiseRestrictedly(relevantProgram, null); // DOESN'T WORK!!!
312// queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); 373 if(materialisationResult != 1)
313 374 throw new RuntimeException("Skolemised materialisation error"); // TODO check consistency
314 if(querySkolemisedRelevantSubset(relevantOntologySubset, queryRecord))
315 return;
316
317 Timer t = new Timer();
318 Checker summarisedChecker = new HermitSummaryFilter(queryRecord, properties.getToCallHermiT());
319 summarisedChecker.check(queryRecord.getGapAnswers());
320 summarisedChecker.dispose();
321 Utility.logDebug("Total time for full reasoner: " + t.duration());
322 queryRecord.markAsProcessed();
323 Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty());
324 }
325
326 private boolean querySkolemisedRelevantSubset(OWLOntology relevantSubset, QueryRecord queryRecord) {
327 MultiStageQueryEngine relevantStore =
328 new MultiStageQueryEngine("Relevant-store", true); // checkValidity is true
329 DatalogProgram relevantProgram = new DatalogProgram(relevantSubset, false); // toClassify is false
330
331// relevantStore.importRDFData("data", importedData.toString()); // 2 answers more
332 relevantStore.importDataFromABoxOf(relevantSubset);
333
334 int materialisationResult = relevantStore.materialiseSkolemly(relevantProgram, null);
335 if(materialisationResult != 1)
336 throw new RuntimeException("Skolemised materialisation error"); // TODO check consistency
337// relevantStore.materialiseRestrictedly(relevantProgram, null); // it has been tried 375// relevantStore.materialiseRestrictedly(relevantProgram, null); // it has been tried
338 376
339 return queryUpperStore(relevantStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND); 377 return queryUpperStore(relevantStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND);
340 378
341 // the following has been tried 379// return queryUpperStore(limitedSkolemUpperStore, queryRecord, queryRecord.getExtendedQueryText(), Step.L_SKOLEM_UPPER_BOUND);
342// Tuple<String> extendedQueryText = queryRecord.getExtendedQueryText(); 380 }
343// if(queryRecord.hasNonAnsDistinguishedVariables()) { 381
344// queryUpperBound(relevantStore, queryRecord, extendedQueryText.get(0), queryRecord.getAnswerVariables()); 382 enum SatisfiabilityStatus {SATISFIABLE, UNSATISFIABLE, UNCHECKED}
345// queryUpperBound(relevantStore, queryRecord, extendedQueryText.get(1), queryRecord.getDistinguishedVariables());
346// }
347// else
348// queryUpperBound(relevantStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables());
349//
350// return queryRecord.isProcessed();
351
352 }
353
354 @Override
355 public void evaluateUpper(QueryRecord queryRecord) {
356 // TODO add new upper store
357 AnswerTuples rlAnswer = null;
358 boolean useFull = queryRecord.isBottom() || lazyUpperStore == null;
359 try {
360 rlAnswer =
361 (useFull ? trackingStore : lazyUpperStore).evaluate(queryRecord.getQueryText(), queryRecord.getAnswerVariables());
362 queryRecord.updateUpperBoundAnswers(rlAnswer, true);
363 } finally {
364 if(rlAnswer != null) rlAnswer.dispose();
365 }
366 }
367
368 @Override
369 public void dispose() {
370 if (encoder != null) encoder.dispose();
371 if (rlLowerStore != null) rlLowerStore.dispose();
372 if (lazyUpperStore != null) lazyUpperStore.dispose();
373 if (elLowerStore != null) elLowerStore.dispose();
374 if (trackingStore != null) trackingStore.dispose();
375
376// if(limitedSkolemUpperStore != null) limitedSkolemUpperStore.dispose();
377 super.dispose();
378 }
379
380 enum SatisfiabilityStatus {SATISFIABLE, UNSATISFIABLE, UNCHECKED}
381 383
382} 384}