Skip to content

Method: getBoolean(String)

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.jena.query;
19:
20:
21: import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
22: import cz.cvut.kbss.ontodriver.exception.VariableNotBoundException;
23: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
24: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
25: import org.apache.jena.query.QueryExecution;
26: import org.apache.jena.query.QuerySolution;
27: import org.apache.jena.query.ResultSet;
28: import org.apache.jena.rdf.model.Literal;
29: import org.apache.jena.rdf.model.RDFNode;
30:
31: import java.net.URI;
32: import java.util.Objects;
33:
34: public class SelectResultSet extends AbstractResultSet {
35:
36: private final QueryExecution execution;
37: private final ResultSet jenaResult;
38: private QuerySolution current;
39:
40: public SelectResultSet(QueryExecution execution, ResultSet jenaResult) {
41: this.execution = execution;
42: this.jenaResult = jenaResult;
43: }
44:
45: @Override
46: public int findColumn(String variableName) {
47: ensureOpen();
48: return jenaResult.getResultVars().indexOf(variableName);
49: }
50:
51: @Override
52: public int getColumnCount() {
53: ensureOpen();
54: return jenaResult.getResultVars().size();
55: }
56:
57: @Override
58: public boolean isBound(int variableIndex) {
59: ensureState();
60: return variableIndex >= 0 && variableIndex < jenaResult.getResultVars().size() && current
61: .get(getVariableAt(variableIndex)) != null;
62: }
63:
64: @Override
65: public boolean isBound(String variableName) {
66: ensureState();
67: return jenaResult.getResultVars().contains(variableName) && current.get(variableName) != null;
68: }
69:
70: @Override
71: public boolean getBoolean(int variableIndex) throws JenaDriverException {
72: ensureState();
73: return getBoolean(getVariableAt(variableIndex));
74: }
75:
76: @Override
77: public boolean getBoolean(String variableName) throws JenaDriverException {
78: ensureState();
79: ensureVariableExists(variableName);
80: return getLiteral(variableName).getBoolean();
81: }
82:
83: private Literal getLiteral(String varName) throws JenaDriverException {
84: Objects.requireNonNull(varName);
85: assert current != null;
86: final RDFNode value = getCurrent(varName);
87: if (!value.isLiteral()) {
88: throw new JenaDriverException("Expected value " + value + " to be a literal.");
89: }
90: return current.get(varName).asLiteral();
91: }
92:
93: private RDFNode getCurrent(String varName) {
94: final RDFNode value = current.get(varName);
95: if (value == null) {
96: throw new VariableNotBoundException("Variable " + varName + " is not bound in the current result row.");
97: }
98: return value;
99: }
100:
101: @Override
102: public byte getByte(int variableIndex) throws JenaDriverException {
103: ensureState();
104: return getLiteral(getVariableAt(variableIndex)).getByte();
105: }
106:
107: @Override
108: public byte getByte(String variableName) throws JenaDriverException {
109: ensureState();
110: ensureVariableExists(variableName);
111: return getLiteral(variableName).getByte();
112: }
113:
114: @Override
115: public double getDouble(int variableIndex) throws JenaDriverException {
116: ensureState();
117: return getLiteral(getVariableAt(variableIndex)).getDouble();
118: }
119:
120: @Override
121: public double getDouble(String variableName) throws JenaDriverException {
122: ensureState();
123: ensureVariableExists(variableName);
124: return getLiteral(variableName).getDouble();
125: }
126:
127: @Override
128: public float getFloat(int variableIndex) throws JenaDriverException {
129: ensureState();
130: return getLiteral(getVariableAt(variableIndex)).getFloat();
131: }
132:
133: @Override
134: public float getFloat(String variableName) throws JenaDriverException {
135: ensureState();
136: ensureVariableExists(variableName);
137: return getLiteral(variableName).getFloat();
138: }
139:
140: @Override
141: public int getInt(int variableIndex) throws JenaDriverException {
142: ensureState();
143: return getLiteral(getVariableAt(variableIndex)).getInt();
144: }
145:
146: @Override
147: public int getInt(String variableName) throws JenaDriverException {
148: ensureState();
149: ensureVariableExists(variableName);
150: return getLiteral(variableName).getInt();
151: }
152:
153: @Override
154: public long getLong(int variableIndex) throws JenaDriverException {
155: ensureState();
156: return getLiteral(getVariableAt(variableIndex)).getLong();
157: }
158:
159: @Override
160: public long getLong(String variableName) throws JenaDriverException {
161: ensureState();
162: ensureVariableExists(variableName);
163: return getLiteral(variableName).getLong();
164: }
165:
166: @Override
167: public Object getObject(int variableIndex) {
168: ensureState();
169: assert current != null;
170: return toObject(getCurrent(getVariableAt(variableIndex)));
171: }
172:
173: private static Object toObject(RDFNode value) {
174: assert value != null;
175: if (value.isLiteral()) {
176: return JenaUtils.literalToValue(value.asLiteral());
177: } else {
178: assert value.isResource();
179: if (value.isURIResource()) {
180: return URI.create(value.asResource().getURI());
181: } else {
182: return value.asResource().getId().getLabelString();
183: }
184: }
185: }
186:
187: @Override
188: public Object getObject(String variableName) {
189: ensureState();
190: ensureVariableExists(variableName);
191: assert current != null;
192: return toObject(getCurrent(Objects.requireNonNull(variableName)));
193: }
194:
195: @Override
196: public <T> T getObject(int variableIndex, Class<T> cls) throws JenaDriverException {
197: ensureState();
198: assert current != null;
199: return toObject(getCurrent(getVariableAt(variableIndex)), cls);
200: }
201:
202: private static <T> T toObject(RDFNode value, Class<T> cls) {
203: Objects.requireNonNull(cls);
204: if (cls.isAssignableFrom(value.getClass())) {
205: return cls.cast(value);
206: }
207: Object objectValue;
208: if (value.isLiteral()) {
209: objectValue = JenaUtils.literalToValue(value.asLiteral());
210: } else {
211: assert value.isResource();
212: if (value.isURIResource()) {
213: objectValue = URI.create(value.asResource().getURI());
214: } else {
215: objectValue = value.asResource().getId().getLabelString();
216: }
217: }
218: return DatatypeTransformer.transform(objectValue, cls);
219: }
220:
221: @Override
222: public <T> T getObject(String variableName, Class<T> cls) throws JenaDriverException {
223: ensureState();
224: ensureVariableExists(variableName);
225: assert current != null;
226: return toObject(getCurrent(variableName), cls);
227: }
228:
229: @Override
230: public short getShort(int variableIndex) throws JenaDriverException {
231: ensureState();
232: return getLiteral(getVariableAt(variableIndex)).getShort();
233: }
234:
235: @Override
236: public short getShort(String variableName) throws JenaDriverException {
237: ensureState();
238: ensureVariableExists(variableName);
239: return getLiteral(variableName).getShort();
240: }
241:
242: @Override
243: public String getString(int variableIndex) {
244: ensureState();
245: return getString(getVariableAt(variableIndex));
246: }
247:
248: private String getVariableAt(int index) {
249: if (index < 0 || index >= jenaResult.getResultVars().size()) {
250: throw new IllegalArgumentException("Variable index " + index + " is out of bounds.");
251: }
252: return jenaResult.getResultVars().get(index);
253: }
254:
255: @Override
256: public String getString(String variableName) {
257: ensureState();
258: ensureVariableExists(variableName);
259: assert current != null;
260: final RDFNode value = getCurrent(variableName);
261: if (value.isResource()) {
262: return value.isURIResource() ? value.asResource().getURI() : value.asResource().getId().getLabelString();
263: } else {
264: return value.asLiteral().getString();
265: }
266: }
267:
268: private void ensureVariableExists(String name) {
269: assert current != null;
270: if (!current.contains(name)) {
271: throw new IllegalArgumentException("Variable '" + name + "' not found in the result set.");
272: }
273: }
274:
275: @Override
276: public boolean hasNext() {
277: ensureOpen();
278: return jenaResult.hasNext();
279: }
280:
281: @Override
282: public void next() {
283: super.next();
284: this.current = jenaResult.next();
285: }
286:
287: @Override
288: public void close() throws JenaDriverException {
289: try {
290: execution.close();
291: super.close();
292: } catch (RuntimeException e) {
293: throw new JenaDriverException("Unable to close result set.", e);
294: }
295: }
296: }