Skip to content

Method: toOwlObjectPropertyAssertionAxiom(Axiom)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
19: import org.semanticweb.owlapi.model.*;
20:
21: /**
22: * Adapts OWLAPI axioms to JOPA (OntoDriver) axioms and vice versa.
23: */
24: public class AxiomAdapter {
25:
26: private final OWLDataFactory dataFactory;
27: private final String language;
28:
29: public AxiomAdapter(OWLDataFactory dataFactory, String language) {
30: this.dataFactory = dataFactory;
31: this.language = language;
32: }
33:
34: OWLAxiom toOwlClassAssertionAxiom(Axiom<?> axiom) {
35: final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(axiom.getValue().stringValue()));
36: return dataFactory.getOWLClassAssertionAxiom(owlClass, toOWLIndividual(axiom.getSubject()));
37: }
38:
39: public OWLAxiom toOwlObjectPropertyAssertionAxiom(Axiom<?> axiom) {
40: final OWLObjectProperty objectProperty = dataFactory
41: .getOWLObjectProperty(IRI.create(axiom.getAssertion().getIdentifier()));
42: final OWLNamedIndividual objectValue = dataFactory.getOWLNamedIndividual(
43: IRI.create(axiom.getValue().stringValue()));
44: return dataFactory.getOWLObjectPropertyAssertionAxiom(objectProperty, toOWLIndividual(axiom.getSubject()),
45: objectValue);
46: }
47:
48: OWLAxiom toOwlDataPropertyAssertionAxiom(Axiom<?> axiom) {
49: final OWLDataProperty dataProperty = dataFactory
50: .getOWLDataProperty(IRI.create(axiom.getAssertion().getIdentifier()));
51: final OWLLiteral dataValue = OwlapiUtils.createOWLLiteralFromValue(axiom.getValue().getValue(),
52: dataFactory, language(axiom.getAssertion()));
53: return dataFactory
54: .getOWLDataPropertyAssertionAxiom(dataProperty, toOWLIndividual(axiom.getSubject()), dataValue);
55: }
56:
57: private String language(Assertion assertion) {
58: return assertion.hasLanguage() ? assertion.getLanguage() : language;
59: }
60:
61: OWLAxiom toOwlAnnotationPropertyAssertionAxiom(Axiom<?> axiom) {
62: final OWLAnnotationProperty annotationProperty = dataFactory.getOWLAnnotationProperty(IRI.create(
63: axiom.getAssertion().getIdentifier()));
64: final Object value = axiom.getValue().getValue();
65: final OWLAnnotationValue annotationValue;
66: if (OwlapiUtils.isIndividualIri(value)) {
67: annotationValue = IRI.create(value.toString());
68: } else {
69: annotationValue = OwlapiUtils.createOWLLiteralFromValue(
70: axiom.getValue().getValue(), dataFactory, language(axiom.getAssertion()));
71: }
72: return dataFactory
73: .getOWLAnnotationAssertionAxiom(annotationProperty, toOWLIndividual(axiom.getSubject()).getIRI(),
74: annotationValue);
75: }
76:
77: private OWLNamedIndividual toOWLIndividual(NamedResource subject) {
78: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
79: }
80:
81: Axiom<?> toAxiom(NamedResource subject, OWLDataPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
82: final Assertion assertion = Assertion
83: .createDataPropertyAssertion(assertionAxiom.getProperty().asOWLDataProperty().getIRI().toURI(),
84: isInferred);
85: return createAxiom(subject, assertion, OwlapiUtils.owlLiteralToValue(assertionAxiom.getObject()));
86: }
87:
88: public <V> Axiom<V> createAxiom(NamedResource subject, Assertion assertion, V value) {
89: return new AxiomImpl<>(subject, assertion, new Value<>(value));
90: }
91:
92: Axiom<?> toAxiom(NamedResource subject, OWLObjectPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
93: final Assertion assertion = Assertion
94: .createObjectPropertyAssertion(assertionAxiom.getProperty().asOWLObjectProperty().getIRI().toURI(),
95: isInferred);
96: final IRI target = assertionAxiom.getObject().asOWLNamedIndividual().getIRI();
97: return createAxiom(subject, assertion, NamedResource.create(target.toURI()));
98: }
99:
100: Axiom<?> toAxiom(NamedResource subject, OWLAnnotationAssertionAxiom assertionAxiom, boolean isInferred) {
101: final Assertion assertion = Assertion
102: .createAnnotationPropertyAssertion(
103: assertionAxiom.getProperty().asOWLAnnotationProperty().getIRI().toURI(),
104: isInferred);
105: if (assertionAxiom.getValue().asIRI().isPresent()) {
106: return createAxiom(subject, assertion, assertionAxiom.getValue().asIRI().get().toURI());
107: } else {
108: return createAxiom(subject, assertion, OwlapiUtils.owlLiteralToValue(
109: assertionAxiom.getValue().asLiteral().get()));
110: }
111: }
112: }