aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/approx
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-10 18:17:06 +0100
committerFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-11 12:34:47 +0100
commit17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch)
tree47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/approx
parent8651164cd632a5db310b457ce32d4fbc97bdc41c (diff)
downloadACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz
ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip
[pagoda] Move project to Scala
This commit includes a few changes: - The repository still uses Maven to manage dependency but it is now a Scala project. - The code has been ported from OWLAPI 3.4.10 to 5.1.20 - A proof of concept program using both RSAComb and PAGOdA has been added.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/approx')
-rw-r--r--src/uk/ac/ox/cs/pagoda/approx/Clause.java725
-rw-r--r--src/uk/ac/ox/cs/pagoda/approx/Clausifier.java37
-rw-r--r--src/uk/ac/ox/cs/pagoda/approx/KnowledgeBase.java18
-rw-r--r--src/uk/ac/ox/cs/pagoda/approx/RLOntology.java190
-rw-r--r--src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java607
5 files changed, 0 insertions, 1577 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/approx/Clause.java b/src/uk/ac/ox/cs/pagoda/approx/Clause.java
deleted file mode 100644
index 29bc74e..0000000
--- a/src/uk/ac/ox/cs/pagoda/approx/Clause.java
+++ /dev/null
@@ -1,725 +0,0 @@
1package uk.ac.ox.cs.pagoda.approx;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Iterator;
6import java.util.Map;
7import java.util.Set;
8
9import org.semanticweb.HermiT.model.*;
10import org.semanticweb.owlapi.model.*;
11
12import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
13import uk.ac.ox.cs.pagoda.util.Namespace;
14import uk.ac.ox.cs.pagoda.util.Utility;
15
16public class Clause {
17
18 Set<Atom> headAtoms;
19 Set<Atom> bodyAtoms;
20
21 Set<String> dataProperties;
22 OWLDataFactory factory;
23 // OWLClass top = null;
24
25 private Set<OWLClassExpression> superClasses = new HashSet<OWLClassExpression>();
26 private Set<OWLClassExpression> subClasses = new HashSet<OWLClassExpression>();
27
28 public Clause(Clausifier clausifier, DLClause clause) {
29 this.dataProperties = clausifier.dataProperties;
30 this.factory = clausifier.factory;
31 // top = ontology.top;
32
33 headAtoms = Utility.toSet(clause.getHeadAtoms());
34 bodyAtoms = Utility.toSet(clause.getBodyAtoms());
35
36 rollingUp();
37 }
38
39 private static final Variable X = Variable.create("X");
40
41 private void rollingUp() {
42 Map<Variable, Set<Variable>> varCliques = new HashMap<Variable, Set<Variable>>();
43
44 for (Iterator<Atom> iter = bodyAtoms.iterator(); iter.hasNext();) {
45 Atom atom = iter.next();
46 if (atom.getDLPredicate() instanceof Inequality)
47 if (atom.getArgument(0) instanceof Variable
48 && atom.getArgument(1) instanceof Variable) {
49 Variable var1 = atom.getArgumentVariable(0), var2 = atom
50 .getArgumentVariable(1);
51 Set<Variable> rep;
52 if ((rep = varCliques.get(var1)) == null)
53 if ((rep = varCliques.get(var2)) == null)
54 rep = new HashSet<Variable>();
55 rep.add(var1);
56 rep.add(var2);
57 varCliques.put(var1, rep);
58 varCliques.put(var2, rep);
59 iter.remove();
60 }
61 }
62
63 eliminateEquality();
64
65 Map<Variable, Atom> var2atom = new HashMap<Variable, Atom>();
66
67 getVariableOccurrence(var2atom, headAtoms);
68 getVariableOccurrence(var2atom, bodyAtoms);
69
70 DLPredicate predicate;
71 Term W = null;
72
73 Map<Variable, String> nom2iri = new HashMap<Variable, String>();
74 Map<Variable, Constant> nom2datatype = new HashMap<Variable, Constant>();
75
76 for (Iterator<Atom> iter = headAtoms.iterator(); iter.hasNext();) {
77 Atom tAtom = iter.next();
78 predicate = tAtom.getDLPredicate();
79 if (predicate instanceof AtomicNegationDataRange) {
80 AtomicNegationDataRange andr = (AtomicNegationDataRange) predicate;
81 AtomicDataRange adr = andr.getNegatedDataRange();
82 if (adr instanceof ConstantEnumeration) {
83 ConstantEnumeration e = (ConstantEnumeration) adr;
84 if (e.getNumberOfConstants() == 1) {
85 Variable v = tAtom.getArgumentVariable(0);
86 nom2datatype.put(v, e.getConstant(0));
87 iter.remove();
88 continue;
89 }
90 }
91 }
92 }
93
94 for (Atom atom : bodyAtoms) {
95 predicate = atom.getDLPredicate();
96 if (predicate instanceof AtomicConcept) {
97 AtomicConcept concept = (AtomicConcept) predicate;
98 Variable v = atom.getArgumentVariable(0);
99 if (v == X)
100 subClasses.add(factory.getOWLClass(IRI.create(concept.getIRI())));
101 else if (predicate.toString().startsWith("<internal:nom#"))
102 nom2iri.put(v, DLClauseHelper.getIRI4Nominal(concept));
103 } else if (predicate instanceof AtomicRole) {
104 AtomicRole role = (AtomicRole) predicate;
105
106 if (dataProperties.contains(role.getIRI())) {
107 OWLDataRange dataRange;
108 OWLDataPropertyExpression dataPropertyExp = factory
109 .getOWLDataProperty(IRI.create(role.getIRI()));
110 Term term = atom.getArgument(1);
111 if (term instanceof Constant)
112 subClasses.add(factory
113 .getOWLDataHasValue(dataPropertyExp,
114 getOWLLiteral((Constant) term)));
115 else if (term instanceof Variable) {
116 W = (Variable) term;
117 if (nom2datatype.containsKey(W)) {
118 subClasses.add(factory.getOWLDataHasValue(
119 dataPropertyExp,
120 getOWLLiteral(nom2datatype.get(W))));
121 } else if (var2atom.containsKey(W)) {
122 Atom tAtom = var2atom.get(W);
123 DLPredicate tPredicate = tAtom.getDLPredicate();
124 if (tPredicate instanceof DatatypeRestriction) {
125 DatatypeRestriction restriction = (DatatypeRestriction) tPredicate;
126 dataRange = factory.getOWLDatatype(IRI
127 .create(restriction.getDatatypeURI()));
128 }
129 // else if (tPredicate instanceof
130 // AtomicNegationDataRange) {
131 // // TODO how to deal with AtomicNegationDataRange
132 // e.g. not({ "5"^^xsd:integer })
133 //
134 // }
135 else if (tPredicate instanceof AtomicConcept) {
136 dataRange = factory.getOWLDatatype(IRI
137 .create(((AtomicConcept) tPredicate)
138 .getIRI()));
139 } else {
140 dataRange = null;
141 Utility.logError(tPredicate,
142 "strange ... -___-|||");
143 }
144
145 if (headAtoms.contains(tAtom)) {
146 superClasses.add(factory
147 .getOWLDataAllValuesFrom(
148 dataPropertyExp, dataRange));
149 subClasses.add(factory
150 .getOWLDataSomeValuesFrom(
151 dataPropertyExp,
152 factory.getTopDatatype()));
153 headAtoms.remove(tAtom);
154 } else
155 subClasses.add(factory
156 .getOWLDataSomeValuesFrom(
157 dataPropertyExp, dataRange));
158
159 } else
160 subClasses.add(factory.getOWLDataSomeValuesFrom(
161 dataPropertyExp, factory.getTopDatatype()));
162 } else {
163 Utility.logError(term, "strange ... -___-|||");
164 }
165 continue;
166 }
167
168 OWLObjectPropertyExpression roleExp = factory
169 .getOWLObjectProperty(IRI.create(role.getIRI()));
170 if ((W = atom.getArgument(1)).equals(X)) {
171 roleExp = roleExp.getInverseProperty();
172 W = atom.getArgument(0);
173 }
174
175 if (X == W)
176 subClasses.add(factory.getOWLObjectHasSelf(roleExp));
177 else if (W instanceof Individual)
178 subClasses.add(factory.getOWLObjectHasValue(roleExp, factory.getOWLNamedIndividual(IRI.create(((Individual) W).getIRI()))));
179 else {
180 AtomicConcept concept;
181 OWLClassExpression clsExp = null;
182 int number = 1;
183 Set<Variable> set = varCliques.get(W);
184 if (set != null)
185 number = set.size();
186
187 if (var2atom.containsKey(W)) {
188 Atom tAtom = var2atom.get(W);
189 DLPredicate tPredicate = tAtom.getDLPredicate();
190 if (tPredicate instanceof AtomicConcept) {
191 concept = (AtomicConcept) tPredicate;
192 clsExp = factory.getOWLClass(IRI.create(concept
193 .getIRI()));
194 if (headAtoms.contains(tAtom)) {
195 superClasses.add(factory.getOWLObjectAllValuesFrom(
196 roleExp, clsExp));
197 subClasses.add(factory.getOWLObjectSomeValuesFrom(
198 roleExp, factory.getOWLThing()));
199 headAtoms.remove(tAtom);
200 } else {
201 if (number == 1)
202 subClasses.add(factory
203 .getOWLObjectSomeValuesFrom(roleExp,
204 clsExp));
205 else
206 subClasses.add(factory
207 .getOWLObjectMinCardinality(number,
208 roleExp, clsExp));
209 }
210 } else {
211 Utility.logDebug(tAtom, "strange ... -___-|||");
212 }
213 }
214 else {
215 if (number == 1)
216 subClasses.add(factory.getOWLObjectSomeValuesFrom(
217 roleExp, factory.getOWLThing()));
218 else
219 subClasses.add(factory.getOWLObjectMinCardinality(
220 number, roleExp));
221 }
222 }
223 }
224 }
225
226 OWLObjectPropertyExpression objExp;
227 for (Atom atom : headAtoms) {
228 predicate = atom.getDLPredicate();
229 if (predicate instanceof AtomicConcept) {
230 if (atom.getArgumentVariable(0) == X)
231 superClasses
232 .add(getClassExpression((AtomicConcept) predicate));
233 } else if (predicate instanceof AtomicRole) {
234 if (!dataProperties.contains(((AtomicRole) predicate).getIRI())) {
235 objExp = factory.getOWLObjectProperty(IRI
236 .create(((AtomicRole) predicate).getIRI()));
237 Term V = atom.getArgument(1);
238 if (V == X) {
239 objExp = factory.getOWLObjectInverseOf(objExp);
240 V = atom.getArgument(0);
241 }
242
243 if (V == X)
244 superClasses.add(factory.getOWLObjectHasSelf(objExp));
245 else if (V instanceof Individual) {
246 superClasses.add(factory.getOWLObjectHasValue(objExp,
247 factory.getOWLNamedIndividual(IRI
248 .create(((Individual) V).getIRI()))));
249 } else
250 superClasses.add(factory.getOWLObjectHasValue(objExp,
251 factory.getOWLNamedIndividual(IRI.create(nom2iri
252 .get((Variable) V)))));
253 }
254 else {
255 Constant c = (Constant) atom.getArgument(1);
256 OWLDataProperty dataProp = factory.getOWLDataProperty(IRI.create(((AtomicRole) predicate).getIRI()));
257 superClasses.add(factory.getOWLDataHasValue(dataProp, getOWLLiteral(c)));
258 }
259 } else if (predicate instanceof AtLeastConcept)
260 superClasses
261 .add(getMinCardinalityExpression((AtLeastConcept) predicate));
262 else if (predicate instanceof AtLeastDataRange)
263 superClasses
264 .add(getDataMinCardinalityExpression((AtLeastDataRange) predicate));
265
266 else {
267 Utility.logError(atom.toString(),
268 "strange head atoms left here~~~~~");
269 // superClasses.add(getDataRange(getDataRange((LiteralDataRange)
270 // predicate)));
271 }
272 }
273 }
274
275 private OWLLiteral getOWLLiteral(Constant constant) {
276 if (!constant.getDatatypeURI().equals(Namespace.RDF_PLAIN_LITERAL))
277 return factory.getOWLLiteral(constant.getLexicalForm(), factory
278 .getOWLDatatype(IRI.create(constant.getDatatypeURI())));
279 else {
280 String lexicalForm = constant.getLexicalForm();
281 int index = lexicalForm.indexOf("@");
282 return factory.getOWLLiteral(lexicalForm.substring(0, index),
283 lexicalForm.substring(index + 1));
284 }
285 }
286
287 // private OWLObjectSomeValuesFrom
288 // addSomeValuesFromAxiom(OWLObjectPropertyExpression roleExp,
289 // OWLClassExpression classExp) {
290 // return factory.getOWLObjectSomeValuesFrom(roleExp, classExp);
291 // }
292
293 private void getVariableOccurrence(Map<Variable, Atom> var2atom,
294 Set<Atom> atoms) {
295 for (Atom atom : atoms)
296 if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable
297 && !atom.getArgument(0).equals(X))
298 var2atom.put((Variable) atom.getArgumentVariable(0), atom);
299 }
300
301 private OWLClassExpression getMinCardinalityExpression(
302 AtLeastConcept atLeast) {
303 OWLObjectPropertyExpression propExp = getObjectPropertyExpression(atLeast
304 .getOnRole());
305 OWLClassExpression clsExp = getClassExpression(atLeast.getToConcept());
306 if (atLeast.getNumber() == 1)
307 return factory.getOWLObjectSomeValuesFrom(propExp, clsExp);
308 else
309 return factory.getOWLObjectMinCardinality(atLeast.getNumber(),
310 propExp, clsExp);
311 }
312
313 private OWLClassExpression getDataMinCardinalityExpression(
314 AtLeastDataRange atLeast) {
315 OWLDataPropertyExpression propExp = getDataPropertyExpression(atLeast
316 .getOnRole());
317 OWLDataRange dataRange = getDataRange(atLeast.getToDataRange());
318 if (atLeast.getNumber() == 1)
319 return factory.getOWLDataSomeValuesFrom(propExp, dataRange);
320 else
321 return factory.getOWLDataMinCardinality(atLeast.getNumber(),
322 propExp, dataRange);
323 }
324
325 public Set<OWLClassExpression> getSuperClasses() {
326 return superClasses;
327 }
328
329 public Set<OWLClassExpression> getSubClasses() {
330 return subClasses;
331 }
332
333 // public OWLClassExpression getSubClass() {
334 // if (subClasses.isEmpty())
335 // return factory.getOWLThing();
336 // if (subClasses.size() == 1)
337 // return subClasses.iterator().next();
338 //
339 // return factory.getOWLObjectIntersectionOf(subClasses);
340 // }
341
342 private void eliminateEquality() {
343 Set<Atom> eHeadAtoms = new HashSet<Atom>();
344 Set<Atom> eBodyAtoms = new HashSet<Atom>();
345 Set<Variable> eVariables = new HashSet<Variable>();
346 seperateEquality4Clause(eBodyAtoms, eHeadAtoms, eVariables);
347
348 OWLNamedIndividual individual;
349 /*
350 * remove equalities that are introduced by MaxCardinalityConstraints
351 */
352 DLPredicate predicate;
353 Map<Variable, Set<Variable>> groups = new HashMap<Variable, Set<Variable>>();
354 OWLObjectMaxCardinality maxCardinality;
355 OWLClassExpression exp;
356 Set<Variable> mVariables = new HashSet<Variable>();
357 Variable tVar, tVar1, tVar2;
358 Set<Variable> tVariables;
359
360 for (Iterator<Atom> iter = eHeadAtoms.iterator(); iter.hasNext(); ){
361 Atom atom = iter.next();
362 predicate = atom.getDLPredicate();
363 if (predicate instanceof AnnotatedEquality) {
364 superClasses.add(maxCardinality = getMaxCardinalityExpression((AnnotatedEquality)predicate));
365 if (!((exp = maxCardinality.getFiller()) instanceof OWLObjectComplementOf))
366 subClasses.add(factory.getOWLObjectSomeValuesFrom(maxCardinality.getProperty(), exp));
367 else
368 subClasses.add(factory.getOWLObjectSomeValuesFrom(maxCardinality.getProperty(), factory.getOWLThing()));
369 mVariables.add(atom.getArgumentVariable(0));
370 mVariables.add(atom.getArgumentVariable(1));
371 iter.remove();
372 }
373 else if (predicate instanceof Equality) {
374 if (atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) {
375 mVariables.add(tVar1 = atom.getArgumentVariable(0));
376 mVariables.add(tVar2 = atom.getArgumentVariable(1));
377 iter.remove();
378
379 if (tVar1.getName().compareTo(tVar2.getName()) > 0) {
380 tVar = tVar1; tVar1 = tVar2; tVar2 = tVar;
381 }
382 tVariables = groups.get(tVar1);
383 if (groups.containsKey(tVar2)) {
384 if (tVariables == null)
385 groups.put(tVar1, tVariables = groups.get(tVar2));
386 else {
387 tVariables.addAll(groups.get(tVar2));
388 groups.get(tVar2).clear();
389 groups.put(tVar2, tVariables);
390 }
391 }
392 if (tVariables == null) {
393 groups.put(tVar1, tVariables = new HashSet<Variable>());
394 groups.put(tVar2, tVariables);
395 }
396 tVariables.add(tVar1);
397 tVariables.add(tVar2);
398 }
399 }
400 }
401
402 Map<Variable, Object> maxCardToConcepts = new HashMap<Variable, Object>();
403
404 for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) {
405 Atom atom = iter.next();
406 if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable) {
407 if (mVariables.contains(tVar = atom.getArgumentVariable(0))) {
408 maxCardToConcepts.put(tVar, atom.getDLPredicate());
409 iter.remove();
410 }
411 }
412 }
413
414 for (Iterator<Atom> iter = eHeadAtoms.iterator(); iter.hasNext(); ) {
415 Atom atom = iter.next();
416 if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable) {
417 if (mVariables.contains(tVar = atom.getArgumentVariable(0))) {
418 maxCardToConcepts.put(tVar, AtomicNegationConcept.create((AtomicConcept) atom.getDLPredicate()));
419 iter.remove();
420 }
421 }
422 }
423
424 Map<Variable, Object> maxCardToProperty = new HashMap<Variable, Object>();
425
426 for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) {
427 Atom atom = iter.next();
428 if (atom.getArity() == 2 && atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) {
429 tVar1 = atom.getArgumentVariable(0); tVar2 = atom.getArgumentVariable(1);
430 if (mVariables.contains(tVar1)) {
431 if (groups.containsKey(tVar1))
432 maxCardToProperty.put(tVar1, ((AtomicRole) atom.getDLPredicate()).getInverse());
433 iter.remove();
434 } else if (mVariables.contains(tVar2)) {
435 if (groups.containsKey(tVar2))
436 maxCardToProperty.put(tVar2, atom.getDLPredicate());
437 iter.remove();
438 }
439 }
440 }
441
442 int n;
443 Object r, A;
444 for (Variable var: groups.keySet()) {
445 if ((tVariables = groups.get(var)).isEmpty())
446 continue;
447 n = tVariables.size() - 1;
448 tVariables.clear();
449 r = maxCardToProperty.get(var);
450 if (r instanceof AtomicRole) {
451 if (isDataProperty(r)) {
452 if ((A = maxCardToConcepts.get(var)) != null) {
453 Utility.logError("Unknown data range: " + A);
454 }
455
456 superClasses.add(
457 factory.getOWLDataMaxCardinality(
458 n,
459 factory.getOWLDataProperty(IRI.create(((AtomicRole) r).getIRI()))));
460 }
461 else {
462 OWLClassExpression clsExp = null;
463 if ((A = maxCardToConcepts.get(var)) != null)
464 if (A instanceof AtomicConcept)
465 clsExp = factory.getOWLClass(IRI.create(((AtomicConcept) A).getIRI()));
466 else if (A instanceof AtomicNegationConcept)
467 clsExp = factory.getOWLObjectComplementOf(factory.getOWLClass(IRI.create(((AtomicNegationConcept) A).getNegatedAtomicConcept().getIRI())));
468 else
469 Utility.logError("Unknown to concept: " + A);
470
471 if (A == null)
472 superClasses.add(
473 factory.getOWLObjectMaxCardinality(
474 n,
475 factory.getOWLObjectProperty(IRI.create(((AtomicRole) r).getIRI()))
476 ));
477 else
478 superClasses.add(
479 factory.getOWLObjectMaxCardinality(
480 n,
481 factory.getOWLObjectProperty(IRI.create(((AtomicRole) r).getIRI())),
482 clsExp));
483 }
484 }
485 else if (r instanceof InverseRole) {
486 OWLClassExpression clsExp = null;
487 if ((A = maxCardToConcepts.get(var)) != null) {
488 if (A instanceof AtomicConcept)
489 clsExp = factory.getOWLClass(IRI.create(((AtomicConcept) A).getIRI()));
490 else if (A instanceof AtomicNegationConcept)
491 clsExp = factory.getOWLObjectComplementOf(factory.getOWLClass(IRI.create(((AtomicNegationConcept) A).getNegatedAtomicConcept().getIRI())));
492 else
493 Utility.logError("Unknown to concept: " + A);
494 }
495
496 if (A == null)
497 superClasses.add(
498 factory.getOWLObjectMaxCardinality(
499 n,
500 factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(((InverseRole) r).getInverseOf().getIRI())))
501 ));
502 else
503 superClasses.add(
504 factory.getOWLObjectMaxCardinality(
505 n,
506 factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(((InverseRole) r).getInverseOf().getIRI()))),
507 clsExp));
508
509 }
510 else
511 Utility.logError("Unknown property: " + r);
512 }
513
514 /*
515 * dealing with equalities of nominal
516 */
517 Map<Variable, String> nom2iri = new HashMap<Variable, String>();
518 for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) {
519 Atom atom = iter.next();
520 predicate = atom.getDLPredicate();
521 if (predicate instanceof AtomicConcept && predicate.toString().startsWith("<internal:nom#")) {
522 nom2iri.put(atom.getArgumentVariable(0), DLClauseHelper.getIRI4Nominal(predicate));
523 iter.remove();
524 }
525 }
526
527 Term first, second;
528 Map<Variable, Set<Term>> equEdges = new HashMap<Variable, Set<Term>>();
529 Set<Term> terms = new HashSet<Term>();
530 for (Atom atom: eHeadAtoms) {
531 predicate = atom.getDLPredicate();
532 if (predicate instanceof Equality) {
533 first = atom.getArgument(0);
534 second = atom.getArgument(1);
535
536 if (first instanceof Variable) {
537 if ((terms = equEdges.get(first)) == null)
538 equEdges.put((Variable) first, (terms = new HashSet<Term>()));
539 terms.add(second);
540 }
541
542 if (second instanceof Variable) {
543 if ((terms = equEdges.get(second)) == null)
544 equEdges.put((Variable) second, (terms = new HashSet<Term>()));
545 terms.add(first);
546 }
547 }
548 }
549
550 OWLObjectPropertyExpression objExp;
551
552 Set<OWLNamedIndividual> individuals = new HashSet<OWLNamedIndividual>();
553 if (equEdges.containsKey(X)) {
554 for (Term t: equEdges.get(X))
555 if (t instanceof Variable) {
556 Variable var = (Variable) t;
557 individual = factory.getOWLNamedIndividual(IRI.create(nom2iri.get(var)));
558// superClasses.add(factory.getOWLObjectOneOf(individual));
559 individuals.add(individual);
560 }
561 else if (t instanceof Individual)
562 individuals.add(factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI())));
563 }
564
565 if (individuals.size() > 0) {
566 superClasses.add(factory.getOWLObjectOneOf(individuals));
567 individuals.clear();
568 }
569
570 for (Atom atom: eBodyAtoms) {
571 predicate = atom.getDLPredicate();
572 if (predicate instanceof AtomicRole) {
573 first = atom.getArgumentVariable(0);
574 second = atom.getArgumentVariable(1);
575
576 objExp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) predicate).getIRI()));
577 if (eVariables.contains(first)) {
578 second = first;
579 objExp = factory.getOWLObjectInverseOf(objExp);
580 }
581
582 for (Term t: equEdges.get(second)) {
583 if (t instanceof Variable) {
584 Variable var = (Variable) t;
585 individuals.add(factory.getOWLNamedIndividual(IRI.create(nom2iri.get(var))));
586 }
587 else if (t instanceof Individual) {
588 individuals.add(factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI())));
589 }
590 }
591 if (!individuals.isEmpty()) {
592 superClasses.add(factory.getOWLObjectAllValuesFrom(objExp, factory.getOWLObjectOneOf(individuals)));
593 individuals.clear();
594 }
595 }
596 }
597
598 }
599
600 private boolean isDataProperty(Object r) {
601 if (!(r instanceof AtomicRole)) return false;
602 String iri = ((AtomicRole) r).getIRI();
603 return dataProperties.contains(iri);
604 }
605
606 private OWLObjectMaxCardinality getMaxCardinalityExpression(
607 AnnotatedEquality equ) {
608 OWLObjectPropertyExpression propExp = getObjectPropertyExpression(equ
609 .getOnRole());
610 OWLClassExpression clsExp = getClassExpression(equ.getToConcept());
611 return factory.getOWLObjectMaxCardinality(equ.getCaridnality(),
612 propExp, clsExp);
613 }
614
615 private OWLObjectPropertyExpression getObjectPropertyExpression(Role role) {
616 if (role instanceof AtomicRole)
617 return factory.getOWLObjectProperty(IRI.create(((AtomicRole) role)
618 .getIRI()));
619 return factory.getOWLObjectProperty(
620 IRI.create(((InverseRole) role).getInverseOf().getIRI()))
621 .getInverseProperty();
622 }
623
624 private OWLDataProperty getDataPropertyExpression(Role role) {
625 return factory.getOWLDataProperty(IRI.create(((AtomicRole) role)
626 .getIRI()));
627 }
628
629 private OWLClassExpression getClassExpression(LiteralConcept concept) {
630 if (concept instanceof AtomicConcept)
631 return factory.getOWLClass(IRI.create(((AtomicConcept) concept)
632 .getIRI()));
633 return factory.getOWLClass(
634 IRI.create(((AtomicNegationConcept) concept)
635 .getNegatedAtomicConcept().getIRI()))
636 .getComplementNNF();
637 }
638
639 private OWLDataRange getDataRange(LiteralDataRange dataRange) {
640 if (dataRange instanceof InternalDatatype)
641 return factory.getOWLDatatype(IRI
642 .create(((InternalDatatype) dataRange).getIRI()));
643 if (dataRange instanceof DatatypeRestriction)
644 return factory
645 .getOWLDatatype(IRI
646 .create(((DatatypeRestriction) dataRange)
647 .getDatatypeURI()));
648 if (dataRange instanceof ConstantEnumeration) {
649 ConstantEnumeration e = (ConstantEnumeration) dataRange;
650 OWLLiteral[] values = new OWLLiteral[e.getNumberOfConstants()];
651 for (int i = 0; i < values.length; ++i) {
652 Constant c = e.getConstant(i);
653 values[i] = factory.getOWLLiteral(c.getDataValue().toString(),
654 factory.getOWLDatatype(IRI.create(c.getDatatypeURI())));
655 }
656 return factory.getOWLDataOneOf(values);
657 }
658 Utility.logError(dataRange.toString(), "strange data type!!!!");
659 return null;
660 }
661
662 public void seperateEquality4Clause(Set<Atom> eBodyAtoms,
663 Set<Atom> eHeadAtoms, Set<Variable> eVariables) {
664 Set<Variable> variables = new HashSet<Variable>();
665 DLPredicate predicate;
666 for (Atom atom : headAtoms) {
667 predicate = atom.getDLPredicate();
668 if (predicate instanceof Equality
669 || predicate instanceof AnnotatedEquality) {
670 variables.clear();
671 atom.getVariables(variables);
672 for (Variable variable : variables)
673 eVariables.add(variable);
674 }
675 }
676 eVariables.remove(X);
677
678 seperateEquality(bodyAtoms, eBodyAtoms, eVariables);
679 seperateEquality(headAtoms, eHeadAtoms, eVariables);
680 }
681
682 public void seperateEquality(Set<Atom> noEquality, Set<Atom> inEquality,
683 Set<Variable> eVariables) {
684 Set<Variable> variables = new HashSet<Variable>();
685 for (Iterator<Atom> iter = noEquality.iterator(); iter.hasNext();) {
686 Atom atom = iter.next();
687 if (atom.getDLPredicate() instanceof Equality
688 || atom.getDLPredicate() instanceof AnnotatedEquality) {
689 iter.remove();
690 inEquality.add(atom);
691 } else {
692 variables.clear();
693 atom.getVariables(variables);
694 for (Variable variable : variables)
695 if (eVariables.contains(variable)) {
696 iter.remove();
697 inEquality.add(atom);
698 break;
699 }
700 }
701 }
702 }
703
704 @Override
705 public String toString() {
706 StringBuilder ret = new StringBuilder();
707 boolean first = true;
708 for (OWLClassExpression exp : superClasses)
709 if (first) {
710 ret.append(exp.toString());
711 first = false;
712 } else
713 ret.append(" v ").append(exp.toString());
714
715 first = true;
716 for (OWLClassExpression exp : subClasses)
717 if (first) {
718 ret.append(" :- ").append(exp.toString());
719 first = false;
720 } else
721 ret.append(" ^ ").append(exp.toString());
722
723 return ret.toString();
724 }
725}
diff --git a/src/uk/ac/ox/cs/pagoda/approx/Clausifier.java b/src/uk/ac/ox/cs/pagoda/approx/Clausifier.java
deleted file mode 100644
index ee23def..0000000
--- a/src/uk/ac/ox/cs/pagoda/approx/Clausifier.java
+++ /dev/null
@@ -1,37 +0,0 @@
1package uk.ac.ox.cs.pagoda.approx;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Set;
6
7import org.semanticweb.HermiT.model.DLClause;
8import org.semanticweb.owlapi.model.OWLDataFactory;
9import org.semanticweb.owlapi.model.OWLDataProperty;
10import org.semanticweb.owlapi.model.OWLOntology;
11
12public class Clausifier {
13
14 OWLDataFactory factory;
15 Set<String> dataProperties = new HashSet<String>();
16
17 private Clausifier(OWLOntology ontology) {
18 factory = ontology.getOWLOntologyManager().getOWLDataFactory();
19 for (OWLDataProperty dataProperty: ontology.getDataPropertiesInSignature(true))
20 dataProperties.add(dataProperty.toStringID());
21 }
22
23 public Clause clasuify(DLClause clause) {
24 return new Clause(this, clause);
25 }
26
27 private static HashMap<OWLOntology, Clausifier> AllInstances = new HashMap<OWLOntology, Clausifier>();
28
29 public static Clausifier getInstance(OWLOntology onto) {
30 Clausifier c = AllInstances.get(onto);
31 if (c != null) return c;
32 c = new Clausifier(onto);
33 AllInstances.put(onto, c);
34 return c;
35 }
36
37}
diff --git a/src/uk/ac/ox/cs/pagoda/approx/KnowledgeBase.java b/src/uk/ac/ox/cs/pagoda/approx/KnowledgeBase.java
deleted file mode 100644
index 4b3c057..0000000
--- a/src/uk/ac/ox/cs/pagoda/approx/KnowledgeBase.java
+++ /dev/null
@@ -1,18 +0,0 @@
1package uk.ac.ox.cs.pagoda.approx;
2
3import org.semanticweb.owlapi.model.OWLOntology;
4import uk.ac.ox.cs.pagoda.constraints.BottomStrategy;
5
6public interface KnowledgeBase {
7
8 void load(OWLOntology ontology, BottomStrategy botStrategy);
9
10 void transform();
11
12 void save();
13
14 String getOutputPath();
15
16 String getDirectory();
17
18}
diff --git a/src/uk/ac/ox/cs/pagoda/approx/RLOntology.java b/src/uk/ac/ox/cs/pagoda/approx/RLOntology.java
deleted file mode 100644
index dba6a56..0000000
--- a/src/uk/ac/ox/cs/pagoda/approx/RLOntology.java
+++ /dev/null
@@ -1,190 +0,0 @@
1package uk.ac.ox.cs.pagoda.approx;
2
3import java.util.Collection;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.LinkedList;
7import java.util.Map;
8import java.util.Set;
9
10import org.semanticweb.owlapi.model.IRI;
11import org.semanticweb.owlapi.model.OWLAxiom;
12import org.semanticweb.owlapi.model.OWLClass;
13import org.semanticweb.owlapi.model.OWLClassExpression;
14import org.semanticweb.owlapi.model.OWLIndividual;
15import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom;
16import org.semanticweb.owlapi.model.OWLObjectHasSelf;
17import org.semanticweb.owlapi.model.OWLObjectOneOf;
18import org.semanticweb.owlapi.model.OWLObjectProperty;
19import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
20
21import uk.ac.ox.cs.pagoda.owl.OWLHelper;
22import uk.ac.ox.cs.pagoda.util.Utility;
23
24public class RLOntology extends RLPlusOntology {
25
26 @Override
27 public void transform() {
28 super.transform();
29
30 eliminateSelf();
31 eliminateNominals();
32 eliminateOWLThing();
33
34 save();
35 if (aBox.getAxiomCount() != 0)
36 save(aBox);
37 }
38
39 private void eliminateSelf() {
40 Collection<OWLAxiom> axioms = new LinkedList<OWLAxiom>(outputOntology.getAxioms());
41 OWLClassExpression subExp, superExp, newSubExp, newSuperExp;
42 for (OWLAxiom axiom: axioms)
43 if (axiom instanceof OWLSubClassOfAxiom) {
44 subExp = ((OWLSubClassOfAxiom) axiom).getSubClass();
45 superExp = ((OWLSubClassOfAxiom) axiom).getSuperClass();
46 newSubExp = approximateSelf4Sub(subExp);
47 newSuperExp = approximateSelf4Super(superExp);
48 if (newSubExp != subExp || newSuperExp != superExp)
49 replaceAxiom4output(axiom, factory.getOWLSubClassOfAxiom(newSubExp, newSuperExp));
50 }
51 }
52
53 private void replaceAxiom4output(OWLAxiom oldAxiom, OWLAxiom newAxiom) {
54 manager.removeAxiom(outputOntology, oldAxiom);
55 manager.addAxiom(outputOntology, newAxiom);
56 correspondence.put(newAxiom, correspondence.remove(oldAxiom));
57 }
58
59 private boolean hasSelf(OWLClassExpression conjunction) {
60 for (OWLClassExpression conjunct: conjunction.asConjunctSet())
61 if (conjunct instanceof OWLObjectHasSelf)
62 return true;
63 return false;
64 }
65
66 private OWLClassExpression approximateSelf4Sub(OWLClassExpression exp) {
67 if (!hasSelf(exp)) return exp;
68 Set<OWLClassExpression> newConjuncts = new HashSet<OWLClassExpression>();
69 for (OWLClassExpression conjunct: exp.asConjunctSet())
70 if (conjunct instanceof OWLObjectHasSelf)
71 newConjuncts.add(factory.getOWLObjectSomeValuesFrom(((OWLObjectHasSelf) exp).getProperty(), factory.getOWLThing()));
72 else
73 newConjuncts.add(conjunct);
74 return OWLHelper.getSimplifiedConjunction(factory, newConjuncts);
75 }
76
77 private OWLClassExpression approximateSelf4Super(OWLClassExpression exp) {
78 if (!hasSelf(exp)) return exp;
79 Set<OWLClassExpression> newConjuncts = new HashSet<OWLClassExpression>();
80 for (OWLClassExpression conjunct: exp.asConjunctSet())
81 if (conjunct instanceof OWLObjectHasSelf) {
82 OWLIndividual freshNominal = getNewIndividual(outputOntology, rlCounter++);
83 newConjuncts.add(factory.getOWLObjectOneOf(freshNominal));
84 newConjuncts.add(factory.getOWLObjectHasValue(((OWLObjectHasSelf) exp).getProperty(), freshNominal));
85 }
86 else
87 newConjuncts.add(conjunct);
88
89 return OWLHelper.getSimplifiedConjunction(factory, newConjuncts);
90 }
91
92 private void eliminateNominals() {
93 Collection<OWLAxiom> axioms = new LinkedList<OWLAxiom>(outputOntology.getAxioms());
94 OWLClassExpression superExp, newSuperExp;
95 for (OWLAxiom axiom: axioms)
96 if (axiom instanceof OWLSubClassOfAxiom) {
97 superExp = ((OWLSubClassOfAxiom) axiom).getSuperClass();
98 newSuperExp = approximateNominals(superExp);
99 if (newSuperExp != superExp)
100 replaceAxiom4output(axiom, factory.getOWLSubClassOfAxiom(((OWLSubClassOfAxiom) axiom).getSubClass(), newSuperExp));
101 }
102 }
103
104 private OWLClassExpression approximateNominals(OWLClassExpression exp) {
105 if (!hasIllegalNominals(exp)) return exp;
106 Set<OWLIndividual> nominals;
107 Set<OWLClassExpression> newConjuncts = new HashSet<OWLClassExpression>();
108 for (OWLClassExpression conjunct: exp.asConjunctSet()) {
109 if (conjunct instanceof OWLObjectOneOf) {
110 nominals = ((OWLObjectOneOf) conjunct).getIndividuals();
111 newConjuncts.add(approximateNominal(nominals));
112 }
113 else if (conjunct instanceof OWLObjectAllValuesFrom) {
114 OWLObjectAllValuesFrom allValuesFrom = ((OWLObjectAllValuesFrom) conjunct);
115 if (allValuesFrom.getFiller() instanceof OWLObjectOneOf) {
116 nominals = ((OWLObjectOneOf) allValuesFrom.getFiller()).getIndividuals();
117 newConjuncts.add(factory.getOWLObjectAllValuesFrom(allValuesFrom.getProperty(),
118 approximateNominal(nominals)));
119 }
120 }
121 }
122 return OWLHelper.getSimplifiedConjunction(factory, newConjuncts);
123 }
124
125 private OWLClassExpression approximateNominal(Set<OWLIndividual> nominals) {
126 if (nominals.size() > 1) {
127 Utility.logError("Error: more than one nominal appearing in OWLObjectOneOf");
128 return null;
129 }
130 OWLIndividual nominal = nominals.iterator().next();
131 OWLObjectProperty freshProperty = getNewRole4Nominal(nominal);
132 addAxiom2output(factory.getOWLInverseFunctionalObjectPropertyAxiom(freshProperty), null);
133 manager.addAxiom(aBox, factory.getOWLObjectPropertyAssertionAxiom(freshProperty, nominal, nominal));
134 return factory.getOWLObjectHasValue(freshProperty, nominal);
135 }
136
137 Map<OWLIndividual, OWLObjectProperty> role4nominal = new HashMap<OWLIndividual, OWLObjectProperty>();
138
139 private OWLObjectProperty getNewRole4Nominal(OWLIndividual nominal) {
140 OWLObjectProperty property;
141 if ((property = role4nominal.get(nominal)) == null)
142 role4nominal.put(nominal, property = getNewRole(outputOntology, rlCounter++));
143 return property;
144 }
145
146 private boolean hasIllegalNominals(OWLClassExpression exp) {
147 for (OWLClassExpression conjunct: exp.asConjunctSet()) {
148 if (conjunct instanceof OWLObjectOneOf) return true;
149 if (conjunct instanceof OWLObjectAllValuesFrom) {
150 OWLObjectAllValuesFrom allValuesFrom = ((OWLObjectAllValuesFrom) conjunct);
151 if (allValuesFrom.getFiller() instanceof OWLObjectOneOf)
152 return true;
153 }
154 }
155 return false;
156 }
157
158 private void eliminateOWLThing() {
159 OWLClassExpression subExp;
160 boolean mark = false;
161 for (Clause clause: clauses) {
162 subExp = OWLHelper.getSimplifiedConjunction(factory, clause.getSubClasses());
163 if (subExp.equals(factory.getOWLThing())) {
164 mark = true;
165 }
166 }
167
168 if (mark) {
169 Utility.logDebug("Top appears in the left of an axiom.");
170
171 OWLSubClassOfAxiom subClassAxiom;
172 OWLClass TOP = factory.getOWLClass(IRI.create(ontologyIRI + "#TOP"));
173 for (OWLAxiom axiom: new HashSet<OWLAxiom>(outputOntology.getAxioms()))
174 if (axiom instanceof OWLSubClassOfAxiom && (subClassAxiom = (OWLSubClassOfAxiom) axiom).getSubClass().equals(factory.getOWLThing()))
175 replaceAxiom4output(axiom, factory.getOWLSubClassOfAxiom(TOP, subClassAxiom.getSuperClass()));
176
177 for (OWLClass c: outputOntology.getClassesInSignature(true)) {
178 if (!c.equals(factory.getOWLThing()))
179 addAxiom2output(factory.getOWLSubClassOfAxiom(c, TOP), null);
180 else
181 addAxiom2output(factory.getOWLSubClassOfAxiom(TOP, c), null);
182 }
183 for (OWLObjectProperty p: outputOntology.getObjectPropertiesInSignature(true)) {
184 addAxiom2output(factory.getOWLObjectPropertyDomainAxiom(p, TOP), null);
185 addAxiom2output(factory.getOWLObjectPropertyRangeAxiom(p, TOP), null);
186 }
187 }
188 }
189
190}
diff --git a/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java b/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java
deleted file mode 100644
index 638a151..0000000
--- a/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java
+++ /dev/null
@@ -1,607 +0,0 @@
1package uk.ac.ox.cs.pagoda.approx;
2
3import org.apache.commons.io.FilenameUtils;
4import org.semanticweb.HermiT.Configuration;
5import org.semanticweb.HermiT.model.DLClause;
6import org.semanticweb.HermiT.model.DLOntology;
7import org.semanticweb.HermiT.structural.OWLClausification;
8import org.semanticweb.owlapi.model.*;
9import org.semanticweb.owlapi.profiles.OWL2RLProfile;
10import org.semanticweb.owlapi.profiles.OWLProfileReport;
11import org.semanticweb.owlapi.profiles.OWLProfileViolation;
12import uk.ac.ox.cs.pagoda.constraints.NullaryBottom;
13import uk.ac.ox.cs.pagoda.constraints.UnaryBottom;
14import uk.ac.ox.cs.pagoda.owl.OWLHelper;
15import uk.ac.ox.cs.pagoda.util.Namespace;
16import uk.ac.ox.cs.pagoda.util.Utility;
17
18import java.io.*;
19import java.nio.file.Paths;
20import java.util.*;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23
24public class RLPlusOntology implements KnowledgeBase {
25
26 private static final String DEFAULT_ONTOLOGY_FILE_EXTENSION = "owl";
27 private static final Pattern ONTOLOGY_ID_REGEX = Pattern.compile("ontologyid\\((?<id>[a-z0-9\\-]+)\\)");
28 OWLOntologyManager manager;
29 OWLDataFactory factory;
30 String ontologyIRI;
31 String corrFileName = null;
32 String outputPath, aBoxPath;
33 OWLOntology inputOntology = null;
34 OWLOntology tBox = null;
35 OWLOntology aBox = null;
36 OWLOntology restOntology = null;
37 OWLOntology outputOntology = null; //RL ontology
38 DLOntology dlOntology = null;
39 int rlCounter = 0;
40 LinkedList<Clause> clauses;
41 Map<OWLAxiom, OWLAxiom> correspondence;
42 BottomStrategy botStrategy;
43 Random random = new Random(19900114);
44 private Map<OWLClassExpression, Integer> subCounter = null;
45 private Map<OWLClass, OWLClass> atomic2negation = new HashMap<OWLClass, OWLClass>();
46
47 // TODO don't know if it is correct
48 @Override
49 public void load(OWLOntology ontology, uk.ac.ox.cs.pagoda.constraints.BottomStrategy bottomStrategy) {
50 if(bottomStrategy instanceof UnaryBottom)
51 botStrategy = BottomStrategy.UNARY;
52 else if(bottomStrategy instanceof NullaryBottom)
53 botStrategy = BottomStrategy.NULLARY;
54 else
55 botStrategy = BottomStrategy.TOREMOVE;
56
57 if(corrFileName == null)
58 corrFileName = "rlplus.crr";
59 manager = ontology.getOWLOntologyManager();
60// manager = OWLManager.createOWLOntologyManager();
61 factory = manager.getOWLDataFactory();
62 inputOntology = ontology;
63
64 try {
65 IRI ontologyIri;
66 if(ontology.isAnonymous()) {
67 Matcher matcher = ONTOLOGY_ID_REGEX.matcher(ontology.getOntologyID().toString().toLowerCase());
68 if(!matcher.matches()) throw new Error("Anonymous ontology without internal id");
69 ontologyIri =
70 IRI.create("http://www.example.org/", matcher.group("id") + "." + DEFAULT_ONTOLOGY_FILE_EXTENSION);
71 }
72 else
73 ontologyIri = inputOntology.getOntologyID().getOntologyIRI();
74
75 String ontologyIriPrefix = ontologyIri.getNamespace();
76 ontologyIRI = ontologyIri.toString();
77 String ontologyIriFragment = ontologyIri.getFragment();
78 String originalFileName = FilenameUtils.removeExtension(ontologyIriFragment);
79 String originalExtension = FilenameUtils.getExtension(ontologyIriFragment);
80 if(originalExtension.isEmpty()) originalExtension = DEFAULT_ONTOLOGY_FILE_EXTENSION;
81
82
83 IRI rlOntologyIRI = IRI.create(ontologyIriPrefix, originalFileName + "-RL." + originalExtension);
84 outputPath = Paths.get(Utility.getGlobalTempDirAbsolutePath(),
85 originalFileName + "-RL." + originalExtension).toString();
86 IRI rlDocumentIRI = IRI.create("file://" + outputPath);
87 outputOntology = manager.createOntology(rlOntologyIRI);
88 manager.setOntologyDocumentIRI(outputOntology, rlDocumentIRI);
89
90 String tBoxOntologyFragment = originalFileName + "-TBox." + originalExtension;
91 IRI tBoxOntologyIRI = IRI.create(ontologyIriPrefix, tBoxOntologyFragment);
92 IRI tBoxDocumentIRI =
93 IRI.create("file://" + Paths.get(Utility.getGlobalTempDirAbsolutePath(), tBoxOntologyFragment));
94
95 String aBoxOntologyFragment = originalFileName + "-ABox." + originalExtension;
96 IRI aBoxOntologyIRI = IRI.create(ontologyIriPrefix, aBoxOntologyFragment);
97 aBoxPath = Paths.get(Utility.getGlobalTempDirAbsolutePath(), aBoxOntologyFragment).toString();
98 IRI aBoxDocumentIRI =
99 IRI.create("file://" + Paths.get(Utility.getGlobalTempDirAbsolutePath(), aBoxOntologyFragment));
100
101 tBox = manager.createOntology(tBoxOntologyIRI);
102 aBox = manager.createOntology(aBoxOntologyIRI);
103 manager.setOntologyDocumentIRI(tBox, tBoxDocumentIRI);
104 manager.setOntologyDocumentIRI(aBox, aBoxDocumentIRI);
105
106 FileOutputStream aBoxOut = new FileOutputStream(aBoxPath);
107 manager.saveOntology(aBox, aBoxOut);
108 aBoxOut.close();
109
110 restOntology = manager.createOntology();
111 } catch(OWLOntologyCreationException | OWLOntologyStorageException | IOException e) {
112 e.printStackTrace();
113 System.exit(1);
114 }
115 }
116
117 public OWLOntology getTBox() {
118 return tBox;
119 }
120
121 public String getABoxPath() {
122 return aBoxPath;
123 }
124
125 public void simplify() {
126 if(simplifyABox()) {
127 save(aBox);
128// save(tBox);
129 }
130 else
131 tBox = inputOntology;
132 }
133
134 @Override
135 public void transform() {
136 simplify();
137 filter();
138 clausify();
139
140 subCounter = new HashMap<OWLClassExpression, Integer>();
141 clauses = new LinkedList<Clause>();
142 Clausifier clausifier = Clausifier.getInstance(restOntology);
143
144 for(DLClause c : dlOntology.getDLClauses()) {
145 Clause clause = new Clause(clausifier, c);
146 clauses.add(clause);
147
148 /*
149 * count the expressions in the left
150 */
151 for(OWLClassExpression exp : clause.getSubClasses()) {
152 if(exp instanceof OWLClass)
153 add2SubCounter(exp);
154 else if(exp instanceof OWLObjectSomeValuesFrom) {
155 OWLObjectSomeValuesFrom someValue = (OWLObjectSomeValuesFrom) exp;
156 add2SubCounter(factory.getOWLObjectSomeValuesFrom(someValue.getProperty(), factory.getOWLThing()));
157 add2SubCounter(someValue.getFiller());
158 }
159 else if(exp instanceof OWLObjectMinCardinality) {
160 OWLObjectMinCardinality minCard = (OWLObjectMinCardinality) exp;
161 add2SubCounter(factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), factory.getOWLThing()));
162 add2SubCounter(minCard.getFiller());
163 }
164 else
165 Utility.logError("strange class expression: " + exp);
166
167 }
168 }
169
170 correspondence = new HashMap<OWLAxiom, OWLAxiom>();
171 Set<OWLAxiom> addedAxioms = new HashSet<OWLAxiom>();
172 OWLClassExpression subExp;
173 for(Clause clause : clauses) {
174 subExp = uk.ac.ox.cs.pagoda.owl.OWLHelper.getSimplifiedConjunction(factory, clause.getSubClasses());
175 addedAxioms.clear();
176 for(OWLClassExpression exp : getDisjunctionApprox0(clause.getSuperClasses())) {
177 addedAxioms.add(factory.getOWLSubClassOfAxiom(subExp, transform(exp, addedAxioms)));
178 for(OWLAxiom a : addedAxioms)
179 addAxiom2output(a, factory.getOWLSubClassOfAxiom(subExp,
180 OWLHelper.getSimplifiedDisjunction(factory, clause.getSuperClasses())));
181 }
182 }
183
184 subCounter.clear();
185 }
186
187 @Override
188 public void save() {
189 if(corrFileName != null)
190 save(correspondence, corrFileName);
191 save(outputOntology);
192 }
193
194 public OWLOntologyManager getOWLOntologyManager() {
195 return inputOntology.getOWLOntologyManager();
196 }
197
198 public String getOntologyIRI() {
199 return ontologyIRI;
200 }
201
202 public OWLOntology getOutputOntology() {
203 return outputOntology;
204 }
205
206 @Override
207 public String getOutputPath() {
208 return outputPath;
209 }
210
211 @Override
212 public String getDirectory() {
213 return outputPath.substring(0, outputPath.lastIndexOf(Utility.FILE_SEPARATOR));
214 }
215
216 public void setCorrespondenceFileLoc(String path) {
217 corrFileName = path;
218 }
219
220 private void add2SubCounter(OWLClassExpression exp) {
221 Integer count = subCounter.get(exp);
222 if(count == null) count = 0;
223 ++count;
224 subCounter.put(exp, count);
225 }
226
227 private void save(Map<OWLAxiom, OWLAxiom> map, String corrFileName) {
228 if(corrFileName == null) return;
229 ObjectOutput output;
230 try {
231 output = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(corrFileName)));
232 output.writeObject(map);
233 output.close();
234 } catch(IOException e) {
235 e.printStackTrace();
236 }
237 }
238
239 protected void save(OWLOntology onto) {
240 try {
241 onto.getOWLOntologyManager().saveOntology(onto);
242 } catch(OWLOntologyStorageException e) {
243 e.printStackTrace();
244 }
245 }
246
247 /*
248 * treat disjunction as conjunction
249 */
250 private Set<OWLClassExpression> getDisjunctionApprox0(Set<OWLClassExpression> superClasses) {
251 return superClasses;
252 }
253
254 /*
255 * choose one simple class disjunct
256 */
257 @SuppressWarnings("unused")
258 private Set<OWLClassExpression> getDisjunctionApprox1(Set<OWLClassExpression> superClasses) {
259 if(superClasses.isEmpty() || superClasses.size() == 1)
260 return superClasses;
261
262 OWLClassExpression rep = null;
263 int min = Integer.MAX_VALUE, o;
264 for(OWLClassExpression exp : superClasses)
265 if(exp instanceof OWLClass && (o = getOccurrence(exp)) < min) {
266 min = o;
267 rep = exp;
268 }
269
270 if(rep == null) rep = superClasses.iterator().next();
271
272 return Collections.singleton(rep);
273 }
274
275 /*
276 * randomly choose a class expression to represent this disjunction
277 */
278 @SuppressWarnings("unused")
279 private Set<OWLClassExpression> getDisjunctionApprox2(Set<OWLClassExpression> superClasses) {
280 if(superClasses.isEmpty() || superClasses.size() == 1)
281 return superClasses;
282
283 int index = random.nextInt() % superClasses.size();
284 if(index < 0) index += superClasses.size();
285
286 int i = 0;
287 for(OWLClassExpression exp : superClasses)
288 if(i++ == index)
289 return Collections.singleton(exp);
290 return null;
291 }
292
293 /*
294 * choose the one that appears least in the l.h.s.
295 */
296 @SuppressWarnings("unused")
297 private Set<OWLClassExpression> getDisjunctionApprox3(Set<OWLClassExpression> superClasses) {
298 if(superClasses.isEmpty() || superClasses.size() == 1)
299 return superClasses;
300
301 OWLClassExpression rep = null, exp1;
302 int occurrence = Integer.MAX_VALUE, o;
303 for(OWLClassExpression exp : superClasses) {
304 o = 0;
305 exp1 = exp;
306 if(exp instanceof OWLObjectMinCardinality) {
307 OWLObjectMinCardinality minCard = (OWLObjectMinCardinality) exp;
308 if(minCard.getCardinality() == 1)
309 exp1 = factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), minCard.getFiller());
310 }
311
312 if(!subCounter.containsKey(exp1) || (o = subCounter.get(exp1)) < occurrence) {
313 rep = exp;
314 occurrence = o;
315 }
316 }
317
318 return Collections.singleton(rep);
319 }
320
321 private int getOccurrence(OWLClassExpression exp) {
322 if(!subCounter.containsKey(exp))
323 return 0;
324 return subCounter.get(exp);
325 }
326
327 @SuppressWarnings("unused")
328 private Set<OWLClassExpression> getDisjunctionApprox4(Set<OWLClassExpression> superClasses) {
329 if(superClasses.isEmpty() || superClasses.size() == 1)
330 return superClasses;
331
332 OWLClassExpression rep = null;
333 int occurrence = Integer.MAX_VALUE, o;
334 for(OWLClassExpression exp : superClasses) {
335 o = 0;
336 if(exp instanceof OWLObjectMinCardinality) {
337 OWLObjectMinCardinality minCard = (OWLObjectMinCardinality) exp;
338 if(minCard.getCardinality() == 1) {
339 o =
340 getOccurrence((factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), factory.getOWLThing())));
341 o += getOccurrence(minCard.getFiller());
342// if (o < o1) o = o1;
343 }
344 }
345 else
346 o = getOccurrence(exp);
347
348 if(o < occurrence || o == occurrence && !(rep instanceof OWLClass)) {
349 rep = exp;
350 occurrence = o;
351 }
352 }
353
354 return Collections.singleton(rep);
355 }
356
357 private boolean simplifyABox() {
358 boolean flag = false;
359 Map<OWLClassExpression, OWLClass> complex2atomic = new HashMap<OWLClassExpression, OWLClass>();
360
361 OWLDatatype anyURI = factory.getOWLDatatype(IRI.create(Namespace.XSD_NS + "anyURI"));
362 OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY));
363 OWLObjectProperty differentFrom = factory.getOWLObjectProperty(IRI.create(Namespace.INEQUALITY));
364
365 for(OWLOntology imported : inputOntology.getImportsClosure())
366 for(OWLAxiom axiom : imported.getAxioms()) {
367 if(axiom instanceof OWLClassAssertionAxiom) {
368 flag = true;
369 OWLClassAssertionAxiom assertion = (OWLClassAssertionAxiom) axiom;
370 OWLClassExpression clsExp = assertion.getClassExpression();
371 OWLClass cls;
372 if(clsExp instanceof OWLClass) {
373 if(((OWLClass) clsExp).toStringID().startsWith("owl:"))
374 manager.addAxiom(tBox, axiom);
375 else manager.addAxiom(aBox, axiom);
376 }
377 else {
378 if((cls = complex2atomic.get(clsExp)) == null) {
379 complex2atomic.put(clsExp, cls = getNewConcept(tBox, rlCounter++));
380 manager.addAxiom(tBox, factory.getOWLSubClassOfAxiom(cls, clsExp));
381 }
382 manager.addAxiom(aBox, factory.getOWLClassAssertionAxiom(cls, assertion.getIndividual()));
383 }
384 }
385 else if(axiom instanceof OWLObjectPropertyAssertionAxiom || axiom instanceof OWLDataPropertyAssertionAxiom || axiom instanceof OWLAnnotationAssertionAxiom) {
386 if(axiom.getDataPropertiesInSignature().contains(anyURI)) continue;
387 flag = true;
388 manager.addAxiom(aBox, axiom);
389 }
390 else if(axiom instanceof OWLSameIndividualAxiom) {
391 OWLIndividual firstIndividual = null, previousIndividual = null, lastIndividual = null;
392 for(OWLIndividual next : ((OWLSameIndividualAxiom) axiom).getIndividuals()) {
393 if(firstIndividual == null) firstIndividual = previousIndividual = next;
394 else
395 manager.addAxiom(aBox, factory.getOWLObjectPropertyAssertionAxiom(sameAs, previousIndividual, next));
396 previousIndividual = lastIndividual = next;
397 }
398 manager.addAxiom(aBox, factory.getOWLObjectPropertyAssertionAxiom(sameAs, lastIndividual, firstIndividual));
399 }
400 else if(axiom instanceof OWLDifferentIndividualsAxiom) {
401 int index1 = 0, index2;
402 for(OWLIndividual individual1 : ((OWLDifferentIndividualsAxiom) axiom).getIndividuals()) {
403 ++index1;
404 index2 = 0;
405 for(OWLIndividual individual2 : ((OWLDifferentIndividualsAxiom) axiom).getIndividuals()) {
406 if(index2++ < index1) {
407 manager.addAxiom(aBox, factory.getOWLObjectPropertyAssertionAxiom(differentFrom, individual1, individual2));
408 }
409 else break;
410 }
411 }
412 }
413 else
414 manager.addAxiom(tBox, axiom);
415 }
416
417 return flag;
418 }
419
420 private void filter() {
421 OWL2RLProfile profile = new OWL2RLProfile();
422 OWLProfileReport report = profile.checkOntology(tBox);
423 Set<OWLAxiom> rlAxioms = tBox.getAxioms();
424 OWLAxiom axiom;
425
426 for(OWLProfileViolation violation : report.getViolations()) {
427 manager.addAxiom(restOntology, axiom = violation.getAxiom());
428 rlAxioms.remove(axiom);
429 }
430
431 for(Iterator<OWLAxiom> iter = rlAxioms.iterator(); iter.hasNext(); )
432 addAxiom2output(iter.next(), null);
433 }
434
435 private void clausify() {
436 Configuration conf = new Configuration();
437 OWLClausification clausifier = new OWLClausification(conf);
438 dlOntology = (DLOntology) clausifier.preprocessAndClausify(restOntology, null)[1];
439 clausifier = null;
440 }
441
442 protected void addAxiom2output(OWLAxiom axiom, OWLAxiom correspondingAxiom) {
443 manager.addAxiom(outputOntology, axiom);
444 if(correspondingAxiom != null)
445 correspondence.put(axiom, correspondingAxiom);
446 }
447
448 private OWLClassExpression transform(OWLClassExpression exp, Set<OWLAxiom> addedAxioms) {
449 if(exp instanceof OWLClass)
450 return exp;
451
452 if(exp instanceof OWLObjectHasValue)
453 return exp;
454
455 if(exp instanceof OWLObjectSomeValuesFrom) {
456 OWLObjectSomeValuesFrom someValueExp = (OWLObjectSomeValuesFrom) exp;
457
458 OWLClassExpression tExp = someValueExp.getFiller();
459 if(tExp.equals(factory.getOWLThing()))
460 exp = factory.getOWLObjectMinCardinality(1, someValueExp.getProperty());
461 else
462 exp = factory.getOWLObjectMinCardinality(1, someValueExp.getProperty(), someValueExp.getFiller());
463 }
464
465 if(exp instanceof OWLObjectMinCardinality) {
466 OWLObjectMinCardinality minExp = (OWLObjectMinCardinality) exp;
467 OWLObjectPropertyExpression r;
468
469 if(minExp.getFiller().equals(factory.getOWLThing())) {
470 r = minExp.getProperty();
471 }
472 //TODO to be restored ...
473 //else if ((r = exists2role.get(someValueExp)) == null) {
474 // deal with r' \subseteq r & range(r') \subseteq C
475 else {
476 r = getNewRole(outputOntology, rlCounter);
477 addedAxioms.add(factory.getOWLSubObjectPropertyOfAxiom(r, minExp.getProperty()));
478 OWLClassExpression tExp = minExp.getFiller();
479 if(!(tExp instanceof OWLObjectComplementOf)) {
480 if(tExp.equals(factory.getOWLThing())) ;
481 else
482 addedAxioms.add(factory.getOWLObjectPropertyRangeAxiom(r, tExp));
483 }
484 else if(botStrategy != BottomStrategy.TOREMOVE) {
485 OWLClass cls = (OWLClass) tExp.getComplementNNF();
486 OWLClass neg;
487 if((neg = atomic2negation.get(cls)) == null) {
488 neg = getNewConcept(outputOntology, rlCounter);
489 addedAxioms.add(factory.getOWLDisjointClassesAxiom(neg, cls));
490 atomic2negation.put(cls, neg);
491 }
492 addedAxioms.add(factory.getOWLObjectPropertyRangeAxiom(r, neg));
493 }
494// exists2role.put(someValueExp, (OWLObjectProperty) r);
495 }
496
497 // deal with r'(x,c)
498 Set<OWLClassExpression> ret = new HashSet<OWLClassExpression>();
499 int num = minExp.getCardinality();
500
501 Set<OWLNamedIndividual> cs = new HashSet<OWLNamedIndividual>();
502 OWLNamedIndividual c;
503 for(int i = 0; i < num; ++i) {
504 c = getNewIndividual(outputOntology, rlCounter++);
505 ret.add(factory.getOWLObjectHasValue(r, c));
506 cs.add(c);
507 }
508
509 if(botStrategy != BottomStrategy.TOREMOVE && cs.size() > 1) {
510 addedAxioms.add(factory.getOWLDifferentIndividualsAxiom(cs));
511 }
512
513 return OWLHelper.getSimplifiedConjunction(factory, ret);
514 }
515
516 if(exp instanceof OWLObjectMaxCardinality) {
517 OWLObjectMaxCardinality maxExp = (OWLObjectMaxCardinality) exp;
518 OWLClassExpression tExp = maxExp.getFiller();
519 int card = maxExp.getCardinality() >= 1 ? 1 : 0;
520 if(!(tExp instanceof OWLObjectComplementOf))
521 return factory.getOWLObjectMaxCardinality(card, maxExp.getProperty(), tExp);
522 else {
523 Utility.logDebug("oh, to be tested ... ");
524 OWLClassExpression tExp1 =
525 factory.getOWLObjectAllValuesFrom(maxExp.getProperty(), tExp.getComplementNNF());
526 if(card == 0)
527 return tExp1;
528 else {
529 OWLClassExpression tExp2 = factory.getOWLObjectMaxCardinality(1, maxExp.getProperty());
530 return factory.getOWLObjectIntersectionOf(tExp1, tExp2);
531 }
532 }
533 }
534
535 if(exp instanceof OWLObjectAllValuesFrom)
536 return exp;
537
538 if(exp instanceof OWLObjectOneOf)
539 if(((OWLObjectOneOf) exp).getIndividuals().size() == 1)
540 return exp;
541 else
542 return null;
543
544 if(exp instanceof OWLDataHasValue)
545 return exp;
546
547 //TODO overapproximation - dealing with OWLDataMinCardinality
548
549 if(exp instanceof OWLDataSomeValuesFrom) {
550 return exp;
551 }
552
553 if(exp instanceof OWLDataMinCardinality) {
554 return exp;
555 }
556
557 if(exp instanceof OWLDataMaxCardinality) {
558 return exp;
559 }
560
561
562 Set<OWLClassExpression> exps = exp.asConjunctSet();
563 if(exps.size() == 1 && exps.iterator().next() == exp) {
564 Utility.logError(exp, "error in transform of Ontology~~~~");
565 }
566 Set<OWLClassExpression> nexps = new HashSet<OWLClassExpression>();
567 OWLClassExpression ne;
568 boolean changes = false;
569 for(OWLClassExpression e : exps) {
570 ne = transform(e, addedAxioms);
571 if(ne != e) changes = true;
572 nexps.add(ne);
573 }
574 if(changes)
575 return OWLHelper.getSimplifiedConjunction(factory, nexps);
576 else
577 return exp;
578 }
579
580 protected OWLNamedIndividual getNewIndividual(OWLOntology onto, int number) {
581 OWLOntologyManager manager = onto.getOWLOntologyManager();
582 OWLDataFactory factory = manager.getOWLDataFactory();
583 OWLNamedIndividual newIndividual =
584 factory.getOWLNamedIndividual(IRI.create(Namespace.PAGODA_ANONY + "NI" + number));
585 manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newIndividual));
586 return newIndividual;
587 }
588
589 protected OWLObjectProperty getNewRole(OWLOntology onto, int number) {
590 OWLOntologyManager manager = onto.getOWLOntologyManager();
591 OWLDataFactory factory = manager.getOWLDataFactory();
592 OWLObjectProperty newProperty = factory.getOWLObjectProperty(IRI.create(Namespace.PAGODA_AUX + "NR" + number));
593 manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newProperty));
594 return newProperty;
595 }
596
597 private OWLClass getNewConcept(OWLOntology onto, int number) {
598 OWLOntologyManager manager = onto.getOWLOntologyManager();
599 OWLDataFactory factory = manager.getOWLDataFactory();
600 OWLClass newClass = factory.getOWLClass(IRI.create(Namespace.PAGODA_AUX + "NC" + number));
601 manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newClass));
602 return newClass;
603 }
604
605 private enum BottomStrategy {TOREMOVE, NULLARY, UNARY}
606}
607