Skip to content

Method: persistAnnotationPropertyValues(NamedResource, Assertion, Collection)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
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: *
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.owlapi.connector.OntologySnapshot;
18: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.model.Value;
24: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
25: import org.semanticweb.owlapi.model.*;
26:
27: import java.net.URI;
28: import java.util.*;
29: import java.util.stream.Collectors;
30:
31: /**
32: * Persists axioms into the ontology snapshot.
33: */
34: class AxiomSaver {
35:
36: private final OwlapiAdapter adapter;
37:
38: private final OWLOntology ontology;
39: private final OntologySnapshot snapshot;
40:
41: private final AxiomAdapter axiomAdapter;
42:
43: AxiomSaver(OwlapiAdapter adapter, OntologySnapshot snapshot) {
44: this.adapter = adapter;
45: this.snapshot = snapshot;
46: this.ontology = snapshot.getOntology();
47: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory());
48: }
49:
50: void persist(AxiomValueDescriptor descriptor) {
51: for (Assertion assertion : descriptor.getAssertions()) {
52: switch (assertion.getType()) {
53: case CLASS:
54: persistTypes(descriptor.getSubject(), descriptor.getAssertionValues(assertion));
55: break;
56: case DATA_PROPERTY:
57: persistDataPropertyValues(descriptor.getSubject(), assertion,
58: descriptor.getAssertionValues(assertion));
59: break;
60: case ANNOTATION_PROPERTY:
61: persistAnnotationPropertyValues(descriptor.getSubject(), assertion,
62: descriptor.getAssertionValues(assertion));
63: break;
64: case OBJECT_PROPERTY:
65: persistObjectPropertyValues(descriptor.getSubject(), assertion,
66: descriptor.getAssertionValues(assertion));
67: break;
68: case PROPERTY:
69: persistPropertyValues(descriptor.getSubject(), assertion, descriptor.getAssertionValues(assertion));
70: break;
71: default:
72: break;
73: }
74: }
75: }
76:
77: private void persistTypes(NamedResource subject, List<Value<?>> types) {
78: final Set<URI> classes = types.stream().map(val -> {
79: if (val.getValue() instanceof URI) {
80: return (URI) val.getValue();
81: } else {
82: return URI.create(val.stringValue());
83: }
84: }).collect(Collectors.toSet());
85: adapter.getTypesHandler().addTypes(subject, null, classes);
86: }
87:
88: private void persistDataPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
89: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
90: .map(value -> axiomAdapter.toOwlDataPropertyAssertionAxiom(
91: new AxiomImpl<>(subject, assertion, value)))
92: .collect(Collectors.toList());
93: addAxioms(axioms);
94: }
95:
96: private void addAxioms(List<? extends OWLAxiom> axioms) {
97: if (axioms.isEmpty()) {
98: return;
99: }
100: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableAddAxiom(ontology, axiom))
101: .collect(Collectors.toList());
102: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
103: }
104:
105: private void persistAnnotationPropertyValues(NamedResource subject, Assertion assertion,
106: Collection<Value<?>> values) {
107: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
108: .map(value -> axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(
109: new AxiomImpl<>(subject, assertion, value)))
110: .collect(Collectors.toList());
111: addAxioms(axioms);
112: }
113:
114: private void persistObjectPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
115: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
116: .map(value -> {
117: // Simplistic version using value.stringValue
118: // We expect the value to be a NamedResource, but in case the property was unspecified and it was only assumed
119: // it is an object property (see #persistPropertyValues), the value would be a simple string
120: return axiomAdapter.toOwlObjectPropertyAssertionAxiom(
121: new AxiomImpl<>(subject, assertion, value));
122: }).collect(Collectors.toList());
123: addAxioms(axioms);
124: }
125:
126: private void persistPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
127: final IRI property = IRI.create(assertion.getIdentifier());
128: if (ontology.containsDataPropertyInSignature(property)) {
129: persistDataPropertyValues(subject, assertion, values);
130: } else if (ontology.containsObjectPropertyInSignature(property)) {
131: persistObjectPropertyValues(subject, assertion, values);
132: } else if (ontology.containsAnnotationPropertyInSignature(property)) {
133: persistAnnotationPropertyValues(subject, assertion, values);
134: } else {
135: persistUnknownPropertyValues(subject, assertion, values);
136: }
137: }
138:
139: private void persistUnknownPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
140: final List<OWLAxiom> axioms = new ArrayList<>();
141: for (Value<?> v : values) {
142: if (OwlapiUtils.isIndividualIri(v.getValue())) {
143: axioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(new AxiomImpl<>(subject, assertion, v)));
144: } else {
145: axioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(new AxiomImpl<>(subject, assertion, v)));
146: }
147: }
148: addAxioms(axioms);
149: }
150:
151: void persistAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> properties) {
152: for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
153: persistPropertyValues(subject, e.getKey(), e.getValue());
154: }
155: }
156: }