aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java')
-rw-r--r--src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java261
1 files changed, 261 insertions, 0 deletions
diff --git a/src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java b/src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java
new file mode 100644
index 0000000..23761f0
--- /dev/null
+++ b/src/main/java/org/semanticweb/karma2/profile/ELHOProfile.java
@@ -0,0 +1,261 @@
1package org.semanticweb.karma2.profile;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import org.semanticweb.owlapi.model.AxiomType;
8import org.semanticweb.owlapi.model.IRI;
9import org.semanticweb.owlapi.model.OWLAxiom;
10import org.semanticweb.owlapi.model.OWLClassExpression;
11import org.semanticweb.owlapi.model.OWLDataHasValue;
12import org.semanticweb.owlapi.model.OWLDataIntersectionOf;
13import org.semanticweb.owlapi.model.OWLDataOneOf;
14import org.semanticweb.owlapi.model.OWLDataProperty;
15import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
16import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
17import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
18import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom;
19import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
20import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
21import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
22import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
23import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
24import org.semanticweb.owlapi.model.OWLObjectHasSelf;
25import org.semanticweb.owlapi.model.OWLObjectMinCardinality;
26import org.semanticweb.owlapi.model.OWLObjectOneOf;
27import org.semanticweb.owlapi.model.OWLOntology;
28import org.semanticweb.owlapi.model.OWLOntologyCreationException;
29import org.semanticweb.owlapi.model.OWLOntologyManager;
30import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
31import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom;
32import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom;
33import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
34import org.semanticweb.owlapi.profiles.OWL2ELProfile;
35import org.semanticweb.owlapi.profiles.OWLProfile;
36import org.semanticweb.owlapi.profiles.OWLProfileReport;
37import org.semanticweb.owlapi.profiles.OWLProfileViolation;
38import org.semanticweb.owlapi.profiles.violations.UseOfDataOneOfWithMultipleLiterals;
39import org.semanticweb.owlapi.profiles.violations.UseOfIllegalAxiom;
40import org.semanticweb.owlapi.profiles.violations.UseOfIllegalClassExpression;
41import org.semanticweb.owlapi.profiles.violations.UseOfObjectOneOfWithMultipleIndividuals;
42import org.semanticweb.owlapi.util.OWLObjectPropertyManager;
43import org.semanticweb.owlapi.util.OWLOntologyWalker;
44import org.semanticweb.owlapi.util.OWLOntologyWalkerVisitor;
45import uk.ac.ox.cs.pagoda.util.Utility;
46
47public class ELHOProfile implements OWLProfile {
48
49 public OWLOntology getFragment(OWLOntology ontology) {
50 OWLOntologyManager manager = ontology.getOWLOntologyManager();
51 OWLOntology elhoOntology = null;
52 try {
53 Utility.logDebug("OntologyID: " + ontology.getOntologyID());
54 try {
55 String ontologyIRI = ontology.getOntologyID().getOntologyIRI().toString();
56 if (ontologyIRI.contains(".owl"))
57 ontologyIRI = ontologyIRI.replace(".owl", "-elho.owl");
58 else
59 ontologyIRI = ontologyIRI + "elho";
60 elhoOntology = manager.createOntology(IRI.create(ontologyIRI));
61 } catch (NullPointerException e) {
62// e.printStackTrace();
63 elhoOntology = manager.createOntology();
64 }
65
66 } catch (OWLOntologyCreationException e) {
67 e.printStackTrace();
68 }
69 for (OWLOntology onto: ontology.getImportsClosure())
70 manager.addAxioms(elhoOntology, onto.getAxioms());
71
72 // TODO to be checked ...
73 manager.removeAxioms(elhoOntology, elhoOntology.getAxioms(AxiomType.DIFFERENT_INDIVIDUALS));
74
75 OWLProfileReport report = checkOntology(elhoOntology);
76
77 for (OWLProfileViolation violation: report.getViolations()) {
78 OWLAxiom axiom = violation.getAxiom();
79 manager.removeAxiom(elhoOntology, axiom);
80 }
81 Utility.logDebug("ELHO fragment extracted ... ");
82
83 return elhoOntology;
84 }
85
86 @Override
87 public OWLProfileReport checkOntology(OWLOntology ontology) {
88 OWL2ELProfile profile = new OWL2ELProfile();
89 OWLProfileReport report = profile.checkOntology(ontology);
90 Set<OWLProfileViolation> violations = new HashSet<OWLProfileViolation>();
91 violations.addAll(report.getViolations());
92 MyOWLOntologyWalker ontologyWalker = new MyOWLOntologyWalker(ontology.getImportsClosure());
93 ELHOProfileObjectVisitor visitor = new ELHOProfileObjectVisitor(ontologyWalker, ontology.getOWLOntologyManager());
94 ontologyWalker.walkStructure(visitor);
95
96 for (Iterator<OWLProfileViolation> iter = violations.iterator(); iter.hasNext(); ) {
97 OWLProfileViolation vio = iter.next();
98 if (vio instanceof UseOfIllegalClassExpression) {
99 OWLClassExpression exp = ((UseOfIllegalClassExpression) vio).getExpression();
100 if (exp instanceof OWLObjectMinCardinality && ((OWLObjectMinCardinality) exp).getCardinality() == 1)
101 iter.remove();
102 }
103 }
104
105 violations.addAll(visitor.getProfileViolations());
106 return new OWLProfileReport(this, violations);
107 }
108
109 @Override
110 public String getName() {
111 return "ELHO";
112 }
113
114 protected class ELHOProfileObjectVisitor extends OWLOntologyWalkerVisitor {
115
116 private final OWLOntologyManager man;
117
118 private OWLObjectPropertyManager propertyManager;
119
120 private final Set<OWLProfileViolation> profileViolations = new HashSet<OWLProfileViolation>();
121
122 public ELHOProfileObjectVisitor(OWLOntologyWalker walker, OWLOntologyManager man) {
123 super(walker);
124 this.man = man;
125 }
126
127 public Set<OWLProfileViolation> getProfileViolations() {
128 return new HashSet<OWLProfileViolation>(profileViolations);
129 }
130
131 @SuppressWarnings("unused")
132 private OWLObjectPropertyManager getPropertyManager() {
133 if (propertyManager == null) {
134 propertyManager = new OWLObjectPropertyManager(getCurrentOntology());
135 }
136 return propertyManager;
137 }
138
139
140
141 @Override
142 public void visit(OWLDataProperty p) {
143 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
144 }
145
146
147 @Override
148 public void visit(OWLObjectOneOf desc) {
149 if (desc.getIndividuals().size() != 1) {
150 profileViolations.add(new UseOfObjectOneOfWithMultipleIndividuals(getCurrentOntology(), getCurrentAxiom(), desc));
151 }
152 }
153
154 @Override
155 public void visit(OWLDataHasValue desc) {
156 profileViolations.add(new UseOfIllegalClassExpression(getCurrentOntology(), getCurrentAxiom(), desc));
157 }
158
159 @Override
160 public void visit(OWLDataSomeValuesFrom desc) {
161 profileViolations.add(new UseOfIllegalClassExpression(getCurrentOntology(), getCurrentAxiom(), desc));
162 }
163
164 @Override
165 public void visit(OWLDataIntersectionOf desc) {
166 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
167 }
168
169 @Override
170 public void visit(OWLSubDataPropertyOfAxiom desc) {
171 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
172 }
173
174 @Override
175 public void visit(OWLEquivalentDataPropertiesAxiom desc) {
176 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
177 }
178
179 @Override
180 public void visit(OWLTransitiveObjectPropertyAxiom desc) {
181 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
182 }
183
184 @Override
185 public void visit(OWLReflexiveObjectPropertyAxiom desc) {
186 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
187 }
188
189 @Override
190 public void visit(OWLDataPropertyDomainAxiom desc) {
191 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
192 }
193
194 @Override
195 public void visit(OWLDataPropertyRangeAxiom desc) {
196 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
197 }
198
199
200
201 @Override
202 public void visit(OWLDataPropertyAssertionAxiom desc) {
203 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
204 }
205
206 @Override
207 public void visit(OWLNegativeDataPropertyAssertionAxiom desc) {
208 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
209 }
210
211 @Override
212 public void visit(OWLNegativeObjectPropertyAssertionAxiom desc) {
213 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
214 }
215
216 @Override
217 public void visit(OWLFunctionalDataPropertyAxiom desc) {
218 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
219 }
220
221 @Override
222 public void visit(OWLHasKeyAxiom desc) {
223 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
224 }
225
226
227
228
229 @Override
230 public void visit(OWLObjectHasSelf node) {
231 profileViolations.add(new UseOfIllegalClassExpression(getCurrentOntology(), getCurrentAxiom(), node));
232 }
233
234
235 @Override
236 public void visit(OWLDataOneOf node) {
237 profileViolations.add(new UseOfDataOneOfWithMultipleLiterals(getCurrentOntology(), getCurrentAxiom(), node));
238 }
239
240
241
242 @Override
243 public void visit(OWLSubPropertyChainOfAxiom axiom) {
244 profileViolations.add(new UseOfIllegalAxiom(getCurrentOntology(), getCurrentAxiom()));
245
246 }
247
248 @Override
249 public void visit(OWLOntology ontology) {
250 propertyManager = null;
251 }
252 }
253
254 @Override
255 public IRI getIRI() {
256 return null;
257 }
258
259
260}
261