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