Skip to content

Package: ReferencedListHandler

ReferencedListHandler

nameinstructionbranchcomplexitylinemethod
ReferencedListHandler(OwlapiAdapter, OntologySnapshot)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addNewNodes(ReferencedListValueDescriptor, int, NamedResource)
M: 0 C: 49
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
createListAxioms(ReferencedListValueDescriptor)
M: 0 C: 55
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
isOrigEmpty(ReferencedListValueDescriptor)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
iterator(ListDescriptor)
M: 4 C: 37
90%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 0 C: 6
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%

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.OwlapiAdapter;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
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.exception.IdentifierGenerationException;
24: import cz.cvut.kbss.ontodriver.model.Assertion;
25: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
26: import cz.cvut.kbss.ontodriver.model.NamedResource;
27: import cz.cvut.kbss.ontodriver.model.Value;
28: import org.semanticweb.owlapi.model.IRI;
29: import org.semanticweb.owlapi.model.OWLAxiom;
30: import org.semanticweb.owlapi.model.OWLOntologyChange;
31:
32: import java.util.ArrayList;
33: import java.util.List;
34:
35: class ReferencedListHandler extends ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> {
36:
37: private static final int NEXT_NODE_GENERATION_THRESHOLD = 100;
38:
39: ReferencedListHandler(OwlapiAdapter owlapiAdapter, OntologySnapshot snapshot) {
40: super(owlapiAdapter, snapshot);
41: }
42:
43: @Override
44: OwlapiListIterator iterator(ListDescriptor descriptor) {
45:• assert descriptor instanceof ReferencedListDescriptor;
46:
47: final ReferencedListDescriptor desc = (ReferencedListDescriptor) descriptor;
48:• if (desc.getListProperty().isInferred() || desc.getNextNode().isInferred() ||
49:• desc.getNodeContent().isInferred()) {
50: return new InferredReferencedListIterator(desc, snapshot, axiomAdapter);
51: } else {
52: return new ReferencedListIterator(desc, snapshot, axiomAdapter);
53: }
54: }
55:
56: @Override
57: List<OWLOntologyChange> createListAxioms(ReferencedListValueDescriptor descriptor) {
58: final ReferencedListNodeGenerator nodeGenerator = new ReferencedListNodeGenerator(
59: descriptor.getListOwner(), descriptor.getNodeContent());
60: boolean first = true;
61: NamedResource previousNode = descriptor.getListOwner();
62: nodeGenerator.setIndex(0);
63:• for (NamedResource value : descriptor.getValues()) {
64:• if (first) {
65: nodeGenerator.setProperty(descriptor.getListProperty());
66: previousNode = nodeGenerator.addListNode(previousNode, value);
67: first = false;
68: } else {
69: nodeGenerator.setProperty(descriptor.getNextNode());
70: previousNode = nodeGenerator.addListNode(previousNode, value);
71: }
72: }
73: return nodeGenerator.getChanges();
74: }
75:
76: @Override
77: boolean isOrigEmpty(ReferencedListValueDescriptor descriptor) {
78: final OwlapiListIterator it = iterator(descriptor);
79:• return !it.hasNext();
80: }
81:
82: @Override
83: void addNewNodes(ReferencedListValueDescriptor descriptor, int index, NamedResource lastNode) {
84:• if (index >= descriptor.getValues().size()) {
85: return;
86: }
87: final ReferencedListNodeGenerator nodeGenerator = new ReferencedListNodeGenerator(descriptor.getListOwner(),
88: descriptor.getNodeContent());
89: nodeGenerator.setProperty(descriptor.getNextNode()); // We know that we are past list head
90: nodeGenerator.setIndex(index);
91:• for (; index < descriptor.getValues().size(); index++) {
92: final NamedResource nextValue = descriptor.getValues().get(index);
93: lastNode = nodeGenerator.addListNode(lastNode, nextValue);
94: }
95: owlapiAdapter.addTransactionalChanges(snapshot.applyChanges(nodeGenerator.getChanges()));
96: }
97:
98: private class ReferencedListNodeGenerator {
99:
100: private final String baseUri;
101: private final List<OWLOntologyChange> changes;
102: private final Assertion nodeContentProperty;
103:
104: private int index;
105: private Assertion property;
106:
107: ReferencedListNodeGenerator(NamedResource baseUri, Assertion nodeContent) {
108: this.baseUri = baseUri.toString() + "-SEQ_";
109: this.changes = new ArrayList<>();
110: this.nodeContentProperty = nodeContent;
111: }
112:
113: private void setIndex(int index) {
114: this.index = index;
115: }
116:
117: private void setProperty(Assertion property) {
118: this.property = property;
119: }
120:
121: private List<OWLOntologyChange> getChanges() {
122: return changes;
123: }
124:
125: private NamedResource addListNode(NamedResource previousNode, NamedResource value) {
126: assert property != null;
127:
128: final NamedResource node = generateNode();
129: final OWLAxiom nodeAxiom = axiomAdapter
130: .toOwlObjectPropertyAssertionAxiom(new AxiomImpl<>(previousNode, property, new Value<>(node)));
131: changes.add(new MutableAddAxiom(ontology, nodeAxiom));
132: changes.add(generateNodeContent(node, value));
133: index++;
134: return node;
135: }
136:
137: private OWLOntologyChange generateNodeContent(NamedResource node, NamedResource value) {
138: final OWLAxiom valueAxiom = axiomAdapter
139: .toOwlObjectPropertyAssertionAxiom(new AxiomImpl<>(node, nodeContentProperty, new Value<>(value)));
140: return new MutableAddAxiom(ontology, valueAxiom);
141: }
142:
143: private NamedResource generateNode() {
144: int i = index;
145: IRI iri;
146: do {
147: iri = IRI.create(baseUri + i);
148: if (!ontology.containsIndividualInSignature(iri)) {
149: return NamedResource.create(iri.toURI());
150: }
151: i++;
152: }
153: while (i < (i + NEXT_NODE_GENERATION_THRESHOLD));
154: throw new IdentifierGenerationException(
155: "Unable to generate identifier for sequence node with base " + baseUri);
156: }
157: }
158: }