aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoderDisjVar1.java
blob: d96c7473dfe6fa2237527ef653890f4eea95cb90 (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
package uk.ac.ox.cs.pagoda.tracking;

import org.semanticweb.HermiT.model.*;
import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
import uk.ac.ox.cs.pagoda.multistage.Normalisation;
import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine;
import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram;
import uk.ac.ox.cs.pagoda.rules.approximators.OverApproxExist;
import uk.ac.ox.cs.pagoda.util.Namespace;
import uk.ac.ox.cs.pagoda.util.Utility;

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

public class TrackingRuleEncoderDisjVar1 extends TrackingRuleEncoderWithGap {

	private Set<DLClause> disjunctiveRules = new HashSet<DLClause>();
	private Variable X = Variable.create("X"), Y = Variable.create("Y");
	private String bottomTrackingProgram = null;
	
	public TrackingRuleEncoderDisjVar1(UpperDatalogProgram program, BasicQueryEngine store) {
		super(program, store);
	}
	
	@Override
	public boolean encodingRules() {
		if (super.encodingRules()) {
			processDisjunctiveRules();
			return true;
		}
		return false;
	}
	
	@Override
	protected void encodingRule(DLClause clause) {
		if (currentQuery.isBottom()) {
//			super.encodingRule(clause);
			encodingBottomQueryClause(clause);
			return;
		}

		DLClause original = program.getCorrespondingClause(clause);
		if (original.getHeadLength() <= 1) {
			super.encodingRule(clause);
		}
		else {
			if (!DLClauseHelper.hasSubsetBodyAtoms(clause, original))
				super.encodingRule(clause);
			addDisjunctiveRule(original);
		}

	}

	private void processDisjunctiveRules() {
		for (DLClause clause: disjunctiveRules)
			encodingDisjunctiveRule(clause);
	}

	private Atom getAuxiliaryAtom(Atom headAtom) {
		DLPredicate p = headAtom.getDLPredicate();
		if (p instanceof AtLeast || p instanceof AtLeast) {
			return Atom.create(generateAuxiliaryRule((AtLeast) p, true), headAtom.getArgument(0));
		}
		if(p instanceof AtomicConcept)
			return Atom.create(generateAuxiliaryRule((AtomicConcept) p), headAtom.getArgument(0));
		if(p instanceof AtomicRole)
			return Atom.create(generateAuxiliaryRule((AtomicRole) p), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Equality || p instanceof AnnotatedEquality)
			return Atom.create(generateAuxiliaryRule(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Inequality)
			return Atom.create(generateAuxiliaryRule((Inequality) p), headAtom.getArgument(0), headAtom.getArgument(1));

		return null;
	}

	private Atom getTrackingAtom(Atom headAtom) {
		DLPredicate p = headAtom.getDLPredicate();
		if (p instanceof AtLeast) {
			p = Normalisation.toAtLeastConcept((AtLeast) p);
			return Atom.create(getTrackingDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom
					.getArgument(0));
		}
		if(p instanceof AtomicConcept)
			return Atom.create(getTrackingDLPredicate(p), headAtom.getArgument(0));
		if(p instanceof AtomicRole)
			return Atom.create(getTrackingDLPredicate(p), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Equality || p instanceof AnnotatedEquality)
			return Atom.create(getTrackingDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Inequality)
			return Atom.create(getTrackingDLPredicate(p), headAtom.getArgument(0), headAtom.getArgument(1));

		return null;
	}

	private Atom getGapAtom(Atom headAtom) {
		DLPredicate p = headAtom.getDLPredicate();
		if (p instanceof AtLeast) {
			p = Normalisation.toAtLeastConcept((AtLeast) p);
			return Atom.create(getGapDLPredicate(AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p))), headAtom
					.getArgument(0));
		}
		if(p instanceof AtomicConcept)
			return Atom.create(getGapDLPredicate(p), headAtom.getArgument(0));
		if(p instanceof AtomicRole)
			return Atom.create(getGapDLPredicate(p), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Equality || p instanceof AnnotatedEquality)
			return Atom.create(getGapDLPredicate(Equality.INSTANCE), headAtom.getArgument(0), headAtom.getArgument(1));
		if(p instanceof Inequality)
			return Atom.create(getGapDLPredicate(p), headAtom.getArgument(0), headAtom.getArgument(1));
		if (p instanceof DatatypeRestriction)
			return Atom.create(getGapDLPredicate(p), headAtom.getArgument(0));
		Utility.logError(p + " is not recognised.");
		return null;
	}

	private void encodingDisjunctiveRule(DLClause clause) {
		int headLength = clause.getHeadLength();

		Atom[] auxAtoms = new Atom[headLength];
		for (int i = 0; i < headLength; ++i)
			auxAtoms[i] = getAuxiliaryAtom(clause.getHeadAtom(i));

		Atom[] trackingAtoms = new Atom[headLength];
		for (int i = 0; i < headLength; ++i)
			trackingAtoms[i] = getTrackingAtom(clause.getHeadAtom(i));

		Atom[] gapAtoms = new Atom[headLength];
		for (int i = 0; i < headLength; ++i)
			gapAtoms[i] = getGapAtom(clause.getHeadAtom(i));

		Atom[] bodyAtoms = clause.getBodyAtoms();

		LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>();
		DLPredicate selected = AtomicConcept.create(getSelectedPredicate());
		newHeadAtoms.add(Atom.create(selected, getIndividual4GeneralRule(clause)));

		for (Atom atom: bodyAtoms) {
			Atom newAtom = Atom.create(
					getTrackingDLPredicate(atom.getDLPredicate()),
					DLClauseHelper.getArguments(atom));
			newHeadAtoms.add(newAtom);
		}

		DLClause newClause;
		int index;
		for (int j = 0; j < headLength; ++j) {
			Atom[] newBodyAtoms = new Atom[headLength * 2 + bodyAtoms.length];
			index = 0;
			for (int i = 0; i < headLength; ++i, ++index)
				newBodyAtoms[index] = gapAtoms[i];
			for (int i = 0; i < headLength; ++i, ++index)
				if (i != j)
					newBodyAtoms[index] = auxAtoms[i];
				else
					newBodyAtoms[index] = trackingAtoms[i];

			for (int i = 0; i < bodyAtoms.length; ++i, ++index)
				newBodyAtoms[index] = bodyAtoms[i];

			for (Atom atom: newHeadAtoms) {
				newClause = DLClause.create(new Atom[]{atom}, newBodyAtoms);
				addTrackingClause(newClause);
			}
		}
	}
	
	private void addTrackingClause(DLClause clause) {
		trackingClauses.add(clause);
	}

	private void addDisjunctiveRule(DLClause clause) {
		disjunctiveRules.add(clause);
	}

	private DLPredicate getAuxPredicate(DLPredicate p) {
		if (p instanceof AtLeastConcept) {
			StringBuilder builder = new StringBuilder(
					Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p));
			builder.append("_AUXa").append(currentQuery.getQueryID());
			return AtomicConcept.create(builder.toString());
		}

		return getDLPredicate(p, "_AUXa" + currentQuery.getQueryID());
	}

	private DLPredicate getTrackingBottomDLPredicate(DLPredicate p) {
		return getDLPredicate(p, getTrackingSuffix("0"));
	}

	private DLPredicate generateAuxiliaryRule(AtLeast p1, boolean withAux) {
		AtLeastConcept p = Normalisation.toAtLeastConcept(p1);

		int num = p.getNumber();
		Variable[] Ys = new Variable[num];
		if (num > 1)
			for(int i = 0; i < num; ++i)
				Ys[i] = Variable.create("Y" + (i + 1));
		else
			Ys[0] = Y;

		Collection<Atom> expandedAtom = new LinkedList<Atom>();
		Collection<Atom> representativeAtom = new LinkedList<Atom>();
		if (p.getOnRole() instanceof AtomicRole) {
			AtomicRole r = (AtomicRole) p.getOnRole();
			for(int i = 0; i < num; ++i)
				expandedAtom.add(Atom.create(r, X, Ys[i]));
			representativeAtom.add(Atom.create(r, X, Ys[0]));
		}
		else {
			AtomicRole r = ((InverseRole) p.getOnRole()).getInverseOf();
			for(int i = 0; i < num; ++i)
				expandedAtom.add(Atom.create(r, Ys[i], X));
			representativeAtom.add(Atom.create(r, Ys[0], X));
		}

		if (num > 1) {
			representativeAtom.add(Atom.create(Inequality.INSTANCE, Ys[0], Ys[1]));
		}
		for (int i = 0; i < num; ++i)
			for (int j = i + 1; j < num; ++j)
				expandedAtom.add(Atom.create(Inequality.INSTANCE, Ys[i], Ys[j]));

		if (!p.getToConcept().equals(AtomicConcept.THING)) {
			AtomicConcept c;
			if(p.getToConcept() instanceof AtomicConcept)
				c = (AtomicConcept) p.getToConcept();
			else {
				c = OverApproxExist.getNegationConcept(((AtomicNegationConcept) p.getToConcept()).getNegatedAtomicConcept());
			}
			for (int i = 0; i < num; ++i)
				expandedAtom.add(Atom.create(c, Ys[i]));
			representativeAtom.add(Atom.create(c, Ys[0]));
		}

		AtomicConcept ac = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct(p));
		DLPredicate trackingPredicate = getTrackingDLPredicate(ac);
		DLPredicate gapPredicate = getGapDLPredicate(ac);
		DLPredicate auxPredicate = withAux ? getAuxPredicate(p) : null;

		for (Atom atom: representativeAtom) {
			Atom[] bodyAtoms = new Atom[expandedAtom.size() + 1];
			if (atom.getArity() == 1)
				bodyAtoms[0] = Atom.create(getTrackingDLPredicate(atom.getDLPredicate()), atom.getArgument(0));
			else
				bodyAtoms[0] = Atom.create(getTrackingDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1));
			int i = 0;
			for (Atom bodyAtom: expandedAtom)
				bodyAtoms[++i] = bodyAtom;
			addTrackingClause(DLClause.create(new Atom[] {Atom.create(trackingPredicate, X)}, bodyAtoms));

			bodyAtoms = new Atom[expandedAtom.size() + 1];
			if (atom.getArity() == 1)
				bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0));
			else
				bodyAtoms[0] = Atom.create(getGapDLPredicate(atom.getDLPredicate()), atom.getArgument(0), atom.getArgument(1));
			i = 0;
			for (Atom bodyAtom: expandedAtom)
				bodyAtoms[++i] = bodyAtom;
			addTrackingClause(DLClause.create(new Atom[] {Atom.create(gapPredicate, X)}, bodyAtoms));

			if (withAux) {
				bodyAtoms = new Atom[expandedAtom.size() + 1];
				bodyAtoms[0] = getAuxiliaryAtom(atom);
				i = 0;
				for (Atom bodyAtom: expandedAtom)
					bodyAtoms[++i] = bodyAtom;
				addTrackingClause(DLClause.create(new Atom[] {Atom.create(auxPredicate, X)}, bodyAtoms));
			}
		}

		return withAux ? auxPredicate : trackingPredicate;
	}

	private DLPredicate generateAuxiliaryRule(AtomicRole p) {
		if(currentQuery.isBottom())
			return getTrackingDLPredicate(p);

		DLPredicate ret = getAuxPredicate(p);
		Atom[] headAtom = new Atom[] {Atom.create(ret, X, Y)};

		addTrackingClause(
				DLClause.create(headAtom, new Atom[]{Atom.create(getTrackingDLPredicate(p), X, Y)}));
		addTrackingClause(
				DLClause.create(headAtom, new Atom[]{Atom.create(getTrackingBottomDLPredicate(p), X, Y)}));

		return ret;
	}

	private DLPredicate generateAuxiliaryRule(AtomicConcept p) {
		if (currentQuery.isBottom())
			return getTrackingDLPredicate(p);

		DLPredicate ret = getAuxPredicate(p);
		Atom[] headAtom = new Atom[]{Atom.create(ret, X)};
		addTrackingClause(
				DLClause.create(headAtom,
								new Atom[]{Atom.create(getTrackingDLPredicate(p), X)}));
		addTrackingClause(
				DLClause.create(headAtom,
						new Atom[] { Atom.create(getTrackingBottomDLPredicate(p), X)}));

		return ret;
	}

	private DLPredicate generateAuxiliaryRule(Equality instance) {
		return generateAuxiliaryRule(AtomicRole.create(Namespace.EQUALITY));
	}

	private DLPredicate generateAuxiliaryRule(Inequality instance) {
		return generateAuxiliaryRule(AtomicRole.create(Namespace.INEQUALITY));
	}

	@Override
	public String getTrackingProgram() {
		StringBuilder sb = getTrackingProgramBody();
		if (currentQuery.isBottom())
			sb.append(getBottomTrackingProgram());
		sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText());
		return sb.toString();
	}

	private String getBottomTrackingProgram() {
		if (bottomTrackingProgram != null) return bottomTrackingProgram.replace("_tn", getTrackingPredicate(""));  
		
		String bottomSuffix = getTrackingSuffix("0"); 
		LinkedList<DLClause> clauses = new LinkedList<DLClause>();  
		Variable X = Variable.create("X"); 
		for (String concept: unaryPredicates)
			clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicConcept.create(concept + bottomSuffix) , X)}, 
										new Atom[] {Atom.create(AtomicConcept.create(concept + "_tn"), X)}));
		Variable Y = Variable.create("Y"); 
		for (String role: binaryPredicates)
			clauses.add(DLClause.create(new Atom[] {Atom.create(AtomicRole.create(role + bottomSuffix) , X, Y)}, 
										new Atom[] {Atom.create(AtomicRole.create(role + "_tn"), X, Y) }));
		
		StringBuilder builder = new StringBuilder(DLClauseHelper.toString(clauses));
		bottomTrackingProgram = builder.toString();
		return bottomTrackingProgram.replace("_tn", getTrackingPredicate("")); 
	}

	private void encodingBottomQueryClause(DLClause clause) {
		if (!clause.toString().contains("owl:Nothing"))
			clause = program.getCorrespondingClause(clause);
		
//		Term t;
//		for (Atom tAtom: clause.getHeadAtoms()) {
//			for (int i = 0; i < tAtom.getArity(); ++i)
//				if ((t = tAtom.getArgument(i)) instanceof Individual) 
//					if (((Individual) t).getIRI().startsWith(OverApproxExist.SKOLEMISED_INDIVIDUAL_PREFIX))
//						clause = program.getCorrespondingClause(clause); 				
//		}
		
		LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>();
		Atom selectAtom = Atom.create(selected, getIndividual4GeneralRule(program.getCorrespondingClause(clause)));
		
		for (Atom atom: clause.getBodyAtoms()) {
			atom = Atom.create(
					getTrackingDLPredicate(atom.getDLPredicate()), 
					DLClauseHelper.getArguments(atom));
			newHeadAtoms.add(atom);
		}

		DLClause newClause;
		
		boolean botInHead = clause.getBodyLength() == 1 && clause.getBodyAtom(0).getDLPredicate().toString().contains("owl:Nothing");
		
		DLPredicate[] trackingPredicates = new DLPredicate[clause.getHeadLength()];
		DLPredicate[] predicates = new DLPredicate[clause.getHeadLength()];
		int headIndex = 0; 
		DLPredicate trackingPredicate, p; 
		for (Atom headAtom: clause.getHeadAtoms()) {
			if ((p = headAtom.getDLPredicate()) instanceof AtLeastConcept) {
				trackingPredicate = generateAuxiliaryRule((AtLeastConcept) p, false);
				p = AtomicConcept.create(Normalisation.getAuxiliaryConcept4Disjunct((AtLeastConcept) p));
				trackingClauses.add(DLClause.create(
						new Atom[] { Atom.create(getDLPredicate(p, getTrackingSuffix("0")), X) }, 
						new Atom[] { Atom.create(trackingPredicate, X) })); 
			}
			else 
				trackingPredicate = getTrackingDLPredicate(p);
			
			trackingPredicates[headIndex] = trackingPredicate;
			predicates[headIndex] = p; 
			++headIndex; 			
		}
		
		headIndex = 0;
		int headLength = clause.getHeadLength(); 
		Atom[] gapAtoms = new Atom[headLength];
		for (int i = 0; i < headLength; ++i)
			gapAtoms[i] = getGapAtom(clause.getHeadAtom(i)); 
//			Atom.create(getGapDLPredicate(predicates[headIndex]), DLClauseHelper.getArguments(clause.getHeadAtom(i))); 
		int index, selectIndex; 
		for (Atom headAtom: clause.getHeadAtoms()) {
			index = 0; selectIndex = 0; 
			Atom[] newBodyAtoms = new Atom[clause.getBodyLength() + 1 + headLength - 1 + (botInHead ? 0 : headLength)];
			Atom[] selectBodyAtoms = new Atom[clause.getBodyLength() + 1 + (botInHead ? 0 : headLength)];
			newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = Atom.create(trackingPredicates[headIndex], DLClauseHelper.getArguments(headAtom));
			
			if (!botInHead) {
				for (int i = 0; i < headLength; ++i)
					newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = gapAtoms[i];
			}
	
			for (int i = 0; i < headLength; ++i)
				if (i != headIndex) {
					newBodyAtoms[index++] = Atom.create(getDLPredicate(predicates[i], getTrackingSuffix("0")), DLClauseHelper.getArguments(clause.getHeadAtom(i)));
				}
				
			for (int i = 0; i < clause.getBodyLength(); ++i) 
				newBodyAtoms[index++] = selectBodyAtoms[selectIndex++] = clause.getBodyAtom(i);

			for (Atom atom: newHeadAtoms) {
				newClause = DLClause.create(new Atom[] {atom}, newBodyAtoms); 
				trackingClauses.add(newClause);
			}
			trackingClauses.add(DLClause.create(new Atom[] {selectAtom}, selectBodyAtoms)); 
			++headIndex; 
		}
	}
	
}