-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomValueGatherer.java
165 lines (147 loc) · 6.51 KB
/
AxiomValueGatherer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (C) 2019 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import java.net.URI;
import java.util.*;
/**
* Gathers axiom values in descriptor for persist or update.
*/
class AxiomValueGatherer {
private final AxiomValueDescriptor axiomDescriptor;
private final Collection<SimpleListValueDescriptor> simpleListDescriptors = new ArrayList<>();
private final Collection<ReferencedListValueDescriptor> referencedListDescriptors = new ArrayList<>();
private Set<URI> typesToAdd;
private Set<URI> typesToRemove;
private URI typesContext;
private Map<Assertion, Set<Value<?>>> propertiesToAdd;
private Map<Assertion, Set<Value<?>>> propertiesToRemove;
private URI propertiesContext;
AxiomValueGatherer(NamedResource subject, URI subjectContext) {
this.axiomDescriptor = new AxiomValueDescriptor(subject);
axiomDescriptor.setSubjectContext(subjectContext);
}
NamedResource getSubjectIdentifier() {
return axiomDescriptor.getSubject();
}
void addValue(Assertion assertion, Value<?> value, URI context) {
addValues(assertion, Collections.singleton(value), context);
}
void addValues(Assertion assertion, Collection<Value<?>> values, URI context) {
axiomDescriptor.addAssertion(assertion);
for (Value<?> v : values) {
axiomDescriptor.addAssertionValue(assertion, v);
}
if (!Objects.equals(axiomDescriptor.getSubjectContext(), context)) {
axiomDescriptor.setAssertionContext(assertion, context);
}
}
void addSimpleListValues(SimpleListValueDescriptor listDescriptor) {
simpleListDescriptors.add(listDescriptor);
}
void addReferencedListValues(ReferencedListValueDescriptor listDescriptor) {
referencedListDescriptors.add(listDescriptor);
}
void addTypes(Set<URI> types, URI context) {
if (typesToAdd == null) {
this.typesToAdd = new HashSet<>();
}
appendTypes(typesToAdd, types, context);
}
private void appendTypes(Set<URI> target, Set<URI> types, URI context) {
target.addAll(types);
this.typesContext = context;
}
void removeTypes(Set<URI> types, URI context) {
if (typesToRemove == null) {
this.typesToRemove = new HashSet<>();
}
appendTypes(typesToRemove, types, context);
}
void addProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
if (propertiesToAdd == null) {
this.propertiesToAdd = new HashMap<>();
}
appendProperties(propertiesToAdd, properties, context);
}
private void appendProperties(Map<Assertion, Set<Value<?>>> target, Map<Assertion, Set<Value<?>>> properties,
URI context) {
for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
if (!target.containsKey(e.getKey())) {
target.put(e.getKey(), e.getValue());
} else {
target.get(e.getKey()).addAll(e.getValue());
}
}
this.propertiesContext = context;
}
void removeProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
if (propertiesToRemove == null) {
this.propertiesToRemove = new HashMap<>();
}
appendProperties(propertiesToRemove, properties, context);
}
void persist(Connection connection) {
try {
connection.persist(axiomDescriptor);
if (typesToAdd != null) {
connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
}
if (propertiesToAdd != null) {
connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
}
for (SimpleListValueDescriptor d : simpleListDescriptors) {
connection.lists().persistSimpleList(d);
}
for (ReferencedListValueDescriptor d : referencedListDescriptors) {
connection.lists().persistReferencedList(d);
}
} catch (OntoDriverException e) {
throw new StorageAccessException(e);
}
}
void update(Connection connection) {
try {
connection.update(axiomDescriptor);
if (typesToAdd != null) {
connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
}
if (typesToRemove != null) {
connection.types().removeTypes(axiomDescriptor.getSubject(), typesContext, typesToRemove);
}
if (propertiesToAdd != null) {
connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
}
if (propertiesToRemove != null) {
connection.properties()
.removeProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToRemove);
}
for (SimpleListValueDescriptor d : simpleListDescriptors) {
connection.lists().updateSimpleList(d);
}
for (ReferencedListValueDescriptor d : referencedListDescriptors) {
connection.lists().updateReferencedList(d);
}
} catch (OntoDriverException e) {
throw new StorageAccessException(e);
}
}
}