-
Notifications
You must be signed in to change notification settings - Fork 14
/
SelectResultSet.java
426 lines (372 loc) · 14.8 KB
/
SelectResultSet.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* 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.owlapi.query;
import cz.cvut.kbss.ontodriver.Statement;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.exception.VariableNotBoundException;
import cz.cvut.kbss.ontodriver.owlapi.exception.BindingValueMismatchException;
import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import cz.cvut.kbss.owl2query.model.GroundTerm;
import cz.cvut.kbss.owl2query.model.QueryResult;
import cz.cvut.kbss.owl2query.model.ResultBinding;
import cz.cvut.kbss.owl2query.model.Variable;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.*;
import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
class SelectResultSet extends AbstractResultSet {
private final QueryResult<OWLObject> queryResult;
private Iterator<ResultBinding<OWLObject>> iterator;
private final Map<String, Variable<OWLObject>> namesToVariables;
private final Map<Integer, Variable<OWLObject>> indexesToVariables;
private int currentIndex;
private ResultBinding<OWLObject> currentRow;
public SelectResultSet(QueryResult<OWLObject> queryResult, Statement statement) {
super(statement);
this.queryResult = queryResult;
this.iterator = queryResult.iterator();
this.currentIndex = -1;
final int bindingSize = queryResult.getResultVars().size();
this.namesToVariables = new HashMap<>(bindingSize);
this.indexesToVariables = new HashMap<>(bindingSize);
resolveVariableNamesAndIndexes();
}
private void resolveVariableNamesAndIndexes() {
Integer i = 0;
for (Variable<OWLObject> v : queryResult.getResultVars()) {
namesToVariables.put(v.getName(), v);
indexesToVariables.put(i, v);
i++;
}
}
@Override
public int findColumn(String columnLabel) {
ensureOpen();
final Variable<OWLObject> v = namesToVariables.get(columnLabel);
if (v == null) {
return -1;
}
for (Map.Entry<Integer, Variable<OWLObject>> e : indexesToVariables.entrySet()) {
if (e.getValue().equals(v)) {
return e.getKey();
}
}
return -1;
}
@Override
public int getColumnCount() {
ensureOpen();
return namesToVariables.size();
}
@Override
public boolean isBound(int variableIndex) {
ensureState();
return indexesToVariables.containsKey(variableIndex) && currentRow
.get(indexesToVariables.get(variableIndex)) != null;
}
@Override
public boolean isBound(String variableName) {
ensureState();
return namesToVariables.containsKey(variableName) && currentRow.get(namesToVariables.get(variableName)) != null;
}
@Override
public void first() throws OntoDriverException {
ensureOpen();
this.currentIndex = -1;
this.iterator = queryResult.iterator();
next();
}
@Override
public boolean getBoolean(int columnIndex) throws OntoDriverException {
return getPrimitiveValue(Boolean.class, columnIndex);
}
private <T> T getPrimitiveValue(Class<T> cls, int columnIndex) throws OntoDriverException {
final Object val = OwlapiUtils.owlLiteralToValue(getLiteral(columnIndex));
if (!cls.isAssignableFrom(val.getClass())) {
throw new BindingValueMismatchException("Value " + val + " cannot be returned as " + cls.getSimpleName());
}
return cls.cast(val);
}
private OWLLiteral getLiteral(int columnIndex) throws OntoDriverException {
final OWLObject currentValue = getCurrentValue(columnIndex);
if (!(currentValue instanceof OWLLiteral)) {
throw new BindingValueMismatchException("Value " + currentValue + " is not an OWLLiteral.");
}
return (OWLLiteral) currentValue;
}
private OWLObject getCurrentValue(int columnIndex) throws OwlapiDriverException {
ensureState();
if (!indexesToVariables.containsKey(columnIndex)) {
throw new OwlapiDriverException("No result binding found for index " + columnIndex);
}
final Variable<OWLObject> v = indexesToVariables.get(columnIndex);
final GroundTerm<OWLObject> gt = currentRow.get(v);
if (gt == null) {
throw new VariableNotBoundException(
"Variable at index " + columnIndex + " not bound in the current result row.");
}
return gt.getWrappedObject();
}
private void ensureState() {
ensureOpen();
if (currentRow == null) {
throw new IllegalStateException("Current row is null.");
}
}
@Override
public boolean getBoolean(String columnLabel) throws OntoDriverException {
return getPrimitiveValue(Boolean.class, columnLabel);
}
private <T> T getPrimitiveValue(Class<T> cls, String columnLabel) throws OwlapiDriverException {
final Object val = OwlapiUtils.owlLiteralToValue(getLiteral(columnLabel));
if (!cls.isAssignableFrom(val.getClass())) {
throw new BindingValueMismatchException("Value " + val + " cannot be returned as " + cls.getSimpleName());
}
return cls.cast(val);
}
private OWLLiteral getLiteral(String columnLabel) throws OwlapiDriverException {
final OWLObject currentValue = getCurrentValue(columnLabel);
if (!(currentValue instanceof OWLLiteral)) {
throw new BindingValueMismatchException("Value " + currentValue + " is not an OWLLiteral.");
}
return (OWLLiteral) currentValue;
}
private OWLObject getCurrentValue(String columnLabel) throws OwlapiDriverException {
ensureState();
if (!namesToVariables.containsKey(columnLabel)) {
throw new OwlapiDriverException("No result binding found for label " + columnLabel);
}
final Variable<OWLObject> v = namesToVariables.get(columnLabel);
final GroundTerm<OWLObject> gt = currentRow.get(v);
if (gt == null) {
throw new VariableNotBoundException(
"Variable \"" + columnLabel + "\" not bound in the current result row.");
}
return gt.getWrappedObject();
}
@Override
public byte getByte(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.byteValue();
}
@Override
public byte getByte(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.byteValue();
}
@Override
public double getDouble(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.doubleValue();
}
@Override
public double getDouble(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.doubleValue();
}
@Override
public float getFloat(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.floatValue();
}
@Override
public float getFloat(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.floatValue();
}
@Override
public int getInt(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.intValue();
}
@Override
public int getInt(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.intValue();
}
@Override
public long getLong(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.longValue();
}
@Override
public long getLong(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.longValue();
}
@Override
public Object getObject(int columnIndex) throws OntoDriverException {
return owlObjectToObject(getCurrentValue(columnIndex));
}
private Object owlObjectToObject(OWLObject owlValue) {
if (owlValue == null) {
return null;
}
if (owlValue instanceof OWLLiteral) {
return OwlapiUtils.owlLiteralToValue((OWLLiteral) owlValue);
}
final Set<OWLEntity> sig = owlValue.getSignature();
if (sig.isEmpty()) {
return owlValue.toString();
} else {
return URI.create(sig.iterator().next().toStringID());
}
}
@Override
public Object getObject(String columnLabel) throws OntoDriverException {
return owlObjectToObject(getCurrentValue(columnLabel));
}
@Override
public <T> T getObject(int columnIndex, Class<T> cls) throws OntoDriverException {
Objects.requireNonNull(cls);
return owlObjectToType(getCurrentValue(columnIndex), cls);
}
private <T> T owlObjectToType(OWLObject owlValue, Class<T> cls) throws OntoDriverException {
if (cls.isAssignableFrom(owlValue.getClass())) {
return cls.cast(owlValue);
}
if (owlValue instanceof OWLLiteral) {
final Object ob = OwlapiUtils.owlLiteralToValue((OWLLiteral) owlValue);
if (cls.isAssignableFrom(ob.getClass())) {
return cls.cast(ob);
}
} else {
final Set<OWLEntity> sig = owlValue.getSignature();
if (!sig.isEmpty()) {
final URI uri = URI.create(sig.iterator().next().toStringID());
if (cls.isAssignableFrom(uri.getClass())) {
return cls.cast(uri);
}
return tryInstantiatingClassUsingConstructor(cls, uri);
}
}
throw new OwlapiDriverException("Conversion to type " + cls + " is not supported.");
}
private <T> T tryInstantiatingClassUsingConstructor(Class<T> cls, URI uri) throws OwlapiDriverException {
try {
final Constructor<T> constructor = cls.getDeclaredConstructor(uri.getClass());
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance(uri);
} catch (NoSuchMethodException e) {
throw new OwlapiDriverException(
"No constructor taking parameter of type " + uri.getClass().getName() + " found in class " + cls,
e);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
throw new OwlapiDriverException(
"Unable to create instance of class " + cls + " using constructor with argument " + uri, e);
}
}
@Override
public <T> T getObject(String columnLabel, Class<T> cls) throws OntoDriverException {
Objects.requireNonNull(cls, getNPXMessageSupplier("cls"));
return owlObjectToType(getCurrentValue(columnLabel), cls);
}
@Override
public int getRowIndex() {
return currentIndex;
}
@Override
public short getShort(int columnIndex) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnIndex);
return num.shortValue();
}
@Override
public short getShort(String columnLabel) throws OntoDriverException {
final Number num = getPrimitiveValue(Number.class, columnLabel);
return num.shortValue();
}
@Override
public String getString(int columnIndex) throws OntoDriverException {
return owlValueToString(getCurrentValue(columnIndex));
}
private String owlValueToString(OWLObject owlValue) {
if (owlValue instanceof OWLLiteral) {
return OwlapiUtils.owlLiteralToValue((OWLLiteral) owlValue).toString();
}
final Set<OWLEntity> sig = owlValue.getSignature();
if (sig.isEmpty()) {
return owlValue.toString();
} else {
return sig.iterator().next().toStringID();
}
}
@Override
public String getString(String columnLabel) throws OntoDriverException {
return owlValueToString(getCurrentValue(columnLabel));
}
@Override
public boolean isFirst() throws OntoDriverException {
ensureOpen();
return currentIndex == 0;
}
@Override
public boolean hasNext() throws OntoDriverException {
ensureOpen();
return iterator.hasNext();
}
@Override
public void last() throws OntoDriverException {
ensureOpen();
while (hasNext()) {
next();
}
}
@Override
public void next() throws OntoDriverException {
ensureOpen();
if (!hasNext()) {
throw new NoSuchElementException("The result set has no more rows.");
}
this.currentRow = iterator.next();
currentIndex++;
}
@Override
public void previous() throws OntoDriverException {
ensureOpen();
relative(-1);
}
@Override
public void registerObserver(Observer observer) throws OntoDriverException {
// Not implemented yet
}
@Override
public void relative(int rows) throws OntoDriverException {
ensureOpen();
setRowIndex(currentIndex + rows);
}
@Override
public void setRowIndex(int rowIndex) throws OntoDriverException {
ensureOpen();
if (rowIndex == currentIndex) {
return;
}
if (rowIndex < 0) {
throw new IllegalArgumentException("Cannot set row index to a number less than 0.");
}
if (rowIndex < currentIndex) {
first();
}
while (rowIndex > currentIndex && hasNext()) {
next();
}
}
}