diff options
Diffstat (limited to 'src/main/java/uk/ac/ox/cs/pagoda/owl')
4 files changed, 1327 insertions, 0 deletions
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java b/src/main/java/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java new file mode 100644 index 0000000..50865d4 --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.owl; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.IOException; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.Configuration; | ||
| 7 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 8 | import org.semanticweb.HermiT.model.AtLeast; | ||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.DLClause; | ||
| 11 | import org.semanticweb.HermiT.model.DLOntology; | ||
| 12 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 13 | import org.semanticweb.HermiT.model.Equality; | ||
| 14 | import org.semanticweb.HermiT.model.Inequality; | ||
| 15 | import org.semanticweb.HermiT.structural.OWLClausification; | ||
| 16 | import org.semanticweb.owlapi.model.IRI; | ||
| 17 | import org.semanticweb.owlapi.model.OWLAnnotationAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 19 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 20 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDeclarationAxiom; | ||
| 22 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 23 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 24 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 25 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 26 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 27 | import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; | ||
| 28 | import org.semanticweb.owlapi.model.UnknownOWLOntologyException; | ||
| 29 | |||
| 30 | import uk.ac.ox.cs.pagoda.hermit.TermGraph; | ||
| 31 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 32 | |||
| 33 | public class EqualitiesEliminator { | ||
| 34 | |||
| 35 | String fileName; | ||
| 36 | OWLOntologyManager manager; | ||
| 37 | OWLOntology inputOntology, outputOntology = null; | ||
| 38 | |||
| 39 | public EqualitiesEliminator(OWLOntology o) { | ||
| 40 | this.fileName = OWLHelper.getOntologyPath(o); | ||
| 41 | inputOntology = o; | ||
| 42 | manager = inputOntology.getOWLOntologyManager(); | ||
| 43 | } | ||
| 44 | |||
| 45 | public void removeEqualities() throws OWLOntologyCreationException { | ||
| 46 | outputOntology = manager.createOntology(IRI.create(inputOntology.getOntologyID().getOntologyIRI().toString().replace(".owl", "-minus.owl"))); | ||
| 47 | try { | ||
| 48 | manager.setOntologyDocumentIRI(outputOntology, IRI.create(Utility.toFileIRI(getOutputFile().getCanonicalPath()))); | ||
| 49 | } catch (UnknownOWLOntologyException e) { | ||
| 50 | e.printStackTrace(); | ||
| 51 | } catch (IOException e) { | ||
| 52 | e.printStackTrace(); | ||
| 53 | } | ||
| 54 | |||
| 55 | for (OWLOntology onto: inputOntology.getImportsClosure()) | ||
| 56 | for (OWLAxiom axiom: onto.getAxioms()) { | ||
| 57 | if (axiom instanceof OWLAnnotationAxiom | ||
| 58 | || axiom instanceof OWLDeclarationAxiom | ||
| 59 | || axiom instanceof OWLTransitiveObjectPropertyAxiom | ||
| 60 | || axiom instanceof OWLClassAssertionAxiom | ||
| 61 | || axiom instanceof OWLObjectPropertyAssertionAxiom | ||
| 62 | || axiom instanceof OWLDataPropertyAssertionAxiom | ||
| 63 | ) { | ||
| 64 | manager.removeAxiom(onto, axiom); | ||
| 65 | manager.addAxiom(outputOntology, axiom); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | Configuration conf = new Configuration(); | ||
| 70 | OWLClausification clausifier = new OWLClausification(conf); | ||
| 71 | DLOntology dlOntology = (DLOntology)clausifier.preprocessAndClausify(inputOntology, null)[1]; | ||
| 72 | |||
| 73 | TermGraph termGraph; | ||
| 74 | for (DLClause dlClause: dlOntology.getDLClauses()) { | ||
| 75 | if (!containsEqualities(dlClause)) { | ||
| 76 | termGraph = new TermGraph(dlClause); | ||
| 77 | manager.addAxiom(outputOntology, OWLHelper.getOWLAxiom(inputOntology, termGraph.simplify())); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | private boolean containsEqualities(DLClause dlClause) { | ||
| 83 | DLPredicate predicate; | ||
| 84 | for (Atom headAtom: dlClause.getHeadAtoms()) { | ||
| 85 | predicate = headAtom.getDLPredicate(); | ||
| 86 | if (predicate instanceof Equality || predicate instanceof AnnotatedEquality || predicate instanceof Inequality) { | ||
| 87 | return true; | ||
| 88 | } | ||
| 89 | |||
| 90 | if (predicate instanceof AtLeast) { | ||
| 91 | AtLeast atLeast = (AtLeast) predicate; | ||
| 92 | if (atLeast.getNumber() >= 2) | ||
| 93 | return true; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | return false; | ||
| 97 | } | ||
| 98 | |||
| 99 | public void save() { | ||
| 100 | if (outputOntology == null) | ||
| 101 | try { | ||
| 102 | removeEqualities(); | ||
| 103 | } catch (OWLOntologyCreationException e) { | ||
| 104 | e.printStackTrace(); | ||
| 105 | return ; | ||
| 106 | } | ||
| 107 | try { | ||
| 108 | manager.saveOntology(outputOntology, IRI.create(getOutputFile())); | ||
| 109 | } catch (OWLOntologyStorageException e) { | ||
| 110 | e.printStackTrace(); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | public File getOutputFile() { | ||
| 115 | return new File(fileName.replace(".owl", "-minus.owl")); | ||
| 116 | } | ||
| 117 | |||
| 118 | public OWLOntology getOutputOntology() { | ||
| 119 | if (outputOntology == null) | ||
| 120 | try { | ||
| 121 | removeEqualities(); | ||
| 122 | } catch (OWLOntologyCreationException e) { | ||
| 123 | e.printStackTrace(); | ||
| 124 | } | ||
| 125 | return outputOntology; | ||
| 126 | } | ||
| 127 | |||
| 128 | public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException { | ||
| 129 | args = ("/home/yzhou/ontologies/uobm/univ-bench-dl.owl").split("\\ "); | ||
| 130 | |||
| 131 | EqualitiesEliminator eliminator = new EqualitiesEliminator(OWLHelper.loadOntology(args[0])); | ||
| 132 | eliminator.save(); | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | } | ||
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/owl/MyHornAxiomVisitorEx.java b/src/main/java/uk/ac/ox/cs/pagoda/owl/MyHornAxiomVisitorEx.java new file mode 100644 index 0000000..be22ded --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/owl/MyHornAxiomVisitorEx.java | |||
| @@ -0,0 +1,493 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.owl; | ||
| 2 | |||
| 3 | import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; | ||
| 4 | import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; | ||
| 5 | import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; | ||
| 6 | import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; | ||
| 7 | import org.semanticweb.owlapi.model.OWLAxiomVisitorEx; | ||
| 8 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 9 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 10 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; | ||
| 12 | import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; | ||
| 13 | import org.semanticweb.owlapi.model.OWLDataExactCardinality; | ||
| 14 | import org.semanticweb.owlapi.model.OWLDataHasValue; | ||
| 15 | import org.semanticweb.owlapi.model.OWLDataMaxCardinality; | ||
| 16 | import org.semanticweb.owlapi.model.OWLDataMinCardinality; | ||
| 17 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; | ||
| 19 | import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; | ||
| 20 | import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; | ||
| 22 | import org.semanticweb.owlapi.model.OWLDeclarationAxiom; | ||
| 23 | import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; | ||
| 24 | import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; | ||
| 25 | import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; | ||
| 26 | import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; | ||
| 27 | import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; | ||
| 28 | import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; | ||
| 29 | import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; | ||
| 30 | import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; | ||
| 31 | import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; | ||
| 32 | import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; | ||
| 33 | import org.semanticweb.owlapi.model.OWLHasKeyAxiom; | ||
| 34 | import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; | ||
| 35 | import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; | ||
| 36 | import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; | ||
| 37 | import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; | ||
| 38 | import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; | ||
| 39 | import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; | ||
| 40 | import org.semanticweb.owlapi.model.OWLObjectComplementOf; | ||
| 41 | import org.semanticweb.owlapi.model.OWLObjectExactCardinality; | ||
| 42 | import org.semanticweb.owlapi.model.OWLObjectHasSelf; | ||
| 43 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 44 | import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; | ||
| 45 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 46 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 47 | import org.semanticweb.owlapi.model.OWLObjectOneOf; | ||
| 48 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 49 | import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; | ||
| 50 | import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; | ||
| 51 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 52 | import org.semanticweb.owlapi.model.OWLObjectUnionOf; | ||
| 53 | import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; | ||
| 54 | import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; | ||
| 55 | import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; | ||
| 56 | import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; | ||
| 57 | import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; | ||
| 58 | import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; | ||
| 59 | import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; | ||
| 60 | import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; | ||
| 61 | import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; | ||
| 62 | import org.semanticweb.owlapi.model.SWRLRule; | ||
| 63 | |||
| 64 | public class MyHornAxiomVisitorEx implements OWLAxiomVisitorEx<Boolean> { | ||
| 65 | final PositiveAppearanceVisitorEx positive = new PositiveAppearanceVisitorEx(); | ||
| 66 | final NegativeAppearanceVisitorEx negative = new NegativeAppearanceVisitorEx(); | ||
| 67 | |||
| 68 | @Override | ||
| 69 | public Boolean visit(OWLSubAnnotationPropertyOfAxiom axiom) { | ||
| 70 | return Boolean.TRUE; | ||
| 71 | } | ||
| 72 | |||
| 73 | @Override | ||
| 74 | public Boolean visit(OWLAnnotationPropertyDomainAxiom axiom) { | ||
| 75 | return Boolean.TRUE; | ||
| 76 | } | ||
| 77 | |||
| 78 | @Override | ||
| 79 | public Boolean visit(OWLAnnotationPropertyRangeAxiom axiom) { | ||
| 80 | return Boolean.TRUE; | ||
| 81 | } | ||
| 82 | |||
| 83 | @Override | ||
| 84 | public Boolean visit(OWLSubClassOfAxiom axiom) { | ||
| 85 | return Boolean.valueOf(axiom.getSubClass().accept(negative).booleanValue() | ||
| 86 | && axiom.getSuperClass().accept(positive).booleanValue()); | ||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public Boolean visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { | ||
| 91 | return Boolean.FALSE; | ||
| 92 | } | ||
| 93 | |||
| 94 | @Override | ||
| 95 | public Boolean visit(OWLAsymmetricObjectPropertyAxiom axiom) { | ||
| 96 | return Boolean.FALSE; | ||
| 97 | } | ||
| 98 | |||
| 99 | @Override | ||
| 100 | public Boolean visit(OWLReflexiveObjectPropertyAxiom axiom) { | ||
| 101 | return Boolean.FALSE; | ||
| 102 | } | ||
| 103 | |||
| 104 | @Override | ||
| 105 | public Boolean visit(OWLDisjointClassesAxiom axiom) { | ||
| 106 | for (OWLClassExpression c : axiom.getClassExpressions()) { | ||
| 107 | if (!c.accept(negative).booleanValue()) { | ||
| 108 | return Boolean.FALSE; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return Boolean.TRUE; | ||
| 112 | } | ||
| 113 | |||
| 114 | @Override | ||
| 115 | public Boolean visit(OWLDataPropertyDomainAxiom axiom) { | ||
| 116 | return Boolean.TRUE; | ||
| 117 | } | ||
| 118 | |||
| 119 | @Override | ||
| 120 | public Boolean visit(OWLObjectPropertyDomainAxiom axiom) { | ||
| 121 | return axiom.getDomain().accept(positive); | ||
| 122 | } | ||
| 123 | |||
| 124 | @Override | ||
| 125 | public Boolean visit(OWLEquivalentObjectPropertiesAxiom axiom) { | ||
| 126 | return Boolean.TRUE; | ||
| 127 | } | ||
| 128 | |||
| 129 | @Override | ||
| 130 | public Boolean visit(OWLNegativeDataPropertyAssertionAxiom axiom) { | ||
| 131 | return Boolean.TRUE; | ||
| 132 | } | ||
| 133 | |||
| 134 | @Override | ||
| 135 | public Boolean visit(OWLDifferentIndividualsAxiom axiom) { | ||
| 136 | return Boolean.FALSE; | ||
| 137 | } | ||
| 138 | |||
| 139 | @Override | ||
| 140 | public Boolean visit(OWLDisjointDataPropertiesAxiom axiom) { | ||
| 141 | return Boolean.TRUE; | ||
| 142 | } | ||
| 143 | |||
| 144 | @Override | ||
| 145 | public Boolean visit(OWLDisjointObjectPropertiesAxiom axiom) { | ||
| 146 | return Boolean.FALSE; | ||
| 147 | } | ||
| 148 | |||
| 149 | @Override | ||
| 150 | public Boolean visit(OWLObjectPropertyRangeAxiom axiom) { | ||
| 151 | return axiom.getRange().accept(positive); | ||
| 152 | } | ||
| 153 | |||
| 154 | @Override | ||
| 155 | public Boolean visit(OWLObjectPropertyAssertionAxiom axiom) { | ||
| 156 | return Boolean.FALSE; | ||
| 157 | } | ||
| 158 | |||
| 159 | @Override | ||
| 160 | public Boolean visit(OWLFunctionalObjectPropertyAxiom axiom) { | ||
| 161 | return Boolean.TRUE; | ||
| 162 | } | ||
| 163 | |||
| 164 | @Override | ||
| 165 | public Boolean visit(OWLSubObjectPropertyOfAxiom axiom) { | ||
| 166 | return Boolean.TRUE; | ||
| 167 | } | ||
| 168 | |||
| 169 | @Override | ||
| 170 | public Boolean visit(OWLDisjointUnionAxiom axiom) { | ||
| 171 | OWLClassExpression c1 = axiom.getOWLClass(); | ||
| 172 | if (!c1.accept(positive).booleanValue() || !c1.accept(negative).booleanValue()) { | ||
| 173 | return Boolean.FALSE; | ||
| 174 | } | ||
| 175 | for (OWLClassExpression c : axiom.getClassExpressions()) { | ||
| 176 | if (!c.accept(positive).booleanValue() || !c.accept(negative).booleanValue()) { | ||
| 177 | return Boolean.FALSE; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | return Boolean.TRUE; | ||
| 181 | } | ||
| 182 | |||
| 183 | @Override | ||
| 184 | public Boolean visit(OWLDeclarationAxiom axiom) { | ||
| 185 | return Boolean.TRUE; | ||
| 186 | } | ||
| 187 | |||
| 188 | @Override | ||
| 189 | public Boolean visit(OWLAnnotationAssertionAxiom axiom) { | ||
| 190 | return Boolean.TRUE; | ||
| 191 | } | ||
| 192 | |||
| 193 | @Override | ||
| 194 | public Boolean visit(OWLSymmetricObjectPropertyAxiom axiom) { | ||
| 195 | return Boolean.TRUE; | ||
| 196 | } | ||
| 197 | |||
| 198 | @Override | ||
| 199 | public Boolean visit(OWLDataPropertyRangeAxiom axiom) { | ||
| 200 | return Boolean.TRUE; | ||
| 201 | } | ||
| 202 | |||
| 203 | @Override | ||
| 204 | public Boolean visit(OWLFunctionalDataPropertyAxiom axiom) { | ||
| 205 | return Boolean.TRUE; | ||
| 206 | } | ||
| 207 | |||
| 208 | @Override | ||
| 209 | public Boolean visit(OWLEquivalentDataPropertiesAxiom axiom) { | ||
| 210 | return Boolean.TRUE; | ||
| 211 | } | ||
| 212 | |||
| 213 | @Override | ||
| 214 | public Boolean visit(OWLClassAssertionAxiom axiom) { | ||
| 215 | return Boolean.FALSE; | ||
| 216 | } | ||
| 217 | |||
| 218 | @Override | ||
| 219 | public Boolean visit(OWLEquivalentClassesAxiom axiom) { | ||
| 220 | for (OWLClassExpression c : axiom.getClassExpressions()) { | ||
| 221 | if (!c.accept(positive).booleanValue() || !c.accept(negative).booleanValue()) { | ||
| 222 | return Boolean.FALSE; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | return Boolean.TRUE; | ||
| 226 | } | ||
| 227 | |||
| 228 | @Override | ||
| 229 | public Boolean visit(OWLDataPropertyAssertionAxiom axiom) { | ||
| 230 | return Boolean.TRUE; | ||
| 231 | } | ||
| 232 | |||
| 233 | @Override | ||
| 234 | public Boolean visit(OWLTransitiveObjectPropertyAxiom axiom) { | ||
| 235 | return Boolean.TRUE; | ||
| 236 | } | ||
| 237 | |||
| 238 | @Override | ||
| 239 | public Boolean visit(OWLIrreflexiveObjectPropertyAxiom axiom) { | ||
| 240 | return Boolean.FALSE; | ||
| 241 | } | ||
| 242 | |||
| 243 | @Override | ||
| 244 | public Boolean visit(OWLSubDataPropertyOfAxiom axiom) { | ||
| 245 | return Boolean.TRUE; | ||
| 246 | } | ||
| 247 | |||
| 248 | @Override | ||
| 249 | public Boolean visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { | ||
| 250 | return Boolean.TRUE; | ||
| 251 | } | ||
| 252 | |||
| 253 | @Override | ||
| 254 | public Boolean visit(OWLSameIndividualAxiom axiom) { | ||
| 255 | return Boolean.FALSE; | ||
| 256 | } | ||
| 257 | |||
| 258 | @Override | ||
| 259 | public Boolean visit(OWLSubPropertyChainOfAxiom axiom) { | ||
| 260 | return Boolean.FALSE; | ||
| 261 | } | ||
| 262 | |||
| 263 | @Override | ||
| 264 | public Boolean visit(OWLInverseObjectPropertiesAxiom axiom) { | ||
| 265 | return Boolean.TRUE; | ||
| 266 | } | ||
| 267 | |||
| 268 | @Override | ||
| 269 | public Boolean visit(OWLHasKeyAxiom axiom) { | ||
| 270 | return Boolean.FALSE; | ||
| 271 | } | ||
| 272 | |||
| 273 | @Override | ||
| 274 | public Boolean visit(OWLDatatypeDefinitionAxiom axiom) { | ||
| 275 | return Boolean.TRUE; | ||
| 276 | } | ||
| 277 | |||
| 278 | @Override | ||
| 279 | public Boolean visit(SWRLRule rule) { | ||
| 280 | return Boolean.FALSE; | ||
| 281 | } | ||
| 282 | |||
| 283 | private class PositiveAppearanceVisitorEx implements | ||
| 284 | OWLClassExpressionVisitorEx<Boolean> { | ||
| 285 | public PositiveAppearanceVisitorEx() { | ||
| 286 | } | ||
| 287 | |||
| 288 | @Override | ||
| 289 | public Boolean visit(OWLClass ce) { | ||
| 290 | return Boolean.TRUE; | ||
| 291 | } | ||
| 292 | |||
| 293 | @Override | ||
| 294 | public Boolean visit(OWLObjectIntersectionOf ce) { | ||
| 295 | for (OWLClassExpression c : ce.getOperands()) { | ||
| 296 | if (c.accept(this).equals(Boolean.FALSE)) { | ||
| 297 | return Boolean.FALSE; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | return Boolean.TRUE; | ||
| 301 | } | ||
| 302 | |||
| 303 | @Override | ||
| 304 | public Boolean visit(OWLObjectUnionOf ce) { | ||
| 305 | return Boolean.FALSE; | ||
| 306 | } | ||
| 307 | |||
| 308 | @Override | ||
| 309 | public Boolean visit(OWLObjectComplementOf ce) { | ||
| 310 | return ce.getOperand().accept(negative); | ||
| 311 | } | ||
| 312 | |||
| 313 | @Override | ||
| 314 | public Boolean visit(OWLObjectSomeValuesFrom ce) { | ||
| 315 | return ce.getFiller().accept(this); | ||
| 316 | } | ||
| 317 | |||
| 318 | @Override | ||
| 319 | public Boolean visit(OWLObjectAllValuesFrom ce) { | ||
| 320 | return ce.getFiller().accept(this); | ||
| 321 | } | ||
| 322 | |||
| 323 | @Override | ||
| 324 | public Boolean visit(OWLObjectHasValue ce) { | ||
| 325 | return Boolean.FALSE; | ||
| 326 | } | ||
| 327 | |||
| 328 | @Override | ||
| 329 | public Boolean visit(OWLObjectMinCardinality ce) { | ||
| 330 | return ce.getFiller().accept(this); | ||
| 331 | } | ||
| 332 | |||
| 333 | @Override | ||
| 334 | public Boolean visit(OWLObjectExactCardinality ce) { | ||
| 335 | return Boolean.valueOf(ce.getCardinality() <= 1 | ||
| 336 | && ce.getFiller().accept(this).booleanValue() | ||
| 337 | && ce.getFiller().accept(negative).booleanValue()); | ||
| 338 | } | ||
| 339 | |||
| 340 | @Override | ||
| 341 | public Boolean visit(OWLObjectMaxCardinality ce) { | ||
| 342 | return Boolean.valueOf(ce.getCardinality() <= 1 | ||
| 343 | && ce.getFiller().accept(negative).booleanValue()); | ||
| 344 | } | ||
| 345 | |||
| 346 | @Override | ||
| 347 | public Boolean visit(OWLObjectHasSelf ce) { | ||
| 348 | return Boolean.FALSE; | ||
| 349 | } | ||
| 350 | |||
| 351 | @Override | ||
| 352 | public Boolean visit(OWLObjectOneOf ce) { | ||
| 353 | return Boolean.FALSE; | ||
| 354 | } | ||
| 355 | |||
| 356 | @Override | ||
| 357 | public Boolean visit(OWLDataSomeValuesFrom ce) { | ||
| 358 | return Boolean.TRUE; | ||
| 359 | } | ||
| 360 | |||
| 361 | @Override | ||
| 362 | public Boolean visit(OWLDataAllValuesFrom ce) { | ||
| 363 | return Boolean.TRUE; | ||
| 364 | } | ||
| 365 | |||
| 366 | @Override | ||
| 367 | public Boolean visit(OWLDataHasValue ce) { | ||
| 368 | return Boolean.TRUE; | ||
| 369 | } | ||
| 370 | |||
| 371 | @Override | ||
| 372 | public Boolean visit(OWLDataMinCardinality ce) { | ||
| 373 | return Boolean.TRUE; | ||
| 374 | } | ||
| 375 | |||
| 376 | @Override | ||
| 377 | public Boolean visit(OWLDataExactCardinality ce) { | ||
| 378 | return ce.getCardinality() <= 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | @Override | ||
| 382 | public Boolean visit(OWLDataMaxCardinality ce) { | ||
| 383 | return ce.getCardinality() <= 1; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | private class NegativeAppearanceVisitorEx implements | ||
| 388 | OWLClassExpressionVisitorEx<Boolean> { | ||
| 389 | public NegativeAppearanceVisitorEx() { | ||
| 390 | } | ||
| 391 | |||
| 392 | @Override | ||
| 393 | public Boolean visit(OWLClass ce) { | ||
| 394 | return Boolean.TRUE; | ||
| 395 | } | ||
| 396 | |||
| 397 | @Override | ||
| 398 | public Boolean visit(OWLObjectIntersectionOf ce) { | ||
| 399 | for (OWLClassExpression c : ce.getOperands()) { | ||
| 400 | if (c.accept(this).equals(Boolean.FALSE)) { | ||
| 401 | return Boolean.FALSE; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | return Boolean.TRUE; | ||
| 405 | } | ||
| 406 | |||
| 407 | @Override | ||
| 408 | public Boolean visit(OWLObjectUnionOf ce) { | ||
| 409 | for (OWLClassExpression c : ce.getOperands()) { | ||
| 410 | if (c.accept(this).equals(Boolean.FALSE)) { | ||
| 411 | return Boolean.FALSE; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | return Boolean.TRUE; | ||
| 415 | } | ||
| 416 | |||
| 417 | @Override | ||
| 418 | public Boolean visit(OWLObjectComplementOf ce) { | ||
| 419 | return Boolean.FALSE; | ||
| 420 | } | ||
| 421 | |||
| 422 | @Override | ||
| 423 | public Boolean visit(OWLObjectSomeValuesFrom ce) { | ||
| 424 | return ce.getFiller().accept(this); | ||
| 425 | } | ||
| 426 | |||
| 427 | @Override | ||
| 428 | public Boolean visit(OWLObjectAllValuesFrom ce) { | ||
| 429 | return Boolean.FALSE; | ||
| 430 | } | ||
| 431 | |||
| 432 | @Override | ||
| 433 | public Boolean visit(OWLObjectHasValue ce) { | ||
| 434 | return Boolean.FALSE; | ||
| 435 | } | ||
| 436 | |||
| 437 | @Override | ||
| 438 | public Boolean visit(OWLObjectMinCardinality ce) { | ||
| 439 | return Boolean.valueOf(ce.getCardinality() <= 1 | ||
| 440 | && ce.getFiller().accept(this).booleanValue()); | ||
| 441 | } | ||
| 442 | |||
| 443 | @Override | ||
| 444 | public Boolean visit(OWLObjectExactCardinality ce) { | ||
| 445 | return Boolean.FALSE; | ||
| 446 | } | ||
| 447 | |||
| 448 | @Override | ||
| 449 | public Boolean visit(OWLObjectMaxCardinality ce) { | ||
| 450 | return Boolean.FALSE; | ||
| 451 | } | ||
| 452 | |||
| 453 | @Override | ||
| 454 | public Boolean visit(OWLObjectHasSelf ce) { | ||
| 455 | return Boolean.FALSE; | ||
| 456 | } | ||
| 457 | |||
| 458 | @Override | ||
| 459 | public Boolean visit(OWLObjectOneOf ce) { | ||
| 460 | return Boolean.FALSE; | ||
| 461 | } | ||
| 462 | |||
| 463 | @Override | ||
| 464 | public Boolean visit(OWLDataSomeValuesFrom ce) { | ||
| 465 | return Boolean.TRUE; | ||
| 466 | } | ||
| 467 | |||
| 468 | @Override | ||
| 469 | public Boolean visit(OWLDataAllValuesFrom ce) { | ||
| 470 | return Boolean.FALSE; | ||
| 471 | } | ||
| 472 | |||
| 473 | @Override | ||
| 474 | public Boolean visit(OWLDataHasValue ce) { | ||
| 475 | return Boolean.TRUE; | ||
| 476 | } | ||
| 477 | |||
| 478 | @Override | ||
| 479 | public Boolean visit(OWLDataMinCardinality ce) { | ||
| 480 | return ce.getCardinality() <= 1; | ||
| 481 | } | ||
| 482 | |||
| 483 | @Override | ||
| 484 | public Boolean visit(OWLDataExactCardinality ce) { | ||
| 485 | return Boolean.FALSE; | ||
| 486 | } | ||
| 487 | |||
| 488 | @Override | ||
| 489 | public Boolean visit(OWLDataMaxCardinality ce) { | ||
| 490 | return Boolean.FALSE; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | } | ||
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/owl/OWLHelper.java b/src/main/java/uk/ac/ox/cs/pagoda/owl/OWLHelper.java new file mode 100644 index 0000000..e7be96b --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/owl/OWLHelper.java | |||
| @@ -0,0 +1,471 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.owl; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileOutputStream; | ||
| 5 | import java.io.IOException; | ||
| 6 | import java.util.HashSet; | ||
| 7 | import java.util.LinkedList; | ||
| 8 | import java.util.List; | ||
| 9 | import java.util.Set; | ||
| 10 | |||
| 11 | import org.semanticweb.HermiT.model.Atom; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 13 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 14 | import org.semanticweb.HermiT.model.DLClause; | ||
| 15 | import org.semanticweb.HermiT.model.Individual; | ||
| 16 | import org.semanticweb.karma2.profile.ELHOProfile; | ||
| 17 | import org.semanticweb.owlapi.apibinding.OWLManager; | ||
| 18 | import org.semanticweb.owlapi.model.AddImport; | ||
| 19 | import org.semanticweb.owlapi.model.IRI; | ||
| 20 | import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; | ||
| 21 | import org.semanticweb.owlapi.model.OWLAnnotationSubject; | ||
| 22 | import org.semanticweb.owlapi.model.OWLAnnotationValue; | ||
| 23 | import org.semanticweb.owlapi.model.OWLAnonymousIndividual; | ||
| 24 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 25 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 26 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 27 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 28 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 29 | import org.semanticweb.owlapi.model.OWLDatatype; | ||
| 30 | import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; | ||
| 31 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 32 | import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; | ||
| 33 | import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; | ||
| 34 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 35 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 36 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 37 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 38 | import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; | ||
| 39 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 40 | import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; | ||
| 41 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 42 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 43 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 44 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 45 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 46 | import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; | ||
| 47 | import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; | ||
| 48 | import org.semanticweb.owlapi.model.UnknownOWLOntologyException; | ||
| 49 | import org.semanticweb.owlapi.profiles.OWL2RLProfile; | ||
| 50 | import org.semanticweb.owlapi.profiles.OWLProfileReport; | ||
| 51 | import org.semanticweb.owlapi.profiles.OWLProfileViolation; | ||
| 52 | import org.semanticweb.owlapi.profiles.violations.UseOfUndeclaredClass; | ||
| 53 | import org.semanticweb.owlapi.profiles.violations.UseOfUndeclaredDataProperty; | ||
| 54 | import org.semanticweb.owlapi.profiles.violations.UseOfUndeclaredObjectProperty; | ||
| 55 | import org.semanticweb.owlapi.util.OWLOntologyMerger; | ||
| 56 | |||
| 57 | import uk.ac.ox.cs.pagoda.approx.Clause; | ||
| 58 | import uk.ac.ox.cs.pagoda.approx.Clausifier; | ||
| 59 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 60 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 61 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 62 | |||
| 63 | public class OWLHelper { | ||
| 64 | |||
| 65 | public static OWLOntology loadOntology(OWLOntologyManager manager, String fileName) { | ||
| 66 | // TODO to be uncommented to set silent missing imports ... | ||
| 67 | // manager.setSilentMissingImportsHandling(true); | ||
| 68 | OWLOntology ontology = null; | ||
| 69 | File file = new File(fileName); | ||
| 70 | try { | ||
| 71 | ontology = manager.loadOntologyFromOntologyDocument(file); | ||
| 72 | } catch (OWLOntologyCreationException e) { | ||
| 73 | e.printStackTrace(); | ||
| 74 | } catch (UnknownOWLOntologyException e) { | ||
| 75 | e.printStackTrace(); | ||
| 76 | } | ||
| 77 | return ontology; | ||
| 78 | } | ||
| 79 | |||
| 80 | public static OWLOntology loadOntology(String fileName) { | ||
| 81 | return loadOntology(OWLManager.createOWLOntologyManager(), fileName); | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | public static OWLOntology getImportedOntology(OWLOntology tbox, String... dataFiles) throws OWLOntologyCreationException, OWLOntologyStorageException, IOException { | ||
| 86 | if (dataFiles == null || dataFiles.length == 0 || dataFiles.length == 1 && (dataFiles[0] == null || dataFiles[0].isEmpty())) return tbox; | ||
| 87 | |||
| 88 | OWLOntologyManager manager = tbox.getOWLOntologyManager(); | ||
| 89 | OWLOntology importedOntology = manager.createOntology(); | ||
| 90 | |||
| 91 | for (String dataFile: dataFiles) { | ||
| 92 | File data = new File(dataFile); | ||
| 93 | if (!data.exists()) | ||
| 94 | Utility.logError(dataFile.toString() + " doesn't exist."); | ||
| 95 | |||
| 96 | // importDeclaration(manager, importedOntology, tbox.getOntologyID().getOntologyIRI().toString()); | ||
| 97 | importDeclaration(manager, importedOntology, Utility.toFileIRI(getOntologyPath(tbox))); | ||
| 98 | if (data.isDirectory()) { | ||
| 99 | for (File file: data.listFiles()) { | ||
| 100 | importDeclaration(manager, importedOntology, Utility.toFileIRI(file.getCanonicalPath())); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | importDeclaration(manager, importedOntology, Utility.toFileIRI(new File(dataFile).getCanonicalPath())); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | String path = "./imported.owl"; | ||
| 109 | FileOutputStream out = new FileOutputStream(path); | ||
| 110 | manager.saveOntology(importedOntology, out); | ||
| 111 | out.close(); | ||
| 112 | |||
| 113 | System.out.println("In the closure: " + importedOntology.getImportsClosure().size()); | ||
| 114 | for (OWLOntology onto: importedOntology.getImportsClosure()) { | ||
| 115 | System.out.println(onto.getOntologyID().toString()); | ||
| 116 | } | ||
| 117 | manager.removeOntology(importedOntology); | ||
| 118 | manager.removeOntology(tbox); | ||
| 119 | System.out.println("Removed imported and tbox ontology."); | ||
| 120 | OWLOntologyManager newManager = OWLManager.createOWLOntologyManager(); | ||
| 121 | importedOntology = loadOntology(newManager, path); | ||
| 122 | newManager.setOntologyDocumentIRI(importedOntology, IRI.create(Utility.toFileIRI(new File(path).getCanonicalPath()))); | ||
| 123 | System.out.println("In the closure: " + importedOntology.getImportsClosure().size()); | ||
| 124 | for (OWLOntology onto: importedOntology.getImportsClosure()) { | ||
| 125 | System.out.println(onto.getOntologyID().toString()); | ||
| 126 | } | ||
| 127 | return importedOntology; | ||
| 128 | } | ||
| 129 | |||
| 130 | public static OWLOntology getMergedOntology(String ontologyFile, String dataFile) throws OWLOntologyCreationException, IOException, OWLOntologyStorageException { | ||
| 131 | OWLOntology tbox = loadOntology(ontologyFile); | ||
| 132 | OWLOntologyManager manager = tbox.getOWLOntologyManager(); | ||
| 133 | OWLOntology mergedOntology = new OWLOntologyMerger(manager).createMergedOntology(manager, null); | ||
| 134 | for (OWLOntology o: manager.getOntologies()) | ||
| 135 | if (o != mergedOntology) { | ||
| 136 | for (OWLAxiom axiom: o.getAxioms()) | ||
| 137 | manager.addAxiom(mergedOntology, axiom); | ||
| 138 | } | ||
| 139 | Utility.logDebug("loaded and merged the ontology and the dataset: "); | ||
| 140 | return mergedOntology; | ||
| 141 | } | ||
| 142 | |||
| 143 | public static boolean removeAnnotations(OWLOntology inputOntology) { | ||
| 144 | OWLOntologyManager manager = inputOntology.getOWLOntologyManager(); | ||
| 145 | boolean updated = false; | ||
| 146 | for (OWLAxiom axiom: inputOntology.getAxioms()) | ||
| 147 | if (axiom.toString().contains("Annotation")) { | ||
| 148 | manager.removeAxiom(inputOntology, axiom); | ||
| 149 | updated = true; | ||
| 150 | } | ||
| 151 | return updated; | ||
| 152 | } | ||
| 153 | |||
| 154 | public static boolean correctDataTypeRangeAxioms(OWLOntology inputOntology) { | ||
| 155 | OWLOntologyManager manager = inputOntology.getOWLOntologyManager(); | ||
| 156 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 157 | boolean updated = false; | ||
| 158 | OWLClass cls; | ||
| 159 | OWLDatatype datatype; | ||
| 160 | for (OWLOntology onto: inputOntology.getImportsClosure()) | ||
| 161 | for (OWLAxiom axiom: onto.getAxioms()) | ||
| 162 | if (axiom instanceof OWLObjectPropertyRangeAxiom) { | ||
| 163 | OWLObjectPropertyRangeAxiom a = (OWLObjectPropertyRangeAxiom) axiom; | ||
| 164 | if (a.getRange() instanceof OWLClass && a.getProperty() instanceof OWLObjectProperty && (cls = (OWLClass) a.getRange()).toString().contains("datatype")) { | ||
| 165 | manager.removeAxiom(onto, axiom); | ||
| 166 | manager.removeAxiom(onto, factory.getOWLDeclarationAxiom(cls)); | ||
| 167 | |||
| 168 | datatype = factory.getOWLDatatype(cls.getIRI()); | ||
| 169 | manager.addAxiom(onto, factory.getOWLDeclarationAxiom(datatype)); | ||
| 170 | manager.addAxiom(onto, factory.getOWLDataPropertyRangeAxiom( | ||
| 171 | factory.getOWLDataProperty(((OWLObjectProperty) a.getProperty()).getIRI()), | ||
| 172 | datatype)); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | return updated; | ||
| 177 | } | ||
| 178 | |||
| 179 | private static void importDeclaration(OWLOntologyManager manager, OWLOntology ontology, String location) { | ||
| 180 | AddImport change = new AddImport(ontology, manager.getOWLDataFactory().getOWLImportsDeclaration(IRI.create(location))); | ||
| 181 | manager.applyChange(change); | ||
| 182 | } | ||
| 183 | |||
| 184 | public static OWLClassExpression getSimplifiedDisjunction(OWLDataFactory factory, Set<OWLClassExpression> set) { | ||
| 185 | if (set.size() == 0) | ||
| 186 | return factory.getOWLNothing(); | ||
| 187 | else if (set.size() == 1) | ||
| 188 | return set.iterator().next(); | ||
| 189 | else return factory.getOWLObjectUnionOf(set); | ||
| 190 | } | ||
| 191 | |||
| 192 | public static OWLClassExpression getSimplifiedConjunction(OWLDataFactory factory, Set<OWLClassExpression> set) { | ||
| 193 | if (set.size() == 0) | ||
| 194 | return factory.getOWLThing(); | ||
| 195 | else if (set.size() == 1) | ||
| 196 | return set.iterator().next(); | ||
| 197 | else return factory.getOWLObjectIntersectionOf(set); | ||
| 198 | } | ||
| 199 | |||
| 200 | public static OWLAxiom getOWLAxiom(OWLOntology ontology, DLClause dlClause) { | ||
| 201 | OWLAxiom owlAxiom; | ||
| 202 | OWLDataFactory factory = ontology.getOWLOntologyManager().getOWLDataFactory(); | ||
| 203 | |||
| 204 | dlClause = DLClauseHelper.replaceOtherBottom(dlClause); | ||
| 205 | |||
| 206 | if (dlClause.isAtomicConceptInclusion()) { | ||
| 207 | OWLClass subClass = factory.getOWLClass(IRI.create(((AtomicConcept) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 208 | OWLClass superClass = factory.getOWLClass(IRI.create(((AtomicConcept) dlClause.getHeadAtom(0).getDLPredicate()).getIRI())); | ||
| 209 | return factory.getOWLSubClassOfAxiom(subClass, superClass); | ||
| 210 | } | ||
| 211 | else if (dlClause.isAtomicRoleInclusion()) { | ||
| 212 | OWLObjectProperty subProp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 213 | OWLObjectProperty superProp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getHeadAtom(0).getDLPredicate()).getIRI())); | ||
| 214 | return factory.getOWLSubObjectPropertyOfAxiom(subProp, superProp); | ||
| 215 | } | ||
| 216 | else if (dlClause.isAtomicRoleInverseInclusion()) { | ||
| 217 | OWLObjectProperty subProp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 218 | OWLObjectProperty superProp = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getHeadAtom(0).getDLPredicate()).getIRI())); | ||
| 219 | return factory.getOWLSubObjectPropertyOfAxiom(subProp, superProp.getInverseProperty()); | ||
| 220 | } | ||
| 221 | else if (dlClause.isFunctionalityAxiom()) { | ||
| 222 | OWLObjectProperty prop = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 223 | return factory.getOWLFunctionalObjectPropertyAxiom(prop); | ||
| 224 | } | ||
| 225 | else if (dlClause.isInverseFunctionalityAxiom()) { | ||
| 226 | OWLObjectProperty prop = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 227 | return factory.getOWLInverseFunctionalObjectPropertyAxiom(prop); | ||
| 228 | } | ||
| 229 | else if (DLClauseHelper.isFunctionalDataPropertyAxioms(dlClause)) { | ||
| 230 | OWLDataProperty prop = factory.getOWLDataProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 231 | return factory.getOWLFunctionalDataPropertyAxiom(prop); | ||
| 232 | } | ||
| 233 | int flag; | ||
| 234 | if ((flag = DLClauseHelper.isRoleCompositionAxioms(dlClause)) >= 0) { | ||
| 235 | OWLObjectPropertyExpression R = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(0).getDLPredicate()).getIRI())); | ||
| 236 | OWLObjectPropertyExpression S = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getBodyAtom(1).getDLPredicate()).getIRI())); | ||
| 237 | OWLObjectPropertyExpression T = factory.getOWLObjectProperty(IRI.create(((AtomicRole) dlClause.getHeadAtom(0).getDLPredicate()).getIRI())); | ||
| 238 | |||
| 239 | if (flag % 2 != 0) T = T.getInverseProperty(); flag /= 2; | ||
| 240 | if (flag % 2 != 0) S = S.getInverseProperty(); flag /= 2; | ||
| 241 | if (flag % 2 != 0) R = R.getInverseProperty(); | ||
| 242 | if (R.equals(S) && S.equals(T)) | ||
| 243 | return factory.getOWLTransitiveObjectPropertyAxiom(R); | ||
| 244 | else { | ||
| 245 | List<OWLObjectPropertyExpression> list = new LinkedList<OWLObjectPropertyExpression>(); | ||
| 246 | list.add(R); list.add(S); | ||
| 247 | return factory.getOWLSubPropertyChainOfAxiom(list, T); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | Clausifier clausifier = Clausifier.getInstance(ontology); | ||
| 252 | // if (dlClause.isGeneralConceptInclusion()) { | ||
| 253 | |||
| 254 | Clause myClause = new Clause(clausifier, dlClause); | ||
| 255 | owlAxiom = getSimplifiedGCI(factory, myClause.getSubClasses(), myClause.getSuperClasses()); | ||
| 256 | |||
| 257 | |||
| 258 | if (owlAxiom == null) | ||
| 259 | Utility.logError("more kinds of DLClause: " + dlClause.toString()); | ||
| 260 | |||
| 261 | return owlAxiom; | ||
| 262 | } | ||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | private static OWLAxiom getSimplifiedGCI(OWLDataFactory factory, Set<OWLClassExpression> subClasses, Set<OWLClassExpression> superClasses) { | ||
| 267 | if (superClasses.size() > 1) { | ||
| 268 | Set<OWLClassExpression> toRemoved = new HashSet<OWLClassExpression>(); | ||
| 269 | for (OWLClassExpression cls: new HashSet<OWLClassExpression>(superClasses)) | ||
| 270 | if (cls instanceof OWLObjectMaxCardinality) { | ||
| 271 | OWLObjectMaxCardinality maxCard = (OWLObjectMaxCardinality) cls; | ||
| 272 | OWLObjectMinCardinality minCard = factory.getOWLObjectMinCardinality(maxCard.getCardinality() + 1, maxCard.getProperty()); | ||
| 273 | for (OWLClassExpression exp: subClasses) | ||
| 274 | if (isSubsumedByMinCard(exp, minCard)) | ||
| 275 | toRemoved.add(exp); | ||
| 276 | subClasses.add(minCard); | ||
| 277 | superClasses.remove(maxCard); | ||
| 278 | } | ||
| 279 | subClasses.removeAll(toRemoved); | ||
| 280 | } | ||
| 281 | |||
| 282 | return factory.getOWLSubClassOfAxiom(getSimplifiedConjunction(factory, subClasses), getSimplifiedDisjunction(factory, superClasses)); | ||
| 283 | } | ||
| 284 | |||
| 285 | public static OWLAxiom getABoxAssertion(OWLDataFactory factory, Atom atom) { | ||
| 286 | if (atom.getDLPredicate().toString().contains("internal:nom#")) | ||
| 287 | return null; | ||
| 288 | try { | ||
| 289 | if (atom.getArity() == 1) | ||
| 290 | return factory.getOWLClassAssertionAxiom( | ||
| 291 | factory.getOWLClass(IRI.create(((AtomicConcept) atom.getDLPredicate()).getIRI())), | ||
| 292 | factory.getOWLNamedIndividual(IRI.create(((Individual) atom.getArgument(0)).getIRI())) | ||
| 293 | ); | ||
| 294 | else | ||
| 295 | return factory.getOWLObjectPropertyAssertionAxiom( | ||
| 296 | factory.getOWLObjectProperty(IRI.create(((AtomicRole) atom.getDLPredicate()).getIRI())), | ||
| 297 | factory.getOWLNamedIndividual(IRI.create(((Individual) atom.getArgument(0)).getIRI())), | ||
| 298 | factory.getOWLNamedIndividual(IRI.create(((Individual) atom.getArgument(1)).getIRI())) | ||
| 299 | ); | ||
| 300 | } catch (Exception e) { | ||
| 301 | return null; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | private static boolean isSubsumedByMinCard(OWLClassExpression exp, OWLObjectMinCardinality minCard) { | ||
| 306 | if (exp instanceof OWLObjectSomeValuesFrom) { | ||
| 307 | OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) exp; | ||
| 308 | return minCard.getCardinality() > 0 && | ||
| 309 | minCard.getProperty().equals(someValuesFrom.getProperty()) && | ||
| 310 | minCard.getFiller().equals(someValuesFrom.getFiller()); | ||
| 311 | } | ||
| 312 | |||
| 313 | if (exp instanceof OWLObjectMinCardinality) { | ||
| 314 | OWLObjectMinCardinality minCard2 = (OWLObjectMinCardinality) exp; | ||
| 315 | return minCard.getCardinality() >= minCard2.getCardinality() && | ||
| 316 | minCard.getProperty().equals(minCard2.getProperty()) && | ||
| 317 | minCard.getFiller().equals(minCard2.getFiller()); | ||
| 318 | } | ||
| 319 | return false; | ||
| 320 | } | ||
| 321 | |||
| 322 | public static String removeAngles(String quotedString) { | ||
| 323 | if (quotedString.startsWith("<") && quotedString.endsWith(">")) | ||
| 324 | return quotedString.substring(1, quotedString.length() - 1); | ||
| 325 | else if (quotedString.contains(":")) | ||
| 326 | return quotedString; | ||
| 327 | else | ||
| 328 | Utility.logError("paused here due to the action to remove Angle in an unquotedString"); | ||
| 329 | return quotedString; | ||
| 330 | } | ||
| 331 | |||
| 332 | public static String addAngles(String name) { | ||
| 333 | StringBuilder sb = new StringBuilder(); | ||
| 334 | sb.append("<").append(name).append(">"); | ||
| 335 | return sb.toString(); | ||
| 336 | } | ||
| 337 | |||
| 338 | public static boolean isContradiction(OWLDataFactory factory, OWLAxiom axiom) { | ||
| 339 | if (!(axiom instanceof OWLSubClassOfAxiom)) | ||
| 340 | return false; | ||
| 341 | OWLSubClassOfAxiom subClassAxiom = (OWLSubClassOfAxiom) axiom; | ||
| 342 | return subClassAxiom.getSubClass().equals(factory.getOWLThing()) && subClassAxiom.getSuperClass().equals(factory.getOWLNothing()); | ||
| 343 | } | ||
| 344 | |||
| 345 | static MyHornAxiomVisitorEx visitor = new MyHornAxiomVisitorEx(); | ||
| 346 | |||
| 347 | public static boolean isDisjunctiveAxiom(OWLAxiom axiom) { | ||
| 348 | boolean isHorn = false; | ||
| 349 | if (axiom instanceof OWLSubClassOfAxiom) | ||
| 350 | isHorn = visitor.visit((OWLSubClassOfAxiom) axiom); | ||
| 351 | else if (axiom instanceof OWLSubObjectPropertyOfAxiom) | ||
| 352 | isHorn = visitor.visit((OWLSubObjectPropertyOfAxiom) axiom); | ||
| 353 | else if (axiom instanceof OWLInverseObjectPropertiesAxiom) | ||
| 354 | isHorn = visitor.visit((OWLInverseObjectPropertiesAxiom) axiom); | ||
| 355 | else if (axiom instanceof OWLObjectPropertyDomainAxiom) | ||
| 356 | isHorn = visitor.visit((OWLObjectPropertyDomainAxiom) axiom); | ||
| 357 | else if (axiom instanceof OWLFunctionalObjectPropertyAxiom) | ||
| 358 | isHorn = visitor.visit((OWLFunctionalObjectPropertyAxiom) axiom); | ||
| 359 | else if (axiom instanceof OWLInverseFunctionalObjectPropertyAxiom) | ||
| 360 | isHorn = visitor.visit((OWLInverseFunctionalObjectPropertyAxiom) axiom); | ||
| 361 | else { | ||
| 362 | Utility.logError(axiom); | ||
| 363 | } | ||
| 364 | |||
| 365 | if (isHorn) | ||
| 366 | Utility.logDebug(axiom.toString() + " is NOT a disjunctive axiom."); | ||
| 367 | else | ||
| 368 | Utility.logDebug(axiom.toString() + " is a disjunctive axiom."); | ||
| 369 | |||
| 370 | return !isHorn; | ||
| 371 | } | ||
| 372 | |||
| 373 | public boolean isELHO(OWLOntology ontology) { | ||
| 374 | Utility.logDebug("checking whether ELHO ... "); | ||
| 375 | ELHOProfile profile = new ELHOProfile(); | ||
| 376 | OWLProfileReport report = profile.checkOntology(ontology); | ||
| 377 | for (OWLProfileViolation v: report.getViolations()) | ||
| 378 | if (v instanceof UseOfUndeclaredClass || | ||
| 379 | v instanceof UseOfUndeclaredObjectProperty || | ||
| 380 | v instanceof UseOfUndeclaredDataProperty); | ||
| 381 | else { | ||
| 382 | Utility.logDebug(v.toString(), "not ELHO"); | ||
| 383 | return false; | ||
| 384 | } | ||
| 385 | return true; | ||
| 386 | } | ||
| 387 | |||
| 388 | public static void identifyAndChangeAnnotationAssertions(OWLOntology ontology) { | ||
| 389 | OWLOntologyManager manager = ontology.getOWLOntologyManager(); | ||
| 390 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 391 | |||
| 392 | Set<String> objectProperties = new HashSet<String>(); | ||
| 393 | for (OWLOntology onto: ontology.getImportsClosure()) { | ||
| 394 | for (OWLObjectProperty prop: onto.getObjectPropertiesInSignature()) { | ||
| 395 | objectProperties.add(prop.toStringID()); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | Set<OWLAnnotationAssertionAxiom> toRemove = new HashSet<OWLAnnotationAssertionAxiom>(); | ||
| 400 | Set<OWLObjectPropertyAssertionAxiom> toAdd = new HashSet<OWLObjectPropertyAssertionAxiom>(); | ||
| 401 | OWLAnnotationAssertionAxiom assertion; | ||
| 402 | OWLAnnotationSubject anSub; | ||
| 403 | OWLAnnotationValue anValue; | ||
| 404 | |||
| 405 | OWLObjectPropertyAssertionAxiom newAssertion; | ||
| 406 | String property; | ||
| 407 | OWLIndividual sub, obj; | ||
| 408 | |||
| 409 | for (OWLOntology onto: ontology.getImportsClosure()) { | ||
| 410 | for (OWLAxiom axiom: onto.getAxioms()) | ||
| 411 | if (axiom instanceof OWLAnnotationAssertionAxiom) { | ||
| 412 | assertion = (OWLAnnotationAssertionAxiom) axiom; | ||
| 413 | if (objectProperties.contains(property = assertion.getProperty().toStringID())) { | ||
| 414 | if ((anSub = assertion.getSubject()) instanceof OWLAnonymousIndividual) | ||
| 415 | sub = (OWLAnonymousIndividual) anSub; | ||
| 416 | else | ||
| 417 | sub = factory.getOWLNamedIndividual((IRI) anSub); | ||
| 418 | |||
| 419 | if ((anValue = assertion.getValue()) instanceof OWLAnonymousIndividual) | ||
| 420 | obj = (OWLAnonymousIndividual) anValue; | ||
| 421 | else if (anValue instanceof IRI) | ||
| 422 | obj = factory.getOWLNamedIndividual((IRI) anValue); | ||
| 423 | else | ||
| 424 | continue; | ||
| 425 | |||
| 426 | newAssertion = factory.getOWLObjectPropertyAssertionAxiom( | ||
| 427 | factory.getOWLObjectProperty(IRI.create(property)), sub, obj); | ||
| 428 | toRemove.add(assertion); | ||
| 429 | toAdd.add(newAssertion); | ||
| 430 | } | ||
| 431 | } | ||
| 432 | manager.removeAxioms(onto, toRemove); | ||
| 433 | manager.addAxioms(onto, toAdd); | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | public static String getOntologyPath(OWLOntology o) { | ||
| 438 | return getDocumentIRI(o).substring(5); | ||
| 439 | } | ||
| 440 | |||
| 441 | public static String getDocumentIRI(OWLOntology o) { | ||
| 442 | return o.getOWLOntologyManager().getOntologyDocumentIRI(o).toString(); | ||
| 443 | } | ||
| 444 | |||
| 445 | public static boolean isInOWL2RL(OWLOntology o) { | ||
| 446 | OWL2RLProfile profile = new OWL2RLProfile(); | ||
| 447 | return profile.checkOntology(o).isInProfile(); | ||
| 448 | } | ||
| 449 | |||
| 450 | public static boolean isInELHO(OWLOntology o) { | ||
| 451 | ELHOProfile profile = new ELHOProfile(); | ||
| 452 | return profile.checkOntology(o).isInProfile(); | ||
| 453 | } | ||
| 454 | |||
| 455 | public static String getOriginalMarkProgram(OWLOntology ontology) { | ||
| 456 | String originalConcept = Namespace.PAGODA_ORIGINAL; | ||
| 457 | StringBuilder sb = new StringBuilder(); | ||
| 458 | for (OWLDataProperty p: ontology.getDataPropertiesInSignature()) { | ||
| 459 | sb.append("<").append(originalConcept).append(">(?x) :- <").append(p.toStringID()).append(">(?x,?y) .\n"); | ||
| 460 | sb.append("<").append(originalConcept).append(">(?y) :- <").append(p.toStringID()).append(">(?x,?y) .\n"); | ||
| 461 | } | ||
| 462 | for (OWLObjectProperty p: ontology.getObjectPropertiesInSignature()) { | ||
| 463 | sb.append("<").append(originalConcept).append(">(?x) :- <").append(p.toStringID()).append(">(?x,?y) .\n"); | ||
| 464 | sb.append("<").append(originalConcept).append(">(?y) :- <").append(p.toStringID()).append(">(?x,?y) .\n"); | ||
| 465 | } | ||
| 466 | for (OWLClass c: ontology.getClassesInSignature()) | ||
| 467 | sb.append("<").append(originalConcept).append(">(?x) :- <").append(c.toStringID()).append(">(?x) .\n"); | ||
| 468 | return sb.toString(); | ||
| 469 | } | ||
| 470 | |||
| 471 | } | ||
diff --git a/src/main/java/uk/ac/ox/cs/pagoda/owl/QueryRoller.java b/src/main/java/uk/ac/ox/cs/pagoda/owl/QueryRoller.java new file mode 100644 index 0000000..f486bbf --- /dev/null +++ b/src/main/java/uk/ac/ox/cs/pagoda/owl/QueryRoller.java | |||
| @@ -0,0 +1,227 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.owl; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 10 | import org.semanticweb.HermiT.model.Atom; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 13 | import org.semanticweb.HermiT.model.DLClause; | ||
| 14 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 15 | import org.semanticweb.HermiT.model.Equality; | ||
| 16 | import org.semanticweb.HermiT.model.Inequality; | ||
| 17 | import org.semanticweb.HermiT.model.Term; | ||
| 18 | import org.semanticweb.HermiT.model.Variable; | ||
| 19 | import org.semanticweb.owlapi.model.IRI; | ||
| 20 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 22 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 23 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 24 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 25 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 26 | |||
| 27 | import uk.ac.ox.cs.pagoda.summary.Summary; | ||
| 28 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 29 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 30 | |||
| 31 | public class QueryRoller { | ||
| 32 | |||
| 33 | private OWLDataFactory factory; | ||
| 34 | |||
| 35 | protected Map<String, LinkedList<String>> edges, concepts; | ||
| 36 | |||
| 37 | public QueryRoller(OWLDataFactory factory) { | ||
| 38 | this.factory = factory; | ||
| 39 | } | ||
| 40 | |||
| 41 | private OWLClassExpression getOWLClassExpression(String u, String father, Set<String> visited) { | ||
| 42 | visited.add(u); | ||
| 43 | String[] temp; | ||
| 44 | Set<OWLClassExpression> exps = new HashSet<OWLClassExpression>(); | ||
| 45 | OWLObjectPropertyExpression prop; | ||
| 46 | if (concepts.containsKey(u)) | ||
| 47 | for (String concept: concepts.get(u)) { | ||
| 48 | exps.add(factory.getOWLClass(IRI.create(concept))); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (edges.containsKey(u)) | ||
| 52 | for (String pair: edges.get(u)) { | ||
| 53 | temp = pair.split(" "); | ||
| 54 | if (temp[0].startsWith("-")) | ||
| 55 | prop = factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(temp[0].substring(1)))); | ||
| 56 | else | ||
| 57 | prop = factory.getOWLObjectProperty(IRI.create(temp[0])); | ||
| 58 | |||
| 59 | if (temp[1].startsWith("@")) | ||
| 60 | exps.add(factory.getOWLObjectHasValue(prop, factory.getOWLNamedIndividual(IRI.create(OWLHelper.removeAngles(temp[1].substring(1)))))); | ||
| 61 | else if (visited.contains(temp[1])) { | ||
| 62 | if (father != null && father.equals(temp[1])) | ||
| 63 | continue; | ||
| 64 | if (!u.equals(temp[1])) { | ||
| 65 | Utility.logError("The query cannot be rolled up!"); | ||
| 66 | return null; | ||
| 67 | } | ||
| 68 | exps.add(factory.getOWLObjectHasSelf(prop)); | ||
| 69 | } | ||
| 70 | else | ||
| 71 | exps.add(factory.getOWLObjectSomeValuesFrom(prop, getOWLClassExpression(temp[1], u, visited))); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (exps.size() == 0) | ||
| 75 | return factory.getOWLThing(); | ||
| 76 | if (exps.size() == 1) | ||
| 77 | return exps.iterator().next(); | ||
| 78 | else | ||
| 79 | return factory.getOWLObjectIntersectionOf(exps); | ||
| 80 | } | ||
| 81 | |||
| 82 | String currentMainVariable; | ||
| 83 | |||
| 84 | public OWLClassExpression rollUp(DLClause query, String var) { | ||
| 85 | currentMainVariable = var; | ||
| 86 | query = removeSameAs(query); | ||
| 87 | edges = new HashMap<String, LinkedList<String>>(); | ||
| 88 | concepts = new HashMap<String, LinkedList<String>>(); | ||
| 89 | String arg1, arg2, predicate; | ||
| 90 | DLPredicate dlPredicate; | ||
| 91 | Term t1, t2; | ||
| 92 | for (Atom atom: query.getBodyAtoms()) { | ||
| 93 | dlPredicate = atom.getDLPredicate(); | ||
| 94 | if (dlPredicate instanceof AtomicRole) { | ||
| 95 | arg1 = (t1 = atom.getArgument(0)).toString(); | ||
| 96 | arg2 = (t2 = atom.getArgument(1)).toString(); | ||
| 97 | predicate = ((AtomicRole) dlPredicate).getIRI(); | ||
| 98 | if (!predicate.equals(Namespace.RDF_TYPE)) { | ||
| 99 | if (t1 instanceof Variable) | ||
| 100 | if (t2 instanceof Variable) { | ||
| 101 | addEntry(edges, arg1, predicate + " " + arg2); | ||
| 102 | addEntry(edges, arg2, "-" + predicate + " " + arg1); | ||
| 103 | } | ||
| 104 | else | ||
| 105 | addEntry(edges, arg1, predicate + " @" + arg2); | ||
| 106 | else | ||
| 107 | addEntry(edges, arg2, "-" + predicate + " @" + arg1); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | if (t2 instanceof Variable) return null; | ||
| 111 | addEntry(concepts, arg1, arg2); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | else | ||
| 115 | addEntry(concepts, atom.getArgument(0).toString(), ((AtomicConcept) dlPredicate).getIRI()); | ||
| 116 | } | ||
| 117 | |||
| 118 | return getOWLClassExpression(var, null, new HashSet<String>()); | ||
| 119 | } | ||
| 120 | |||
| 121 | private DLClause removeSameAs(DLClause query) { | ||
| 122 | int equalityStatement = 0; | ||
| 123 | |||
| 124 | Map<Term, Term> ufs = new HashMap<Term, Term>(); | ||
| 125 | for (Atom atom: query.getBodyAtoms()) | ||
| 126 | if (isEquality(atom.getDLPredicate())) { | ||
| 127 | ++equalityStatement; | ||
| 128 | merge(ufs, atom.getArgument(0), atom.getArgument(1)); | ||
| 129 | } | ||
| 130 | |||
| 131 | if (equalityStatement == 0) return query; | ||
| 132 | |||
| 133 | Atom[] bodyAtoms = new Atom[query.getBodyLength() - equalityStatement]; | ||
| 134 | int index = 0; | ||
| 135 | for (Atom atom: query.getBodyAtoms()) | ||
| 136 | if (!isEquality(atom.getDLPredicate())) { | ||
| 137 | if (atom.getArity() == 1) | ||
| 138 | bodyAtoms[index++] = Atom.create(atom.getDLPredicate(), find(ufs, atom.getArgument(0))); | ||
| 139 | else | ||
| 140 | bodyAtoms[index++] = Atom.create(atom.getDLPredicate(), find(ufs, atom.getArgument(0)), find (ufs, atom.getArgument(1))); | ||
| 141 | } | ||
| 142 | |||
| 143 | return DLClause.create(query.getHeadAtoms(), bodyAtoms); | ||
| 144 | } | ||
| 145 | |||
| 146 | private boolean isEquality(DLPredicate p) { | ||
| 147 | return p instanceof Equality || p instanceof AnnotatedEquality || p instanceof Inequality | ||
| 148 | || p.toString().equals(Namespace.EQUALITY_QUOTED) | ||
| 149 | || p.toString().equals(Namespace.EQUALITY_ABBR); | ||
| 150 | } | ||
| 151 | |||
| 152 | private Term find(Map<Term, Term> ufs, Term u) { | ||
| 153 | Term v = u, w; | ||
| 154 | while ((w = ufs.get(v)) != null) v = w; | ||
| 155 | while ((w = ufs.get(u)) != null) { | ||
| 156 | ufs.put(u, v); | ||
| 157 | u = w; | ||
| 158 | } | ||
| 159 | return v; | ||
| 160 | } | ||
| 161 | |||
| 162 | private void merge(Map<Term, Term> ufs, Term u, Term v) { | ||
| 163 | u = find(ufs, u); | ||
| 164 | v = find(ufs, v); | ||
| 165 | if (compare(u, v) <= 0) ufs.put(v, u); | ||
| 166 | else ufs.put(u, v); | ||
| 167 | } | ||
| 168 | |||
| 169 | private int compare(Term u, Term v) { | ||
| 170 | int ret = rank(u) - rank(v); | ||
| 171 | if (ret != 0) return ret; | ||
| 172 | else | ||
| 173 | return u.toString().compareTo(v.toString()); | ||
| 174 | } | ||
| 175 | |||
| 176 | private int rank(Term u) { | ||
| 177 | if (u instanceof Variable) { | ||
| 178 | Variable v = (Variable) u; | ||
| 179 | if (v.getName().equals(currentMainVariable)) | ||
| 180 | return 0; | ||
| 181 | else | ||
| 182 | return 2; | ||
| 183 | } | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | private void addEntry(Map<String, LinkedList<String>> map, String key, String value) { | ||
| 188 | LinkedList<String> list; | ||
| 189 | if ((list = map.get(key)) == null) { | ||
| 190 | list = new LinkedList<String>(); | ||
| 191 | map.put(key, list); | ||
| 192 | } | ||
| 193 | list.add(value); | ||
| 194 | } | ||
| 195 | |||
| 196 | @Deprecated | ||
| 197 | public OWLClassExpression summarise(Summary sum, OWLClassExpression exp) { | ||
| 198 | if (exp == null) return null; | ||
| 199 | |||
| 200 | Set<OWLClassExpression> exps = exp.asConjunctSet(); | ||
| 201 | if (exps.size() == 1) { | ||
| 202 | OWLClassExpression tempExp = exps.iterator().next(); | ||
| 203 | |||
| 204 | // TODO reference: getOWLClassExpression(String) | ||
| 205 | |||
| 206 | if (tempExp instanceof OWLObjectHasValue) { | ||
| 207 | OWLObjectHasValue hasValue = (OWLObjectHasValue) tempExp; | ||
| 208 | OWLNamedIndividual individual = sum.getRepresentativeIndividual(hasValue.getValue().toStringID()); | ||
| 209 | return factory.getOWLObjectHasValue(hasValue.getProperty(), individual); | ||
| 210 | |||
| 211 | } | ||
| 212 | if (tempExp instanceof OWLObjectSomeValuesFrom) { | ||
| 213 | OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) tempExp; | ||
| 214 | return factory.getOWLObjectSomeValuesFrom(someValuesFrom.getProperty(), summarise(sum, someValuesFrom.getFiller())); | ||
| 215 | } | ||
| 216 | return tempExp; | ||
| 217 | } | ||
| 218 | |||
| 219 | Set<OWLClassExpression> newExps = new HashSet<OWLClassExpression>(); | ||
| 220 | for (OWLClassExpression clsExp: exps) | ||
| 221 | newExps.add(summarise(sum, clsExp)); | ||
| 222 | |||
| 223 | return factory.getOWLObjectIntersectionOf(newExps); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | } | ||
