diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-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/RLPlusOntology.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | 607 |
1 files changed, 0 insertions, 607 deletions
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 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.approx; | ||
| 2 | |||
| 3 | import org.apache.commons.io.FilenameUtils; | ||
| 4 | import org.semanticweb.HermiT.Configuration; | ||
| 5 | import org.semanticweb.HermiT.model.DLClause; | ||
| 6 | import org.semanticweb.HermiT.model.DLOntology; | ||
| 7 | import org.semanticweb.HermiT.structural.OWLClausification; | ||
| 8 | import org.semanticweb.owlapi.model.*; | ||
| 9 | import org.semanticweb.owlapi.profiles.OWL2RLProfile; | ||
| 10 | import org.semanticweb.owlapi.profiles.OWLProfileReport; | ||
| 11 | import org.semanticweb.owlapi.profiles.OWLProfileViolation; | ||
| 12 | import uk.ac.ox.cs.pagoda.constraints.NullaryBottom; | ||
| 13 | import uk.ac.ox.cs.pagoda.constraints.UnaryBottom; | ||
| 14 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 15 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 16 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 17 | |||
| 18 | import java.io.*; | ||
| 19 | import java.nio.file.Paths; | ||
| 20 | import java.util.*; | ||
| 21 | import java.util.regex.Matcher; | ||
| 22 | import java.util.regex.Pattern; | ||
| 23 | |||
| 24 | public 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 | |||
