aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/hermit/DLClauseHelper.java
blob: c3f7a2a0ef6ba5ca5297c0e18fb95d5aa0eeb7f6 (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package uk.ac.ox.cs.pagoda.hermit;

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

import org.semanticweb.HermiT.Prefixes;
import org.semanticweb.HermiT.model.AnnotatedEquality;
import org.semanticweb.HermiT.model.AtLeastDataRange;
import org.semanticweb.HermiT.model.Atom;
import org.semanticweb.HermiT.model.AtomicConcept;
import org.semanticweb.HermiT.model.AtomicDataRange;
import org.semanticweb.HermiT.model.AtomicNegationDataRange;
import org.semanticweb.HermiT.model.AtomicRole;
import org.semanticweb.HermiT.model.Constant;
import org.semanticweb.HermiT.model.ConstantEnumeration;
import org.semanticweb.HermiT.model.DLClause;
import org.semanticweb.HermiT.model.DLPredicate;
import org.semanticweb.HermiT.model.Equality;
import org.semanticweb.HermiT.model.Individual;
import org.semanticweb.HermiT.model.Inequality;
import org.semanticweb.HermiT.model.Term;
import org.semanticweb.HermiT.model.Variable;

import uk.ac.ox.cs.pagoda.owl.OWLHelper;
import uk.ac.ox.cs.pagoda.util.Namespace;
import uk.ac.ox.cs.pagoda.util.SparqlHelper;
import uk.ac.ox.cs.pagoda.util.Utility;

public class DLClauseHelper {

	public static String getIRI4Nominal(DLPredicate predicate) {
		return ((AtomicConcept) predicate).getIRI().replace("internal:nom#", "");
	}

	public static DLClause removeNominalConcept(DLClause clause) {
		boolean hasNominals = false; 
		Map<Variable, String> nom2iri = new HashMap<Variable, String>();
		DLPredicate predicate;
		Collection<Atom> remainingBodyAtoms = new LinkedList<Atom>();
		for (Atom atom: clause.getBodyAtoms()) {
			predicate = atom.getDLPredicate();
			if (predicate instanceof AtomicConcept && predicate.toString().startsWith("<internal:nom#")) {
				nom2iri.put(atom.getArgumentVariable(0), getIRI4Nominal(predicate));
				hasNominals = true; 
			}
			else remainingBodyAtoms.add(atom);
		}
		
		if (!hasNominals) return clause; 
		
		Atom[] newHeadAtoms = new Atom[clause.getHeadLength()];
		int i = 0; 
		for (Atom atom: clause.getHeadAtoms()) {
			newHeadAtoms[i++] = getNewAtom(atom, nom2iri); 
		}
		
		Atom[] newBodyAtoms = new Atom[remainingBodyAtoms.size()]; 
		i = 0; 
		for (Atom atom: remainingBodyAtoms) {
			newBodyAtoms[i++] = getNewAtom(atom, nom2iri);  
		}

		return DLClause.create(newHeadAtoms, newBodyAtoms); 
	}
	
	public static Atom getNewAtom(Atom atom, Map<Variable, String> nom2iri) {
		String iri; 
		DLPredicate predicate = atom.getDLPredicate();
		if (predicate instanceof AtomicRole) { 
			boolean updated = false; 
			Term newArg0 = atom.getArgument(0), newArg1 = atom.getArgument(1);
			if (newArg0 instanceof Variable && ((iri = nom2iri.get((Variable) newArg0)) != null))  {
				newArg0 = Individual.create(iri); 
				updated = true; 
			}
			if (newArg1 instanceof Variable && ((iri = nom2iri.get((Variable) newArg1)) != null))  {
				newArg1 = Individual.create(iri); 
				updated = true; 
			}
			if (updated) {
				return Atom.create(atom.getDLPredicate(), newArg0, newArg1);
			}
		}
		else if (predicate instanceof Equality || predicate instanceof AnnotatedEquality) {
//			System.out.println("To be removed ... \n" + atom); 
			Term two[] = new Term[] { atom.getArgument(0), atom.getArgument(1) }; 
			Term t; 
			for (int i = 0; i < 2; ++i) {
				t = atom.getArgument(i);
				if (t != null && t instanceof Variable && (iri = nom2iri.get((Variable) t)) != null) {
					two[i] = Individual.create(iri); 
				}
			}
			return Atom.create(Equality.INSTANCE, two);
		}
		
		return atom; 
	}
	
	/**
	 * This is a preprocess for StatOil:
	 *  
	 * if (AtomicNegationDataRange o (ConstantEnumerate (value))) (Z) 
	 * appears the head, then replace all Z in the clause by value. 
	 * 
	 * 
	 * @param clause
	 */
	public static DLClause replaceWithDataValue(DLClause clause) {
		Map<Variable, Constant> var2dataValue = new HashMap<Variable, Constant>();
		
		boolean update = false; 
		LinkedList<Atom> newHeadAtoms = new LinkedList<Atom>();
		for (Atom cAtom: clause.getHeadAtoms()) {
			DLPredicate dlPredicate = cAtom.getDLPredicate();
			if (dlPredicate instanceof AtomicNegationDataRange) {
				AtomicDataRange atomicDataRange = ((AtomicNegationDataRange) dlPredicate).getNegatedDataRange(); 
				if (atomicDataRange instanceof ConstantEnumeration) {
					ConstantEnumeration enumeration = (ConstantEnumeration) atomicDataRange;  
					if (enumeration.getNumberOfConstants() == 1) {
						Constant constant = enumeration.getConstant(0);
						//TODO replace unsupported datatypes
//						if (constant.getDatatypeURI().endsWith("boolean"))
//							constant = Constant.create(constant.getLexicalForm(), constant.getDatatypeURI().replace("boolean", "string"));  
						var2dataValue.put(cAtom.getArgumentVariable(0), constant);
					}
				}
			}
			else if (dlPredicate instanceof ConstantEnumeration) {
				ConstantEnumeration enu = (ConstantEnumeration) dlPredicate; 
				for (int i = 0; i < enu.getNumberOfConstants(); ++i) 
					newHeadAtoms.add(Atom.create(Equality.INSTANCE, cAtom.getArgument(0), enu.getConstant(i)));
				update = true; 
			}
			else if (dlPredicate instanceof AtLeastDataRange &&	((AtLeastDataRange) dlPredicate).getToDataRange() instanceof ConstantEnumeration) {
				AtLeastDataRange atLeast = (AtLeastDataRange) dlPredicate; 
				ConstantEnumeration enu = (ConstantEnumeration) atLeast.getToDataRange();
				for (int i = 0; i < enu.getNumberOfConstants(); ++i)
					newHeadAtoms.add(Atom.create((AtomicRole) atLeast.getOnRole(), cAtom.getArgument(0), enu.getConstant(i)));
				update = true; 
			}
			else 
				newHeadAtoms.add(cAtom); 
		}
		
		if (var2dataValue.isEmpty()) return update ? DLClause.create(newHeadAtoms.toArray(new Atom[0]), clause.getBodyAtoms()) : clause;
		
		Atom[] newBodyAtoms = new Atom[clause.getBodyLength()]; 
		Term[] newArgs; 
		int index = 0;
		boolean atomUpdated; 
		for (Atom bodyAtom: clause.getBodyAtoms()) {
			newBodyAtoms[index] = bodyAtom;
			atomUpdated = false; 
			newArgs = new Term[bodyAtom.getArity()]; 
			for (int i = 0; i < newArgs.length; ++i) {
				newArgs[i] = bodyAtom.getArgument(i); 
				if (newArgs[i] instanceof Variable && var2dataValue.containsKey(newArgs[i])) { 
					newArgs[i] = var2dataValue.get(newArgs[i]);
					atomUpdated = true; 
				}
			}
			if (atomUpdated)
				newBodyAtoms[index] = Atom.create(bodyAtom.getDLPredicate(), newArgs);
			++index; 
		}

		return DLClause.create(newHeadAtoms.toArray(new Atom[0]), newBodyAtoms); 
	}
	
	public static DLClause simplified(DLClause clause) {
		TermGraph termGraph = new TermGraph(clause); 
		return termGraph.simplify(); 
	}

	public static DLClause contructor_differentFrom(Individual u,	Individual v) {
		Atom equalityAtom = Atom.create(AtomicRole.create(Namespace.EQUALITY), u, v);
		return DLClause.create(new Atom[0], new Atom[] {equalityAtom});  
	}
	
	public static String toString(Collection<DLClause> clauses) {
		StringBuilder buf = new StringBuilder();
		for (DLClause cls: clauses) {
			buf.append(RuleHelper.getText(cls));
			buf.append(Utility.LINE_SEPARATOR); 
		}
		return buf.toString(); 
	}

	public static DLClause getInstance(DLClause queryClause, Map<Variable, Term> assignment) {
		Atom[] newBodyAtoms = new Atom[queryClause.getBodyLength()];
		int index = 0; 
		for (Atom atom: queryClause.getBodyAtoms()) {
			newBodyAtoms[index++] = getInstance(atom, assignment); 
		}
		return DLClause.create(queryClause.getHeadAtoms(), newBodyAtoms);
	}
	
	public static Atom getInstance(Atom atom, Map<Variable, Term> assignment) {
		Term[] args = getArguments(atom);
		for (int i = 0; i < args.length; ++i)
			args[i] = getInstance(args[i], assignment); 
		return Atom.create(atom.getDLPredicate(), args);
	}
	
	private static Term getInstance(Term t, Map<Variable, Term> assignment) {
		if (t instanceof Variable && assignment.containsKey((Variable) t)) 
			return assignment.get(t);
		return t;
	}

	public static Term[] getArguments(Atom atom) {
		int len = atom.getArity(); 
		if (len >= 2) len = 2; 
		Term[] args = new Term[len];
		for (int i = 0; i < len; ++i)
			args[i] = atom.getArgument(i);
		return args;
	}
	
	public static DLClause getQuery(String queryText, Collection<String> answerVariables) {
		Collection<Atom> bodyAtoms = new LinkedList<Atom>();
		SparqlHelper.parse(queryText.replace("_:", "?"), answerVariables, bodyAtoms);
		return DLClause.create(new Atom[0], bodyAtoms.toArray(new Atom[0]));
	}
	
	public static DLClause getQuery_old(String queryText, Collection<String> answerVariables) {
		String key, value;
		Prefixes prefixes = new Prefixes(); 
		if (answerVariables != null) 
			answerVariables.clear();
		String[] temp;
		Term subject, object;
		LinkedList<Atom> bodyAtoms = new LinkedList<Atom>(); 

		for (String line: queryText.split("\n")) {
			line = line.trim(); 
			if (line.startsWith("PREFIX")) {
				key = line.substring(7, line.indexOf(':') + 1);
				value = line.substring(line.indexOf('<') + 1, line.length() - 1);
				prefixes.declarePrefix(key, value);
			} 
			else if (line.startsWith("SELECT")) {
				if (answerVariables == null)
					continue;
				temp = line.split(" ");
				for (int i = 1; i < temp.length; ++i)
					answerVariables.add(temp[i].substring(1));
			} 
			else if (line.startsWith("WHERE"))
				;
			else if (line.startsWith("}"))
				;
			else {
				temp = line.split(" ");
				subject = getTerm(getAbsoluteIRI(temp[0], prefixes));
				temp[1] = getAbsoluteIRI(temp[1], prefixes);
				temp[2] = getAbsoluteIRI(temp[2], prefixes); 
				if (temp[1].equals(Namespace.RDF_TYPE)) {
					temp[2] = getAbsoluteIRI(temp[2], prefixes); 
					bodyAtoms.add(Atom.create(AtomicConcept.create(temp[2]), subject));
				} else {
					object = getTerm(temp[2]);
					Term[] args = {subject, object};
					bodyAtoms.add(Atom.create(AtomicRole.create(temp[1]), args));
				}
			}
		}

		return DLClause.create(new Atom[0], bodyAtoms.toArray(new Atom[0]));
	}

	private static String getAbsoluteIRI(String iri, Prefixes prefixes) {
		if (prefixes.canBeExpanded(iri)) 
			return prefixes.expandAbbreviatedIRI(iri);
		if (iri.startsWith("<"))
			return OWLHelper.removeAngles(iri);
		return iri; 
	}

	private static Term getTerm(String iri) {
		if (iri.startsWith("?"))
			return Variable.create(iri.substring(1));
		else if (iri.contains("http")) 
			return Individual.create(iri); 
		else 
			return getConstant(iri); 
	}

	private static Constant getConstant(String iri) {
		String lexicalForm, datatypeIRI; 
		int index = iri.indexOf("^^"); 
		if (index == -1) { 
			lexicalForm = iri;
			int atPos = iri.indexOf("@"); 
			datatypeIRI = atPos == -1 ? Namespace.XSD_STRING : Namespace.RDF_PLAIN_LITERAL;
		}
		else { 
			if (iri.charAt(index + 2) == '<')
				datatypeIRI = iri.substring(index + 3, iri.length() - 1);
			else 
				datatypeIRI = iri.substring(index + 2);
			lexicalForm = iri.substring(1, index - 1); 
		}
		return Constant.create(lexicalForm, datatypeIRI); 
	}

	public static boolean isFunctionalDataPropertyAxioms(DLClause dlClause) {
		 if (dlClause.getBodyLength()==2 && dlClause.getHeadLength()==1) {
	            DLPredicate atomicRole=dlClause.getBodyAtom(0).getDLPredicate();
	            if (atomicRole instanceof AtomicRole) {
	                if (dlClause.getBodyAtom(1).getDLPredicate().equals(atomicRole) && 
	                		(dlClause.getHeadAtom(0).getDLPredicate() instanceof Equality ||
	                				dlClause.getHeadAtom(0).getDLPredicate() instanceof AnnotatedEquality)) {
	                    Variable x=dlClause.getBodyAtom(0).getArgumentVariable(0);
	                    if (x!=null && x.equals(dlClause.getBodyAtom(1).getArgument(0))) {
	                        Variable y1=dlClause.getBodyAtom(0).getArgumentVariable(1);
	                        Variable y2=dlClause.getBodyAtom(1).getArgumentVariable(1);
	                        Variable headY1=dlClause.getHeadAtom(0).getArgumentVariable(0);
	                        Variable headY2=dlClause.getHeadAtom(0).getArgumentVariable(1);
	                        if (y1!=null && y2!=null && !y1.equals(y2) && headY1!=null && headY2!=null && ((y1.equals(headY1) && y2.equals(headY2)) || (y1.equals(headY2) && y2.equals(headY1))))
	                            return true;
	                    }
	                }
	            }
	        }
	        return false;
	}
	
	/**
	 * 
	 * @param dlClause
	 * @return 000 -> 111 R \circ S \sqsubseteq T if R, S, T is inverse. -1 if it is not a role composition axiom.
	 */
	public static int isRoleCompositionAxioms(DLClause dlClause) {
		 if (dlClause.getBodyLength()==2 && dlClause.getHeadLength()==1) {
			 Atom body1 = dlClause.getBodyAtom(0), body2 = dlClause.getBodyAtom(1);
			 Atom head = dlClause.getHeadAtom(0); 
			 if (!(head.getDLPredicate() instanceof AtomicRole)) return -1; 
			 if (!(body1.getDLPredicate() instanceof AtomicRole)) return -1;
			 if (!(body2.getDLPredicate() instanceof AtomicRole)) return -1;
			 if (!(head.getArgument(0) instanceof Variable)) return -1;
			 if (!(head.getArgument(1) instanceof Variable)) return -1;
			 if (!(body1.getArgument(0) instanceof Variable)) return -1;
			 if (!(body1.getArgument(1) instanceof Variable)) return -1;
			 if (!(body2.getArgument(0) instanceof Variable)) return -1;
			 if (!(body2.getArgument(1) instanceof Variable)) return -1;
			 Variable rx, ry, sx, sy, tx, ty;
			 tx = head.getArgumentVariable(0); ty = head.getArgumentVariable(1); 
			 rx = body1.getArgumentVariable(0); ry = body1.getArgumentVariable(1); 
			 sx = body2.getArgumentVariable(0); sy = body2.getArgumentVariable(1);
			 
			 if (rx.equals(tx))
				 if (ry.equals(sx))
					 if (sy.equals(ty)) return 0; 
					 else return -1; 
				 else if (ry.equals(sy))
					 if (sx.equals(ty)) return 2;
					 else return -1; 
				 else 
					 return -1; 
			 else if (rx.equals(ty))
				 if (ry.equals(sx))
					 if (sy.equals(tx)) return 1; 
					 else return -1; 
				 else if (ry.equals(sy))
					 if (sx.equals(tx)) return 3; 
					 else return -1; 
				 else
					 return -1; 
			 else if (ry.equals(tx)) 
				 if (rx.equals(sx))
					 if (sy.equals(ty)) return 4; 
					 else return -1; 
				 else if (rx.equals(sy))
					 if (sx.equals(ty)) return 6; 
					 else return -1; 
				 else 
					 return -1; 
			 else if (ry.equals(ty))
				 if (rx.equals(sx))
					 if (sy.equals(tx)) return 5; 
					 else return -1; 
				 else if (rx.equals(sy))
					 if (sx.equals(tx)) return 7; 
					 else return -1; 
				 else return -1; 
			 else 
				 return -1; 
		 }
		 return -1;
	}

	public static boolean isGround(Atom tHeadAtom) {
		for (int i = 0; i < tHeadAtom.getArity(); ++i)
			if (tHeadAtom.getArgument(i) instanceof Variable)
				return false; 
		return true;
	}
	/**
	 * true if a \subseteq b
	 * @param a
	 * @param b
	 * @return
	 */
	private static boolean hasSubsetAtoms(Atom[] a, Atom[] b) {
		Set<String> atoms = new HashSet<String>();
		for (int i = 0; i < b.length; ++i)
			atoms.add(b[i].toString()); 
		
		for (int i = 0; i < a.length; ++i)
			if (!atoms.remove(a[i].toString()))
				return false; 
		
		return true;
	}

	public static boolean hasSubsetBodyAtoms(DLClause c1, DLClause c2) {
		return hasSubsetAtoms(c1.getBodyAtoms(), c2.getBodyAtoms()); 
	}

	public static DLClause replaceOtherBottom(DLClause dlClause) {
		Atom[] newHeadAtoms = dlClause.getHeadAtoms(), newBodyAtoms = dlClause.getBodyAtoms();
		if (containsOtherBottom(newHeadAtoms))
			newHeadAtoms = replaceOtherBottom(newHeadAtoms); 
		if (containsOtherBottom(newBodyAtoms))
			newBodyAtoms = replaceOtherBottom(newBodyAtoms); 

		if (newHeadAtoms == dlClause.getHeadAtoms() && newBodyAtoms == dlClause.getBodyAtoms())
			return dlClause;
		
		return DLClause.create(newHeadAtoms, newBodyAtoms); 
	}
	
	private static Atom[] replaceOtherBottom(Atom[] atoms) {
		Atom[] newAtoms = new Atom[atoms.length];
		int index = 0; 
		for (Atom atom: atoms) 
			if (isOtherBottom(atom.getDLPredicate()))
				newAtoms[index++] = Atom.create(AtomicConcept.NOTHING, atom.getArgument(0)); 
			else 
				newAtoms[index++] = atom;
		return newAtoms; 
	}

	private static boolean isOtherBottom(DLPredicate p) {
		if (!(p instanceof AtomicConcept)) return false; 
		
		String name = p.toString();
		int index; 
		char ch; 
		if ((index = name.indexOf("owl:Nothing")) != -1) {
			index += 11; 
			if (name.length() <= index) return true;  
			if ((ch = name.charAt(index)) == '_') return true; 
			if (ch >= '0' && ch <= '9') return true; 
		}
		return false; 
	}
	
	private static boolean containsOtherBottom(Atom[] atoms) {
		for (Atom atom: atoms) 
			if (isOtherBottom(atom.getDLPredicate()))
				return true; 
		return false; 
	}

	public static boolean isTautologyAboutDifferentFrom(DLClause clause) {
		if (clause.getHeadLength() > 1 || clause.getBodyLength() != 1) return false;
		if (clause.getHeadLength() == 1 && !clause.getHeadAtom(0).getDLPredicate().toString().contains(AtomicConcept.NOTHING.toString()))
			return false;
		Atom bodyAtom = clause.getBodyAtom(0); 
		return (bodyAtom.getDLPredicate() instanceof Inequality) && bodyAtom.getArgument(0).equals(bodyAtom.getArgument(1)); 
	}

}