aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java
blob: 9930009eb7ddf7be3874dc71e94e9f42815631d6 (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
package uk.ac.ox.cs.pagoda.rules;

import org.apache.commons.io.FilenameUtils;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.HermiT.model.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.Node;
import uk.ac.ox.cs.pagoda.constraints.BottomStrategy;
import uk.ac.ox.cs.pagoda.constraints.NullaryBottom;
import uk.ac.ox.cs.pagoda.constraints.UnaryBottom;
import uk.ac.ox.cs.pagoda.constraints.UpperUnaryBottom;
import uk.ac.ox.cs.pagoda.multistage.Normalisation;
import uk.ac.ox.cs.pagoda.multistage.RestrictedApplication;
import uk.ac.ox.cs.pagoda.rules.approximators.Approximator;
import uk.ac.ox.cs.pagoda.util.Timer;
import uk.ac.ox.cs.pagoda.util.Utility;

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

public class LowerDatalogProgram extends ApproxProgram implements IncrementalProgram {
	
	boolean m_toClassify; 
	
	public LowerDatalogProgram() {
		m_toClassify = true; 
	}
	
	public LowerDatalogProgram(boolean toClassify) {
		m_toClassify = toClassify; // false; // 
	}
	
	void clone(Program program) {
		super.clone(program); 
		if (botStrategy instanceof UpperUnaryBottom)
			botStrategy = new UnaryBottom(); 
	}


	@Override
	public void transform() {
		if (m_toClassify) {
			ClassifyThread thread = new ClassifyThread(this);
			thread.start();
			super.transform();
			try {
				thread.join(5000);
			} catch (InterruptedException e) {
				return ;
			}
			if (!thread.isAlive()) thread.dispose();
			else thread.interrupt();
		}
		else 
			super.transform();
		
		Normalisation norm = new Normalisation(dlOntology.getDLClauses(), ontology, new NullaryBottom());
		BottomStrategy tBottom = new NullaryBottom(); 
		norm.process();
		for (DLClause nClause: norm.getNormlisedClauses()) {
			if (nClause.getHeadLength() != 1)
				for (DLClause newClause: RestrictedApplication.addAdditionalDatalogRules(nClause, tBottom, norm)) {
//					System.out.println(newClause); 
					if (newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) {
//						System.out.println(newClause); 
						clauses.add(newClause);
					}
				}
			
			if (nClause.getHeadLength() == 1 && (nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) && clauses.add(nClause)) {
//				System.out.println(nClause);
			}
		}
	}

	@Override
	public String getOutputPath() {
		return FilenameUtils.concat(getDirectory(), "lower.dlog");
	}
	
//	@Override
//	public String getDirectory() {
//		File dir = new File(ontologyDirectory + Utility.FILE_SEPARATOR + "datalog");
//		if (!dir.exists())
//			dir.mkdirs();
//		return dir.getPath();
//	}

	@Override
	public void enrich(Collection<DLClause> delta) {
		synchronized (clauses) {
			Iterator<DLClause> iter = delta.iterator();
			while (iter.hasNext()) 
				clauses.add(iter.next()); 
		}
	}
	
	@Override
	public String toString() {
		String text; 
		synchronized (clauses) {
			text = super.toString();
		}
		return text; 
	}
	
	@Override
	protected void initApproximator() {
		m_approx = new IgnoreBoth();
	}

	private class IgnoreBoth implements Approximator {

		@Override
		public Collection<DLClause> convert(DLClause clause, DLClause originalClause) {
			Collection<DLClause> ret = new LinkedList<DLClause>();

			if (clause.getHeadLength() > 1) return ret;

			if (clause.getHeadLength() > 0) {
				DLPredicate predicate = clause.getHeadAtom(0).getDLPredicate();

				if (predicate instanceof AtLeast) return ret;
			}

			ret.add(clause);
			return ret;
		}
	}

}

class ClassifyThread extends Thread {
	
	IncrementalProgram m_program; 
	Collection<DLClause> clauses = new LinkedList<DLClause>(); 
	
	Variable X = Variable.create("X"), Y = Variable.create("Y");
	Reasoner hermitReasoner;
	OWLOntology ontology;
	ClassifyThread(IncrementalProgram program) {
		m_program = program;
	}
	
	@Override
	public void run() {
		ontology = m_program.getOntology();
		try {
			hermitReasoner = new Reasoner(ontology);
			Timer t = new Timer(); 
			hermitReasoner.classifyClasses();
			Utility.logInfo("HermiT classification done: " + t.duration());
		} catch (Exception e) {
//			e.printStackTrace();
			Utility.logInfo("HermiT cannot classify the ontology.");
			hermitReasoner = null; 
		}
	}
	
	public void dispose() {
		if (hermitReasoner == null)
			return ; 
		Set<OWLClass> classes;
		OWLClass lastClass = null, currentClass; 
		for (OWLClass subClass: ontology.getClassesInSignature()) {
			Node<OWLClass> node = hermitReasoner.getEquivalentClasses(subClass);
			if (!subClass.equals(node.getRepresentativeElement())) continue; 
			
			classes = node.getEntities(); 
			lastClass = subClass; 
			for (Iterator<OWLClass> iter = classes.iterator(); iter.hasNext(); ) {
				currentClass = iter.next();
				if (currentClass.equals(subClass)) continue; 
				addClause(lastClass, currentClass); 
				lastClass = currentClass; 
			}
			addClause(lastClass, subClass); 
			
			for (Node<OWLClass> tNode: hermitReasoner.getSuperClasses(subClass, true)) {
				OWLClass superClass = tNode.getRepresentativeElement();
				addClause(subClass, superClass); 
			}
		}
		
		Set<OWLObjectPropertyExpression> properties;
		OWLObjectPropertyExpression lastProperty, currentProperty; 
		for (OWLObjectProperty subProperty: ontology.getObjectPropertiesInSignature()) {
			Node<OWLObjectPropertyExpression> node = hermitReasoner.getEquivalentObjectProperties(subProperty);
			if (!subProperty.equals(node.getRepresentativeElement())) continue; 
			
			properties = node.getEntities(); 
			lastProperty = subProperty; 
			for (Iterator<OWLObjectPropertyExpression> iter = properties.iterator(); iter.hasNext(); ) {
				currentProperty = iter.next(); 
				if (currentProperty.equals(subProperty)) continue; 
				addClause(lastProperty, currentProperty); 
				lastProperty = currentProperty; 
			}
			addClause(lastProperty, subProperty); 
			
			for (Node<OWLObjectPropertyExpression> tNode: hermitReasoner.getSuperObjectProperties(subProperty, true)) {
				OWLObjectPropertyExpression superProperty = tNode.getRepresentativeElement();
				addClause(subProperty, superProperty);
			}
		}

		m_program.enrich(clauses);
		Utility.logInfo("classification done and enriched lower bound rules."); 
	}
	

	private void addClause(OWLObjectPropertyExpression subProperty, OWLObjectPropertyExpression superProperty) {
		if (subProperty.equals(superProperty)) return ; 
		if (superProperty.toString().equals("owl:topObjectProperty")) return ; 
		clauses.add(DLClause.create(new Atom[] { getAtom(superProperty) }, new Atom[] { getAtom(subProperty) })); 
	}

	private Atom getAtom(OWLObjectPropertyExpression p) {
		if (p instanceof OWLObjectInverseOf)
			return Atom.create(AtomicRole.create(((OWLObjectProperty) ((OWLObjectInverseOf) p).getInverse()).toStringID()), Y, X);
		
		return Atom.create(AtomicRole.create(((OWLObjectProperty) p).toStringID()), X, Y);
	}

	private void addClause(OWLClass subClass, OWLClass superClass) {
		if (subClass.equals(superClass)) return ; 
		if (subClass.toString().equals("owl:Nothing")) return ; 
		if (superClass.toString().equals("owl:Thing")) return ; 
		clauses.add(DLClause.create(new Atom[] { getAtom(superClass) }, new Atom[] { getAtom(subClass) })); 		
	}

	private Atom getAtom(OWLClass c) {
		return Atom.create(AtomicConcept.create(c.toStringID()), X);
	}
}