Package: ReferencedListHandler
ReferencedListHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ReferencedListHandler(OwlapiAdapter, OntologySnapshot) |
|
|
|
|
|
||||||||||||||||||||
addNewNodes(ReferencedListValueDescriptor, int, NamedResource) |
|
|
|
|
|
||||||||||||||||||||
createListAxioms(ReferencedListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
isOrigEmpty(ReferencedListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
iterator(ListDescriptor) |
|
|
|
|
|
||||||||||||||||||||
loadList(ReferencedListDescriptor) |
|
|
|
|
|
||||||||||||||||||||
mergeLists(ReferencedListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
persistList(ReferencedListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
removeObsoleteNodes(ReferencedListIterator) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
updateList(ReferencedListValueDescriptor) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2023 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.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
22: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
26: import cz.cvut.kbss.ontodriver.owlapi.OwlapiAdapter;
27: import cz.cvut.kbss.ontodriver.owlapi.change.TransactionalChange;
28: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
29: import org.semanticweb.owlapi.model.OWLOntology;
30:
31: import java.util.ArrayList;
32: import java.util.List;
33:
34: public class ReferencedListHandler {
35:
36: static final int NEXT_NODE_GENERATION_THRESHOLD = 100;
37:
38: protected final OwlapiAdapter owlapiAdapter;
39: protected final AxiomAdapter axiomAdapter;
40:
41: protected final OWLOntology ontology;
42:
43: protected final OntologySnapshot snapshot;
44:
45: public ReferencedListHandler(OwlapiAdapter owlapiAdapter, OntologySnapshot snapshot) {
46: this.owlapiAdapter = owlapiAdapter;
47: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory());
48: this.snapshot = snapshot;
49: this.ontology = snapshot.getOntology();
50: }
51:
52: public List<Axiom<?>> loadList(ReferencedListDescriptor descriptor) {
53: final List<Axiom<?>> list = new ArrayList<>();
54: final ReferencedListIterator<?> iterator = iterator(descriptor);
55:• while (iterator.hasNext()) {
56: list.add(iterator.next());
57: }
58: return list;
59: }
60:
61: public <V> void persistList(ReferencedListValueDescriptor<V> descriptor) {
62:• if (descriptor.getValues().isEmpty()) {
63: return;
64: }
65: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(createListAxioms(descriptor)));
66: }
67:
68: <V> ReferencedListIterator<V> iterator(ListDescriptor descriptor) {
69:• assert descriptor instanceof ReferencedListDescriptor;
70:
71: final ReferencedListDescriptor desc = (ReferencedListDescriptor) descriptor;
72:• if (desc.getListProperty().isInferred() || desc.getNextNode().isInferred() ||
73:• desc.getNodeContent().isInferred()) {
74: return new InferredReferencedListIterator<>(desc, snapshot, axiomAdapter);
75: } else {
76: return new ReferencedListIterator<>(desc, snapshot, axiomAdapter);
77: }
78: }
79:
80: <V> List<TransactionalChange> createListAxioms(ReferencedListValueDescriptor<V> descriptor) {
81: final ReferencedListNodeGenerator nodeGenerator = new ReferencedListNodeGenerator(descriptor, axiomAdapter, ontology);
82: NamedResource previousNode = descriptor.getListOwner();
83: nodeGenerator.setIndex(0);
84:• for (V value : descriptor.getValues()) {
85: previousNode = nodeGenerator.addListNode(previousNode, value);
86: }
87: return nodeGenerator.getChanges();
88: }
89:
90: public <V> void updateList(ReferencedListValueDescriptor<V> descriptor) {
91:• if (descriptor.getValues().isEmpty()) {
92: removeObsoleteNodes(iterator(descriptor));
93:• } else if (isOrigEmpty(descriptor)) {
94: persistList(descriptor);
95: } else {
96: mergeLists(descriptor);
97: }
98: }
99:
100: <V> boolean isOrigEmpty(ReferencedListValueDescriptor<V> descriptor) {
101: final ReferencedListIterator<V> it = iterator(descriptor);
102:• return !it.hasNext();
103: }
104:
105: <V> void addNewNodes(ReferencedListValueDescriptor<V> descriptor, int index, NamedResource lastNode) {
106:• if (index >= descriptor.getValues().size()) {
107: return;
108: }
109: final ReferencedListNodeGenerator nodeGenerator = new ReferencedListNodeGenerator(descriptor, axiomAdapter, ontology);
110: nodeGenerator.setIndex(index);
111:• for (; index < descriptor.getValues().size(); index++) {
112: final V nextValue = descriptor.getValues().get(index);
113: lastNode = nodeGenerator.addListNode(lastNode, nextValue);
114: }
115: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(nodeGenerator.getChanges()));
116: }
117:
118: private <V> void mergeLists(ReferencedListValueDescriptor<V> descriptor) {
119: final ReferencedListIterator<V> it = iterator(descriptor);
120: final List<V> values = descriptor.getValues();
121: final List<TransactionalChange> changes = new ArrayList<>(values.size());
122: int i = 0;
123: NamedResource lastNode = null;
124:• while (it.hasNext() && i < values.size()) {
125: final V newValue = values.get(i);
126: final V currentValue = it.nextValue();
127:• if (!newValue.equals(currentValue)) {
128: changes.addAll(snapshot.applyChanges(it.replaceNode(newValue)));
129: }
130: lastNode = it.getCurrentNode();
131: i++;
132: }
133: owlapiAdapter.addTransactionalChanges(changes);
134:• assert lastNode != null;
135: removeObsoleteNodes(it);
136: addNewNodes(descriptor, i, lastNode);
137: }
138:
139: private <V> void removeObsoleteNodes(ReferencedListIterator<V> iterator) {
140:• if (!iterator.hasNext()) {
141: return;
142: }
143: final List<TransactionalChange> changes = new ArrayList<>();
144:• while (iterator.hasNext()) {
145: iterator.next();
146: changes.addAll(iterator.removeWithoutReconnect());
147: }
148: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(changes));
149: }
150:
151: }