Skip to content

Package: OwlapiAdapter

OwlapiAdapter

nameinstructionbranchcomplexitylinemethod
OwlapiAdapter(Connector, Configuration)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
addTransactionalChanges(Collection)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
asOwlAxioms(Axiom)
M: 25 C: 49
66%
M: 2 C: 4
67%
M: 2 C: 4
67%
M: 5 C: 11
69%
M: 0 C: 1
100%
commit()
M: 0 C: 22
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
100%
M: 0 C: 1
100%
containsAxiom(Axiom, URI)
M: 0 C: 38
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
createStatement(OwlapiConnection)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
dataFactory()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
find(AxiomDescriptor)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
generateIdentifier(URI)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getLanguage()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getOntologyIri()
M: 8 C: 18
69%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
getPropertiesHandler()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getReferencedListHandler()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getSimpleListHandler()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getTypesHandler()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
instanceExists(NamedResource)
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
isConsistent(URI)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
ontology()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
persist(AxiomValueDescriptor)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
prepareStatement(String, OwlapiConnection)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
reasoner()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
remove(AxiomDescriptor)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
rollback()
M: 0 C: 17
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
startTransactionIfNotActive()
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
transactionCleanup()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
update(AxiomValueDescriptor)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

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.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: }