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/owl/EqualitiesEliminator.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java b/src/uk/ac/ox/cs/pagoda/owl/EqualitiesEliminator.java new file mode 100644 index 0000000..50865d4 --- /dev/null +++ b/src/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 | } | ||
