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/Clause.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/Clause.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/approx/Clause.java | 725 |
1 files changed, 0 insertions, 725 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 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.approx; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.Iterator; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.*; | ||
| 10 | import org.semanticweb.owlapi.model.*; | ||
| 11 | |||
| 12 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 13 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 14 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 15 | |||
| 16 | public 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 | } | ||
