Skip to contentMethod: nextValue()
1: /**
2: * Copyright (C) 2019 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.jena.list;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
19: import cz.cvut.kbss.ontodriver.model.*;
20: import org.apache.jena.rdf.model.RDFNode;
21: import org.apache.jena.rdf.model.Resource;
22: import org.apache.jena.rdf.model.Statement;
23:
24: import java.net.URI;
25: import java.util.ArrayList;
26: import java.util.Collections;
27: import java.util.List;
28:
29: import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
30:
31: class SimpleListIterator extends AbstractListIterator {
32:
33: SimpleListIterator(SimpleListDescriptor descriptor, StorageConnector connector) {
34: super(descriptor, connector);
35: }
36:
37: @Override
38: Axiom<NamedResource> nextAxiom() {
39: final NamedResource subject = NamedResource.create(currentNode.getURI());
40: final Assertion assertion = Assertion
41: .createObjectPropertyAssertion(URI.create(first() ? hasListProperty.getURI() : hasNextProperty.getURI()), false);
42: final NamedResource value = nextValue();
43: return new AxiomImpl<>(subject, assertion, new Value<>(value));
44: }
45:
46: @Override
47: NamedResource nextValue() {
48: resolveNextListNode();
49: return NamedResource.create(currentNode.getURI());
50: }
51:
52: /**
53: * Replaces the current node with the specified replacement, connecting it into the list. Original links to the current
54: * element are removed.
55: *
56: * @param replacement The replacement node
57: */
58: @Override
59: void replace(Resource replacement) {
60: removeWithoutReconnect();
61: final List<Statement> toAdd = new ArrayList<>(2);
62: toAdd.add(createStatement(previousNode, index == 0 ? hasListProperty : hasNextProperty, replacement));
63: if (hasNext()) {
64: verifySuccessorCount();
65: final RDFNode nextNode = cursor.iterator().next().getObject();
66: remove(currentNode, hasNextProperty, nextNode);
67: if (!nextNode.equals(replacement)) {
68: final Statement linkToNext = createStatement(replacement, hasNextProperty, nextNode);
69: toAdd.add(linkToNext);
70: this.cursor = Collections.singleton(linkToNext);
71: } else {
72: moveCursor(replacement);
73: }
74: }
75: this.currentNode = replacement;
76: connector.add(toAdd, context);
77: }
78: }