Skip to content

Method: commit()

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena;
16:
17: import cz.cvut.kbss.ontodriver.Wrapper;
18: import cz.cvut.kbss.ontodriver.descriptor.*;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
21: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
22: import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
23: import cz.cvut.kbss.ontodriver.jena.list.ListHandler;
24: import cz.cvut.kbss.ontodriver.jena.query.JenaPreparedStatement;
25: import cz.cvut.kbss.ontodriver.jena.query.JenaStatement;
26: import cz.cvut.kbss.ontodriver.jena.util.IdentifierGenerator;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28: import cz.cvut.kbss.ontodriver.util.Transaction;
29:
30: import java.net.URI;
31: import java.util.Collection;
32: import java.util.List;
33: import java.util.stream.Collectors;
34:
35: /**
36: * Transformations between OntoDriver API-based values and Jena-based ones.
37: * <p>
38: * Implementation notes:
39: * <ul>
40: * <li>Datatype literal types are based on Jena's mapping, as described in a table at <a
41: * href="https://jena.apache.org/documentation/notes/typed-literals.html">https://jena.apache.org/documentation/notes/typed-literals.html</a></li>
42: * </ul>
43: */
44: public class JenaAdapter implements Wrapper {
45:
46: private final Transaction transaction = new Transaction();
47:
48: private final StorageConnector connector;
49: private final InferredStorageConnector inferenceConnector;
50:
51: JenaAdapter(StorageConnector connector, InferredStorageConnector inferenceConnector) {
52: this.connector = connector;
53: this.inferenceConnector = inferenceConnector;
54: }
55:
56: void commit() throws JenaDriverException {
57:• if (transaction.isActive()) {
58: transaction.commit();
59: connector.commit();
60: transaction.afterCommit();
61: }
62: }
63:
64: void rollback() {
65: if (transaction.isActive()) {
66: transaction.rollback();
67: connector.rollback();
68: transaction.afterRollback();
69: }
70: }
71:
72: void persist(AxiomValueDescriptor descriptor) {
73: beginTransactionIfNotActive();
74: new AxiomSaver(connector).saveAxioms(descriptor);
75: }
76:
77: private void beginTransactionIfNotActive() {
78: if (!transaction.isActive()) {
79: connector.begin();
80: transaction.begin();
81: }
82: }
83:
84: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
85: beginTransactionIfNotActive();
86: return new MainAxiomLoader(connector, inferenceConnector).find(descriptor);
87: }
88:
89: boolean contains(Axiom<?> axiom, URI context) {
90: beginTransactionIfNotActive();
91: return new MainAxiomLoader(connector, inferenceConnector).contains(axiom, context);
92: }
93:
94: List<URI> getContext() {
95: beginTransactionIfNotActive();
96: return connector.getContexts().stream().map(URI::create).collect(Collectors.toList());
97: }
98:
99: URI generateIdentifier(URI classUri) {
100: beginTransactionIfNotActive();
101: return new IdentifierGenerator(connector).generateIdentifier(classUri);
102: }
103:
104: boolean isConsistent(URI context) {
105: beginTransactionIfNotActive();
106: return inferenceConnector.isConsistent(context != null ? context.toString() : null);
107: }
108:
109: void update(AxiomValueDescriptor descriptor) {
110: beginTransactionIfNotActive();
111: new EpistemicAxiomRemover(connector).remove(descriptor);
112: new AxiomSaver(connector).saveAxioms(descriptor);
113: }
114:
115: void remove(AxiomDescriptor descriptor) {
116: beginTransactionIfNotActive();
117: new EpistemicAxiomRemover(connector).remove(descriptor);
118: }
119:
120: TypesHandler typesHandler() {
121: beginTransactionIfNotActive();
122: return new TypesHandler(connector, inferenceConnector);
123: }
124:
125: PropertiesHandler propertiesHandler() {
126: beginTransactionIfNotActive();
127: return new PropertiesHandler(connector);
128: }
129:
130: public ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> simpleListHandler() {
131: beginTransactionIfNotActive();
132: return ListHandler.simpleListHandler(connector);
133: }
134:
135: public ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> referencedListHandler() {
136: beginTransactionIfNotActive();
137: return ListHandler.referencedListHandler(connector);
138: }
139:
140: JenaStatement createStatement() {
141: beginTransactionIfNotActive();
142: return new JenaStatement(inferenceConnector);
143: }
144:
145: JenaPreparedStatement prepareStatement(String sparql) {
146: beginTransactionIfNotActive();
147: return new JenaPreparedStatement(inferenceConnector, sparql);
148: }
149:
150: void close() throws JenaDriverException {
151: connector.close();
152: }
153:
154: @Override
155: public <T> T unwrap(Class<T> cls) throws OntoDriverException {
156: if (cls.isAssignableFrom(getClass())) {
157: return cls.cast(this);
158: }
159: return connector.unwrap(cls);
160: }
161: }