aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-10 18:17:06 +0100
committerFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-11 12:34:47 +0100
commit17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch)
tree47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java
parent8651164cd632a5db310b457ce32d4fbc97bdc41c (diff)
downloadACQuA-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/tracking/QueryTracker.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java440
1 files changed, 0 insertions, 440 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java b/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java
deleted file mode 100644
index 27d3a53..0000000
--- a/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java
+++ /dev/null
@@ -1,440 +0,0 @@
1package uk.ac.ox.cs.pagoda.tracking;
2
3import org.semanticweb.HermiT.model.DLClause;
4import org.semanticweb.owlapi.model.*;
5import uk.ac.ox.cs.JRDFox.JRDFStoreException;
6import uk.ac.ox.cs.JRDFox.model.Datatype;
7import uk.ac.ox.cs.JRDFox.store.DataStore;
8import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType;
9import uk.ac.ox.cs.JRDFox.store.Resource;
10import uk.ac.ox.cs.JRDFox.store.TupleIterator;
11import uk.ac.ox.cs.pagoda.MyPrefixes;
12import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
13import uk.ac.ox.cs.pagoda.owl.OWLHelper;
14import uk.ac.ox.cs.pagoda.query.AnswerTuple;
15import uk.ac.ox.cs.pagoda.query.QueryRecord;
16import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine;
17import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager;
18import uk.ac.ox.cs.pagoda.util.Namespace;
19import uk.ac.ox.cs.pagoda.util.Timer;
20import uk.ac.ox.cs.pagoda.util.UFS;
21import uk.ac.ox.cs.pagoda.util.Utility;
22
23import java.util.HashSet;
24import java.util.Iterator;
25import java.util.LinkedList;
26import java.util.Set;
27
28public class QueryTracker {
29
30 QueryRecord m_record;
31 BasicQueryEngine m_dataStore;
32 TrackingRuleEncoder m_encoder;
33
34 OWLOntologyManager m_manager;
35 Set<OWLAxiom> m_tBoxAxioms;
36
37 UFS<String> equalityGroups;
38
39 public QueryTracker(TrackingRuleEncoder encoder, BasicQueryEngine lowerStore, QueryRecord queryRecord) {
40 m_encoder = encoder;
41 m_dataStore = lowerStore;
42 m_record = queryRecord;
43
44 m_manager = m_encoder.getOntology().getOWLOntologyManager();
45 equalityGroups = m_dataStore.getEqualityGroups(true);
46
47 }
48
49 public OWLOntology extract(BasicQueryEngine trackingStore, QueryRecord[] botQuerRecords, boolean incrementally) {
50 try {
51 if (m_record.getRelevantOntology() != null) {
52 m_manager.removeOntology(m_record.getRelevantOntology());
53 m_record.setRelevantOntology(null);
54 m_record.clearClauses();
55 }
56 m_record.setRelevantOntology(m_manager.createOntology());
57 } catch (OWLOntologyCreationException e) {
58 e.printStackTrace();
59 }
60
61 m_encoder.setCurrentQuery(m_record);
62
63// m_encoder.encodingQuery(botQuerRecords);
64// m_encoder.saveTrackingRules("tracking_query" + m_record.getQueryID() + ".dlog");
65
66 DataStore store = trackingStore.getDataStore();
67 long oldTripleCount, tripleCount;
68 try {
69 Timer t1 = new Timer();
70 oldTripleCount = store.getTriplesCount();
71 // store.addRules(new String[] {m_encoder.getTrackingProgram()});
72 store.importRules(m_encoder.getTrackingProgram(), UpdateType.ScheduleForAddition);
73 store.applyReasoning(incrementally);
74 tripleCount = store.getTriplesCount();
75
76 Utility.logDebug("tracking store after materialising tracking program: "
77 + tripleCount
78 + " ("
79 + (tripleCount - oldTripleCount)
80 + " new)");
81 Utility.logDebug("tracking store finished the materialisation of tracking program in "
82 + t1.duration() + " seconds.");
83 } catch (JRDFStoreException e) {
84 e.printStackTrace();
85 }
86 extractAxioms(trackingStore);
87
88 trackingStore.clearRulesAndIDBFacts(m_encoder.getAddedData());
89
90 if (!m_record.isBottom()
91 && !(m_encoder instanceof TrackingRuleEncoderDisj2))
92 addRelatedAxiomsAndClauses(botQuerRecords);
93
94 return m_record.getRelevantOntology();
95 }
96
97 public void extractAxioms(BasicQueryEngine trackingStore) {
98 Set<String> unaryPredicates = new HashSet<String>();
99 Set<String> binaryPredicates = new HashSet<String>();
100
101 getDerivedPredicates(trackingStore, unaryPredicates, binaryPredicates);
102
103 /**
104 * To add all the relavant ABox assertions to the fragment
105 */
106
107 int aboxAxiomCounter = extractUnaryTuples(trackingStore,
108 m_manager.getOWLDataFactory(), unaryPredicates)
109 + extractBinaryTuples(trackingStore,
110 m_manager.getOWLDataFactory(), binaryPredicates);
111
112 /**
113 * To all all the relavant TBox rules to the fragment
114 */
115 String queryText = m_encoder.getSelectedSPARQLQuery();
116
117 DLClause clause;
118 m_tBoxAxioms = new HashSet<OWLAxiom>();
119 OWLAxiom tBoxAxiom;
120 TupleIterator answers = null;
121 String answer;
122 try {
123 answers = trackingStore.internal_evaluate(queryText);
124 for (long multi = answers.open(); multi != 0; multi = answers.getNext()) {
125 answer = answers.getResource(0).m_lexicalForm;
126 clause = m_encoder.getSelectedClause(MyPrefixes.PAGOdAPrefixes.expandIRI(answer));
127 if (DLClauseHelper.isTautologyAboutDifferentFrom(clause))
128 continue;
129 tBoxAxiom = m_encoder.getProgram().getEquivalentAxiom(clause);
130 m_record.addRelevantClauses(clause);
131 m_tBoxAxioms.add(tBoxAxiom);
132 }
133 } catch (JRDFStoreException e) {
134 e.printStackTrace();
135 } finally {
136 if (answers != null) answers.dispose();
137 }
138
139 Utility.logTrace("Extracted TBox axioms: ");
140 for (OWLAxiom axiom : m_tBoxAxioms) {
141 Utility.logTrace(axiom);
142 m_manager.addAxiom(m_record.getRelevantOntology(), axiom);
143 }
144 Utility.logDebug("TBox extraction Done");
145
146 Utility.logDebug("Before adding bottom fragment:\nABoxAxiomsCount = "
147 + aboxAxiomCounter + ", TBoxAxiomsCount = "
148 + m_tBoxAxioms.size());
149
150 }
151
152 public void addRelatedAxiomsAndClauses(QueryRecord[] botQueryRecords) {
153 LinkedList<QueryRecord> toAddedRecords = new LinkedList<QueryRecord>();
154
155 for(QueryRecord botQueryRecord : botQueryRecords)
156 if(overlappingDisjunctiveClauses(botQueryRecord) != null)
157 toAddedRecords.add(botQueryRecord);
158
159 for(QueryRecord botQueryRecord : toAddedRecords) {
160 m_manager.addAxioms(m_record.getRelevantOntology(), botQueryRecord.getRelevantOntology().getAxioms());
161 for(DLClause clause : botQueryRecord.getRelevantClauses())
162 m_record.addRelevantClauses(clause);
163 }
164
165 if(!toAddedRecords.isEmpty())
166 Utility.logDebug("Part of bottom fragments is added for this query.");
167 else
168 Utility.logDebug("None of bottom fragments is added for this query.");
169 }
170
171 private int extractBinaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> binaryPredicates) {
172 OWLOntology fragment = m_record.getRelevantOntology();
173 int count;
174 int aboxAxiomCounter = 0;
175 Resource sub, obj;
176 OWLAxiom aboxAxiom;
177 String trackingIRI;
178 Set<Integer> trackedIDEqualities = new HashSet<Integer>();
179 Set<String> trackedEntityEqualities = new HashSet<String>();
180 TupleIterator trackingAnswers, lowerAnswers;
181
182 for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) {
183 trackingIRI = iter.next();
184 String propIRI = m_encoder.getOriginalPredicate(trackingIRI);
185 if(propIRI == null) continue;
186 if (!propIRI.equals(Namespace.EQUALITY_QUOTED)) continue;
187 trackingAnswers = null;
188 try {
189 trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI));
190 for (long multi = trackingAnswers.open(); multi != 0; multi = trackingAnswers.getNext()) {
191 if (trackingAnswers.getResourceID(0) != trackingAnswers.getResourceID(1)) {
192 for(int i = 0; i < 2; ++i)
193 if (trackedIDEqualities.add(trackingAnswers.getResourceID(i))) {
194 trackedEntityEqualities.add(trackingAnswers.getResource(i).m_lexicalForm);
195 }
196 }
197 }
198 } catch (JRDFStoreException e) {
199 e.printStackTrace();
200 } finally {
201 if(trackingAnswers != null) trackingAnswers.dispose();
202 }
203 iter.remove();
204 break;
205 }
206
207 String sub_rep, obj_rep;
208
209 for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) {
210 trackingIRI = iter.next();
211 count = 0;
212 String propIRI = m_encoder.getOriginalPredicate(trackingIRI);
213 if(propIRI == null) continue;
214 iter.remove();
215 lowerAnswers = null;
216 trackingAnswers = null;
217 Set<String> lower = new HashSet<String>();
218 OWLObject prop = null;
219 try {
220 trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI));
221 trackingAnswers.open();
222 if(trackingAnswers.getMultiplicity() == 0) continue;
223
224 lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Binary(propIRI));
225 lowerAnswers.open();
226 if(lowerAnswers.getMultiplicity() == 0) continue;
227
228 StringBuilder builder = new StringBuilder();
229 for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) {
230 sub = lowerAnswers.getResource(0);
231 obj = lowerAnswers.getResource(1);
232 builder.setLength(0);
233 builder.append(equalityGroups.find(sub.m_lexicalForm))
234 .append(AnswerTuple.SEPARATOR)
235 .append(equalityGroups.find(obj.m_lexicalForm));
236 lower.add(builder.toString());
237 }
238
239 for (long multi = trackingAnswers.getMultiplicity(); multi != 0; multi = trackingAnswers.getNext()) {
240 sub = trackingAnswers.getResource(0);
241 obj = trackingAnswers.getResource(1);
242 builder.setLength(0);
243 sub_rep = equalityGroups.find(sub.m_lexicalForm);
244 obj_rep = equalityGroups.find(obj.m_lexicalForm);
245 if (!sub_rep.equals(sub.m_lexicalForm) || !obj_rep.equals(obj.m_lexicalForm)) continue;
246
247 builder.append(sub_rep).append(AnswerTuple.SEPARATOR).append(obj_rep);
248 if (lower.contains(builder.toString())) {
249 OWLObject owlObj = getOWLObject(obj, factory);
250 if (owlObj instanceof OWLIndividual) {
251 if (prop == null)
252 prop = factory.getOWLObjectProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI));
253 aboxAxiom = factory.getOWLObjectPropertyAssertionAxiom(
254 (OWLObjectProperty) prop,
255 factory.getOWLNamedIndividual(IRI.create(sub_rep)),
256 factory.getOWLNamedIndividual(IRI.create(obj_rep)));
257 }
258 else if (owlObj instanceof OWLLiteral) {
259 if (prop == null)
260 prop = factory.getOWLDataProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI));
261 aboxAxiom = factory.getOWLDataPropertyAssertionAxiom(
262 (OWLDataProperty) prop,
263 factory.getOWLNamedIndividual(IRI.create(sub_rep)),
264 (OWLLiteral) owlObj);
265 }
266 else {
267 Utility.logError("There might be an error here ... ");
268 continue;
269 }
270 if (!fragment.containsAxiom(aboxAxiom)) {
271 m_manager.addAxiom(fragment, aboxAxiom);
272 ++aboxAxiomCounter;
273 ++count;
274 }
275 }
276 }
277 } catch (JRDFStoreException e) {
278 e.printStackTrace();
279 } finally {
280 if (trackingAnswers != null) trackingAnswers.dispose();
281 if (lowerAnswers != null) lowerAnswers.dispose();
282 lower.clear();
283 }
284 Utility.logDebug("property: " + propIRI + " " + count);
285 }
286
287 count = 0;
288 String value;
289 OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY));
290 for (String key: equalityGroups.keySet()) {
291 if(!trackedEntityEqualities.contains(key)) continue;
292 value = equalityGroups.find(key);
293 m_manager.addAxiom(fragment, factory.getOWLObjectPropertyAssertionAxiom(
294 sameAs,
295 factory.getOWLNamedIndividual(IRI.create(key)),
296 factory.getOWLNamedIndividual(IRI.create(value))));
297 ++aboxAxiomCounter;
298 ++count;
299 }
300 Utility.logDebug("property: " + Namespace.EQUALITY_QUOTED + " " + count);
301
302 trackedEntityEqualities.clear();
303 trackedIDEqualities.clear();
304 Utility.logTrace(Namespace.EQUALITY_QUOTED + " " + count);
305
306 Utility.logDebug("ABox extraction Done");
307 return aboxAxiomCounter;
308 }
309
310 private OWLObject getOWLObject(Resource rdfoxTerm, OWLDataFactory factory) {
311 if (rdfoxTerm.m_datatype.equals(Datatype.IRI_REFERENCE))
312 return factory.getOWLNamedIndividual(IRI.create(rdfoxTerm.m_lexicalForm));
313// if (rdfoxTerm.m_datatype.equals(Datatype.OWL_REAL) || rdfoxTerm.m_datatype.equals(Datatype.OWL_RATIONAL) ||
314// rdfoxTerm.m_datatype.equals(Datatype.XSD_DECIMAL) || rdfoxTerm.m_datatype.equals(Datatype.XSD_INTEGER) ||
315// rdfoxTerm.m_datatype.equals(Datatype.XSD_NON_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_POSITIVE_INTEGER) ||
316// rdfoxTerm.m_datatype.equals(Datatype.XSD_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_LONG) ||
317// rdfoxTerm.m_datatype.equals(Datatype.XSD_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_SHORT) ||
318// rdfoxTerm.m_datatype.equals(Datatype.XSD_BYTE) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_LONG) ||
319// rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_SHORT) ||
320// rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_BYTE))
321 if (rdfoxTerm.m_datatype.equals(Datatype.XSD_DATE))
322 return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(Namespace.XSD_STRING)));
323
324 else
325 return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(rdfoxTerm.m_datatype
326 .getIRI())));
327 }
328
329 private int extractUnaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> unaryPredicates) {
330 OWLOntology fragment = m_record.getRelevantOntology();
331 int count;
332 int aboxAxiomCounter = 0;
333 String answer;
334 OWLAxiom aboxAxiom;
335 for (String trackingIRI : unaryPredicates) {
336 count = 0;
337 String clsIRI = m_encoder.getOriginalPredicate(trackingIRI);
338 if (clsIRI == null)
339 continue;
340 TupleIterator answers = null, lowerAnswers = null;
341 Set<String> lower = new HashSet<String>();
342 OWLClass cls = factory.getOWLClass(IRI.create(clsIRI.startsWith("<") ? OWLHelper.removeAngles(clsIRI) : clsIRI));
343 try {
344 answers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Unary(trackingIRI));
345 answers.open();
346 if(answers.getMultiplicity() == 0) continue;
347
348 lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Unary(clsIRI));
349 lowerAnswers.open();
350 if (lowerAnswers.getMultiplicity() == 0) continue;
351
352 for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext())
353 lower.add(equalityGroups.find(lowerAnswers.getResource(0).m_lexicalForm));
354
355 for (long multi = answers.getMultiplicity(); multi != 0; multi = answers.getNext()) {
356 answer = equalityGroups.find(answers.getResource(0).m_lexicalForm);
357 if (lower.contains(answer)) {
358 OWLIndividual instance = factory.getOWLNamedIndividual(IRI.create(answer));
359 aboxAxiom = factory.getOWLClassAssertionAxiom(cls,instance);
360 if (!fragment.containsAxiom(aboxAxiom)) {
361 m_manager.addAxiom(fragment, aboxAxiom);
362 ++aboxAxiomCounter;
363 ++count;
364 }
365 }
366 }
367 } catch (JRDFStoreException e) {
368 e.printStackTrace();
369 } finally {
370 if (answers != null) answers.dispose();
371 if (lowerAnswers != null) lowerAnswers.dispose();
372 lower.clear();
373 }
374 Utility.logDebug("class: " + clsIRI + " " + count);
375 }
376 return aboxAxiomCounter;
377 }
378
379 private void getDerivedPredicates(BasicQueryEngine trackingStore, Set<String> unaryPredicates, Set<String> binaryPredicates) {
380 TupleIterator derivedTuples = null;
381 String selectedPredicate = OWLHelper.addAngles(m_encoder.getSelectedPredicate());
382 try {
383 derivedTuples = trackingStore.internal_evaluateAgainstIDBs("select distinct ?z where { ?x <" + Namespace.RDF_TYPE + "> ?z . }");
384 for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) {
385 String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0));
386 if (p.equals(selectedPredicate)) ;
387 else if (m_encoder.isAuxPredicate(p)) ;
388 else unaryPredicates.add(p);
389 }
390 } catch (JRDFStoreException e) {
391 e.printStackTrace();
392 } finally {
393 if (derivedTuples != null) derivedTuples.dispose();
394 }
395
396 derivedTuples = null;
397 try {
398 derivedTuples = trackingStore.internal_evaluateAgainstIDBs("select distinct ?y where { ?x ?y ?z . }");
399 for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) {
400 String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0));
401 if (p.equals(Namespace.RDF_TYPE_ABBR) || p.equals(Namespace.RDF_TYPE_QUOTED)) ;
402 else if (p.equals(Namespace.EQUALITY_ABBR) || p.equals(Namespace.EQUALITY_QUOTED)) ;
403 else if (m_encoder.isAuxPredicate(p)) ;
404 else binaryPredicates.add(p);
405 }
406 } catch (JRDFStoreException e) {
407 e.printStackTrace();
408 } finally {
409 if (derivedTuples != null) derivedTuples.dispose();
410 }
411 }
412
413 private Set<DLClause> overlappingDisjunctiveClauses(
414 QueryRecord botQueryRecord) {
415 if (m_tBoxAxioms == null)
416 return null;
417
418 Set<DLClause> disjunctiveRules = new HashSet<DLClause>();
419 Set<DLClause> clauses = m_record.getRelevantClauses();
420 for (DLClause clause : botQueryRecord.getRelevantClauses())
421 if (clause.getHeadLength() > 1 && clauses.contains(clause))
422 disjunctiveRules.add(clause);
423
424 return disjunctiveRules.isEmpty() ? null : disjunctiveRules;
425 }
426
427 private String getSPARQLQuery4Unary(String p) {
428 StringBuilder builder = new StringBuilder();
429 builder.append("select ?x where { ?x <").append(Namespace.RDF_TYPE).append("> ");
430 builder.append(p).append(" . }");
431 return builder.toString();
432 }
433
434 private String getSPARQLQuery4Binary(String p) {
435 StringBuilder builder = new StringBuilder();
436 builder.append("select ?x ?y where { ?x ").append(p).append(" ?y . }");
437 return builder.toString();
438 }
439
440}