Skip to contentPackage: OwlapiAdapter$TransactionState
OwlapiAdapter$TransactionState
Coverage
1: /**
2: * Copyright (C) 2016 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.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.config.ConfigParam;
18: import cz.cvut.kbss.ontodriver.config.Configuration;
19: import cz.cvut.kbss.ontodriver.descriptor.*;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import cz.cvut.kbss.ontodriver.owlapi.connector.Connector;
22: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
23: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
24: import cz.cvut.kbss.ontodriver.owlapi.list.ListHandler;
25: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiPreparedStatement;
26: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiStatement;
27: import cz.cvut.kbss.ontodriver.owlapi.query.StatementExecutorFactory;
28: import cz.cvut.kbss.ontodriver.owlapi.util.IdentifierGenerator;
29: import org.semanticweb.owlapi.model.OWLAxiom;
30: import org.semanticweb.owlapi.model.OWLDataFactory;
31: import org.semanticweb.owlapi.model.OWLOntology;
32: import org.semanticweb.owlapi.model.OWLOntologyChange;
33: import org.semanticweb.owlapi.reasoner.OWLReasoner;
34:
35: import java.net.URI;
36: import java.util.ArrayList;
37: import java.util.Collection;
38: import java.util.Collections;
39: import java.util.List;
40:
41: /**
42: * Adapter between OntoDriver API and OWLAPI.
43: *
44: * @author ledvima1
45: */
46: public class OwlapiAdapter {
47:
48: private final Connector connector;
49: private OntologySnapshot ontologySnapshot;
50: private final String language;
51:
52: private StatementExecutorFactory statementExecutorFactory;
53:
54: private TransactionState transactionState = TransactionState.INITIAL;
55: private List<OWLOntologyChange> pendingChanges = new ArrayList<>();
56:
57: private enum TransactionState {
58: INITIAL, RUNNING
59: }
60:
61: public OwlapiAdapter(Connector connector, Configuration configuration) {
62: this.connector = connector;
63: this.language = configuration.getProperty(ConfigParam.ONTOLOGY_LANGUAGE, "en");
64: }
65:
66: private void startTransactionIfNotActive() {
67: if (transactionState == TransactionState.INITIAL) {
68: this.ontologySnapshot = connector.getOntologySnapshot();
69: this.transactionState = TransactionState.RUNNING;
70: this.statementExecutorFactory = new StatementExecutorFactory(ontologySnapshot, connector);
71: }
72: }
73:
74: void commit() {
75: if (transactionState != TransactionState.RUNNING) {
76: return;
77: }
78: if (!pendingChanges.isEmpty()) {
79: connector.applyChanges(pendingChanges);
80: this.pendingChanges = new ArrayList<>();
81: }
82: transactionCleanup();
83: }
84:
85: private void transactionCleanup() {
86: connector.closeSnapshot(ontologySnapshot);
87: this.ontologySnapshot = null;
88: this.transactionState = TransactionState.INITIAL;
89: }
90:
91: void rollback() {
92: if (transactionState != TransactionState.RUNNING) {
93: return;
94: }
95: if (!pendingChanges.isEmpty()) {
96: pendingChanges = new ArrayList<>();
97: }
98: transactionCleanup();
99: }
100:
101: boolean isConsistent(URI context) {
102: startTransactionIfNotActive();
103:
104: return ontologySnapshot.getReasoner().isConsistent();
105: }
106:
107: private OWLReasoner reasoner() {
108: return ontologySnapshot.getReasoner();
109: }
110:
111: private OWLOntology ontology() {
112: return ontologySnapshot.getOntology();
113: }
114:
115: private OWLDataFactory dataFactory() {
116: return ontologySnapshot.getDataFactory();
117: }
118:
119: List<URI> getContexts() {
120: startTransactionIfNotActive();
121: return Collections.singletonList(connector.getOntologyUri());
122: }
123:
124: boolean containsAxiom(Axiom<?> axiom, URI context) {
125: startTransactionIfNotActive();
126: final Collection<OWLAxiom> owlAxiom = asOwlAxioms(axiom);
127: boolean contains;
128: for (OWLAxiom ax : owlAxiom) {
129: if (axiom.getAssertion().isInferred()) {
130: contains = reasoner().isEntailed(ax);
131: } else {
132: contains = ontology().containsAxiom(ax);
133: }
134: if (contains) {
135: return true;
136: }
137: }
138: return false;
139: }
140:
141: private Collection<OWLAxiom> asOwlAxioms(Axiom<?> axiom) {
142: final Collection<OWLAxiom> owlAxioms = new ArrayList<>(3);
143: final AxiomAdapter axiomAdapter = new AxiomAdapter(dataFactory(), language);
144: switch (axiom.getAssertion().getType()) {
145: case CLASS:
146: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
147: break;
148: case PROPERTY:
149: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
150: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
151: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
152: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
153: break;
154: case OBJECT_PROPERTY:
155: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
156: break;
157: case DATA_PROPERTY:
158: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
159: break;
160: case ANNOTATION_PROPERTY:
161: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
162: break;
163: }
164: return owlAxioms;
165: }
166:
167: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
168: startTransactionIfNotActive();
169: return new MainAxiomLoader(this, ontologySnapshot).findAxioms(descriptor);
170: }
171:
172: void persist(AxiomValueDescriptor descriptor) {
173: startTransactionIfNotActive();
174: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
175: }
176:
177: URI generateIdentifier(URI classUri) {
178: startTransactionIfNotActive();
179: return new IdentifierGenerator(ontology()).generateIdentifier(classUri);
180: }
181:
182: void update(AxiomValueDescriptor descriptor) {
183: startTransactionIfNotActive();
184: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
185: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
186: }
187:
188: void remove(AxiomDescriptor descriptor) {
189: startTransactionIfNotActive();
190: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
191: }
192:
193: TypesHandler getTypesHandler() {
194: startTransactionIfNotActive();
195: return new TypesHandler(this, ontologySnapshot);
196: }
197:
198: PropertiesHandler getPropertiesHandler() {
199: startTransactionIfNotActive();
200: return new PropertiesHandler(this, ontologySnapshot);
201: }
202:
203: public void addTransactionalChanges(Collection<OWLOntologyChange> changes) {
204: pendingChanges.addAll(changes);
205: }
206:
207: public String getLanguage() {
208: return language;
209: }
210:
211: public ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() {
212: startTransactionIfNotActive();
213: return ListHandler.getSimpleListHandler(this, ontologySnapshot);
214: }
215:
216: public ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() {
217: startTransactionIfNotActive();
218: return ListHandler.getReferencedListHandler(this, ontologySnapshot);
219: }
220:
221: public OwlapiStatement createStatement(OwlapiConnection connection) {
222: startTransactionIfNotActive();
223: return new OwlapiStatement(statementExecutorFactory, connection);
224: }
225:
226: public OwlapiPreparedStatement prepareStatement(String statement, OwlapiConnection connection) {
227: startTransactionIfNotActive();
228: return new OwlapiPreparedStatement(statementExecutorFactory, connection, statement);
229: }
230:
231: public <T> T unwrap(Class<T> cls) throws OwlapiDriverException {
232: startTransactionIfNotActive();
233: if (cls.isAssignableFrom(this.getClass())) {
234: return cls.cast(this);
235: } else if (cls.isAssignableFrom(OWLOntology.class)) {
236: return cls.cast(ontologySnapshot.getOntology());
237: }
238: throw new OwlapiDriverException("Unsupported type " + cls);
239: }
240: }