diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | 608 |
1 files changed, 608 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java b/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java new file mode 100644 index 0000000..a43d9af --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | |||
| @@ -0,0 +1,608 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.approx; | ||
| 2 | |||
| 3 | import java.io.BufferedOutputStream; | ||
| 4 | import java.io.FileOutputStream; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.io.ObjectOutput; | ||
| 7 | import java.io.ObjectOutputStream; | ||
| 8 | import java.util.Collections; | ||
| 9 | import java.util.HashMap; | ||
| 10 | import java.util.HashSet; | ||
| 11 | import java.util.Iterator; | ||
| 12 | import java.util.LinkedList; | ||
| 13 | import java.util.Map; | ||
| 14 | import java.util.Random; | ||
| 15 | import java.util.Set; | ||
| 16 | |||
| 17 | import org.semanticweb.HermiT.Configuration; | ||
| 18 | import org.semanticweb.HermiT.model.DLClause; | ||
| 19 | import org.semanticweb.HermiT.model.DLOntology; | ||
| 20 | import org.semanticweb.HermiT.structural.OWLClausification; | ||
| 21 | import org.semanticweb.owlapi.model.IRI; | ||
| 22 | import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; | ||
| 23 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 24 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 25 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 26 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 27 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 28 | import org.semanticweb.owlapi.model.OWLDataHasValue; | ||
| 29 | import org.semanticweb.owlapi.model.OWLDataMaxCardinality; | ||
| 30 | import org.semanticweb.owlapi.model.OWLDataMinCardinality; | ||
| 31 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 32 | import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; | ||
| 33 | import org.semanticweb.owlapi.model.OWLDatatype; | ||
| 34 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 35 | import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; | ||
| 36 | import org.semanticweb.owlapi.model.OWLObjectComplementOf; | ||
| 37 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 38 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 39 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 40 | import org.semanticweb.owlapi.model.OWLObjectOneOf; | ||
| 41 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 42 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 43 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 44 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 45 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 46 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 47 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 48 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 49 | import org.semanticweb.owlapi.profiles.OWL2RLProfile; | ||
| 50 | import org.semanticweb.owlapi.profiles.OWLProfileReport; | ||
| 51 | import org.semanticweb.owlapi.profiles.OWLProfileViolation; | ||
| 52 | |||
| 53 | import uk.ac.ox.cs.pagoda.constraints.NullaryBottom; | ||
| 54 | import uk.ac.ox.cs.pagoda.constraints.UnaryBottom; | ||
| 55 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 56 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 57 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 58 | |||
| 59 | public class RLPlusOntology implements KnowledgeBase { | ||
| 60 | |||
| 61 | OWLOntologyManager manager; | ||
| 62 | OWLDataFactory factory; | ||
| 63 | String ontologyIRI; | ||
| 64 | String corrFileName = null; | ||
| 65 | String outputPath, aBoxPath; | ||
| 66 | |||
| 67 | OWLOntology inputOntology = null; | ||
| 68 | OWLOntology tBox = null; | ||
| 69 | OWLOntology aBox = null; | ||
| 70 | OWLOntology restOntology = null; | ||
| 71 | OWLOntology outputOntology = null; //RL ontology | ||
| 72 | |||
| 73 | DLOntology dlOntology = null; | ||
| 74 | int rlCounter = 0; | ||
| 75 | |||
| 76 | LinkedList<Clause> clauses; | ||
| 77 | Map<OWLAxiom, OWLAxiom> correspondence; | ||
| 78 | |||
| 79 | BottomStrategy botStrategy; | ||
| 80 | |||
| 81 | @Override | ||
| 82 | public void load(OWLOntology o, uk.ac.ox.cs.pagoda.constraints.BottomStrategy bottomStrategy) { | ||
| 83 | if (bottomStrategy instanceof UnaryBottom) | ||
| 84 | botStrategy = BottomStrategy.UNARY; | ||
| 85 | else if (bottomStrategy instanceof NullaryBottom) | ||
| 86 | botStrategy = BottomStrategy.NULLARY; | ||
| 87 | else | ||
| 88 | botStrategy = BottomStrategy.TOREMOVE; | ||
| 89 | |||
| 90 | if (corrFileName == null) | ||
| 91 | corrFileName = "rlplus.crr"; | ||
| 92 | manager = o.getOWLOntologyManager(); | ||
| 93 | // manager = OWLManager.createOWLOntologyManager(); | ||
| 94 | factory = manager.getOWLDataFactory(); | ||
| 95 | inputOntology = o; | ||
| 96 | |||
| 97 | try { | ||
| 98 | String path = OWLHelper.getOntologyPath(inputOntology); | ||
| 99 | String name = path.substring(path.lastIndexOf(Utility.JAVA_FILE_SEPARATOR)); | ||
| 100 | String originalExtension = name.lastIndexOf(".") >= 0 ? name.substring(name.lastIndexOf(".")) : ""; | ||
| 101 | |||
| 102 | if (inputOntology.getOntologyID().getOntologyIRI() == null) | ||
| 103 | ontologyIRI = "http://www.example.org/anonymous-ontology" + originalExtension; | ||
| 104 | else | ||
| 105 | ontologyIRI = inputOntology.getOntologyID().getOntologyIRI().toString(); | ||
| 106 | |||
| 107 | String tOntoIRI = ontologyIRI; | ||
| 108 | if (!tOntoIRI.endsWith(originalExtension)) tOntoIRI += originalExtension; | ||
| 109 | |||
| 110 | String rlOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-RL.owl" : tOntoIRI.replaceFirst(originalExtension, "-RL.owl"); | ||
| 111 | String rlDocumentIRI = (outputPath = Utility.TempDirectory + "RL.owl"); | ||
| 112 | outputOntology = manager.createOntology(IRI.create(rlOntologyIRI)); | ||
| 113 | manager.setOntologyDocumentIRI(outputOntology, IRI.create(Utility.toFileIRI(rlDocumentIRI))); | ||
| 114 | |||
| 115 | String tBoxOntologyIRI, aBoxOntologyIRI; | ||
| 116 | tBoxOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-TBox.owl" : tOntoIRI.replaceFirst(originalExtension, "-TBox.owl"); | ||
| 117 | aBoxOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-ABox.owl" : tOntoIRI.replaceFirst(originalExtension, "-ABox.owl"); | ||
| 118 | |||
| 119 | String tBoxDocumentIRI = (Utility.TempDirectory + "TBox.owl"); | ||
| 120 | String aBoxDocumentIRI = (aBoxPath = Utility.TempDirectory + "ABox.owl"); | ||
| 121 | tBox = manager.createOntology(IRI.create(tBoxOntologyIRI)); | ||
| 122 | aBox = manager.createOntology(IRI.create(aBoxOntologyIRI)); | ||
| 123 | manager.setOntologyDocumentIRI(tBox, IRI.create(Utility.toFileIRI(tBoxDocumentIRI))); | ||
| 124 | manager.setOntologyDocumentIRI(aBox, IRI.create(Utility.toFileIRI(aBoxDocumentIRI))); | ||
| 125 | |||
| 126 | FileOutputStream aBoxOut = new FileOutputStream(aBoxPath); | ||
| 127 | manager.saveOntology(aBox, aBoxOut); | ||
| 128 | aBoxOut.close(); | ||
| 129 | |||
| 130 | restOntology = manager.createOntology(); | ||
| 131 | } | ||
| 132 | catch (OWLOntologyCreationException e) { | ||
| 133 | e.printStackTrace(); | ||
| 134 | } catch (OWLOntologyStorageException e) { | ||
| 135 | // TODO Auto-generated catch block | ||
| 136 | e.printStackTrace(); | ||
| 137 | } catch (IOException e) { | ||
| 138 | // TODO Auto-generated catch block | ||
| 139 | e.printStackTrace(); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | public OWLOntology getTBox() { | ||
| 144 | return tBox; | ||
| 145 | } | ||
| 146 | |||
| 147 | public String getABoxPath() { | ||
| 148 | return aBoxPath; | ||
| 149 | } | ||
| 150 | |||
| 151 | private void add2SubCounter(OWLClassExpression exp) { | ||
| 152 | Integer count = subCounter.get(exp); | ||
| 153 | if (count == null) count = 0; | ||
| 154 | ++count; | ||
| 155 | subCounter.put(exp, count); | ||
| 156 | } | ||
| 157 | |||
| 158 | public void simplify() { | ||
| 159 | if (simplifyABox()) { | ||
| 160 | save(aBox); | ||
| 161 | // save(tBox); | ||
| 162 | } | ||
| 163 | else | ||
| 164 | tBox = inputOntology; | ||
| 165 | } | ||
| 166 | |||
| 167 | @Override | ||
| 168 | public void transform() { | ||
| 169 | simplify(); | ||
| 170 | filter(); | ||
| 171 | clausify(); | ||
| 172 | |||
| 173 | subCounter = new HashMap<OWLClassExpression, Integer>(); | ||
| 174 | clauses = new LinkedList<Clause>(); | ||
| 175 | Clausifier clausifier = Clausifier.getInstance(restOntology); | ||
| 176 | |||
| 177 | for (DLClause c: dlOntology.getDLClauses()) { | ||
| 178 | Clause clause = new Clause(clausifier, c); | ||
| 179 | clauses.add(clause); | ||
| 180 | |||
| 181 | /* | ||
| 182 | * count the expressions in the left | ||
| 183 | */ | ||
| 184 | for (OWLClassExpression exp: clause.getSubClasses()) { | ||
| 185 | if (exp instanceof OWLClass) | ||
| 186 | add2SubCounter(exp); | ||
| 187 | else if (exp instanceof OWLObjectSomeValuesFrom) { | ||
| 188 | OWLObjectSomeValuesFrom someValue = (OWLObjectSomeValuesFrom)exp; | ||
| 189 | add2SubCounter(factory.getOWLObjectSomeValuesFrom(someValue.getProperty(), factory.getOWLThing())); | ||
| 190 | add2SubCounter(someValue.getFiller()); | ||
| 191 | } | ||
| 192 | else if (exp instanceof OWLObjectMinCardinality) { | ||
| 193 | OWLObjectMinCardinality minCard = (OWLObjectMinCardinality)exp; | ||
| 194 | add2SubCounter(factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), factory.getOWLThing())); | ||
| 195 | add2SubCounter(minCard.getFiller()); | ||
| 196 | } | ||
| 197 | else | ||
| 198 | Utility.logError("strange class expression: " + exp); | ||
| 199 | |||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | correspondence = new HashMap<OWLAxiom, OWLAxiom>(); | ||
| 204 | Set<OWLAxiom> addedAxioms = new HashSet<OWLAxiom>(); | ||
| 205 | OWLClassExpression subExp; | ||
| 206 | for (Clause clause: clauses) { | ||
| 207 | subExp = uk.ac.ox.cs.pagoda.owl.OWLHelper.getSimplifiedConjunction(factory, clause.getSubClasses()); | ||
| 208 | addedAxioms.clear(); | ||
| 209 | for (OWLClassExpression exp: getDisjunctionApprox0(clause.getSuperClasses())) { | ||
| 210 | addedAxioms.add(factory.getOWLSubClassOfAxiom(subExp, transform(exp, addedAxioms))); | ||
| 211 | for (OWLAxiom a: addedAxioms) | ||
| 212 | addAxiom2output(a, factory.getOWLSubClassOfAxiom(subExp, | ||
| 213 | OWLHelper.getSimplifiedDisjunction(factory, clause.getSuperClasses()))); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | subCounter.clear(); | ||
| 218 | } | ||
| 219 | |||
| 220 | @Override | ||
| 221 | public void save() { | ||
| 222 | if (corrFileName != null) | ||
| 223 | save(correspondence, corrFileName); | ||
| 224 | save(outputOntology); | ||
| 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 | Random random = new Random(19900114); | ||
| 276 | /* | ||
| 277 | * randomly choose a class expression to represent this disjunction | ||
| 278 | */ | ||
| 279 | @SuppressWarnings("unused") | ||
| 280 | private Set<OWLClassExpression> getDisjunctionApprox2(Set<OWLClassExpression> superClasses) { | ||
| 281 | if (superClasses.isEmpty() || superClasses.size() == 1) | ||
| 282 | return superClasses; | ||
| 283 | |||
| 284 | int index = random.nextInt() % superClasses.size(); | ||
| 285 | if (index < 0) index += superClasses.size(); | ||
| 286 | |||
| 287 | int i = 0; | ||
| 288 | for (OWLClassExpression exp: superClasses) | ||
| 289 | if (i++ == index) | ||
| 290 | return Collections.singleton(exp); | ||
| 291 | return null; | ||
| 292 | } | ||
| 293 | |||
| 294 | private Map<OWLClassExpression, Integer> subCounter = null; | ||
| 295 | /* | ||
| 296 | * choose the one that appears least in the l.h.s. | ||
| 297 | */ | ||
| 298 | @SuppressWarnings("unused") | ||
| 299 | private Set<OWLClassExpression> getDisjunctionApprox3(Set<OWLClassExpression> superClasses) { | ||
| 300 | if (superClasses.isEmpty() || superClasses.size() == 1) | ||
| 301 | return superClasses; | ||
| 302 | |||
| 303 | OWLClassExpression rep = null, exp1; | ||
| 304 | int occurrence = Integer.MAX_VALUE, o; | ||
| 305 | for (OWLClassExpression exp: superClasses) { | ||
| 306 | o = 0; | ||
| 307 | exp1 = exp; | ||
| 308 | if (exp instanceof OWLObjectMinCardinality) { | ||
| 309 | OWLObjectMinCardinality minCard = (OWLObjectMinCardinality)exp; | ||
| 310 | if (minCard.getCardinality() == 1) | ||
| 311 | exp1 = factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), minCard.getFiller()); | ||
| 312 | } | ||
| 313 | |||
| 314 | if (!subCounter.containsKey(exp1) || (o = subCounter.get(exp1)) < occurrence) { | ||
| 315 | rep = exp; | ||
| 316 | occurrence = o; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | return Collections.singleton(rep); | ||
| 321 | } | ||
| 322 | |||
| 323 | private int getOccurrence(OWLClassExpression exp) { | ||
| 324 | if (!subCounter.containsKey(exp)) | ||
| 325 | return 0; | ||
| 326 | return subCounter.get(exp); | ||
| 327 | } | ||
| 328 | |||
| 329 | @SuppressWarnings("unused") | ||
| 330 | private Set<OWLClassExpression> getDisjunctionApprox4(Set<OWLClassExpression> superClasses) { | ||
| 331 | if (superClasses.isEmpty() || superClasses.size() == 1) | ||
| 332 | return superClasses; | ||
| 333 | |||
| 334 | OWLClassExpression rep = null; | ||
| 335 | int occurrence = Integer.MAX_VALUE, o; | ||
| 336 | for (OWLClassExpression exp: superClasses) { | ||
| 337 | o = 0; | ||
| 338 | if (exp instanceof OWLObjectMinCardinality) { | ||
| 339 | OWLObjectMinCardinality minCard = (OWLObjectMinCardinality)exp; | ||
| 340 | if (minCard.getCardinality() == 1) { | ||
| 341 | o = getOccurrence((factory.getOWLObjectSomeValuesFrom(minCard.getProperty(), factory.getOWLThing()))); | ||
| 342 | o += getOccurrence(minCard.getFiller()); | ||
| 343 | // if (o < o1) o = o1; | ||
| 344 | } | ||
| 345 | } | ||
| 346 | else | ||
| 347 | o = getOccurrence(exp); | ||
| 348 | |||
| 349 | if (o < occurrence || o == occurrence && !(rep instanceof OWLClass)) { | ||
| 350 | rep = exp; | ||
| 351 | occurrence = o; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | return Collections.singleton(rep); | ||
| 356 | } | ||
| 357 | |||
| 358 | private boolean simplifyABox() { | ||
| 359 | boolean flag = false; | ||
| 360 | Map<OWLClassExpression, OWLClass> complex2atomic= new HashMap<OWLClassExpression, OWLClass>(); | ||
| 361 | |||
| 362 | OWLDatatype anyURI = factory.getOWLDatatype(IRI.create(Namespace.XSD_NS + "anyURI")); | ||
| 363 | for (OWLOntology imported: inputOntology.getImportsClosure()) | ||
| 364 | for (OWLAxiom axiom: imported.getAxioms()) { | ||
| 365 | if (axiom instanceof OWLClassAssertionAxiom) { | ||
| 366 | flag = true; | ||
| 367 | OWLClassAssertionAxiom assertion = (OWLClassAssertionAxiom)axiom; | ||
| 368 | OWLClassExpression clsExp = assertion.getClassExpression(); | ||
| 369 | OWLClass cls; | ||
| 370 | if (clsExp instanceof OWLClass) { | ||
| 371 | if (((OWLClass) clsExp).toStringID().startsWith("owl:")) | ||
| 372 | manager.addAxiom(tBox, axiom); | ||
| 373 | else manager.addAxiom(aBox, axiom); | ||
| 374 | } | ||
| 375 | else { | ||
| 376 | if ((cls = complex2atomic.get(clsExp)) == null) { | ||
| 377 | complex2atomic.put(clsExp, cls = getNewConcept(tBox, rlCounter++)); | ||
| 378 | manager.addAxiom(tBox, factory.getOWLSubClassOfAxiom(cls, clsExp)); | ||
| 379 | } | ||
| 380 | manager.addAxiom(aBox, factory.getOWLClassAssertionAxiom(cls, assertion.getIndividual())); | ||
| 381 | } | ||
| 382 | } | ||
| 383 | else if (axiom instanceof OWLObjectPropertyAssertionAxiom || axiom instanceof OWLDataPropertyAssertionAxiom || axiom instanceof OWLAnnotationAssertionAxiom) { | ||
| 384 | if (axiom.containsEntityInSignature(anyURI)) continue; | ||
| 385 | flag = true; | ||
| 386 | manager.addAxiom(aBox, axiom); | ||
| 387 | } | ||
| 388 | else | ||
| 389 | manager.addAxiom(tBox, axiom); | ||
| 390 | } | ||
| 391 | |||
| 392 | return flag; | ||
| 393 | } | ||
| 394 | |||
| 395 | private void filter() { | ||
| 396 | OWL2RLProfile profile = new OWL2RLProfile(); | ||
| 397 | OWLProfileReport report = profile.checkOntology(tBox); | ||
| 398 | Set<OWLAxiom> rlAxioms = tBox.getAxioms(); | ||
| 399 | OWLAxiom axiom; | ||
| 400 | |||
| 401 | for (OWLProfileViolation violation: report.getViolations()) { | ||
| 402 | manager.addAxiom(restOntology, axiom = violation.getAxiom()); | ||
| 403 | rlAxioms.remove(axiom); | ||
| 404 | } | ||
| 405 | |||
| 406 | for (Iterator<OWLAxiom> iter = rlAxioms.iterator(); iter.hasNext(); ) | ||
| 407 | addAxiom2output(iter.next(), null); | ||
| 408 | } | ||
| 409 | |||
| 410 | private void clausify() { | ||
| 411 | Configuration conf = new Configuration(); | ||
| 412 | OWLClausification clausifier = new OWLClausification(conf); | ||
| 413 | dlOntology = (DLOntology)clausifier.preprocessAndClausify(restOntology, null)[1]; | ||
| 414 | clausifier = null; | ||
| 415 | } | ||
| 416 | |||
| 417 | protected void addAxiom2output(OWLAxiom axiom, OWLAxiom correspondingAxiom) { | ||
| 418 | manager.addAxiom(outputOntology, axiom); | ||
| 419 | if (correspondingAxiom != null) | ||
| 420 | correspondence.put(axiom, correspondingAxiom); | ||
| 421 | } | ||
| 422 | |||
| 423 | private Map<OWLClass, OWLClass> atomic2negation = new HashMap<OWLClass, OWLClass>(); | ||
| 424 | |||
| 425 | private OWLClassExpression transform(OWLClassExpression exp, Set<OWLAxiom> addedAxioms) { | ||
| 426 | if (exp instanceof OWLClass) | ||
| 427 | return exp; | ||
| 428 | |||
| 429 | if (exp instanceof OWLObjectHasValue) | ||
| 430 | return exp; | ||
| 431 | |||
| 432 | if (exp instanceof OWLObjectSomeValuesFrom) { | ||
| 433 | OWLObjectSomeValuesFrom someValueExp = (OWLObjectSomeValuesFrom)exp; | ||
| 434 | |||
| 435 | OWLClassExpression tExp = someValueExp.getFiller(); | ||
| 436 | if (tExp.equals(factory.getOWLThing())) | ||
| 437 | exp = factory.getOWLObjectMinCardinality(1, someValueExp.getProperty()); | ||
| 438 | else | ||
| 439 | exp = factory.getOWLObjectMinCardinality(1, someValueExp.getProperty(), someValueExp.getFiller()); | ||
| 440 | } | ||
| 441 | |||
| 442 | if (exp instanceof OWLObjectMinCardinality) { | ||
| 443 | OWLObjectMinCardinality minExp = (OWLObjectMinCardinality)exp; | ||
| 444 | OWLObjectPropertyExpression r; | ||
| 445 | |||
| 446 | if (minExp.getFiller().equals(factory.getOWLThing())) { | ||
| 447 | r = minExp.getProperty(); | ||
| 448 | } | ||
| 449 | //TODO to be restored ... | ||
| 450 | //else if ((r = exists2role.get(someValueExp)) == null) { | ||
| 451 | // deal with r' \subseteq r & range(r') \subseteq C | ||
| 452 | else { | ||
| 453 | r = getNewRole(outputOntology, rlCounter); | ||
| 454 | addedAxioms.add(factory.getOWLSubObjectPropertyOfAxiom(r, minExp.getProperty())); | ||
| 455 | OWLClassExpression tExp = minExp.getFiller(); | ||
| 456 | if (!(tExp instanceof OWLObjectComplementOf)) { | ||
| 457 | if (tExp.equals(factory.getOWLThing())); | ||
| 458 | else | ||
| 459 | addedAxioms.add(factory.getOWLObjectPropertyRangeAxiom(r, tExp)); | ||
| 460 | } | ||
| 461 | else if (botStrategy != BottomStrategy.TOREMOVE) { | ||
| 462 | OWLClass cls = (OWLClass) ((OWLObjectComplementOf) tExp).getComplementNNF(); | ||
| 463 | OWLClass neg; | ||
| 464 | if ((neg = atomic2negation.get(cls)) == null) { | ||
| 465 | neg = getNewConcept(outputOntology, rlCounter); | ||
| 466 | addedAxioms.add(factory.getOWLDisjointClassesAxiom(neg, cls)); | ||
| 467 | atomic2negation.put(cls, neg); | ||
| 468 | } | ||
| 469 | addedAxioms.add(factory.getOWLObjectPropertyRangeAxiom(r, neg)); | ||
| 470 | } | ||
| 471 | // exists2role.put(someValueExp, (OWLObjectProperty) r); | ||
| 472 | } | ||
| 473 | |||
| 474 | // deal with r'(x,c) | ||
| 475 | Set<OWLClassExpression> ret = new HashSet<OWLClassExpression>(); | ||
| 476 | int num = minExp.getCardinality(); | ||
| 477 | |||
| 478 | Set<OWLNamedIndividual> cs = new HashSet<OWLNamedIndividual>(); | ||
| 479 | OWLNamedIndividual c; | ||
| 480 | for (int i = 0; i < num; ++i) { | ||
| 481 | c = getNewIndividual(outputOntology, rlCounter++); | ||
| 482 | ret.add(factory.getOWLObjectHasValue(r, c)); | ||
| 483 | cs.add(c); | ||
| 484 | } | ||
| 485 | |||
| 486 | if (botStrategy != BottomStrategy.TOREMOVE && cs.size() > 1) { | ||
| 487 | addedAxioms.add(factory.getOWLDifferentIndividualsAxiom(cs)); | ||
| 488 | } | ||
| 489 | |||
| 490 | return OWLHelper.getSimplifiedConjunction(factory, ret); | ||
| 491 | } | ||
| 492 | |||
| 493 | if (exp instanceof OWLObjectMaxCardinality) { | ||
| 494 | OWLObjectMaxCardinality maxExp = (OWLObjectMaxCardinality)exp; | ||
| 495 | OWLClassExpression tExp = maxExp.getFiller(); | ||
| 496 | int card = maxExp.getCardinality() >= 1 ? 1 : 0; | ||
| 497 | if (!(tExp instanceof OWLObjectComplementOf)) | ||
| 498 | return factory.getOWLObjectMaxCardinality(card, maxExp.getProperty(), tExp); | ||
| 499 | else { | ||
| 500 | Utility.logDebug("oh, to be tested ... "); | ||
| 501 | OWLClassExpression tExp1 = factory.getOWLObjectAllValuesFrom(maxExp.getProperty(), tExp.getComplementNNF()); | ||
| 502 | if (card == 0) | ||
| 503 | return tExp1; | ||
| 504 | else { | ||
| 505 | OWLClassExpression tExp2 = factory.getOWLObjectMaxCardinality(1, maxExp.getProperty()); | ||
| 506 | return factory.getOWLObjectIntersectionOf(tExp1, tExp2); | ||
| 507 | } | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | if (exp instanceof OWLObjectAllValuesFrom) | ||
| 512 | return exp; | ||
| 513 | |||
| 514 | if (exp instanceof OWLObjectOneOf) | ||
| 515 | if (((OWLObjectOneOf) exp).getIndividuals().size() == 1) | ||
| 516 | return exp; | ||
| 517 | else | ||
| 518 | return null; | ||
| 519 | |||
| 520 | if (exp instanceof OWLDataHasValue) | ||
| 521 | return exp; | ||
| 522 | |||
| 523 | //TODO overapproximation - dealing with OWLDataMinCardinality | ||
| 524 | |||
| 525 | if (exp instanceof OWLDataSomeValuesFrom) { | ||
| 526 | return exp; | ||
| 527 | } | ||
| 528 | |||
| 529 | if (exp instanceof OWLDataMinCardinality) { | ||
| 530 | return exp; | ||
| 531 | } | ||
| 532 | |||
| 533 | if (exp instanceof OWLDataMaxCardinality) { | ||
| 534 | return exp; | ||
| 535 | } | ||
| 536 | |||
| 537 | |||
| 538 | Set<OWLClassExpression> exps = exp.asConjunctSet(); | ||
| 539 | if (exps.size() == 1 && exps.iterator().next() == exp) { | ||
| 540 | Utility.logError(exp, "error in transform of Ontology~~~~"); | ||
| 541 | } | ||
| 542 | Set<OWLClassExpression> nexps = new HashSet<OWLClassExpression>(); | ||
| 543 | OWLClassExpression ne; | ||
| 544 | boolean changes = false; | ||
| 545 | for (OWLClassExpression e: exps) { | ||
| 546 | ne = transform(e, addedAxioms); | ||
| 547 | if (ne != e) changes = true; | ||
| 548 | nexps.add(ne); | ||
| 549 | } | ||
| 550 | if (changes) | ||
| 551 | return OWLHelper.getSimplifiedConjunction(factory, nexps); | ||
| 552 | else | ||
| 553 | return exp; | ||
| 554 | } | ||
| 555 | |||
| 556 | protected OWLNamedIndividual getNewIndividual(OWLOntology onto, int number) { | ||
| 557 | OWLOntologyManager manager = onto.getOWLOntologyManager(); | ||
| 558 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 559 | OWLNamedIndividual newIndividual = factory.getOWLNamedIndividual(IRI.create(Namespace.PAGODA_ANONY + "NI" + number)); | ||
| 560 | manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newIndividual)); | ||
| 561 | return newIndividual; | ||
| 562 | } | ||
| 563 | |||
| 564 | protected OWLObjectProperty getNewRole(OWLOntology onto, int number) { | ||
| 565 | OWLOntologyManager manager = onto.getOWLOntologyManager(); | ||
| 566 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 567 | OWLObjectProperty newProperty = factory.getOWLObjectProperty(IRI.create(Namespace.PAGODA_AUX + "NR" + number)); | ||
| 568 | manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newProperty)); | ||
| 569 | return newProperty; | ||
| 570 | } | ||
| 571 | |||
| 572 | private OWLClass getNewConcept(OWLOntology onto, int number) { | ||
| 573 | OWLOntologyManager manager = onto.getOWLOntologyManager(); | ||
| 574 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 575 | OWLClass newClass = factory.getOWLClass(IRI.create(Namespace.PAGODA_AUX + "NC" + number)); | ||
| 576 | manager.addAxiom(onto, factory.getOWLDeclarationAxiom(newClass)); | ||
| 577 | return newClass; | ||
| 578 | } | ||
| 579 | |||
| 580 | public OWLOntologyManager getOWLOntologyManager() { | ||
| 581 | return inputOntology.getOWLOntologyManager(); | ||
| 582 | } | ||
| 583 | |||
| 584 | public String getOntologyIRI() { | ||
| 585 | return ontologyIRI; | ||
| 586 | } | ||
| 587 | |||
| 588 | public OWLOntology getOutputOntology() { | ||
| 589 | return outputOntology; | ||
| 590 | } | ||
| 591 | |||
| 592 | @Override | ||
| 593 | public String getOutputPath() { | ||
| 594 | return outputPath; | ||
| 595 | } | ||
| 596 | |||
| 597 | @Override | ||
| 598 | public String getDirectory() { | ||
| 599 | return outputPath.substring(0, outputPath.lastIndexOf(Utility.FILE_SEPARATOR)); | ||
| 600 | } | ||
| 601 | |||
| 602 | public void setCorrespondenceFileLoc(String path) { | ||
| 603 | corrFileName = path; | ||
| 604 | } | ||
| 605 | |||
| 606 | private static enum BottomStrategy { TOREMOVE, NULLARY, UNARY } | ||
| 607 | } | ||
| 608 | |||
