Skip to content

Method: ontology()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.owlapi;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.owlapi.change.TransactionalChange;
24: import cz.cvut.kbss.ontodriver.owlapi.connector.Connector;
25: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
26: import cz.cvut.kbss.ontodriver.owlapi.container.ContainerHandler;
27: import cz.cvut.kbss.ontodriver.owlapi.exception.OwlapiDriverException;
28: import cz.cvut.kbss.ontodriver.owlapi.list.ReferencedListHandler;
29: import cz.cvut.kbss.ontodriver.owlapi.list.SimpleListHandler;
30: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiPreparedStatement;
31: import cz.cvut.kbss.ontodriver.owlapi.query.OwlapiStatement;
32: import cz.cvut.kbss.ontodriver.owlapi.query.StatementExecutorFactory;
33: import cz.cvut.kbss.ontodriver.owlapi.util.IdentifierGenerator;
34: import org.semanticweb.owlapi.model.OWLAxiom;
35: import org.semanticweb.owlapi.model.OWLDataFactory;
36: import org.semanticweb.owlapi.model.OWLOntology;
37: import org.semanticweb.owlapi.reasoner.OWLReasoner;
38:
39: import java.net.URI;
40: import java.util.ArrayList;
41: import java.util.Collection;
42: import java.util.Collections;
43: import java.util.List;
44: import java.util.Set;
45:
46: /**
47: * Adapter between OntoDriver API and OWLAPI.
48: */
49: public class OwlapiAdapter {
50:
51: private final Connector connector;
52: private OntologySnapshot ontologySnapshot;
53:
54: private StatementExecutorFactory statementExecutorFactory;
55:
56: private TransactionState transactionState = TransactionState.INITIAL;
57: private List<TransactionalChange> pendingChanges = new ArrayList<>();
58:
59: private enum TransactionState {
60: INITIAL, RUNNING
61: }
62:
63: public OwlapiAdapter(Connector connector) {
64: this.connector = connector;
65: }
66:
67: private void startTransactionIfNotActive() {
68: if (transactionState == TransactionState.INITIAL) {
69: this.ontologySnapshot = connector.getOntologySnapshot();
70: this.transactionState = TransactionState.RUNNING;
71: this.statementExecutorFactory = new StatementExecutorFactory(ontologySnapshot, connector);
72: }
73: }
74:
75: void commit() {
76: if (transactionState != TransactionState.RUNNING) {
77: return;
78: }
79: if (!pendingChanges.isEmpty()) {
80: connector.applyChanges(pendingChanges);
81: this.pendingChanges = new ArrayList<>();
82: }
83: transactionCleanup();
84: }
85:
86: private void transactionCleanup() {
87: connector.closeSnapshot(ontologySnapshot);
88: this.ontologySnapshot = null;
89: this.transactionState = TransactionState.INITIAL;
90: }
91:
92: void rollback() {
93: if (transactionState != TransactionState.RUNNING) {
94: return;
95: }
96: if (!pendingChanges.isEmpty()) {
97: pendingChanges = new ArrayList<>();
98: }
99: transactionCleanup();
100: }
101:
102: boolean isConsistent(URI context) {
103: startTransactionIfNotActive();
104:
105: return reasoner().isConsistent();
106: }
107:
108: private OWLReasoner reasoner() {
109: return ontologySnapshot.getReasoner();
110: }
111:
112: private OWLOntology ontology() {
113: return ontologySnapshot.getOntology();
114: }
115:
116: private OWLDataFactory dataFactory() {
117: return ontologySnapshot.getDataFactory();
118: }
119:
120: List<URI> getContexts() {
121: startTransactionIfNotActive();
122: return Collections.singletonList(connector.getOntologyUri());
123: }
124:
125: public boolean containsAxiom(Axiom<?> axiom, Set<URI> contexts) {
126: startTransactionIfNotActive();
127: final Collection<OWLAxiom> owlAxiom = asOwlAxioms(axiom);
128: boolean contains;
129: for (OWLAxiom ax : owlAxiom) {
130: if (axiom.getAssertion().isInferred()) {
131: contains = reasoner().isEntailed(ax);
132: } else {
133: contains = ontology().containsAxiom(ax);
134: }
135: if (contains) {
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: boolean isInferred(Axiom<?> axiom, Set<URI> contexts) {
143: startTransactionIfNotActive();
144: final Collection<OWLAxiom> owlAxiom = asOwlAxioms(axiom);
145: reasoner().flush();
146: return owlAxiom.stream().anyMatch(a -> reasoner().isEntailed(a) && !ontology().containsAxiom(a));
147: }
148:
149: private Collection<OWLAxiom> asOwlAxioms(Axiom<?> axiom) {
150: final Collection<OWLAxiom> owlAxioms = new ArrayList<>(3);
151: final AxiomAdapter axiomAdapter = new AxiomAdapter(dataFactory());
152: switch (axiom.getAssertion().getType()) {
153: case CLASS:
154: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
155: break;
156: case OBJECT_PROPERTY:
157: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
158: break;
159: case DATA_PROPERTY:
160: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
161: break;
162: case ANNOTATION_PROPERTY:
163: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
164: break;
165: default:
166: owlAxioms.add(axiomAdapter.toOwlClassAssertionAxiom(axiom));
167: owlAxioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(axiom));
168: owlAxioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(axiom));
169: owlAxioms.add(axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(axiom));
170: break;
171: }
172: return owlAxioms;
173: }
174:
175: public Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
176: startTransactionIfNotActive();
177: return new MainAxiomLoader(this, ontologySnapshot).findAxioms(descriptor);
178: }
179:
180: public void persist(AxiomValueDescriptor descriptor) {
181: startTransactionIfNotActive();
182: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
183: }
184:
185: public URI generateIdentifier(URI classUri) {
186: startTransactionIfNotActive();
187: return new IdentifierGenerator(ontology()).generateIdentifier(classUri);
188: }
189:
190: public void update(AxiomValueDescriptor descriptor) {
191: startTransactionIfNotActive();
192: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
193: new AxiomSaver(this, ontologySnapshot).persist(descriptor);
194: }
195:
196: public void remove(AxiomDescriptor descriptor) {
197: startTransactionIfNotActive();
198: new EpistemicAxiomRemover(this, ontologySnapshot).remove(descriptor);
199: }
200:
201: TypesHandler getTypesHandler() {
202: startTransactionIfNotActive();
203: return new TypesHandler(this, ontologySnapshot);
204: }
205:
206: PropertiesHandler getPropertiesHandler() {
207: startTransactionIfNotActive();
208: return new PropertiesHandler(this, ontologySnapshot);
209: }
210:
211: public void addTransactionalChanges(Collection<TransactionalChange> changes) {
212: pendingChanges.removeIf(tc -> changes.stream().anyMatch(toAdd -> toAdd.overrides(tc)));
213: pendingChanges.addAll(changes);
214: }
215:
216: public SimpleListHandler getSimpleListHandler() {
217: startTransactionIfNotActive();
218: return new SimpleListHandler(this, ontologySnapshot);
219: }
220:
221: public ReferencedListHandler getReferencedListHandler() {
222: startTransactionIfNotActive();
223: return new ReferencedListHandler(this, ontologySnapshot);
224: }
225:
226: public ContainerHandler getContainerHandler() {
227: startTransactionIfNotActive();
228: return new ContainerHandler(this, ontologySnapshot);
229: }
230:
231: public OwlapiStatement createStatement(OwlapiConnection connection) {
232: startTransactionIfNotActive();
233: return new OwlapiStatement(statementExecutorFactory, connection);
234: }
235:
236: public OwlapiPreparedStatement prepareStatement(String statement, OwlapiConnection connection) {
237: startTransactionIfNotActive();
238: return new OwlapiPreparedStatement(statementExecutorFactory, connection, statement);
239: }
240:
241: public <T> T unwrap(Class<T> cls) throws OwlapiDriverException {
242: startTransactionIfNotActive();
243: if (cls.isAssignableFrom(this.getClass())) {
244: return cls.cast(this);
245: } else if (cls.isAssignableFrom(OWLOntology.class)) {
246: return cls.cast(ontology());
247: } else if (cls.isAssignableFrom(OWLReasoner.class)) {
248: return cls.cast(reasoner());
249: }
250: throw new OwlapiDriverException("Unsupported type " + cls);
251: }
252: }