-
Notifications
You must be signed in to change notification settings - Fork 14
/
SesameAdapter.java
222 lines (191 loc) · 8 KB
/
SesameAdapter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* 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.ontodriver.sesame;
import cz.cvut.kbss.ontodriver.Closeable;
import cz.cvut.kbss.ontodriver.Wrapper;
import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
import cz.cvut.kbss.ontodriver.descriptor.*;
import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.sesame.config.RuntimeConfiguration;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.connector.StatementExecutor;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
import cz.cvut.kbss.ontodriver.util.Transaction;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class SesameAdapter implements Closeable, Wrapper {
/**
* Maximum number of attempts to generate a unique identifier
*/
private static final int ID_GENERATION_THRESHOLD = 64;
private final Connector connector;
private final ValueFactory valueFactory;
private final RuntimeConfiguration config;
private boolean open;
private final Transaction transaction;
public SesameAdapter(Connector connector, DriverConfiguration configuration) {
assert connector != null;
this.connector = connector;
this.valueFactory = connector.getValueFactory();
this.config = new RuntimeConfiguration(configuration);
this.open = true;
this.transaction = new Transaction();
}
Connector getConnector() {
return connector;
}
ValueFactory getValueFactory() {
return valueFactory;
}
RuntimeConfiguration getConfig() {
return config;
}
@Override
public void close() throws OntoDriverException {
if (!open) {
return;
}
try {
connector.close();
} finally {
this.open = false;
}
}
@Override
public boolean isOpen() {
return open;
}
void commit() throws SesameDriverException {
if (transaction.isActive()) {
transaction.commit();
connector.commit();
transaction.afterCommit();
}
}
void rollback() throws SesameDriverException {
if (transaction.isActive()) {
transaction.rollback();
connector.rollback();
transaction.afterRollback();
}
}
boolean isConsistent(URI context) {
// Sesame currently doesn't support any consistency checking
// functionality
return true;
}
List<URI> getContexts() throws SesameDriverException {
final List<Resource> contextIds = connector.getContexts();
final List<URI> contexts = new ArrayList<>(contextIds.size());
for (Resource res : contextIds) {
final URI context = SesameUtils.toJavaUri(res);
// We support only named contexts (no blank nodes)
if (context != null) {
contexts.add(context);
}
}
return contexts;
}
URI generateIdentifier(URI classUri) throws SesameDriverException {
startTransactionIfNotActive();
boolean unique = false;
URI id = null;
int counter = 0;
while (!unique && counter++ < ID_GENERATION_THRESHOLD) {
id = IdentifierUtils.generateIdentifier(classUri);
unique = isIdentifierUnique(id, classUri);
}
if (!unique) {
throw new IdentifierGenerationException("Unable to generate a unique identifier.");
}
return id;
}
private void startTransactionIfNotActive() throws SesameDriverException {
if (!transaction.isActive()) {
connector.begin();
transaction.begin();
}
}
private boolean isIdentifierUnique(URI identifier, URI classUri) throws SesameDriverException {
return !connector.containsStatement(
SesameUtils.toSesameIri(identifier, valueFactory), RDF.TYPE,
SesameUtils.toSesameIri(classUri, valueFactory), true);
}
boolean contains(Axiom<?> axiom, URI context) throws SesameDriverException {
startTransactionIfNotActive();
Value value;
if (SesameUtils.isResourceIdentifier(axiom.getValue().getValue())) {
value = valueFactory.createIRI(axiom.getValue().stringValue());
} else {
value = SesameUtils.createDataPropertyLiteral(axiom.getValue().getValue(), config.getLanguage(),
valueFactory);
}
final org.eclipse.rdf4j.model.IRI sesameContext = SesameUtils.toSesameIri(context, valueFactory);
return connector.containsStatement(
SesameUtils.toSesameIri(axiom.getSubject().getIdentifier(), valueFactory),
SesameUtils.toSesameIri(axiom.getAssertion().getIdentifier(), valueFactory), value,
axiom.getAssertion().isInferred(), sesameContext);
}
Collection<Axiom<?>> find(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
return new AxiomLoader(connector, valueFactory, config).loadAxioms(axiomDescriptor);
}
void persist(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
new AxiomSaver(connector, valueFactory, config.getLanguage()).persistAxioms(axiomDescriptor);
}
void update(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
new EpistemicAxiomRemover(connector, valueFactory, config.getLanguage()).remove(axiomDescriptor);
new AxiomSaver(connector, valueFactory, config.getLanguage()).persistAxioms(axiomDescriptor);
}
void remove(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
new EpistemicAxiomRemover(connector, valueFactory, config.getLanguage()).remove(axiomDescriptor);
}
StatementExecutor getQueryExecutor() {
return connector;
}
ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() throws SesameDriverException {
startTransactionIfNotActive();
return ListHandler.createForSimpleList(connector, valueFactory);
}
ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() throws
SesameDriverException {
startTransactionIfNotActive();
return ListHandler.createForReferencedList(connector, valueFactory);
}
TypesHandler getTypesHandler() throws SesameDriverException {
startTransactionIfNotActive();
return new TypesHandler(connector, valueFactory);
}
@Override
public <T> T unwrap(Class<T> cls) throws OntoDriverException {
if (cls.isAssignableFrom(this.getClass())) {
return cls.cast(this);
} else if (cls.isAssignableFrom(valueFactory.getClass())) {
return cls.cast(valueFactory);
}
return connector.unwrap(cls);
}
}