Skip to content

Method: getSimpleListHandler()

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.exception.OWLIndividualExistsException;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.owlapi.connector.Connector;
24: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
25: import cz.cvut.kbss.ontodriver.owlapi.list.ListHandler;
26: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiPreparedStatement;
27: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiStatement;
28: import cz.cvut.kbss.ontodriver.owlapi.query.StatementExecutorFactory;
29: import cz.cvut.kbss.ontodriver.owlapi.util.IdentifierGenerator;
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.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: this.ontologySnapshot = null;
86: this.transactionState = TransactionState.INITIAL;
87: }
88:
89: void rollback() {
90: if (transactionState != TransactionState.RUNNING) {
91: return;
92: }
93: if (!pendingChanges.isEmpty()) {
94: pendingChanges = new ArrayList<>();
95: }
96: transactionCleanup();
97: }
98:
99: boolean isConsistent(URI context) {
100: startTransactionIfNotActive();
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 IRI getOntologyIri() {
118: assert ontologySnapshot != null;
119: assert ontology().getOntologyID().getOntologyIRI().isPresent();
120:
121: return ontology().getOntologyID().getOntologyIRI().get();
122: }
123:
124: List<URI> getContexts() {
125: startTransactionIfNotActive();
126: return Collections.singletonList(getOntologyIri().toURI());
127: }
128:
129: boolean containsAxiom(Axiom<?> axiom, URI context) {
130: startTransactionIfNotActive();
131: final Collection<OWLAxiom> owlAxiom = asOwlAxioms(axiom);
132: boolean contains;
133: for (OWLAxiom ax : owlAxiom) {
134: if (axiom.getAssertion().isInferred()) {
135: contains = reasoner().isEntailed(ax);
136: } else {
137: contains = ontology().containsAxiom(ax);
138: }
139: if (contains) {
140: return true;
141: }
142: }
143: return false;
144: }
145:
146: private Collection<OWLAxiom> asOwlAxioms(Axiom<?> axiom) {
147: final Collection<OWLAxiom> owlAxioms = new ArrayList<>(3);
148: final AxiomAdapter axiomAdapter = new AxiomAdapter(dataFactory(), language);
149: switch (axiom.getAssertion().getType()) {
150: case CLASS:
151: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
152: break;
153: case PROPERTY:
154: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
155: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
156: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
157: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
158: break;
159: case OBJECT_PROPERTY:
160: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
161: break;
162: case DATA_PROPERTY:
163: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
164: break;
165: case ANNOTATION_PROPERTY:
166: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
167: break;
168: }
169: return owlAxioms;
170: }
171:
172: Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
173: startTransactionIfNotActive();
174: return new MainAxiomLoader(this, ontologySnapshot).findAxioms(descriptor);
175: }
176:
177: void persist(AxiomValueDescriptor descriptor) {
178: startTransactionIfNotActive();
179: if (instanceExists(descriptor.getSubject())) {
180: throw new OWLIndividualExistsException(
181: "Individual " + descriptor.getSubject() + " already exists in the ontology.");
182: }
183: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
184: }
185:
186: /**
187: * An individual has to have explicit type(s) to exist in this sense.
188: */
189: private boolean instanceExists(NamedResource subject) {
190: final IRI iri = IRI.create(subject.getIdentifier());
191: if (!ontology().containsIndividualInSignature(iri)) {
192: return false;
193: }
194: final OWLNamedIndividual ind = dataFactory().getOWLNamedIndividual(iri);
195: return (!EntitySearcher.getTypes(ind, ontology()).isEmpty());
196: }
197:
198: URI generateIdentifier(URI classUri) {
199: startTransactionIfNotActive();
200: return new IdentifierGenerator(ontology()).generateIdentifier(classUri);
201: }
202:
203: void update(AxiomValueDescriptor descriptor) {
204: startTransactionIfNotActive();
205: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
206: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
207: }
208:
209: void remove(AxiomDescriptor descriptor) {
210: startTransactionIfNotActive();
211: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
212: }
213:
214: TypesHandler getTypesHandler() {
215: startTransactionIfNotActive();
216: return new TypesHandler(this, ontologySnapshot);
217: }
218:
219: PropertiesHandler getPropertiesHandler() {
220: startTransactionIfNotActive();
221: return new PropertiesHandler(this, ontologySnapshot);
222: }
223:
224: public void addTransactionalChanges(Collection<OWLOntologyChange> changes) {
225: pendingChanges.addAll(changes);
226: }
227:
228: public String getLanguage() {
229: return language;
230: }
231:
232: public ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler() {
233: startTransactionIfNotActive();
234: return ListHandler.getSimpleListHandler(this, ontologySnapshot);
235: }
236:
237: public ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler() {
238: startTransactionIfNotActive();
239: return ListHandler.getReferencedListHandler(this, ontologySnapshot);
240: }
241:
242: public OwlapiStatement createStatement(OwlapiConnection connection) {
243: startTransactionIfNotActive();
244: return new OwlapiStatement(statementExecutorFactory, connection);
245: }
246:
247: public OwlapiPreparedStatement prepareStatement(String statement, OwlapiConnection connection) {
248: startTransactionIfNotActive();
249: return new OwlapiPreparedStatement(statementExecutorFactory, connection, statement);
250: }
251: }