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