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