Skip to content

Package: OwlapiAdapter$TransactionState

OwlapiAdapter$TransactionState

nameinstructionbranchcomplexitylinemethod
static {...}
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

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