Skip to content

Method: createStatement()

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.owlapi;
19:
20: import cz.cvut.kbss.ontodriver.*;
21: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
22: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
26: import cz.cvut.kbss.ontodriver.owlapi.list.OwlapiLists;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.List;
31: import java.util.Objects;
32: import java.util.Set;
33:
34: import static cz.cvut.kbss.ontodriver.util.ErrorUtils.getNPXMessageSupplier;
35:
36: /**
37: * Default implementation of the {@link Connection} interface for OWLAPI driver.
38: */
39: public class OwlapiConnection implements Connection {
40:
41: private boolean open;
42: private boolean autoCommit;
43:
44: private final OwlapiAdapter adapter;
45:
46: private OwlapiTypes types;
47: private OwlapiProperties properties;
48: private OwlapiLists lists;
49:
50: private ConnectionListener listener;
51:
52: OwlapiConnection(OwlapiAdapter adapter) {
53: this.adapter = adapter;
54: this.open = true;
55: this.autoCommit = false;
56: }
57:
58: void setListener(ConnectionListener listener) {
59: ensureOpen();
60: assert listener != null;
61: this.listener = listener;
62: }
63:
64: void removeListener() {
65: this.listener = null;
66: }
67:
68: void setTypes(OwlapiTypes types) {
69: this.types = types;
70: }
71:
72: void setProperties(OwlapiProperties properties) {
73: this.properties = properties;
74: }
75:
76: void setLists(OwlapiLists lists) {
77: this.lists = lists;
78: }
79:
80: @Override
81: public boolean isOpen() {
82: return open;
83: }
84:
85: @Override
86: public void commit() {
87: ensureOpen();
88: adapter.commit();
89: }
90:
91: public void ensureOpen() {
92: if (!open) {
93: throw new IllegalStateException("Connection is closed.");
94: }
95: }
96:
97: @Override
98: public void rollback() {
99: ensureOpen();
100: adapter.rollback();
101: }
102:
103: @Override
104: public void setAutoCommit(boolean autoCommit) {
105: this.autoCommit = autoCommit;
106: }
107:
108: @Override
109: public boolean isAutoCommit() {
110: return autoCommit;
111: }
112:
113: @Override
114: public Statement createStatement() {
115: ensureOpen();
116: return adapter.createStatement(this);
117: }
118:
119: @Override
120: public PreparedStatement prepareStatement(String sparql) {
121: ensureOpen();
122: return adapter.prepareStatement(sparql, this);
123: }
124:
125: @Override
126: public boolean isConsistent(URI context) {
127: ensureOpen();
128: return adapter.isConsistent(context);
129: }
130:
131: @Override
132: public List<URI> getContexts() {
133: ensureOpen();
134: return adapter.getContexts();
135: }
136:
137: @Override
138: public boolean contains(Axiom<?> axiom, Set<URI> contexts) {
139: ensureOpen();
140: Objects.requireNonNull(axiom, getNPXMessageSupplier("axiom"));
141: return adapter.containsAxiom(axiom, contexts);
142: }
143:
144: @Override
145: public boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
146: ensureOpen();
147: Objects.requireNonNull(axiom);
148: return adapter.isInferred(axiom, contexts);
149: }
150:
151: @Override
152: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) throws OntoDriverException {
153: ensureOpen();
154: Objects.requireNonNull(descriptor);
155: try {
156: return adapter.find(descriptor);
157: } catch (RuntimeException e) {
158: throw new OwlapiDriverException(e);
159: }
160: }
161:
162: @Override
163: public void persist(AxiomValueDescriptor descriptor) throws OntoDriverException {
164: ensureOpen();
165: Objects.requireNonNull(descriptor);
166: try {
167: adapter.persist(descriptor);
168: commitIfAuto();
169: } catch (RuntimeException e) {
170: throw new OwlapiDriverException(e);
171: }
172: }
173:
174: @Override
175: public URI generateIdentifier(URI classUri) {
176: ensureOpen();
177: Objects.requireNonNull(classUri);
178: return adapter.generateIdentifier(classUri);
179: }
180:
181: @Override
182: public void update(AxiomValueDescriptor descriptor) throws OntoDriverException {
183: ensureOpen();
184: Objects.requireNonNull(descriptor);
185: try {
186: adapter.update(descriptor);
187: commitIfAuto();
188: } catch (RuntimeException e) {
189: throw new OwlapiDriverException(e);
190: }
191: }
192:
193: @Override
194: public void remove(AxiomDescriptor descriptor) throws OntoDriverException {
195: ensureOpen();
196: Objects.requireNonNull(descriptor);
197: try {
198: adapter.remove(descriptor);
199: commitIfAuto();
200: } catch (RuntimeException e) {
201: throw new OwlapiDriverException(e);
202: }
203: }
204:
205: @Override
206: public Lists lists() {
207: ensureOpen();
208: assert lists != null;
209: return lists;
210: }
211:
212: @Override
213: public Types types() {
214: ensureOpen();
215: assert types != null;
216: return types;
217: }
218:
219: @Override
220: public Properties properties() {
221: ensureOpen();
222: assert properties != null;
223: return properties;
224: }
225:
226: @Override
227: public void close() {
228: if (!open) {
229: return;
230: }
231: if (listener != null) {
232: listener.connectionClosed(this);
233: }
234: this.open = false;
235: }
236:
237: public void commitIfAuto() {
238: if (autoCommit) {
239: adapter.commit();
240: }
241: }
242:
243: @Override
244: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
245: if (cls.isAssignableFrom(this.getClass())) {
246: return cls.cast(this);
247: }
248: return adapter.unwrap(cls);
249: }
250: }