aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java
blob: 27d3a53cdd428dfbf6336aa431d43711c8494118 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package uk.ac.ox.cs.pagoda.tracking;

import org.semanticweb.HermiT.model.DLClause;
import org.semanticweb.owlapi.model.*;
import uk.ac.ox.cs.JRDFox.JRDFStoreException;
import uk.ac.ox.cs.JRDFox.model.Datatype;
import uk.ac.ox.cs.JRDFox.store.DataStore;
import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType;
import uk.ac.ox.cs.JRDFox.store.Resource;
import uk.ac.ox.cs.JRDFox.store.TupleIterator;
import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
import uk.ac.ox.cs.pagoda.owl.OWLHelper;
import uk.ac.ox.cs.pagoda.query.AnswerTuple;
import uk.ac.ox.cs.pagoda.query.QueryRecord;
import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine;
import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager;
import uk.ac.ox.cs.pagoda.util.Namespace;
import uk.ac.ox.cs.pagoda.util.Timer;
import uk.ac.ox.cs.pagoda.util.UFS;
import uk.ac.ox.cs.pagoda.util.Utility;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

public class QueryTracker {

	QueryRecord m_record;
	BasicQueryEngine m_dataStore;
	TrackingRuleEncoder m_encoder;

	OWLOntologyManager m_manager;
	Set<OWLAxiom> m_tBoxAxioms;
	
	UFS<String> equalityGroups; 

	public QueryTracker(TrackingRuleEncoder encoder, BasicQueryEngine lowerStore, QueryRecord queryRecord) {
		m_encoder = encoder;
		m_dataStore = lowerStore;
		m_record = queryRecord;

		m_manager = m_encoder.getOntology().getOWLOntologyManager();
		equalityGroups = m_dataStore.getEqualityGroups(true); 

	}

	public OWLOntology extract(BasicQueryEngine trackingStore, QueryRecord[] botQuerRecords, boolean incrementally) {
		try {
			if (m_record.getRelevantOntology() != null) {
				m_manager.removeOntology(m_record.getRelevantOntology());
				m_record.setRelevantOntology(null);
				m_record.clearClauses();
			}
			m_record.setRelevantOntology(m_manager.createOntology());
		} catch (OWLOntologyCreationException e) {
			e.printStackTrace();
		}

		m_encoder.setCurrentQuery(m_record);

//		m_encoder.encodingQuery(botQuerRecords);
//		m_encoder.saveTrackingRules("tracking_query" + m_record.getQueryID() + ".dlog");

		DataStore store = trackingStore.getDataStore();
		long oldTripleCount, tripleCount;
		try {
			Timer t1 = new Timer();
			oldTripleCount = store.getTriplesCount();
			// store.addRules(new String[] {m_encoder.getTrackingProgram()});
			store.importRules(m_encoder.getTrackingProgram(), UpdateType.ScheduleForAddition);
			store.applyReasoning(incrementally);
			tripleCount = store.getTriplesCount();

			Utility.logDebug("tracking store after materialising tracking program: "
					+ tripleCount
					+ " ("
					+ (tripleCount - oldTripleCount)
					+ " new)");
			Utility.logDebug("tracking store finished the materialisation of tracking program in "
					+ t1.duration() + " seconds.");
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		} 
		extractAxioms(trackingStore);

		trackingStore.clearRulesAndIDBFacts(m_encoder.getAddedData()); 

		if (!m_record.isBottom()
				&& !(m_encoder instanceof TrackingRuleEncoderDisj2))
			addRelatedAxiomsAndClauses(botQuerRecords);
		
		return m_record.getRelevantOntology();
	}

	public void extractAxioms(BasicQueryEngine trackingStore) {
		Set<String> unaryPredicates = new HashSet<String>();
		Set<String> binaryPredicates = new HashSet<String>();

		getDerivedPredicates(trackingStore, unaryPredicates, binaryPredicates);

		/**
		 * To add all the relavant ABox assertions to the fragment
		 */

		int aboxAxiomCounter = extractUnaryTuples(trackingStore,
				m_manager.getOWLDataFactory(), unaryPredicates)
				+ extractBinaryTuples(trackingStore,
						m_manager.getOWLDataFactory(), binaryPredicates);

		/**
		 * To all all the relavant TBox rules to the fragment
		 */
		String queryText = m_encoder.getSelectedSPARQLQuery();

		DLClause clause;
		m_tBoxAxioms = new HashSet<OWLAxiom>();
		OWLAxiom tBoxAxiom;
		TupleIterator answers = null;
		String answer;
		try {
			answers = trackingStore.internal_evaluate(queryText);
			for (long multi = answers.open(); multi != 0; multi = answers.getNext()) {
				answer = answers.getResource(0).m_lexicalForm;
				clause = m_encoder.getSelectedClause(MyPrefixes.PAGOdAPrefixes.expandIRI(answer));
				if (DLClauseHelper.isTautologyAboutDifferentFrom(clause))
					continue;
				tBoxAxiom = m_encoder.getProgram().getEquivalentAxiom(clause);
				m_record.addRelevantClauses(clause);
				m_tBoxAxioms.add(tBoxAxiom);
			}
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		} finally {
			if (answers != null) answers.dispose();
		}

		Utility.logTrace("Extracted TBox axioms: ");
		for (OWLAxiom axiom : m_tBoxAxioms) {
			Utility.logTrace(axiom);
			m_manager.addAxiom(m_record.getRelevantOntology(), axiom);
		}
		Utility.logDebug("TBox extraction Done");

		Utility.logDebug("Before adding bottom fragment:\nABoxAxiomsCount = "
				+ aboxAxiomCounter + ", TBoxAxiomsCount = "
				+ m_tBoxAxioms.size());

	}

	public void addRelatedAxiomsAndClauses(QueryRecord[] botQueryRecords) {
		LinkedList<QueryRecord> toAddedRecords = new LinkedList<QueryRecord>();

		for(QueryRecord botQueryRecord : botQueryRecords)
			if(overlappingDisjunctiveClauses(botQueryRecord) != null)
				toAddedRecords.add(botQueryRecord);

		for(QueryRecord botQueryRecord : toAddedRecords) {
			m_manager.addAxioms(m_record.getRelevantOntology(), botQueryRecord.getRelevantOntology().getAxioms());
			for(DLClause clause : botQueryRecord.getRelevantClauses())
				m_record.addRelevantClauses(clause);
		}

		if(!toAddedRecords.isEmpty())
			Utility.logDebug("Part of bottom fragments is added for this query.");
		else
			Utility.logDebug("None of bottom fragments is added for this query.");
	}

	private int extractBinaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> binaryPredicates) {
		OWLOntology fragment = m_record.getRelevantOntology();
		int count;
		int aboxAxiomCounter = 0;
		Resource sub, obj;
		OWLAxiom aboxAxiom;
		String trackingIRI;
		Set<Integer> trackedIDEqualities = new HashSet<Integer>();
		Set<String> trackedEntityEqualities = new HashSet<String>();
		TupleIterator trackingAnswers, lowerAnswers;

		for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) {
			trackingIRI = iter.next();
			String propIRI = m_encoder.getOriginalPredicate(trackingIRI);
			if(propIRI == null) continue;
			if (!propIRI.equals(Namespace.EQUALITY_QUOTED)) continue;
			trackingAnswers = null;
			try {
				trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI));
				for (long multi = trackingAnswers.open(); multi != 0; multi = trackingAnswers.getNext()) {
					if (trackingAnswers.getResourceID(0) != trackingAnswers.getResourceID(1)) {
						for(int i = 0; i < 2; ++i)
							if (trackedIDEqualities.add(trackingAnswers.getResourceID(i))) {
								trackedEntityEqualities.add(trackingAnswers.getResource(i).m_lexicalForm);
							}
					}
				}
			} catch (JRDFStoreException e) {
				e.printStackTrace();
			} finally {
				if(trackingAnswers != null) trackingAnswers.dispose();
			}
			iter.remove();
			break;
		}

		String sub_rep, obj_rep;

		for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) {
			trackingIRI = iter.next();
			count = 0;
			String propIRI = m_encoder.getOriginalPredicate(trackingIRI);
			if(propIRI == null) continue;
			iter.remove();
			lowerAnswers = null;
			trackingAnswers = null;
			Set<String> lower = new HashSet<String>();
			OWLObject prop = null;
			try {
				trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI));
				trackingAnswers.open();
				if(trackingAnswers.getMultiplicity() == 0) continue;

				lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Binary(propIRI));
				lowerAnswers.open();
				if(lowerAnswers.getMultiplicity() == 0) continue;

				StringBuilder builder = new StringBuilder();
				for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) {
					sub = lowerAnswers.getResource(0);
					obj = lowerAnswers.getResource(1);
					builder.setLength(0);
					builder.append(equalityGroups.find(sub.m_lexicalForm))
						   .append(AnswerTuple.SEPARATOR)
						   .append(equalityGroups.find(obj.m_lexicalForm));
					lower.add(builder.toString());
				}

				for (long multi = trackingAnswers.getMultiplicity(); multi != 0; multi = trackingAnswers.getNext()) {
					sub = trackingAnswers.getResource(0);
					obj = trackingAnswers.getResource(1);
					builder.setLength(0);
					sub_rep = equalityGroups.find(sub.m_lexicalForm);
					obj_rep = equalityGroups.find(obj.m_lexicalForm);
					if (!sub_rep.equals(sub.m_lexicalForm) || !obj_rep.equals(obj.m_lexicalForm)) continue;

					builder.append(sub_rep).append(AnswerTuple.SEPARATOR).append(obj_rep);
					if (lower.contains(builder.toString())) {
						OWLObject owlObj = getOWLObject(obj, factory);
						if (owlObj instanceof OWLIndividual) {
							if (prop == null)
								prop = factory.getOWLObjectProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI));
							aboxAxiom = factory.getOWLObjectPropertyAssertionAxiom(
									(OWLObjectProperty) prop,
									factory.getOWLNamedIndividual(IRI.create(sub_rep)),
									factory.getOWLNamedIndividual(IRI.create(obj_rep)));
						}
						else if (owlObj instanceof OWLLiteral) {
							if (prop == null)
								prop = factory.getOWLDataProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI));
							aboxAxiom = factory.getOWLDataPropertyAssertionAxiom(
									(OWLDataProperty) prop,
									factory.getOWLNamedIndividual(IRI.create(sub_rep)),
									(OWLLiteral) owlObj);
						}
						else {
							Utility.logError("There might be an error here ... ");
							continue;
						}
						if (!fragment.containsAxiom(aboxAxiom)) {
							m_manager.addAxiom(fragment, aboxAxiom);
							++aboxAxiomCounter;
							++count;
						}
					}
				}
			} catch (JRDFStoreException e) {
				e.printStackTrace();
			} finally {
				if (trackingAnswers != null) trackingAnswers.dispose();
				if (lowerAnswers != null) lowerAnswers.dispose();
				lower.clear();
			}
			Utility.logDebug("property: " + propIRI + " " + count);
		}

		count = 0;
		String value;
		OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY));
		for (String key: equalityGroups.keySet()) {
			if(!trackedEntityEqualities.contains(key)) continue;
			value = equalityGroups.find(key);
			m_manager.addAxiom(fragment, factory.getOWLObjectPropertyAssertionAxiom(
					sameAs,
					factory.getOWLNamedIndividual(IRI.create(key)),
					factory.getOWLNamedIndividual(IRI.create(value))));
			++aboxAxiomCounter;
			++count;
		}
		Utility.logDebug("property: " + Namespace.EQUALITY_QUOTED + " " + count);

		trackedEntityEqualities.clear();
		trackedIDEqualities.clear();
		Utility.logTrace(Namespace.EQUALITY_QUOTED + " " + count);

		Utility.logDebug("ABox extraction Done");
		return aboxAxiomCounter;
	}

	private OWLObject getOWLObject(Resource rdfoxTerm, OWLDataFactory factory) {
		if (rdfoxTerm.m_datatype.equals(Datatype.IRI_REFERENCE))
			return factory.getOWLNamedIndividual(IRI.create(rdfoxTerm.m_lexicalForm));
//		if (rdfoxTerm.m_datatype.equals(Datatype.OWL_REAL) || rdfoxTerm.m_datatype.equals(Datatype.OWL_RATIONAL) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_DECIMAL) || rdfoxTerm.m_datatype.equals(Datatype.XSD_INTEGER) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_NON_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_POSITIVE_INTEGER) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_LONG) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_SHORT) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_BYTE) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_LONG) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_SHORT) ||
//				rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_BYTE))
		if (rdfoxTerm.m_datatype.equals(Datatype.XSD_DATE))
			return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(Namespace.XSD_STRING)));

		else
			return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(rdfoxTerm.m_datatype
																											.getIRI())));
	}

	private int extractUnaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> unaryPredicates) {
		OWLOntology fragment = m_record.getRelevantOntology();
		int count;
		int aboxAxiomCounter = 0;
		String answer;
		OWLAxiom aboxAxiom;
		for (String trackingIRI : unaryPredicates) {
			count = 0;
			String clsIRI = m_encoder.getOriginalPredicate(trackingIRI);
			if (clsIRI == null)
				continue;
			TupleIterator answers = null, lowerAnswers = null;
			Set<String> lower = new HashSet<String>();
			OWLClass cls = factory.getOWLClass(IRI.create(clsIRI.startsWith("<") ? OWLHelper.removeAngles(clsIRI) : clsIRI));
			try {
				answers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Unary(trackingIRI));
				answers.open();
				if(answers.getMultiplicity() == 0) continue;

				lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Unary(clsIRI));
				lowerAnswers.open();
				if (lowerAnswers.getMultiplicity() == 0) continue;

				for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext())
					lower.add(equalityGroups.find(lowerAnswers.getResource(0).m_lexicalForm));

				for (long multi = answers.getMultiplicity(); multi != 0; multi = answers.getNext()) {
					answer = equalityGroups.find(answers.getResource(0).m_lexicalForm);
					if (lower.contains(answer)) {
						OWLIndividual instance = factory.getOWLNamedIndividual(IRI.create(answer));
						aboxAxiom = factory.getOWLClassAssertionAxiom(cls,instance);
						if (!fragment.containsAxiom(aboxAxiom)) {
							m_manager.addAxiom(fragment, aboxAxiom);
							++aboxAxiomCounter;
							++count;
						}
					}
				}
			} catch (JRDFStoreException e) {
				e.printStackTrace();
			} finally {
				if (answers != null) answers.dispose();
				if (lowerAnswers != null) lowerAnswers.dispose();
				lower.clear();
			}
			Utility.logDebug("class: " + clsIRI + " " + count);
		}
		return aboxAxiomCounter;
	}

	private void getDerivedPredicates(BasicQueryEngine trackingStore, Set<String> unaryPredicates, Set<String> binaryPredicates) {
		TupleIterator derivedTuples = null;
		String selectedPredicate = OWLHelper.addAngles(m_encoder.getSelectedPredicate());
		try {
			derivedTuples = trackingStore.internal_evaluateAgainstIDBs("select distinct ?z where { ?x <" + Namespace.RDF_TYPE + "> ?z . }");
			for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) {
				String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0));
				if (p.equals(selectedPredicate)) ;
				else if (m_encoder.isAuxPredicate(p)) ;
				else unaryPredicates.add(p);
			}
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		} finally {
			if (derivedTuples != null) derivedTuples.dispose();
		}

		derivedTuples = null;
		try {
			derivedTuples = trackingStore.internal_evaluateAgainstIDBs("select distinct ?y where { ?x ?y ?z . }");
			for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) {
				String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0));
				if (p.equals(Namespace.RDF_TYPE_ABBR) || p.equals(Namespace.RDF_TYPE_QUOTED)) ;
				else if (p.equals(Namespace.EQUALITY_ABBR) || p.equals(Namespace.EQUALITY_QUOTED)) ;
				else if (m_encoder.isAuxPredicate(p)) ;
				else binaryPredicates.add(p);
			}
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		} finally {
			if (derivedTuples != null) derivedTuples.dispose();
		}
	}

	private Set<DLClause> overlappingDisjunctiveClauses(
			QueryRecord botQueryRecord) {
		if (m_tBoxAxioms == null)
			return null;

		Set<DLClause> disjunctiveRules = new HashSet<DLClause>();
		Set<DLClause> clauses = m_record.getRelevantClauses();
		for (DLClause clause : botQueryRecord.getRelevantClauses())
			if (clause.getHeadLength() > 1 && clauses.contains(clause))
				disjunctiveRules.add(clause);

		return disjunctiveRules.isEmpty() ? null : disjunctiveRules;
	}

	private String getSPARQLQuery4Unary(String p) {
		StringBuilder builder = new StringBuilder();
		builder.append("select ?x where { ?x <").append(Namespace.RDF_TYPE).append("> ");
		builder.append(p).append(" . }");
		return builder.toString();
	}

	private String getSPARQLQuery4Binary(String p) {
		StringBuilder builder = new StringBuilder();
		builder.append("select ?x ?y where { ?x ").append(p).append(" ?y . }");
		return builder.toString();
	}

}