001 /**
002 * Copyright (C) 2007-2011, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
018 */
019
020 package org.dllearner.utilities.owl;
021
022 import static org.dllearner.utilities.owl.OWLAPIDescriptionConvertVisitor.getOWLClassExpression;
023
024 import java.util.HashSet;
025 import java.util.Set;
026
027 import org.dllearner.core.owl.AsymmetricObjectPropertyAxiom;
028 import org.dllearner.core.owl.Axiom;
029 import org.dllearner.core.owl.AxiomVisitor;
030 import org.dllearner.core.owl.BooleanDatatypePropertyAssertion;
031 import org.dllearner.core.owl.ClassAssertionAxiom;
032 import org.dllearner.core.owl.DataRange;
033 import org.dllearner.core.owl.Datatype;
034 import org.dllearner.core.owl.DatatypePropertyDomainAxiom;
035 import org.dllearner.core.owl.DatatypePropertyRangeAxiom;
036 import org.dllearner.core.owl.Description;
037 import org.dllearner.core.owl.DifferentIndividualsAxiom;
038 import org.dllearner.core.owl.DisjointClassesAxiom;
039 import org.dllearner.core.owl.DisjointDatatypePropertyAxiom;
040 import org.dllearner.core.owl.DisjointObjectPropertyAxiom;
041 import org.dllearner.core.owl.DoubleDatatypePropertyAssertion;
042 import org.dllearner.core.owl.EquivalentClassesAxiom;
043 import org.dllearner.core.owl.EquivalentDatatypePropertiesAxiom;
044 import org.dllearner.core.owl.EquivalentObjectPropertiesAxiom;
045 import org.dllearner.core.owl.FunctionalDatatypePropertyAxiom;
046 import org.dllearner.core.owl.FunctionalObjectPropertyAxiom;
047 import org.dllearner.core.owl.Individual;
048 import org.dllearner.core.owl.InverseFunctionalObjectPropertyAxiom;
049 import org.dllearner.core.owl.InverseObjectPropertyAxiom;
050 import org.dllearner.core.owl.IrreflexiveObjectPropertyAxiom;
051 import org.dllearner.core.owl.KB;
052 import org.dllearner.core.owl.ObjectPropertyAssertion;
053 import org.dllearner.core.owl.ObjectPropertyDomainAxiom;
054 import org.dllearner.core.owl.ObjectPropertyRangeAxiom;
055 import org.dllearner.core.owl.ReflexiveObjectPropertyAxiom;
056 import org.dllearner.core.owl.StringDatatypePropertyAssertion;
057 import org.dllearner.core.owl.SubClassAxiom;
058 import org.dllearner.core.owl.SubDatatypePropertyAxiom;
059 import org.dllearner.core.owl.SubObjectPropertyAxiom;
060 import org.dllearner.core.owl.SymmetricObjectPropertyAxiom;
061 import org.dllearner.core.owl.TransitiveObjectPropertyAxiom;
062 import org.semanticweb.owlapi.apibinding.OWLManager;
063 import org.semanticweb.owlapi.model.AddAxiom;
064 import org.semanticweb.owlapi.model.IRI;
065 import org.semanticweb.owlapi.model.OWLAxiom;
066 import org.semanticweb.owlapi.model.OWLClassExpression;
067 import org.semanticweb.owlapi.model.OWLDataFactory;
068 import org.semanticweb.owlapi.model.OWLDataProperty;
069 import org.semanticweb.owlapi.model.OWLDatatype;
070 import org.semanticweb.owlapi.model.OWLIndividual;
071 import org.semanticweb.owlapi.model.OWLLiteral;
072 import org.semanticweb.owlapi.model.OWLObjectProperty;
073 import org.semanticweb.owlapi.model.OWLOntology;
074 import org.semanticweb.owlapi.model.OWLOntologyChangeException;
075 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
076 import org.semanticweb.owlapi.model.OWLOntologyManager;
077
078 /**
079 * A converter from DL-Learner axioms to OWL API axioms based on the visitor
080 * pattern.
081 *
082 * TODO: Investigate whether OWLOntologyManager and OWLOntology should be
083 * removed as parameters. It would be natural to have a DL-Learner KB as input
084 * and an OWL API OWLOntology as output.
085 *
086 * @author Jens Lehmann
087 *
088 */
089 public class OWLAPIAxiomConvertVisitor implements AxiomVisitor {
090
091 OWLDataFactory factory;
092 private OWLOntology ontology;
093 private OWLOntologyManager manager;
094 private OWLAxiom lastAxiom;
095
096 /**
097 * Creates a default visitor with ontology URI "http://example.com"
098 * and default ontology manager.
099 */
100 public OWLAPIAxiomConvertVisitor() {
101 manager = OWLManager.createOWLOntologyManager();
102 IRI ontologyIRI = IRI.create("http://example.com");
103 try {
104 ontology = manager.createOntology(ontologyIRI);
105 } catch (OWLOntologyCreationException e) {
106 e.printStackTrace();
107 }
108 factory = manager.getOWLDataFactory();
109 }
110
111 public OWLAPIAxiomConvertVisitor(OWLOntologyManager manager, OWLOntology ontology) {
112 this.manager = manager;
113 this.ontology = ontology;
114 factory = manager.getOWLDataFactory();
115 }
116
117 public static void fillOWLOntology(OWLOntologyManager manager, OWLOntology ontology, KB kb) {
118 OWLAPIAxiomConvertVisitor converter = new OWLAPIAxiomConvertVisitor(manager, ontology);
119 for(Axiom axiom : kb.getTbox())
120 axiom.accept(converter);
121 for(Axiom axiom : kb.getRbox())
122 axiom.accept(converter);
123 for(Axiom axiom : kb.getAbox())
124 axiom.accept(converter);
125 }
126
127 public static OWLAxiom convertAxiom(Axiom axiom) {
128 OWLAPIAxiomConvertVisitor converter = new OWLAPIAxiomConvertVisitor();
129 axiom.accept(converter);
130 return converter.lastAxiom;
131 }
132
133 // convencience function for adding an axiom to the ontology
134 private void addAxiom(OWLAxiom axiom) {
135 AddAxiom addAxiom = new AddAxiom(ontology, axiom);
136 try {
137 manager.applyChange(addAxiom);
138 } catch (OWLOntologyChangeException e) {
139 e.printStackTrace();
140 }
141 lastAxiom = axiom;
142 }
143
144 /*
145 * (non-Javadoc)
146 *
147 * @see org.dllearner.core.dl.AxiomVisitor#visit(org.dllearner.core.dl.ObjectPropertyAssertion)
148 */
149 public void visit(ObjectPropertyAssertion axiom) {
150 OWLObjectProperty role = factory.getOWLObjectProperty(IRI
151 .create(((ObjectPropertyAssertion) axiom).getRole().getName()));
152 OWLIndividual i1 = factory.getOWLNamedIndividual(IRI.create(((ObjectPropertyAssertion) axiom)
153 .getIndividual1().getName()));
154 OWLIndividual i2 = factory.getOWLNamedIndividual(IRI.create(((ObjectPropertyAssertion) axiom)
155 .getIndividual2().getName()));
156 OWLAxiom axiomOWLAPI = factory.getOWLObjectPropertyAssertionAxiom(role, i1, i2);
157 addAxiom(axiomOWLAPI);
158 }
159
160 /*
161 * (non-Javadoc)
162 *
163 * @see org.dllearner.core.owl.AssertionalAxiomVisitor#visit(org.dllearner.core.owl.ClassAssertionAxiom)
164 */
165 public void visit(ClassAssertionAxiom axiom) {
166 OWLClassExpression d = getOWLClassExpression(axiom.getConcept());
167 OWLIndividual i = factory.getOWLNamedIndividual(IRI.create(((ClassAssertionAxiom) axiom)
168 .getIndividual().getName()));
169 OWLAxiom axiomOWLAPI = factory.getOWLClassAssertionAxiom(d, i);
170 addAxiom(axiomOWLAPI);
171 }
172
173 /*
174 * (non-Javadoc)
175 *
176 * @see org.dllearner.core.owl.AssertionalAxiomVisitor#visit(org.dllearner.core.owl.DoubleDatatypePropertyAssertion)
177 */
178 public void visit(DoubleDatatypePropertyAssertion axiom) {
179 OWLIndividual i = factory.getOWLNamedIndividual(IRI.create(axiom.getIndividual().getName()));
180 OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(axiom.getDatatypeProperty().getName()));
181 double value = axiom.getValue();
182 OWLLiteral valueConstant = factory.getOWLLiteral(value);
183 OWLAxiom axiomOWLAPI = factory.getOWLDataPropertyAssertionAxiom(dp, i, valueConstant);
184 addAxiom(axiomOWLAPI);
185 }
186
187 /* (non-Javadoc)
188 * @see org.dllearner.core.owl.AssertionalAxiomVisitor#visit(org.dllearner.core.owl.BooleanDatatypePropertyAssertion)
189 */
190 public void visit(BooleanDatatypePropertyAssertion axiom) {
191 OWLIndividual i = factory.getOWLNamedIndividual(IRI.create(axiom.getIndividual().getName()));
192 OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(axiom.getDatatypeProperty().getName()));
193 boolean value = axiom.getValue();
194 OWLLiteral valueConstant = factory.getOWLLiteral(value);
195 OWLAxiom axiomOWLAPI = factory.getOWLDataPropertyAssertionAxiom(dp, i, valueConstant);
196 addAxiom(axiomOWLAPI);
197 }
198
199 /*
200 * (non-Javadoc)
201 *
202 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.FunctionalObjectPropertyAxiom)
203 */
204 public void visit(FunctionalObjectPropertyAxiom axiom) {
205 OWLObjectProperty role = factory.getOWLObjectProperty(
206 IRI.create(((FunctionalObjectPropertyAxiom) axiom).getRole().getName()));
207 OWLAxiom axiomOWLAPI = factory.getOWLFunctionalObjectPropertyAxiom(role);
208 addAxiom(axiomOWLAPI);
209 }
210
211 /*
212 * (non-Javadoc)
213 *
214 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.InverseObjectPropertyAxiom)
215 */
216 public void visit(InverseObjectPropertyAxiom axiom) {
217 OWLObjectProperty role = factory.getOWLObjectProperty(
218 IRI.create(((InverseObjectPropertyAxiom) axiom).getRole().getName()));
219 OWLObjectProperty inverseRole = factory.getOWLObjectProperty(
220 IRI.create(((InverseObjectPropertyAxiom) axiom).getInverseRole().getName()));
221 OWLAxiom axiomOWLAPI = factory.getOWLInverseObjectPropertiesAxiom(role, inverseRole);
222 addAxiom(axiomOWLAPI);
223 }
224
225 /*
226 * (non-Javadoc)
227 *
228 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.SymmetricObjectPropertyAxiom)
229 */
230 public void visit(SymmetricObjectPropertyAxiom axiom) {
231 OWLObjectProperty role = factory.getOWLObjectProperty(
232 IRI.create(((SymmetricObjectPropertyAxiom) axiom).getRole().getName()));
233 OWLAxiom axiomOWLAPI = factory.getOWLSymmetricObjectPropertyAxiom(role);
234 addAxiom(axiomOWLAPI);
235 }
236
237 /*
238 * (non-Javadoc)
239 *
240 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.TransitiveObjectPropertyAxiom)
241 */
242 public void visit(TransitiveObjectPropertyAxiom axiom) {
243 OWLObjectProperty role = factory.getOWLObjectProperty(
244 IRI.create(axiom.getRole().getName()));
245 OWLAxiom axiomOWLAPI = factory.getOWLTransitiveObjectPropertyAxiom(role);
246 addAxiom(axiomOWLAPI);
247 }
248
249 @Override
250 public void visit(ReflexiveObjectPropertyAxiom axiom) {
251 OWLObjectProperty role = factory.getOWLObjectProperty(
252 IRI.create(axiom.getRole().getName()));
253 OWLAxiom axiomOWLAPI = factory.getOWLFunctionalObjectPropertyAxiom(role);
254 addAxiom(axiomOWLAPI);
255 }
256
257 /* (non-Javadoc)
258 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.SubObjectPropertyAxiom)
259 */
260 public void visit(SubObjectPropertyAxiom axiom) {
261 OWLObjectProperty role = factory.getOWLObjectProperty(
262 IRI.create(((SubObjectPropertyAxiom) axiom).getRole().getName()));
263 OWLObjectProperty subRole = factory.getOWLObjectProperty(
264 IRI.create(((SubObjectPropertyAxiom) axiom).getSubRole().getName()));
265 OWLAxiom axiomOWLAPI = factory.getOWLSubObjectPropertyOfAxiom(subRole, role);
266 addAxiom(axiomOWLAPI);
267 }
268
269 @Override
270 public void visit(EquivalentObjectPropertiesAxiom axiom) {
271 OWLObjectProperty role = factory.getOWLObjectProperty(
272 IRI.create(axiom.getRole().getName()));
273 OWLObjectProperty equivRole = factory.getOWLObjectProperty(
274 IRI.create(axiom.getEquivalentRole().getName()));
275 OWLAxiom axiomOWLAPI = factory.getOWLEquivalentObjectPropertiesAxiom(equivRole, role);
276 addAxiom(axiomOWLAPI);
277
278 }
279
280 @Override
281 public void visit(EquivalentDatatypePropertiesAxiom axiom) {
282 OWLDataProperty role = factory.getOWLDataProperty(
283 IRI.create(axiom.getRole().getName()));
284 OWLDataProperty equivRole = factory.getOWLDataProperty(
285 IRI.create(axiom.getEquivalentRole().getName()));
286 OWLAxiom axiomOWLAPI = factory.getOWLEquivalentDataPropertiesAxiom(equivRole, role);
287 addAxiom(axiomOWLAPI);
288
289 }
290
291 /*
292 * (non-Javadoc)
293 *
294 * @see org.dllearner.core.owl.TerminologicalAxiomVisitor#visit(org.dllearner.core.owl.EquivalentClassesAxiom)
295 */
296 public void visit(EquivalentClassesAxiom axiom) {
297 OWLClassExpression d1 = getOWLClassExpression(axiom.getConcept1());
298 OWLClassExpression d2 = getOWLClassExpression(axiom.getConcept2());
299 Set<OWLClassExpression> ds = new HashSet<OWLClassExpression>();
300 ds.add(d1);
301 ds.add(d2);
302 OWLAxiom axiomOWLAPI = factory.getOWLEquivalentClassesAxiom(ds);
303 addAxiom(axiomOWLAPI);
304 }
305
306 /*
307 * (non-Javadoc)
308 *
309 * @see org.dllearner.core.owl.TerminologicalAxiomVisitor#visit(org.dllearner.core.owl.SubClassAxiom)
310 */
311 public void visit(SubClassAxiom axiom) {
312 OWLClassExpression d1 = getOWLClassExpression(axiom.getSubConcept());
313 OWLClassExpression d2 = getOWLClassExpression(axiom.getSuperConcept());
314 Set<OWLClassExpression> ds = new HashSet<OWLClassExpression>();
315 ds.add(d1);
316 ds.add(d2);
317 OWLAxiom axiomOWLAPI = factory.getOWLSubClassOfAxiom(d1,d2);
318 addAxiom(axiomOWLAPI);
319 }
320
321 /* (non-Javadoc)
322 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.DatatypePropertyDomainAxiom)
323 */
324 public void visit(DatatypePropertyDomainAxiom datatypePropertyDomainAxiom) {
325 OWLClassExpression d = getOWLClassExpression(datatypePropertyDomainAxiom.getDomain());
326 OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(datatypePropertyDomainAxiom.getProperty().getName()));
327 OWLAxiom axiomOWLAPI = factory.getOWLDataPropertyDomainAxiom(dp, d);
328 addAxiom(axiomOWLAPI);
329 }
330
331 /* (non-Javadoc)
332 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.ObjectPropertyDomainAxiom)
333 */
334 public void visit(ObjectPropertyDomainAxiom objectPropertyDomainAxiom) {
335 OWLClassExpression d = getOWLClassExpression(objectPropertyDomainAxiom.getDomain());
336 OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(objectPropertyDomainAxiom.getProperty().getName()));
337 OWLAxiom axiomOWLAPI = factory.getOWLObjectPropertyDomainAxiom(op, d);
338 addAxiom(axiomOWLAPI);
339 }
340
341 /* (non-Javadoc)
342 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.DatatypePropertyRangeAxiom)
343 */
344 public void visit(DatatypePropertyRangeAxiom axiom) {
345 DataRange dr = axiom.getRange();
346 Datatype dt = (Datatype) dr;
347 OWLDatatype odt = factory.getOWLDatatype(IRI.create(dt.getURI()));
348 OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(axiom.getProperty().getName()));
349 OWLAxiom axiomOWLAPI = factory.getOWLDataPropertyRangeAxiom(dp, odt);
350 addAxiom(axiomOWLAPI);
351 }
352
353 /* (non-Javadoc)
354 * @see org.dllearner.core.owl.PropertyAxiomVisitor#visit(org.dllearner.core.owl.ObjectPropertyRangeAxiom)
355 */
356 public void visit(ObjectPropertyRangeAxiom axiom) {
357 OWLClassExpression d = getOWLClassExpression(axiom.getRange());
358 OWLObjectProperty op = factory.getOWLObjectProperty(IRI.create(axiom.getProperty().getName()));
359 OWLAxiom axiomOWLAPI = factory.getOWLObjectPropertyRangeAxiom(op, d);
360 addAxiom(axiomOWLAPI);
361 }
362
363 /* (non-Javadoc)
364 * @see org.dllearner.core.owl.AssertionalAxiomVisitor#visit(org.dllearner.core.owl.DifferentIndividualsAxiom)
365 */
366 public void visit(DifferentIndividualsAxiom axiom) {
367 Set<Individual> individuals = axiom.getIndividuals();
368 Set<OWLIndividual> owlAPIIndividuals = new HashSet<OWLIndividual>();
369 for(Individual individual : individuals)
370 owlAPIIndividuals.add(factory.getOWLNamedIndividual(IRI.create(individual.getName())));
371 OWLAxiom axiomOWLAPI = factory.getOWLDifferentIndividualsAxiom(owlAPIIndividuals);
372 addAxiom(axiomOWLAPI);
373 }
374
375 /* (non-Javadoc)
376 * @see org.dllearner.core.owl.TerminologicalAxiomVisitor#visit(org.dllearner.core.owl.DisjointClassesAxiom)
377 */
378 public void visit(DisjointClassesAxiom axiom) {
379 Set<Description> descriptions = axiom.getDescriptions();
380 Set<OWLClassExpression> owlAPIDescriptions = new HashSet<OWLClassExpression>();
381 for(Description description : descriptions)
382 owlAPIDescriptions.add(getOWLClassExpression(description));
383 OWLAxiom axiomOWLAPI = factory.getOWLDisjointClassesAxiom(owlAPIDescriptions);
384 addAxiom(axiomOWLAPI);
385 }
386
387 /* (non-Javadoc)
388 * @see org.dllearner.core.owl.AssertionalAxiomVisitor#visit(org.dllearner.core.owl.StringDatatypePropertyAssertion)
389 */
390 @Override
391 public void visit(StringDatatypePropertyAssertion axiom) {
392 //throw new UnsupportedOperationException("String datatype conversion not implemented");
393 OWLIndividual i = factory.getOWLNamedIndividual(IRI.create(axiom.getIndividual().getName()));
394 OWLDataProperty dp = factory.getOWLDataProperty(IRI.create(axiom.getDatatypeProperty().getName()));
395 String value = axiom.getValue();
396 OWLLiteral valueConstant = factory.getOWLLiteral(value);
397 OWLAxiom axiomOWLAPI = factory.getOWLDataPropertyAssertionAxiom(dp, i, valueConstant);
398 addAxiom(axiomOWLAPI);
399 }
400
401 @Override
402 public void visit(FunctionalDatatypePropertyAxiom axiom) {
403 OWLDataProperty role = factory.getOWLDataProperty(
404 IRI.create(axiom.getRole().getName()));
405 OWLAxiom axiomOWLAPI = factory.getOWLFunctionalDataPropertyAxiom(role);
406 addAxiom(axiomOWLAPI);
407
408 }
409
410 @Override
411 public void visit(SubDatatypePropertyAxiom axiom) {
412 OWLDataProperty role = factory.getOWLDataProperty(
413 IRI.create(axiom.getRole().getName()));
414 OWLDataProperty subRole = factory.getOWLDataProperty(
415 IRI.create(axiom.getSubRole().getName()));
416 OWLAxiom axiomOWLAPI = factory.getOWLSubDataPropertyOfAxiom(subRole, role);
417 addAxiom(axiomOWLAPI);
418
419 }
420
421 @Override
422 public void visit(DisjointObjectPropertyAxiom axiom) {
423 OWLObjectProperty role = factory.getOWLObjectProperty(
424 IRI.create(axiom.getRole().getName()));
425 OWLObjectProperty disjointRole = factory.getOWLObjectProperty(
426 IRI.create(axiom.getDisjointRole().getName()));
427 OWLAxiom axiomOWLAPI = factory.getOWLDisjointObjectPropertiesAxiom(role, disjointRole);
428 addAxiom(axiomOWLAPI);
429
430 }
431
432 @Override
433 public void visit(DisjointDatatypePropertyAxiom axiom) {
434 OWLDataProperty role = factory.getOWLDataProperty(
435 IRI.create(axiom.getRole().getName()));
436 OWLDataProperty disjointRole = factory.getOWLDataProperty(
437 IRI.create(axiom.getDisjointRole().getName()));
438 OWLAxiom axiomOWLAPI = factory.getOWLDisjointDataPropertiesAxiom(role, disjointRole);
439 addAxiom(axiomOWLAPI);
440
441 }
442
443 @Override
444 public void visit(InverseFunctionalObjectPropertyAxiom axiom) {
445 OWLObjectProperty role = factory.getOWLObjectProperty(
446 IRI.create(axiom.getRole().getName()));
447 OWLAxiom axiomOWLAPI = factory.getOWLInverseFunctionalObjectPropertyAxiom(role);
448 addAxiom(axiomOWLAPI);
449
450 }
451
452 @Override
453 public void visit(AsymmetricObjectPropertyAxiom axiom) {
454 OWLObjectProperty role = factory.getOWLObjectProperty(
455 IRI.create(axiom.getRole().getName()));
456 OWLAxiom axiomOWLAPI = factory.getOWLAsymmetricObjectPropertyAxiom(role);
457 addAxiom(axiomOWLAPI);
458
459 }
460
461 @Override
462 public void visit(IrreflexiveObjectPropertyAxiom axiom) {
463 OWLObjectProperty role = factory.getOWLObjectProperty(
464 IRI.create(axiom.getRole().getName()));
465 OWLAxiom axiomOWLAPI = factory.getOWLIrreflexiveObjectPropertyAxiom(role);
466 addAxiom(axiomOWLAPI);
467
468 }
469
470
471
472
473
474 }