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