aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java238
1 files changed, 0 insertions, 238 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java b/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java
deleted file mode 100644
index a2676e8..0000000
--- a/src/uk/ac/ox/cs/pagoda/rules/LowerDatalogProgram.java
+++ /dev/null
@@ -1,238 +0,0 @@
1package uk.ac.ox.cs.pagoda.rules;
2
3import org.apache.commons.io.FilenameUtils;
4import org.semanticweb.HermiT.Reasoner;
5import org.semanticweb.HermiT.model.*;
6import org.semanticweb.owlapi.model.*;
7import org.semanticweb.owlapi.reasoner.Node;
8import uk.ac.ox.cs.pagoda.constraints.BottomStrategy;
9import uk.ac.ox.cs.pagoda.constraints.NullaryBottom;
10import uk.ac.ox.cs.pagoda.constraints.UnaryBottom;
11import uk.ac.ox.cs.pagoda.constraints.UpperUnaryBottom;
12import uk.ac.ox.cs.pagoda.multistage.Normalisation;
13import uk.ac.ox.cs.pagoda.multistage.RestrictedApplication;
14import uk.ac.ox.cs.pagoda.rules.approximators.Approximator;
15import uk.ac.ox.cs.pagoda.util.Timer;
16import uk.ac.ox.cs.pagoda.util.Utility;
17
18import java.util.Collection;
19import java.util.Iterator;
20import java.util.LinkedList;
21import java.util.Set;
22
23public class LowerDatalogProgram extends ApproxProgram implements IncrementalProgram {
24
25 boolean m_toClassify;
26
27 public LowerDatalogProgram() {
28 m_toClassify = true;
29 }
30
31 public LowerDatalogProgram(boolean toClassify) {
32 m_toClassify = toClassify; // false; //
33 }
34
35 void clone(Program program) {
36 super.clone(program);
37 if (botStrategy instanceof UpperUnaryBottom)
38 botStrategy = new UnaryBottom();
39 }
40
41
42 // TODO -RULE- filter out unsafe rules
43 @Override
44 public void transform() {
45 if (m_toClassify) {
46 ClassifyThread thread = new ClassifyThread(this);
47 thread.start();
48 super.transform();
49 try {
50 thread.join(5000);
51 } catch (InterruptedException e) {
52 return ;
53 }
54 if (!thread.isAlive()) thread.dispose();
55 else thread.interrupt();
56 }
57 else
58 super.transform();
59
60 Normalisation norm = new Normalisation(dlClauses, ontology, new NullaryBottom());
61 BottomStrategy tBottom = new NullaryBottom();
62 norm.process();
63 for (DLClause nClause: norm.getNormlisedClauses()) {
64 if (nClause.getHeadLength() != 1)
65 for (DLClause newClause: RestrictedApplication.addAdditionalDatalogRules(nClause, tBottom, norm)) {
66// System.out.println(newClause);
67 if (newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || newClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) {
68// System.out.println(newClause);
69 clauses.add(newClause);
70 }
71 }
72
73 if (nClause.getHeadLength() == 1 && (nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicConcept || nClause.getHeadAtom(0).getDLPredicate() instanceof AtomicRole) && clauses.add(nClause)) {
74// System.out.println(nClause);
75 }
76 }
77 }
78
79 @Override
80 public String getOutputPath() {
81 return FilenameUtils.concat(getDirectory(), "lower.dlog");
82 }
83
84// @Override
85// public String getDirectory() {
86// File dir = new File(ontologyDirectory + Utility.FILE_SEPARATOR + "datalog");
87// if (!dir.exists())
88// dir.mkdirs();
89// return dir.getPath();
90// }
91
92 @Override
93 public void enrich(Collection<DLClause> delta) {
94 synchronized (clauses) {
95 Iterator<DLClause> iter = delta.iterator();
96 while (iter.hasNext())
97 clauses.add(iter.next());
98 }
99 }
100
101 @Override
102 public String toString() {
103 String text;
104 synchronized (clauses) {
105 text = super.toString();
106 }
107 return text;
108 }
109
110 @Override
111 protected void initApproximator() {
112 m_approx = new IgnoreBoth();
113 }
114
115 private class IgnoreBoth implements Approximator {
116
117 @Override
118 public Collection<DLClause> convert(DLClause clause, DLClause originalClause) {
119 Collection<DLClause> ret = new LinkedList<DLClause>();
120
121 if (clause.getHeadLength() > 1) return ret;
122
123 if (clause.getHeadLength() > 0) {
124 DLPredicate predicate = clause.getHeadAtom(0).getDLPredicate();
125
126 if (predicate instanceof AtLeast) return ret;
127 }
128
129 ret.add(clause);
130 return ret;
131 }
132 }
133
134}
135
136class ClassifyThread extends Thread {
137
138 IncrementalProgram m_program;
139 Collection<DLClause> clauses = new LinkedList<DLClause>();
140
141 Variable X = Variable.create("X"), Y = Variable.create("Y");
142 Reasoner hermitReasoner;
143 OWLOntology ontology;
144 ClassifyThread(IncrementalProgram program) {
145 m_program = program;
146 }
147
148 @Override
149 public void run() {
150 ontology = m_program.getOntology();
151 try {
152 hermitReasoner = new Reasoner(ontology);
153 Timer t = new Timer();
154 hermitReasoner.classifyClasses();
155 Utility.logInfo("HermiT classification done: " + t.duration());
156 } catch (Exception e) {
157// e.printStackTrace();
158 Utility.logInfo("HermiT cannot classify the ontology.");
159 hermitReasoner = null;
160 }
161 }
162
163 public void dispose() {
164 if (hermitReasoner == null)
165 return ;
166 Set<OWLClass> classes;
167 OWLClass lastClass = null, currentClass;
168 for (OWLClass subClass: ontology.getClassesInSignature()) {
169 Node<OWLClass> node = hermitReasoner.getEquivalentClasses(subClass);
170 if (!subClass.equals(node.getRepresentativeElement())) continue;
171
172 classes = node.getEntities();
173 lastClass = subClass;
174 for (Iterator<OWLClass> iter = classes.iterator(); iter.hasNext(); ) {
175 currentClass = iter.next();
176 if (currentClass.equals(subClass)) continue;
177 addClause(lastClass, currentClass);
178 lastClass = currentClass;
179 }
180 addClause(lastClass, subClass);
181
182 for (Node<OWLClass> tNode: hermitReasoner.getSuperClasses(subClass, true)) {
183 OWLClass superClass = tNode.getRepresentativeElement();
184 addClause(subClass, superClass);
185 }
186 }
187
188 Set<OWLObjectPropertyExpression> properties;
189 OWLObjectPropertyExpression lastProperty, currentProperty;
190 for (OWLObjectProperty subProperty: ontology.getObjectPropertiesInSignature()) {
191 Node<OWLObjectPropertyExpression> node = hermitReasoner.getEquivalentObjectProperties(subProperty);
192 if (!subProperty.equals(node.getRepresentativeElement())) continue;
193
194 properties = node.getEntities();
195 lastProperty = subProperty;
196 for (Iterator<OWLObjectPropertyExpression> iter = properties.iterator(); iter.hasNext(); ) {
197 currentProperty = iter.next();
198 if (currentProperty.equals(subProperty)) continue;
199 addClause(lastProperty, currentProperty);
200 lastProperty = currentProperty;
201 }
202 addClause(lastProperty, subProperty);
203
204 for (Node<OWLObjectPropertyExpression> tNode: hermitReasoner.getSuperObjectProperties(subProperty, true)) {
205 OWLObjectPropertyExpression superProperty = tNode.getRepresentativeElement();
206 addClause(subProperty, superProperty);
207 }
208 }
209
210 m_program.enrich(clauses);
211 Utility.logInfo("classification done and enriched lower bound rules.");
212 }
213
214
215 private void addClause(OWLObjectPropertyExpression subProperty, OWLObjectPropertyExpression superProperty) {
216 if (subProperty.equals(superProperty)) return ;
217 if (superProperty.toString().equals("owl:topObjectProperty")) return ;
218 clauses.add(DLClause.create(new Atom[] { getAtom(superProperty) }, new Atom[] { getAtom(subProperty) }));
219 }
220
221 private Atom getAtom(OWLObjectPropertyExpression p) {
222 if (p instanceof OWLObjectInverseOf)
223 return Atom.create(AtomicRole.create(((OWLObjectProperty) ((OWLObjectInverseOf) p).getInverse()).toStringID()), Y, X);
224
225 return Atom.create(AtomicRole.create(((OWLObjectProperty) p).toStringID()), X, Y);
226 }
227
228 private void addClause(OWLClass subClass, OWLClass superClass) {
229 if (subClass.equals(superClass)) return ;
230 if (subClass.toString().equals("owl:Nothing")) return ;
231 if (superClass.toString().equals("owl:Thing")) return ;
232 clauses.add(DLClause.create(new Atom[] { getAtom(superClass) }, new Atom[] { getAtom(subClass) }));
233 }
234
235 private Atom getAtom(OWLClass c) {
236 return Atom.create(AtomicConcept.create(c.toStringID()), X);
237 }
238} \ No newline at end of file