-
Notifications
You must be signed in to change notification settings - Fork 14
/
SesameAdapter.java
231 lines (201 loc) · 8.55 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
223
224
225
226
227
228
229
230
231
/**
* Copyright (C) 2016 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.config.ConfigParam;
import cz.cvut.kbss.ontodriver.config.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.*;
import cz.cvut.kbss.ontodriver.exception.IdentifierGenerationException;
import cz.cvut.kbss.ontodriver.exception.OWLIndividualExistsException;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
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 org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.vocabulary.RDF;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
class SesameAdapter implements Closeable {
/**
* Maximum number of attempts to generate a unique identifier
*/
private static final int ID_GENERATION_THRESHOLD = 64;
private static final Random RANDOM = new Random();
private final Connector connector;
private final ValueFactory valueFactory;
private final String language;
private boolean open;
private final Transaction transaction;
public SesameAdapter(Connector connector, Configuration configuration) {
assert connector != null;
this.connector = connector;
this.valueFactory = connector.getValueFactory();
this.language = configuration.getProperty(ConfigParam.ONTOLOGY_LANGUAGE);
this.open = true;
this.transaction = new Transaction();
}
Connector getConnector() {
return connector;
}
ValueFactory getValueFactory() {
return valueFactory;
}
String getLanguage() {
return language;
}
@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) {
if (classUri.getFragment() != null) {
id = URI.create(classUri.toString() + "_instance" + RANDOM.nextInt());
} else {
String base = classUri.toString();
if (base.endsWith("/")) {
id = URI.create(base + "_instance" + RANDOM.nextInt());
} else {
id = URI.create(base + "#instance" + RANDOM.nextInt());
}
}
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 {
final Collection<Statement> stmts = connector.findStatements(
SesameUtils.toSesameUri(identifier, valueFactory), RDF.TYPE,
SesameUtils.toSesameUri(classUri, valueFactory), true);
return stmts.isEmpty();
}
boolean contains(Axiom<?> axiom, URI context) throws SesameDriverException {
startTransactionIfNotActive();
Value value;
if (SesameUtils.isResourceIdentifier(axiom.getValue().getValue())) {
value = valueFactory.createURI(axiom.getValue().stringValue());
} else {
value = SesameUtils.createDataPropertyLiteral(axiom.getValue().getValue(), language,
valueFactory);
}
return !(connector.findStatements(
SesameUtils.toSesameUri(axiom.getSubject().getIdentifier(), valueFactory),
SesameUtils.toSesameUri(axiom.getAssertion().getIdentifier(), valueFactory), value,
true, SesameUtils.toSesameUri(context, valueFactory)).isEmpty());
}
Collection<Axiom<?>> find(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
return new AxiomLoader(connector, valueFactory).loadAxioms(axiomDescriptor);
}
void persist(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
if (individualExists(axiomDescriptor.getSubject(), axiomDescriptor.getSubjectContext())) {
throw new SesameDriverException(new OWLIndividualExistsException("An individual with identifier "
+ axiomDescriptor.getSubject() + " already exists in context "
+ axiomDescriptor.getSubjectContext()));
}
new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
}
private boolean individualExists(NamedResource subject, URI subjectContext)
throws SesameDriverException {
final org.openrdf.model.URI sesameSubject = SesameUtils.toSesameUri(
subject.getIdentifier(), valueFactory);
final Collection<Statement> result = connector.findStatements(sesameSubject, RDF.TYPE,
null, true, SesameUtils.toSesameUri(subjectContext, valueFactory));
return !result.isEmpty();
}
void update(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
new AxiomSaver(connector, valueFactory, language).persistAxioms(axiomDescriptor);
}
void remove(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
startTransactionIfNotActive();
new EpistemicAxiomRemover(connector, valueFactory, language).remove(axiomDescriptor);
}
StatementExecutor getQueryExecutor() {
return connector;
}
ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() {
return ListHandler.createForSimpleList(connector, valueFactory);
}
ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() {
return ListHandler.createForReferencedList(connector, valueFactory);
}
TypesHandler getTypesHandler() {
return new TypesHandler(connector, valueFactory);
}
}