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/Clause.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/approx/Clause.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/approx/Clause.java | 722 |
1 files changed, 722 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/approx/Clause.java b/src/uk/ac/ox/cs/pagoda/approx/Clause.java new file mode 100644 index 0000000..9c3f5d0 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/approx/Clause.java | |||
| @@ -0,0 +1,722 @@ | |||
| 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 | Variable 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 | ||
| 101 | .getIRI()))); | ||
| 102 | else if (predicate.toString().startsWith("<internal:nom#")) | ||
| 103 | nom2iri.put(v, DLClauseHelper.getIRI4Nominal(concept)); | ||
| 104 | } else if (predicate instanceof AtomicRole) { | ||
| 105 | AtomicRole role = (AtomicRole) predicate; | ||
| 106 | |||
| 107 | if (dataProperties.contains(role.getIRI())) { | ||
| 108 | OWLDataRange dataRange; | ||
| 109 | OWLDataPropertyExpression dataPropertyExp = factory | ||
| 110 | .getOWLDataProperty(IRI.create(role.getIRI())); | ||
| 111 | Term term = atom.getArgument(1); | ||
| 112 | if (term instanceof Constant) | ||
| 113 | subClasses.add(factory | ||
| 114 | .getOWLDataHasValue(dataPropertyExp, | ||
| 115 | getOWLLiteral((Constant) term))); | ||
| 116 | else if (term instanceof Variable) { | ||
| 117 | W = (Variable) term; | ||
| 118 | if (nom2datatype.containsKey(W)) { | ||
| 119 | subClasses.add(factory.getOWLDataHasValue( | ||
| 120 | dataPropertyExp, | ||
| 121 | getOWLLiteral(nom2datatype.get(W)))); | ||
| 122 | } else if (var2atom.containsKey(W)) { | ||
| 123 | Atom tAtom = var2atom.get(W); | ||
| 124 | DLPredicate tPredicate = tAtom.getDLPredicate(); | ||
| 125 | if (tPredicate instanceof DatatypeRestriction) { | ||
| 126 | DatatypeRestriction restriction = (DatatypeRestriction) tPredicate; | ||
| 127 | dataRange = factory.getOWLDatatype(IRI | ||
| 128 | .create(restriction.getDatatypeURI())); | ||
| 129 | } | ||
| 130 | // else if (tPredicate instanceof | ||
| 131 | // AtomicNegationDataRange) { | ||
| 132 | // // TODO how to deal with AtomicNegationDataRange | ||
| 133 | // e.g. not({ "5"^^xsd:integer }) | ||
| 134 | // | ||
| 135 | // } | ||
| 136 | else if (tPredicate instanceof AtomicConcept) { | ||
| 137 | dataRange = factory.getOWLDatatype(IRI | ||
| 138 | .create(((AtomicConcept) tPredicate) | ||
| 139 | .getIRI())); | ||
| 140 | } else { | ||
| 141 | dataRange = null; | ||
| 142 | Utility.logError(tPredicate, | ||
| 143 | "strange ... -___-|||"); | ||
| 144 | } | ||
| 145 | |||
| 146 | if (headAtoms.contains(tAtom)) { | ||
| 147 | superClasses.add(factory | ||
| 148 | .getOWLDataAllValuesFrom( | ||
| 149 | dataPropertyExp, dataRange)); | ||
| 150 | subClasses.add(factory | ||
| 151 | .getOWLDataSomeValuesFrom( | ||
| 152 | dataPropertyExp, | ||
| 153 | factory.getTopDatatype())); | ||
| 154 | headAtoms.remove(tAtom); | ||
| 155 | } else | ||
| 156 | subClasses.add(factory | ||
| 157 | .getOWLDataSomeValuesFrom( | ||
| 158 | dataPropertyExp, dataRange)); | ||
| 159 | |||
| 160 | } else | ||
| 161 | subClasses.add(factory.getOWLDataSomeValuesFrom( | ||
| 162 | dataPropertyExp, factory.getTopDatatype())); | ||
| 163 | } else { | ||
| 164 | Utility.logError(term, "strange ... -___-|||"); | ||
| 165 | } | ||
| 166 | continue; | ||
| 167 | } | ||
| 168 | |||
| 169 | OWLObjectPropertyExpression roleExp = factory | ||
| 170 | .getOWLObjectProperty(IRI.create(role.getIRI())); | ||
| 171 | if ((W = atom.getArgumentVariable(1)) == X) { | ||
| 172 | roleExp = roleExp.getInverseProperty(); | ||
| 173 | W = atom.getArgumentVariable(0); | ||
| 174 | } | ||
| 175 | |||
| 176 | if (X == W) | ||
| 177 | subClasses.add(factory.getOWLObjectHasSelf(roleExp)); | ||
| 178 | |||
| 179 | AtomicConcept concept; | ||
| 180 | OWLClassExpression clsExp = null; | ||
| 181 | int number = 1; | ||
| 182 | Set<Variable> set = varCliques.get(W); | ||
| 183 | if (set != null) | ||
| 184 | number = set.size(); | ||
| 185 | |||
| 186 | if (var2atom.containsKey(W)) { | ||
| 187 | Atom tAtom = var2atom.get(W); | ||
| 188 | DLPredicate tPredicate = tAtom.getDLPredicate(); | ||
| 189 | if (tPredicate instanceof AtomicConcept) { | ||
| 190 | concept = (AtomicConcept) tPredicate; | ||
| 191 | clsExp = factory.getOWLClass(IRI.create(concept | ||
| 192 | .getIRI())); | ||
| 193 | if (headAtoms.contains(tAtom)) { | ||
| 194 | superClasses.add(factory.getOWLObjectAllValuesFrom( | ||
| 195 | roleExp, clsExp)); | ||
| 196 | subClasses.add(factory.getOWLObjectSomeValuesFrom( | ||
| 197 | roleExp, factory.getOWLThing())); | ||
| 198 | headAtoms.remove(tAtom); | ||
| 199 | } else { | ||
| 200 | if (number == 1) | ||
| 201 | subClasses.add(factory | ||
| 202 | .getOWLObjectSomeValuesFrom(roleExp, | ||
| 203 | clsExp)); | ||
| 204 | else | ||
| 205 | subClasses.add(factory | ||
| 206 | .getOWLObjectMinCardinality(number, | ||
| 207 | roleExp, clsExp)); | ||
| 208 | } | ||
| 209 | } else { | ||
| 210 | Utility.logDebug(tAtom, "strange ... -___-|||"); | ||
| 211 | } | ||
| 212 | } else { | ||
| 213 | if (number == 1) | ||
| 214 | subClasses.add(factory.getOWLObjectSomeValuesFrom( | ||
| 215 | roleExp, factory.getOWLThing())); | ||
| 216 | else | ||
| 217 | subClasses.add(factory.getOWLObjectMinCardinality( | ||
| 218 | number, roleExp)); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | OWLObjectPropertyExpression objExp; | ||
| 224 | for (Atom atom : headAtoms) { | ||
| 225 | predicate = atom.getDLPredicate(); | ||
| 226 | if (predicate instanceof AtomicConcept) { | ||
| 227 | if (atom.getArgumentVariable(0) == X) | ||
| 228 | superClasses | ||
| 229 | .add(getClassExpression((AtomicConcept) predicate)); | ||
| 230 | } else if (predicate instanceof AtomicRole) { | ||
| 231 | if (!dataProperties.contains(((AtomicRole) predicate).getIRI())) { | ||
| 232 | objExp = factory.getOWLObjectProperty(IRI | ||
| 233 | .create(((AtomicRole) predicate).getIRI())); | ||
| 234 | Term V = atom.getArgument(1); | ||
| 235 | if (V == X) { | ||
| 236 | objExp = factory.getOWLObjectInverseOf(objExp); | ||
| 237 | V = atom.getArgument(0); | ||
| 238 | } | ||
| 239 | |||
| 240 | if (V == X) | ||
| 241 | superClasses.add(factory.getOWLObjectHasSelf(objExp)); | ||
| 242 | else if (V instanceof Individual) { | ||
| 243 | superClasses.add(factory.getOWLObjectHasValue(objExp, | ||
| 244 | factory.getOWLNamedIndividual(IRI | ||
| 245 | .create(((Individual) V).getIRI())))); | ||
| 246 | } else | ||
| 247 | superClasses.add(factory.getOWLObjectHasValue(objExp, | ||
| 248 | factory.getOWLNamedIndividual(IRI.create(nom2iri | ||
| 249 | .get((Variable) V))))); | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | Constant c = (Constant) atom.getArgument(1); | ||
| 253 | OWLDataProperty dataProp = factory.getOWLDataProperty(IRI.create(((AtomicRole) predicate).getIRI())); | ||
| 254 | superClasses.add(factory.getOWLDataHasValue(dataProp, getOWLLiteral(c))); | ||
| 255 | } | ||
| 256 | } else if (predicate instanceof AtLeastConcept) | ||
| 257 | superClasses | ||
| 258 | .add(getMinCardinalityExpression((AtLeastConcept) predicate)); | ||
| 259 | else if (predicate instanceof AtLeastDataRange) | ||
| 260 | superClasses | ||
| 261 | .add(getDataMinCardinalityExpression((AtLeastDataRange) predicate)); | ||
| 262 | |||
| 263 | else { | ||
| 264 | Utility.logError(atom.toString(), | ||
| 265 | "strange head atoms left here~~~~~"); | ||
| 266 | // superClasses.add(getDataRange(getDataRange((LiteralDataRange) | ||
| 267 | // predicate))); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | private OWLLiteral getOWLLiteral(Constant constant) { | ||
| 273 | if (!constant.getDatatypeURI().equals(Namespace.RDF_PLAIN_LITERAL)) | ||
| 274 | return factory.getOWLLiteral(constant.getLexicalForm(), factory | ||
| 275 | .getOWLDatatype(IRI.create(constant.getDatatypeURI()))); | ||
| 276 | else { | ||
| 277 | String lexicalForm = constant.getLexicalForm(); | ||
| 278 | int index = lexicalForm.indexOf("@"); | ||
| 279 | return factory.getOWLLiteral(lexicalForm.substring(0, index), | ||
| 280 | lexicalForm.substring(index + 1)); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | // private OWLObjectSomeValuesFrom | ||
| 285 | // addSomeValuesFromAxiom(OWLObjectPropertyExpression roleExp, | ||
| 286 | // OWLClassExpression classExp) { | ||
| 287 | // return factory.getOWLObjectSomeValuesFrom(roleExp, classExp); | ||
| 288 | // } | ||
| 289 | |||
| 290 | private void getVariableOccurrence(Map<Variable, Atom> var2atom, | ||
| 291 | Set<Atom> atoms) { | ||
| 292 | for (Atom atom : atoms) | ||
| 293 | if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable | ||
| 294 | && !atom.getArgument(0).equals(X)) | ||
| 295 | var2atom.put((Variable) atom.getArgumentVariable(0), atom); | ||
| 296 | } | ||
| 297 | |||
| 298 | private OWLClassExpression getMinCardinalityExpression( | ||
| 299 | AtLeastConcept atLeast) { | ||
| 300 | OWLObjectPropertyExpression propExp = getObjectPropertyExpression(atLeast | ||
| 301 | .getOnRole()); | ||
| 302 | OWLClassExpression clsExp = getClassExpression(atLeast.getToConcept()); | ||
| 303 | if (atLeast.getNumber() == 1) | ||
| 304 | return factory.getOWLObjectSomeValuesFrom(propExp, clsExp); | ||
| 305 | else | ||
| 306 | return factory.getOWLObjectMinCardinality(atLeast.getNumber(), | ||
| 307 | propExp, clsExp); | ||
| 308 | } | ||
| 309 | |||
| 310 | private OWLClassExpression getDataMinCardinalityExpression( | ||
| 311 | AtLeastDataRange atLeast) { | ||
| 312 | OWLDataPropertyExpression propExp = getDataPropertyExpression(atLeast | ||
| 313 | .getOnRole()); | ||
| 314 | OWLDataRange dataRange = getDataRange(atLeast.getToDataRange()); | ||
| 315 | if (atLeast.getNumber() == 1) | ||
| 316 | return factory.getOWLDataSomeValuesFrom(propExp, dataRange); | ||
| 317 | else | ||
| 318 | return factory.getOWLDataMinCardinality(atLeast.getNumber(), | ||
| 319 | propExp, dataRange); | ||
| 320 | } | ||
| 321 | |||
| 322 | public Set<OWLClassExpression> getSuperClasses() { | ||
| 323 | return superClasses; | ||
| 324 | } | ||
| 325 | |||
| 326 | public Set<OWLClassExpression> getSubClasses() { | ||
| 327 | return subClasses; | ||
| 328 | } | ||
| 329 | |||
| 330 | // public OWLClassExpression getSubClass() { | ||
| 331 | // if (subClasses.isEmpty()) | ||
| 332 | // return factory.getOWLThing(); | ||
| 333 | // if (subClasses.size() == 1) | ||
| 334 | // return subClasses.iterator().next(); | ||
| 335 | // | ||
| 336 | // return factory.getOWLObjectIntersectionOf(subClasses); | ||
| 337 | // } | ||
| 338 | |||
| 339 | private void eliminateEquality() { | ||
| 340 | Set<Atom> eHeadAtoms = new HashSet<Atom>(); | ||
| 341 | Set<Atom> eBodyAtoms = new HashSet<Atom>(); | ||
| 342 | Set<Variable> eVariables = new HashSet<Variable>(); | ||
| 343 | seperateEquality4Clause(eBodyAtoms, eHeadAtoms, eVariables); | ||
| 344 | |||
| 345 | OWLNamedIndividual individual; | ||
| 346 | /* | ||
| 347 | * remove equalities that are introduced by MaxCardinalityConstraints | ||
| 348 | */ | ||
| 349 | DLPredicate predicate; | ||
| 350 | Map<Variable, Set<Variable>> groups = new HashMap<Variable, Set<Variable>>(); | ||
| 351 | OWLObjectMaxCardinality maxCardinality; | ||
| 352 | OWLClassExpression exp; | ||
| 353 | Set<Variable> mVariables = new HashSet<Variable>(); | ||
| 354 | Variable tVar, tVar1, tVar2; | ||
| 355 | Set<Variable> tVariables; | ||
| 356 | |||
| 357 | for (Iterator<Atom> iter = eHeadAtoms.iterator(); iter.hasNext(); ){ | ||
| 358 | Atom atom = iter.next(); | ||
| 359 | predicate = atom.getDLPredicate(); | ||
| 360 | if (predicate instanceof AnnotatedEquality) { | ||
| 361 | superClasses.add(maxCardinality = getMaxCardinalityExpression((AnnotatedEquality)predicate)); | ||
| 362 | if (!((exp = maxCardinality.getFiller()) instanceof OWLObjectComplementOf)) | ||
| 363 | subClasses.add(factory.getOWLObjectSomeValuesFrom(maxCardinality.getProperty(), exp)); | ||
| 364 | else | ||
| 365 | subClasses.add(factory.getOWLObjectSomeValuesFrom(maxCardinality.getProperty(), factory.getOWLThing())); | ||
| 366 | mVariables.add(atom.getArgumentVariable(0)); | ||
| 367 | mVariables.add(atom.getArgumentVariable(1)); | ||
| 368 | iter.remove(); | ||
| 369 | } | ||
| 370 | else if (predicate instanceof Equality) { | ||
| 371 | if (atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) { | ||
| 372 | mVariables.add(tVar1 = atom.getArgumentVariable(0)); | ||
| 373 | mVariables.add(tVar2 = atom.getArgumentVariable(1)); | ||
| 374 | iter.remove(); | ||
| 375 | |||
| 376 | if (tVar1.getName().compareTo(tVar2.getName()) > 0) { | ||
| 377 | tVar = tVar1; tVar1 = tVar2; tVar2 = tVar; | ||
| 378 | } | ||
| 379 | tVariables = groups.get(tVar1); | ||
| 380 | if (groups.containsKey(tVar2)) { | ||
| 381 | if (tVariables == null) | ||
| 382 | groups.put(tVar1, tVariables = groups.get(tVar2)); | ||
| 383 | else { | ||
| 384 | tVariables.addAll(groups.get(tVar2)); | ||
| 385 | groups.get(tVar2).clear(); | ||
| 386 | groups.put(tVar2, tVariables); | ||
| 387 | } | ||
| 388 | } | ||
| 389 | if (tVariables == null) { | ||
| 390 | groups.put(tVar1, tVariables = new HashSet<Variable>()); | ||
| 391 | groups.put(tVar2, tVariables); | ||
| 392 | } | ||
| 393 | tVariables.add(tVar1); | ||
| 394 | tVariables.add(tVar2); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | Map<Variable, Object> maxCardToConcepts = new HashMap<Variable, Object>(); | ||
| 400 | |||
| 401 | for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) { | ||
| 402 | Atom atom = iter.next(); | ||
| 403 | if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable) { | ||
| 404 | if (mVariables.contains(tVar = atom.getArgumentVariable(0))) { | ||
| 405 | maxCardToConcepts.put(tVar, atom.getDLPredicate()); | ||
| 406 | iter.remove(); | ||
| 407 | } | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | for (Iterator<Atom> iter = eHeadAtoms.iterator(); iter.hasNext(); ) { | ||
| 412 | Atom atom = iter.next(); | ||
| 413 | if (atom.getArity() == 1 && atom.getArgument(0) instanceof Variable) { | ||
| 414 | if (mVariables.contains(tVar = atom.getArgumentVariable(0))) { | ||
| 415 | maxCardToConcepts.put(tVar, AtomicNegationConcept.create((AtomicConcept) atom.getDLPredicate())); | ||
| 416 | iter.remove(); | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | Map<Variable, Object> maxCardToProperty = new HashMap<Variable, Object>(); | ||
| 422 | |||
| 423 | for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) { | ||
| 424 | Atom atom = iter.next(); | ||
| 425 | if (atom.getArity() == 2 && atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) { | ||
| 426 | tVar1 = atom.getArgumentVariable(0); tVar2 = atom.getArgumentVariable(1); | ||
| 427 | if (mVariables.contains(tVar1)) { | ||
| 428 | if (groups.containsKey(tVar1)) | ||
| 429 | maxCardToProperty.put(tVar1, ((AtomicRole) atom.getDLPredicate()).getInverse()); | ||
| 430 | iter.remove(); | ||
| 431 | } else if (mVariables.contains(tVar2)) { | ||
| 432 | if (groups.containsKey(tVar2)) | ||
| 433 | maxCardToProperty.put(tVar2, atom.getDLPredicate()); | ||
| 434 | iter.remove(); | ||
| 435 | } | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | int n; | ||
| 440 | Object r, A; | ||
| 441 | for (Variable var: groups.keySet()) { | ||
| 442 | if ((tVariables = groups.get(var)).isEmpty()) | ||
| 443 | continue; | ||
| 444 | n = tVariables.size() - 1; | ||
| 445 | tVariables.clear(); | ||
| 446 | r = maxCardToProperty.get(var); | ||
| 447 | if (r instanceof AtomicRole) { | ||
| 448 | if (isDataProperty(r)) { | ||
| 449 | if ((A = maxCardToConcepts.get(var)) != null) { | ||
| 450 | Utility.logError("Unknown data range: " + A); | ||
| 451 | } | ||
| 452 | |||
| 453 | superClasses.add( | ||
| 454 | factory.getOWLDataMaxCardinality( | ||
| 455 | n, | ||
| 456 | factory.getOWLDataProperty(IRI.create(((AtomicRole) r).getIRI())))); | ||
| 457 | } | ||
| 458 | else { | ||
| 459 | OWLClassExpression clsExp = null; | ||
| 460 | if ((A = maxCardToConcepts.get(var)) != null) | ||
| 461 | if (A instanceof AtomicConcept) | ||
| 462 | clsExp = factory.getOWLClass(IRI.create(((AtomicConcept) A).getIRI())); | ||
| 463 | else if (A instanceof AtomicNegationConcept) | ||
| 464 | clsExp = factory.getOWLObjectComplementOf(factory.getOWLClass(IRI.create(((AtomicNegationConcept) A).getNegatedAtomicConcept().getIRI()))); | ||
| 465 | else | ||
| 466 | Utility.logError("Unknown to concept: " + A); | ||
| 467 | |||
| 468 | if (A == null) | ||
| 469 | superClasses.add( | ||
| 470 | factory.getOWLObjectMaxCardinality( | ||
| 471 | n, | ||
| 472 | factory.getOWLObjectProperty(IRI.create(((AtomicRole) r).getIRI())) | ||
| 473 | )); | ||
| 474 | else | ||
| 475 | superClasses.add( | ||
| 476 | factory.getOWLObjectMaxCardinality( | ||
| 477 | n, | ||
| 478 | factory.getOWLObjectProperty(IRI.create(((AtomicRole) r).getIRI())), | ||
| 479 | clsExp)); | ||
| 480 | } | ||
| 481 | } | ||
| 482 | else if (r instanceof InverseRole) { | ||
| 483 | OWLClassExpression clsExp = null; | ||
| 484 | if ((A = maxCardToConcepts.get(var)) != null) { | ||
| 485 | if (A instanceof AtomicConcept) | ||
| 486 | clsExp = factory.getOWLClass(IRI.create(((AtomicConcept) A).getIRI())); | ||
| 487 | else if (A instanceof AtomicNegationConcept) | ||
| 488 | clsExp = factory.getOWLObjectComplementOf(factory.getOWLClass(IRI.create(((AtomicNegationConcept) A).getNegatedAtomicConcept().getIRI()))); | ||
| 489 | else | ||
| 490 | Utility.logError("Unknown to concept: " + A); | ||
| 491 | } | ||
| 492 | |||
| 493 | if (A == null) | ||
| 494 | superClasses.add( | ||
| 495 | factory.getOWLObjectMaxCardinality( | ||
| 496 | n, | ||
| 497 | factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(((InverseRole) r).getInverseOf().getIRI()))) | ||
| 498 | )); | ||
| 499 | else | ||
| 500 | superClasses.add( | ||
| 501 | factory.getOWLObjectMaxCardinality( | ||
| 502 | n, | ||
| 503 | factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(((InverseRole) r).getInverseOf().getIRI()))), | ||
| 504 | clsExp)); | ||
| 505 | |||
| 506 | } | ||
| 507 | else | ||
| 508 | Utility.logError("Unknown property: " + r); | ||
| 509 | } | ||
| 510 | |||
| 511 | /* | ||
| 512 | * dealing with equalities of nominal | ||
| 513 | */ | ||
| 514 | Map<Variable, String> nom2iri = new HashMap<Variable, String>(); | ||
| 515 | for (Iterator<Atom> iter = eBodyAtoms.iterator(); iter.hasNext(); ) { | ||
| 516 | Atom atom = iter.next(); | ||
| 517 | predicate = atom.getDLPredicate(); | ||
| 518 | if (predicate instanceof AtomicConcept && predicate.toString().startsWith("<internal:nom#")) { | ||
| 519 | nom2iri.put(atom.getArgumentVariable(0), DLClauseHelper.getIRI4Nominal(predicate)); | ||
| 520 | iter.remove(); | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | Term first, second; | ||
| 525 | Map<Variable, Set<Term>> equEdges = new HashMap<Variable, Set<Term>>(); | ||
| 526 | Set<Term> terms = new HashSet<Term>(); | ||
| 527 | for (Atom atom: eHeadAtoms) { | ||
| 528 | predicate = atom.getDLPredicate(); | ||
| 529 | if (predicate instanceof Equality) { | ||
| 530 | first = atom.getArgument(0); | ||
| 531 | second = atom.getArgument(1); | ||
| 532 | |||
| 533 | if (first instanceof Variable) { | ||
| 534 | if ((terms = equEdges.get(first)) == null) | ||
| 535 | equEdges.put((Variable) first, (terms = new HashSet<Term>())); | ||
| 536 | terms.add(second); | ||
| 537 | } | ||
| 538 | |||
| 539 | if (second instanceof Variable) { | ||
| 540 | if ((terms = equEdges.get(second)) == null) | ||
| 541 | equEdges.put((Variable) second, (terms = new HashSet<Term>())); | ||
| 542 | terms.add(first); | ||
| 543 | } | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | OWLObjectPropertyExpression objExp; | ||
| 548 | |||
| 549 | Set<OWLNamedIndividual> individuals = new HashSet<OWLNamedIndividual>(); | ||
| 550 | if (equEdges.containsKey(X)) { | ||
| 551 | for (Term t: equEdges.get(X)) | ||
| 552 | if (t instanceof Variable) { | ||
| 553 | Variable var = (Variable) t; | ||
| 554 | individual = factory.getOWLNamedIndividual(IRI.create(nom2iri.get(var))); | ||
| 555 | // superClasses.add(factory.getOWLObjectOneOf(individual)); | ||
| 556 | individuals.add(individual); | ||
| 557 | } | ||
| 558 | else if (t instanceof Individual) | ||
| 559 | individuals.add(factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI()))); | ||
| 560 | } | ||
| 561 | |||
| 562 | if (individuals.size() > 0) { | ||
| 563 | superClasses.add(factory.getOWLObjectOneOf(individuals)); | ||
| 564 | individuals.clear(); | ||
| 565 | } | ||
| 566 | |||
| 567 | for (Atom atom: eBodyAtoms) { | ||
| 568 | predicate = atom.getDLPredicate(); | ||
| 569 | if (predicate instanceof AtomicRole) { | ||
| 570 | first = atom.getArgumentVariable(0); | ||
| 571 | second = atom.getArgumentVariable(1); | ||
| 572 | |||
| 573 | objExp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) predicate).getIRI())); | ||
| 574 | if (eVariables.contains(first)) { | ||
| 575 | second = first; | ||
| 576 | objExp = factory.getOWLObjectInverseOf(objExp); | ||
| 577 | } | ||
| 578 | |||
| 579 | for (Term t: equEdges.get(second)) { | ||
| 580 | if (t instanceof Variable) { | ||
| 581 | Variable var = (Variable) t; | ||
| 582 | individuals.add(factory.getOWLNamedIndividual(IRI.create(nom2iri.get(var)))); | ||
| 583 | } | ||
| 584 | else if (t instanceof Individual) { | ||
| 585 | individuals.add(factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI()))); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | if (!individuals.isEmpty()) { | ||
| 589 | superClasses.add(factory.getOWLObjectAllValuesFrom(objExp, factory.getOWLObjectOneOf(individuals))); | ||
| 590 | individuals.clear(); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | } | ||
| 596 | |||
| 597 | private boolean isDataProperty(Object r) { | ||
| 598 | if (!(r instanceof AtomicRole)) return false; | ||
| 599 | String iri = ((AtomicRole) r).getIRI(); | ||
| 600 | return dataProperties.contains(iri); | ||
| 601 | } | ||
| 602 | |||
| 603 | private OWLObjectMaxCardinality getMaxCardinalityExpression( | ||
| 604 | AnnotatedEquality equ) { | ||
| 605 | OWLObjectPropertyExpression propExp = getObjectPropertyExpression(equ | ||
| 606 | .getOnRole()); | ||
| 607 | OWLClassExpression clsExp = getClassExpression(equ.getToConcept()); | ||
| 608 | return factory.getOWLObjectMaxCardinality(equ.getCaridnality(), | ||
| 609 | propExp, clsExp); | ||
| 610 | } | ||
| 611 | |||
| 612 | private OWLObjectPropertyExpression getObjectPropertyExpression(Role role) { | ||
| 613 | if (role instanceof AtomicRole) | ||
| 614 | return factory.getOWLObjectProperty(IRI.create(((AtomicRole) role) | ||
| 615 | .getIRI())); | ||
| 616 | return factory.getOWLObjectProperty( | ||
| 617 | IRI.create(((InverseRole) role).getInverseOf().getIRI())) | ||
| 618 | .getInverseProperty(); | ||
| 619 | } | ||
| 620 | |||
| 621 | private OWLDataProperty getDataPropertyExpression(Role role) { | ||
| 622 | return factory.getOWLDataProperty(IRI.create(((AtomicRole) role) | ||
| 623 | .getIRI())); | ||
| 624 | } | ||
| 625 | |||
| 626 | private OWLClassExpression getClassExpression(LiteralConcept concept) { | ||
| 627 | if (concept instanceof AtomicConcept) | ||
| 628 | return factory.getOWLClass(IRI.create(((AtomicConcept) concept) | ||
| 629 | .getIRI())); | ||
| 630 | return factory.getOWLClass( | ||
| 631 | IRI.create(((AtomicNegationConcept) concept) | ||
| 632 | .getNegatedAtomicConcept().getIRI())) | ||
| 633 | .getComplementNNF(); | ||
| 634 | } | ||
| 635 | |||
| 636 | private OWLDataRange getDataRange(LiteralDataRange dataRange) { | ||
| 637 | if (dataRange instanceof InternalDatatype) | ||
| 638 | return factory.getOWLDatatype(IRI | ||
| 639 | .create(((InternalDatatype) dataRange).getIRI())); | ||
| 640 | if (dataRange instanceof DatatypeRestriction) | ||
| 641 | return factory | ||
| 642 | .getOWLDatatype(IRI | ||
| 643 | .create(((DatatypeRestriction) dataRange) | ||
| 644 | .getDatatypeURI())); | ||
| 645 | if (dataRange instanceof ConstantEnumeration) { | ||
| 646 | ConstantEnumeration e = (ConstantEnumeration) dataRange; | ||
| 647 | OWLLiteral[] values = new OWLLiteral[e.getNumberOfConstants()]; | ||
| 648 | for (int i = 0; i < values.length; ++i) { | ||
| 649 | Constant c = e.getConstant(i); | ||
| 650 | values[i] = factory.getOWLLiteral(c.getDataValue().toString(), | ||
| 651 | factory.getOWLDatatype(IRI.create(c.getDatatypeURI()))); | ||
| 652 | } | ||
| 653 | return factory.getOWLDataOneOf(values); | ||
| 654 | } | ||
| 655 | Utility.logError(dataRange.toString(), "strange data type!!!!"); | ||
| 656 | return null; | ||
| 657 | } | ||
| 658 | |||
| 659 | public void seperateEquality4Clause(Set<Atom> eBodyAtoms, | ||
| 660 | Set<Atom> eHeadAtoms, Set<Variable> eVariables) { | ||
| 661 | Set<Variable> variables = new HashSet<Variable>(); | ||
| 662 | DLPredicate predicate; | ||
| 663 | for (Atom atom : headAtoms) { | ||
| 664 | predicate = atom.getDLPredicate(); | ||
| 665 | if (predicate instanceof Equality | ||
| 666 | || predicate instanceof AnnotatedEquality) { | ||
| 667 | variables.clear(); | ||
| 668 | atom.getVariables(variables); | ||
| 669 | for (Variable variable : variables) | ||
| 670 | eVariables.add(variable); | ||
| 671 | } | ||
| 672 | } | ||
| 673 | eVariables.remove(X); | ||
| 674 | |||
| 675 | seperateEquality(bodyAtoms, eBodyAtoms, eVariables); | ||
| 676 | seperateEquality(headAtoms, eHeadAtoms, eVariables); | ||
| 677 | } | ||
| 678 | |||
| 679 | public void seperateEquality(Set<Atom> noEquality, Set<Atom> inEquality, | ||
| 680 | Set<Variable> eVariables) { | ||
| 681 | Set<Variable> variables = new HashSet<Variable>(); | ||
| 682 | for (Iterator<Atom> iter = noEquality.iterator(); iter.hasNext();) { | ||
| 683 | Atom atom = iter.next(); | ||
| 684 | if (atom.getDLPredicate() instanceof Equality | ||
| 685 | || atom.getDLPredicate() instanceof AnnotatedEquality) { | ||
| 686 | iter.remove(); | ||
| 687 | inEquality.add(atom); | ||
| 688 | } else { | ||
| 689 | variables.clear(); | ||
| 690 | atom.getVariables(variables); | ||
| 691 | for (Variable variable : variables) | ||
| 692 | if (eVariables.contains(variable)) { | ||
| 693 | iter.remove(); | ||
| 694 | inEquality.add(atom); | ||
| 695 | break; | ||
| 696 | } | ||
| 697 | } | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | @Override | ||
| 702 | public String toString() { | ||
| 703 | StringBuilder ret = new StringBuilder(); | ||
| 704 | boolean first = true; | ||
| 705 | for (OWLClassExpression exp : superClasses) | ||
| 706 | if (first) { | ||
| 707 | ret.append(exp.toString()); | ||
| 708 | first = false; | ||
| 709 | } else | ||
| 710 | ret.append(" v ").append(exp.toString()); | ||
| 711 | |||
| 712 | first = true; | ||
| 713 | for (OWLClassExpression exp : subClasses) | ||
| 714 | if (first) { | ||
| 715 | ret.append(" :- ").append(exp.toString()); | ||
| 716 | first = false; | ||
| 717 | } else | ||
| 718 | ret.append(" ^ ").append(exp.toString()); | ||
| 719 | |||
| 720 | return ret.toString(); | ||
| 721 | } | ||
| 722 | } | ||
