Package: ListHandler
ListHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ListHandler(OwlapiAdapter, OntologySnapshot) |
|
|
|
|
|
||||||||||||||||||||
getReferencedListHandler(OwlapiAdapter, OntologySnapshot) |
|
|
|
|
|
||||||||||||||||||||
getSimpleListHandler(OwlapiAdapter, OntologySnapshot) |
|
|
|
|
|
||||||||||||||||||||
loadList(ListDescriptor) |
|
|
|
|
|
||||||||||||||||||||
mergeLists(ListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
persistList(ListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
removeObsoleteNodes(OwlapiListIterator) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
updateList(ListValueDescriptor) |
|
|
|
|
|
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.list;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
18: import cz.cvut.kbss.ontodriver.owlapi.OwlapiAdapter;
19: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
20: import cz.cvut.kbss.ontodriver.descriptor.*;
21: import cz.cvut.kbss.ontodriver.model.Axiom;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import org.semanticweb.owlapi.model.OWLOntology;
24: import org.semanticweb.owlapi.model.OWLOntologyChange;
25:
26: import java.util.ArrayList;
27: import java.util.List;
28:
29: public abstract class ListHandler<D extends ListDescriptor, V extends ListValueDescriptor> {
30:
31: protected final OwlapiAdapter owlapiAdapter;
32: protected final AxiomAdapter axiomAdapter;
33:
34: protected final OWLOntology ontology;
35:
36: protected final OntologySnapshot snapshot;
37:
38: protected ListHandler(OwlapiAdapter owlapiAdapter, OntologySnapshot snapshot) {
39: this.owlapiAdapter = owlapiAdapter;
40: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory(), owlapiAdapter.getLanguage());
41: this.snapshot = snapshot;
42: this.ontology = snapshot.getOntology();
43: }
44:
45: public List<Axiom<NamedResource>> loadList(D descriptor) {
46: final List<Axiom<NamedResource>> list = new ArrayList<>();
47: final OwlapiListIterator iterator = iterator(descriptor);
48:• while (iterator.hasNext()) {
49: list.add(iterator.next());
50: }
51: return list;
52: }
53:
54: public void persistList(V descriptor) {
55:• if (descriptor.getValues().isEmpty()) {
56: return;
57: }
58: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(createListAxioms(descriptor)));
59: }
60:
61: abstract OwlapiListIterator iterator(ListDescriptor descriptor);
62:
63: abstract List<OWLOntologyChange> createListAxioms(V descriptor);
64:
65: public void updateList(V descriptor) {
66:• if (descriptor.getValues().isEmpty()) {
67: removeObsoleteNodes(iterator(descriptor));
68:• } else if (isOrigEmpty(descriptor)) {
69: persistList(descriptor);
70: } else {
71: mergeLists(descriptor);
72: }
73: }
74:
75: abstract boolean isOrigEmpty(V descriptor);
76:
77: private void mergeLists(V descriptor) {
78: final OwlapiListIterator it = iterator(descriptor);
79: final List<NamedResource> values = descriptor.getValues();
80: final List<OWLOntologyChange> changes = new ArrayList<>(values.size());
81: int i = 0;
82: NamedResource lastNode = null;
83:• while (it.hasNext() && i < values.size()) {
84: final NamedResource newValue = values.get(i);
85: final NamedResource currentValue = it.nextValue();
86:• if (!newValue.equals(currentValue)) {
87: changes.addAll(snapshot.applyChanges(it.replaceNode(newValue)));
88: }
89: lastNode = it.getCurrentNode();
90: i++;
91: }
92: owlapiAdapter.addTransactionalChanges(changes);
93:• assert lastNode != null;
94: removeObsoleteNodes(it);
95: addNewNodes(descriptor, i, lastNode);
96: }
97:
98: private void removeObsoleteNodes(OwlapiListIterator iterator) {
99:• if (!iterator.hasNext()) {
100: return;
101: }
102: final List<OWLOntologyChange> changes = new ArrayList<>();
103:• while (iterator.hasNext()) {
104: iterator.next();
105: changes.addAll(iterator.removeWithoutReconnect());
106: }
107: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
108: }
109:
110: abstract void addNewNodes(V descriptor, int index, NamedResource lastNode);
111:
112: public static ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> getSimpleListHandler(
113: OwlapiAdapter adapter, OntologySnapshot snapshot) {
114: return new SimpleListHandler(adapter, snapshot);
115: }
116:
117: public static ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> getReferencedListHandler(
118: OwlapiAdapter adapter, OntologySnapshot snapshot) {
119: return new ReferencedListHandler(adapter, snapshot);
120: }
121: }