diff options
Diffstat (limited to 'src/main/java/org/semanticweb/karma2/clausifier/OntologyProcesser.java')
| -rw-r--r-- | src/main/java/org/semanticweb/karma2/clausifier/OntologyProcesser.java | 572 |
1 files changed, 572 insertions, 0 deletions
diff --git a/src/main/java/org/semanticweb/karma2/clausifier/OntologyProcesser.java b/src/main/java/org/semanticweb/karma2/clausifier/OntologyProcesser.java new file mode 100644 index 0000000..5ff339e --- /dev/null +++ b/src/main/java/org/semanticweb/karma2/clausifier/OntologyProcesser.java | |||
| @@ -0,0 +1,572 @@ | |||
| 1 | package org.semanticweb.karma2.clausifier; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.File; | ||
| 5 | import java.io.FileNotFoundException; | ||
| 6 | import java.io.FileWriter; | ||
| 7 | import java.io.IOException; | ||
| 8 | import java.io.PrintWriter; | ||
| 9 | import java.util.ArrayList; | ||
| 10 | import java.util.Collection; | ||
| 11 | import java.util.HashSet; | ||
| 12 | import java.util.LinkedHashSet; | ||
| 13 | import java.util.List; | ||
| 14 | import java.util.Set; | ||
| 15 | |||
| 16 | import org.semanticweb.HermiT.model.Atom; | ||
| 17 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 18 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 19 | import org.semanticweb.HermiT.model.DLClause; | ||
| 20 | import org.semanticweb.HermiT.model.Individual; | ||
| 21 | import org.semanticweb.HermiT.model.Role; | ||
| 22 | import org.semanticweb.HermiT.model.Term; | ||
| 23 | import org.semanticweb.HermiT.model.Variable; | ||
| 24 | import org.semanticweb.HermiT.structural.BuiltInPropertyManager; | ||
| 25 | import org.semanticweb.HermiT.structural.OWLAxioms; | ||
| 26 | import org.semanticweb.HermiT.structural.OWLAxiomsExpressivity; | ||
| 27 | import org.semanticweb.HermiT.structural.OWLNormalization; | ||
| 28 | import org.semanticweb.HermiT.structural.ObjectPropertyInclusionManager; | ||
| 29 | import org.semanticweb.karma2.exception.IllegalInputOntologyException; | ||
| 30 | import org.semanticweb.karma2.model.Equality; | ||
| 31 | import org.semanticweb.karma2.profile.ELHOProfile; | ||
| 32 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 33 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 34 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 35 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitor; | ||
| 36 | import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; | ||
| 37 | import org.semanticweb.owlapi.model.OWLDataExactCardinality; | ||
| 38 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 39 | import org.semanticweb.owlapi.model.OWLDataHasValue; | ||
| 40 | import org.semanticweb.owlapi.model.OWLDataMaxCardinality; | ||
| 41 | import org.semanticweb.owlapi.model.OWLDataMinCardinality; | ||
| 42 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 43 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 44 | import org.semanticweb.owlapi.model.OWLDataPropertyExpression; | ||
| 45 | import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; | ||
| 46 | import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; | ||
| 47 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 48 | import org.semanticweb.owlapi.model.OWLIndividualAxiom; | ||
| 49 | import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; | ||
| 50 | import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; | ||
| 51 | import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; | ||
| 52 | import org.semanticweb.owlapi.model.OWLObjectComplementOf; | ||
| 53 | import org.semanticweb.owlapi.model.OWLObjectExactCardinality; | ||
| 54 | import org.semanticweb.owlapi.model.OWLObjectHasSelf; | ||
| 55 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 56 | import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; | ||
| 57 | import org.semanticweb.owlapi.model.OWLObjectInverseOf; | ||
| 58 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 59 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 60 | import org.semanticweb.owlapi.model.OWLObjectOneOf; | ||
| 61 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 62 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 63 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 64 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 65 | import org.semanticweb.owlapi.model.OWLObjectUnionOf; | ||
| 66 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 67 | import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; | ||
| 68 | import org.semanticweb.owlapi.profiles.OWLProfileReport; | ||
| 69 | import org.semanticweb.owlapi.model.OWLAxiomVisitor; | ||
| 70 | |||
| 71 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 72 | |||
| 73 | public class OntologyProcesser { | ||
| 74 | |||
| 75 | |||
| 76 | protected static final Variable X=Variable.create("?X"); | ||
| 77 | protected static final Variable Y=Variable.create("?Y"); | ||
| 78 | protected static final Variable Z=Variable.create("?Z"); | ||
| 79 | |||
| 80 | |||
| 81 | public static void transformOntology(OWLOntology root, File dataFile, File ruleFile) throws IllegalInputOntologyException { | ||
| 82 | ELHOProfile profile = new ELHOProfile(); | ||
| 83 | OWLProfileReport report = profile.checkOntology(root); | ||
| 84 | if (!report.isInProfile()) { | ||
| 85 | Utility.logError(report.toString()); | ||
| 86 | throw new IllegalInputOntologyException("the ontology is not ELHO"); | ||
| 87 | } | ||
| 88 | OntologyProcesser processer = new OntologyProcesser(); | ||
| 89 | processer.preprocessAndClausify(root, dataFile, ruleFile); | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | private void preprocessAndClausify(OWLOntology rootOntology, File dataFile, File ruleFile) { | ||
| 94 | OWLDataFactory factory=rootOntology.getOWLOntologyManager().getOWLDataFactory(); | ||
| 95 | String ontologyIRI=rootOntology.getOntologyID().getDefaultDocumentIRI()==null ? "urn:hermit:kb" : rootOntology.getOntologyID().getDefaultDocumentIRI().toString(); | ||
| 96 | Collection<OWLOntology> importClosure=rootOntology.getImportsClosure(); | ||
| 97 | OWLAxioms axioms=new OWLAxioms(); | ||
| 98 | OWLNormalization normalization=new OWLNormalization(factory,axioms,0); | ||
| 99 | for (OWLOntology ontology : importClosure) { | ||
| 100 | normalization.processOntology(ontology); | ||
| 101 | } | ||
| 102 | BuiltInPropertyManager builtInPropertyManager=new BuiltInPropertyManager(factory); | ||
| 103 | builtInPropertyManager.axiomatizeBuiltInPropertiesAsNeeded(axioms); | ||
| 104 | ObjectPropertyInclusionManager objectPropertyInclusionManager=new ObjectPropertyInclusionManager(axioms); | ||
| 105 | objectPropertyInclusionManager.rewriteAxioms(factory,axioms,0); | ||
| 106 | OWLAxiomsExpressivity axiomsExpressivity=new OWLAxiomsExpressivity(axioms); | ||
| 107 | clausify(factory,ontologyIRI,axioms,axiomsExpressivity, dataFile,ruleFile); | ||
| 108 | writeTopRules(rootOntology.getClassesInSignature(), rootOntology.getObjectPropertiesInSignature(), ruleFile); | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | private void writeTopRules(Set<OWLClass> classes, Set<OWLObjectProperty> properties, File ruleFile) { | ||
| 114 | PrintWriter writer = null; | ||
| 115 | try { | ||
| 116 | writer = new PrintWriter(new BufferedWriter(new FileWriter(ruleFile, true))); | ||
| 117 | for (OWLClass cls : classes) { | ||
| 118 | writer.println("<http://www.w3.org/2002/07/owl#Thing>(?X) :- <" + cls.toStringID() + ">(?X)."); | ||
| 119 | } | ||
| 120 | |||
| 121 | for (OWLObjectProperty prop : properties) { | ||
| 122 | writer.println("<http://www.w3.org/2002/07/owl#Thing>(?X) :- <" + prop.toStringID() + ">(?X,?Y)."); | ||
| 123 | writer.println("<http://www.w3.org/2002/07/owl#Thing>(?Y) :- <" + prop.toStringID() + ">(?X,?Y)."); | ||
| 124 | } | ||
| 125 | |||
| 126 | }catch (FileNotFoundException e) { | ||
| 127 | e.printStackTrace(); | ||
| 128 | } catch (IOException e) { | ||
| 129 | e.printStackTrace(); | ||
| 130 | } finally{ | ||
| 131 | writer.close(); | ||
| 132 | classes.clear(); | ||
| 133 | properties.clear(); | ||
| 134 | } | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | private void writeDataFile(Set<Atom> positiveFacts, File dataFile) { | ||
| 139 | PrintWriter writer = null; | ||
| 140 | try { | ||
| 141 | writer = new PrintWriter(dataFile); | ||
| 142 | for (Atom a: positiveFacts) { | ||
| 143 | if (a.getArity() == 1) { | ||
| 144 | writer.println(a.getArgument(0)+ " <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> " + a.getDLPredicate() + " . "); | ||
| 145 | } | ||
| 146 | |||
| 147 | if (a.getArity() == 2) { | ||
| 148 | writer.println(a.getArgument(0)+ " " + a.getDLPredicate() + " "+ a.getArgument(1) + " . "); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | }catch (FileNotFoundException e) { | ||
| 152 | e.printStackTrace(); | ||
| 153 | } finally{ | ||
| 154 | writer.close(); | ||
| 155 | positiveFacts.clear(); | ||
| 156 | positiveFacts = null; | ||
| 157 | } | ||
| 158 | |||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | private void writeRules(Set<DLClause> clauses, File ruleFile) { | ||
| 163 | PrintWriter writer = null; | ||
| 164 | boolean first; | ||
| 165 | Atom emptyHeadAtom = Atom.create(AtomicConcept.NOTHING, X); | ||
| 166 | try { | ||
| 167 | writer = new PrintWriter(ruleFile); | ||
| 168 | for (DLClause clause : clauses) { | ||
| 169 | Atom headAtom = clause.getHeadLength() > 0 ? clause.getHeadAtom(0) : emptyHeadAtom; | ||
| 170 | writer.print(headAtom + " :- "); | ||
| 171 | first = true; | ||
| 172 | for (Atom bodyAtom : clause.getBodyAtoms()) | ||
| 173 | if (first) { | ||
| 174 | writer.print( bodyAtom); | ||
| 175 | first = false; | ||
| 176 | } | ||
| 177 | else | ||
| 178 | writer.print( ", " + bodyAtom); | ||
| 179 | |||
| 180 | writer.println(" ."); | ||
| 181 | } | ||
| 182 | writer.println("<http://www.w3.org/2002/07/owl#sameas>(?X,?Z) :- <http://www.w3.org/2002/07/owl#sameas>(?X,?Y), <http://www.w3.org/2002/07/owl#sameas>(?Y,?Z) ."); | ||
| 183 | writer.println("<http://www.w3.org/2002/07/owl#sameas>(?Y,?X) :- <http://www.w3.org/2002/07/owl#sameas>(?X,?Y) ."); | ||
| 184 | |||
| 185 | }catch (FileNotFoundException e) { | ||
| 186 | e.printStackTrace(); | ||
| 187 | } finally{ | ||
| 188 | writer.close(); | ||
| 189 | clauses.clear(); | ||
| 190 | clauses = null; | ||
| 191 | } | ||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | public void clausify(OWLDataFactory factory,String ontologyIRI,OWLAxioms axioms,OWLAxiomsExpressivity axiomsExpressivity, File dataFile, File ruleFile) { | ||
| 197 | Set<DLClause> dlClauses=new LinkedHashSet<DLClause>(); | ||
| 198 | Set<Atom> positiveFacts=new HashSet<Atom>(); | ||
| 199 | for (OWLObjectPropertyExpression[] inclusion : axioms.m_simpleObjectPropertyInclusions) { | ||
| 200 | Atom subRoleAtom=getRoleAtom(inclusion[0],X,Y); | ||
| 201 | Atom superRoleAtom=getRoleAtom(inclusion[1],X,Y); | ||
| 202 | DLClause dlClause=DLClause.create(new Atom[] { superRoleAtom },new Atom[] { subRoleAtom }); | ||
| 203 | dlClauses.add(dlClause); | ||
| 204 | } | ||
| 205 | NormalizedDatalogAxiomClausifier clausifier=new NormalizedDatalogAxiomClausifier(positiveFacts,factory); | ||
| 206 | for (OWLClassExpression[] inclusion : axioms.m_conceptInclusions) { | ||
| 207 | for (OWLClassExpression description : inclusion) | ||
| 208 | description.accept(clausifier); | ||
| 209 | for(DLClause dlClause :clausifier.getDLClause()) | ||
| 210 | dlClauses.add(dlClause.getSafeVersion(AtomicConcept.THING)); | ||
| 211 | } | ||
| 212 | DatalogFactClausifier factClausifier=new DatalogFactClausifier(positiveFacts); | ||
| 213 | for (OWLIndividualAxiom fact : axioms.m_facts) | ||
| 214 | fact.accept(factClausifier); | ||
| 215 | writeDataFile(positiveFacts, dataFile); | ||
| 216 | writeRules(dlClauses, ruleFile); | ||
| 217 | } | ||
| 218 | |||
| 219 | protected static AtomicRole getAtomicRole(OWLDataPropertyExpression dataPropertyExpression) { | ||
| 220 | return AtomicRole.create(((OWLDataProperty)dataPropertyExpression).getIRI().toString()); | ||
| 221 | } | ||
| 222 | protected static Atom getRoleAtom(OWLObjectPropertyExpression objectProperty,Term first,Term second) { | ||
| 223 | objectProperty=objectProperty.getSimplified(); | ||
| 224 | if (!objectProperty.isAnonymous()) { | ||
| 225 | AtomicRole role=AtomicRole.create(objectProperty.asOWLObjectProperty().getIRI().toString()); | ||
| 226 | return Atom.create(role,first,second); | ||
| 227 | } | ||
| 228 | else if (objectProperty.isAnonymous()) { | ||
| 229 | OWLObjectProperty internalObjectProperty=objectProperty.getNamedProperty(); | ||
| 230 | AtomicRole role=AtomicRole.create(internalObjectProperty.getIRI().toString()); | ||
| 231 | return Atom.create(role,second,first); | ||
| 232 | } | ||
| 233 | else | ||
| 234 | throw new IllegalStateException("Internal error: unsupported type of object property!"); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | protected static Role getRole(OWLObjectPropertyExpression objectPropertyExpression) { | ||
| 239 | objectPropertyExpression=objectPropertyExpression.getSimplified(); | ||
| 240 | if (objectPropertyExpression instanceof OWLObjectProperty) | ||
| 241 | return AtomicRole.create(((OWLObjectProperty)objectPropertyExpression).getIRI().toString()); | ||
| 242 | else if (objectPropertyExpression instanceof OWLObjectInverseOf) { | ||
| 243 | OWLObjectPropertyExpression internal=((OWLObjectInverseOf)objectPropertyExpression).getInverse(); | ||
| 244 | if (!(internal instanceof OWLObjectProperty)) | ||
| 245 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 246 | return AtomicRole.create(((OWLObjectProperty)internal).getIRI().toString()).getInverse(); | ||
| 247 | } | ||
| 248 | else | ||
| 249 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 250 | } | ||
| 251 | |||
| 252 | protected static Atom getRoleAtom(OWLDataPropertyExpression dataProperty,Term first,Term second) { | ||
| 253 | if (dataProperty instanceof OWLDataProperty) { | ||
| 254 | AtomicRole property=AtomicRole.create(((OWLDataProperty)dataProperty).getIRI().toString()); | ||
| 255 | return Atom.create(property,first,second); | ||
| 256 | } | ||
| 257 | else | ||
| 258 | throw new IllegalStateException("Internal error: unsupported type of data property!"); | ||
| 259 | } | ||
| 260 | protected static Individual getIndividual(OWLIndividual individual) { | ||
| 261 | if (individual.isAnonymous()) | ||
| 262 | return Individual.createAnonymous(individual.asOWLAnonymousIndividual().getID().toString()); | ||
| 263 | else | ||
| 264 | return Individual.create(individual.asOWLNamedIndividual().getIRI().toString()); | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | protected static class NormalizedDatalogAxiomClausifier implements OWLClassExpressionVisitor { | ||
| 269 | protected final List<Atom> m_headAtoms; | ||
| 270 | protected final List<Atom> m_bodyAtoms; | ||
| 271 | protected final List<Atom> m_auxAtoms; | ||
| 272 | protected final Set<Atom> m_positiveFacts; | ||
| 273 | protected final OWLDataFactory m_factory; | ||
| 274 | protected int m_yIndex; | ||
| 275 | protected int m_zIndex; | ||
| 276 | |||
| 277 | |||
| 278 | public NormalizedDatalogAxiomClausifier(Set<Atom> positiveFacts,OWLDataFactory factory) { | ||
| 279 | m_headAtoms=new ArrayList<Atom>(); | ||
| 280 | m_bodyAtoms=new ArrayList<Atom>(); | ||
| 281 | m_auxAtoms=new ArrayList<Atom>(); | ||
| 282 | m_positiveFacts=positiveFacts; | ||
| 283 | m_factory=factory; | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | protected Set<DLClause> getDLClause() { | ||
| 289 | |||
| 290 | Set<DLClause> clauses = new HashSet<DLClause>(); | ||
| 291 | Atom[] headAtoms=new Atom[m_headAtoms.size()]; | ||
| 292 | m_headAtoms.toArray(headAtoms); | ||
| 293 | Atom[] bodyAtoms=new Atom[m_bodyAtoms.size()]; | ||
| 294 | m_bodyAtoms.toArray(bodyAtoms); | ||
| 295 | clauses.add(DLClause.create(headAtoms,bodyAtoms)); | ||
| 296 | if (!m_auxAtoms.isEmpty()) { | ||
| 297 | Atom[] auxAtoms=new Atom[m_auxAtoms.size()]; | ||
| 298 | m_auxAtoms.toArray(auxAtoms); | ||
| 299 | clauses.add(DLClause.create(auxAtoms,bodyAtoms)); | ||
| 300 | } | ||
| 301 | m_headAtoms.clear(); | ||
| 302 | m_bodyAtoms.clear(); | ||
| 303 | m_auxAtoms.clear(); | ||
| 304 | m_yIndex=0; | ||
| 305 | m_zIndex=0; | ||
| 306 | return clauses; | ||
| 307 | } | ||
| 308 | protected void ensureYNotZero() { | ||
| 309 | if (m_yIndex==0) | ||
| 310 | m_yIndex++; | ||
| 311 | } | ||
| 312 | protected Variable nextY() { | ||
| 313 | Variable result; | ||
| 314 | if (m_yIndex==0) | ||
| 315 | result=Y; | ||
| 316 | else | ||
| 317 | result=Variable.create("?Y"+m_yIndex); | ||
| 318 | m_yIndex++; | ||
| 319 | return result; | ||
| 320 | } | ||
| 321 | protected Variable nextZ() { | ||
| 322 | Variable result; | ||
| 323 | if (m_zIndex==0) | ||
| 324 | result=Z; | ||
| 325 | else | ||
| 326 | result=Variable.create("?Z"+m_zIndex); | ||
| 327 | m_zIndex++; | ||
| 328 | return result; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | private void existentialRestriction(OWLObjectProperty prop, OWLClassExpression filler) { | ||
| 335 | if (filler.isAnonymous()) | ||
| 336 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 337 | String propertyID = prop.asOWLObjectProperty().toStringID(); | ||
| 338 | String propertyShortID = propertyID.substring(propertyID.indexOf('#')+1); | ||
| 339 | String classID = filler.asOWLClass().toStringID(); | ||
| 340 | String classShortID = classID.substring(classID.indexOf('#')+1); | ||
| 341 | Individual auxInd = Individual.create("http://www.cs.ox.ac.uk/KARMA/anonymous#:"+propertyShortID + "-"+classShortID); | ||
| 342 | m_headAtoms.add(Atom.create(AtomicRole.create(propertyID), X, auxInd)); | ||
| 343 | m_auxAtoms.add(Atom.create(AtomicConcept.create(classID), auxInd)); | ||
| 344 | } | ||
| 345 | |||
| 346 | |||
| 347 | // Various types of descriptions | ||
| 348 | |||
| 349 | public void visit(OWLClass object) { | ||
| 350 | |||
| 351 | m_headAtoms.add(Atom.create(AtomicConcept.create(object.getIRI().toString()),X)); | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | public void visit(OWLObjectIntersectionOf object) { | ||
| 356 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 357 | } | ||
| 358 | public void visit(OWLObjectUnionOf object) { | ||
| 359 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | public void visit(OWLObjectComplementOf object) { | ||
| 364 | OWLClassExpression description=object.getOperand(); | ||
| 365 | if (description instanceof OWLObjectHasSelf) { | ||
| 366 | OWLObjectPropertyExpression objectProperty=((OWLObjectHasSelf)description).getProperty(); | ||
| 367 | Atom roleAtom=getRoleAtom(objectProperty,X,X); | ||
| 368 | m_bodyAtoms.add(roleAtom); | ||
| 369 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 370 | } | ||
| 371 | else if (description instanceof OWLObjectOneOf && ((OWLObjectOneOf)description).getIndividuals().size()==1) { | ||
| 372 | OWLIndividual individual=((OWLObjectOneOf)description).getIndividuals().iterator().next(); | ||
| 373 | m_bodyAtoms.add(Atom.create(Equality.INSTANCE,X, getIndividual(individual))); | ||
| 374 | } | ||
| 375 | else if (!(description instanceof OWLClass)) | ||
| 376 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 377 | else | ||
| 378 | m_bodyAtoms.add(Atom.create(AtomicConcept.create(((OWLClass)description).getIRI().toString()),X)); | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | public void visit(OWLObjectOneOf object) { | ||
| 384 | for (OWLIndividual individual : object.getIndividuals()) { | ||
| 385 | m_headAtoms.add(Atom.create(Equality.INSTANCE,X,getIndividual(individual))); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | public void visit(OWLObjectSomeValuesFrom object) { | ||
| 393 | |||
| 394 | OWLClassExpression filler=object.getFiller(); | ||
| 395 | if (filler instanceof OWLObjectOneOf) { | ||
| 396 | for (OWLIndividual individual : ((OWLObjectOneOf)filler).getIndividuals()) { | ||
| 397 | m_headAtoms.add(getRoleAtom(object.getProperty(),X,getIndividual(individual))); | ||
| 398 | } | ||
| 399 | } else { | ||
| 400 | if (filler.isAnonymous()) | ||
| 401 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 402 | existentialRestriction(object.getProperty().asOWLObjectProperty(), filler); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | public void visit(OWLObjectAllValuesFrom object) { | ||
| 408 | Variable y=nextY(); | ||
| 409 | m_bodyAtoms.add(getRoleAtom(object.getProperty(),X,y)); | ||
| 410 | OWLClassExpression filler=object.getFiller(); | ||
| 411 | |||
| 412 | if (filler instanceof OWLClass) { | ||
| 413 | AtomicConcept atomicConcept=AtomicConcept.create(((OWLClass)filler).getIRI().toString()); | ||
| 414 | if (!atomicConcept.isAlwaysFalse()) | ||
| 415 | m_headAtoms.add(Atom.create(atomicConcept,y)); | ||
| 416 | } | ||
| 417 | else if (filler instanceof OWLObjectOneOf) { | ||
| 418 | for (OWLIndividual individual : ((OWLObjectOneOf)filler).getIndividuals()) { | ||
| 419 | m_headAtoms.add(Atom.create(Equality.INSTANCE,y,getIndividual(individual))); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | else if (filler instanceof OWLObjectComplementOf) { | ||
| 423 | OWLClassExpression operand=((OWLObjectComplementOf)filler).getOperand(); | ||
| 424 | if (operand instanceof OWLClass) { | ||
| 425 | AtomicConcept internalAtomicConcept=AtomicConcept.create(((OWLClass)operand).getIRI().toString()); | ||
| 426 | if (!internalAtomicConcept.isAlwaysTrue()) | ||
| 427 | m_bodyAtoms.add(Atom.create(internalAtomicConcept,y)); | ||
| 428 | } | ||
| 429 | else if (operand instanceof OWLObjectOneOf && ((OWLObjectOneOf)operand).getIndividuals().size()==1) { | ||
| 430 | OWLIndividual individual=((OWLObjectOneOf)operand).getIndividuals().iterator().next(); | ||
| 431 | m_bodyAtoms.add(Atom.create(Equality.INSTANCE,y,getIndividual(individual))); | ||
| 432 | } | ||
| 433 | else | ||
| 434 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 435 | } | ||
| 436 | else | ||
| 437 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 438 | } | ||
| 439 | public void visit(OWLObjectHasValue object) { | ||
| 440 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 441 | } | ||
| 442 | public void visit(OWLObjectHasSelf object) { | ||
| 443 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 444 | } | ||
| 445 | |||
| 446 | public void visit(OWLObjectMinCardinality object) { | ||
| 447 | if (object.getCardinality() != 1) | ||
| 448 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 449 | existentialRestriction(object.getProperty().asOWLObjectProperty(), object.getFiller()); | ||
| 450 | } | ||
| 451 | public void visit(OWLObjectMaxCardinality object) { | ||
| 452 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 453 | // int cardinality=object.getCardinality(); | ||
| 454 | // OWLObjectPropertyExpression onObjectProperty=object.getProperty(); | ||
| 455 | // OWLClassExpression filler=object.getFiller(); | ||
| 456 | // ensureYNotZero(); | ||
| 457 | // boolean isPositive; | ||
| 458 | // AtomicConcept atomicConcept; | ||
| 459 | // if (filler instanceof OWLClass) { | ||
| 460 | // isPositive=true; | ||
| 461 | // atomicConcept=AtomicConcept.create(((OWLClass)filler).getIRI().toString()); | ||
| 462 | // if (atomicConcept.isAlwaysTrue()) | ||
| 463 | // atomicConcept=null; | ||
| 464 | // } | ||
| 465 | // else if (filler instanceof OWLObjectComplementOf) { | ||
| 466 | // OWLClassExpression internal=((OWLObjectComplementOf)filler).getOperand(); | ||
| 467 | // if (!(internal instanceof OWLClass)) | ||
| 468 | // throw new IllegalStateException("Internal error: Invalid ontology normal form."); | ||
| 469 | // isPositive=false; | ||
| 470 | // atomicConcept=AtomicConcept.create(((OWLClass)internal).getIRI().toString()); | ||
| 471 | // if (atomicConcept.isAlwaysFalse()) | ||
| 472 | // atomicConcept=null; | ||
| 473 | // } | ||
| 474 | // else | ||
| 475 | // throw new IllegalStateException("Internal error: Invalid ontology normal form."); | ||
| 476 | // Role onRole=getRole(onObjectProperty); | ||
| 477 | // LiteralConcept toConcept=getLiteralConcept(filler); | ||
| 478 | // AnnotatedEquality annotatedEquality=AnnotatedEquality.create(cardinality,onRole,toConcept); | ||
| 479 | // Variable[] yVars=new Variable[cardinality+1]; | ||
| 480 | // for (int i=0;i<yVars.length;i++) { | ||
| 481 | // yVars[i]=nextY(); | ||
| 482 | // m_bodyAtoms.add(getRoleAtom(onObjectProperty,X,yVars[i])); | ||
| 483 | // if (atomicConcept!=null) { | ||
| 484 | // Atom atom=Atom.create(atomicConcept,yVars[i]); | ||
| 485 | // if (isPositive) | ||
| 486 | // m_bodyAtoms.add(atom); | ||
| 487 | // else | ||
| 488 | // m_headAtoms.add(atom); | ||
| 489 | // } | ||
| 490 | // } | ||
| 491 | // // Node ID comparisons are not needed in case of functionality axioms, | ||
| 492 | // // as the effect of these is simulated by the way in which the rules are applied. | ||
| 493 | // if (yVars.length>2) { | ||
| 494 | // for (int i=0;i<yVars.length-1;i++) | ||
| 495 | // m_bodyAtoms.add(Atom.create(NodeIDLessEqualThan.INSTANCE,yVars[i],yVars[i+1])); | ||
| 496 | // m_bodyAtoms.add(Atom.create(NodeIDsAscendingOrEqual.create(yVars.length),yVars)); | ||
| 497 | // } | ||
| 498 | // for (int i=0;i<yVars.length;i++) | ||
| 499 | // for (int j=i+1;j<yVars.length;j++) | ||
| 500 | // m_headAtoms.add(Atom.create(annotatedEquality,yVars[i],yVars[j],X)); | ||
| 501 | } | ||
| 502 | public void visit(OWLObjectExactCardinality object) { | ||
| 503 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 504 | } | ||
| 505 | public void visit(OWLDataSomeValuesFrom object) { | ||
| 506 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 507 | } | ||
| 508 | public void visit(OWLDataAllValuesFrom object) { | ||
| 509 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 510 | } | ||
| 511 | public void visit(OWLDataHasValue object) { | ||
| 512 | throw new IllegalStateException("Internal error: Invalid normal form."); | ||
| 513 | } | ||
| 514 | public void visit(OWLDataMinCardinality object) { | ||
| 515 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 516 | } | ||
| 517 | public void visit(OWLDataMaxCardinality object) { | ||
| 518 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 519 | } | ||
| 520 | public void visit(OWLDataExactCardinality object) { | ||
| 521 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | protected static class DatalogFactClausifier implements OWLAxiomVisitor { | ||
| 526 | protected final Set<Atom> m_positiveFacts; | ||
| 527 | |||
| 528 | public DatalogFactClausifier(Set<Atom> positiveFacts) { | ||
| 529 | m_positiveFacts=positiveFacts; | ||
| 530 | } | ||
| 531 | public void visit(OWLSameIndividualAxiom object) { | ||
| 532 | OWLIndividual[] individuals=new OWLIndividual[object.getIndividuals().size()]; | ||
| 533 | object.getIndividuals().toArray(individuals); | ||
| 534 | for (int i=0;i<individuals.length-1;i++) | ||
| 535 | m_positiveFacts.add(Atom.create(Equality.create(),getIndividual(individuals[i]),getIndividual(individuals[i+1]))); | ||
| 536 | } | ||
| 537 | public void visit(OWLDifferentIndividualsAxiom object) { | ||
| 538 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 539 | } | ||
| 540 | public void visit(OWLClassAssertionAxiom object) { | ||
| 541 | OWLClassExpression description=object.getClassExpression(); | ||
| 542 | if (description instanceof OWLClass) { | ||
| 543 | AtomicConcept atomicConcept=AtomicConcept.create(((OWLClass)description).getIRI().toString()); | ||
| 544 | m_positiveFacts.add(Atom.create(atomicConcept,getIndividual(object.getIndividual()))); | ||
| 545 | } | ||
| 546 | else if (description instanceof OWLObjectComplementOf && ((OWLObjectComplementOf)description).getOperand() instanceof OWLClass) { | ||
| 547 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 548 | } | ||
| 549 | else if (description instanceof OWLObjectHasSelf) { | ||
| 550 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 551 | } | ||
| 552 | else if (description instanceof OWLObjectComplementOf && ((OWLObjectComplementOf)description).getOperand() instanceof OWLObjectHasSelf) { | ||
| 553 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 554 | } | ||
| 555 | else | ||
| 556 | throw new IllegalStateException("Internal error: invalid normal form."); | ||
| 557 | } | ||
| 558 | public void visit(OWLObjectPropertyAssertionAxiom object) { | ||
| 559 | m_positiveFacts.add(getRoleAtom(object.getProperty(),getIndividual(object.getSubject()),getIndividual(object.getObject()))); | ||
| 560 | } | ||
| 561 | public void visit(OWLNegativeObjectPropertyAssertionAxiom object) { | ||
| 562 | throw new IllegalStateException("Internal error: invalid normal form."); } | ||
| 563 | public void visit(OWLDataPropertyAssertionAxiom object) { | ||
| 564 | |||
| 565 | } | ||
| 566 | public void visit(OWLNegativeDataPropertyAssertionAxiom object) { | ||
| 567 | |||
| 568 | } | ||
| 569 | } | ||
| 570 | |||
| 571 | } | ||
| 572 | |||
