Skip to contentMethod: currentContent()
1: /**
2: * Copyright (C) 2020 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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
18: import cz.cvut.kbss.ontodriver.model.Assertion;
19: import cz.cvut.kbss.ontodriver.model.Axiom;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import org.eclipse.rdf4j.model.IRI;
24: import org.eclipse.rdf4j.model.Resource;
25: import org.eclipse.rdf4j.model.Statement;
26: import org.eclipse.rdf4j.model.ValueFactory;
27:
28: import java.util.ArrayList;
29: import java.util.Collection;
30: import java.util.Collections;
31: import java.util.List;
32:
33: class SimpleListIterator extends AbstractSesameIterator {
34:
35: private final SimpleListDescriptor listDescriptor;
36:
37: private IRI currentProperty;
38:
39: private Statement current;
40: private Collection<Statement> next;
41:
42: public SimpleListIterator(SimpleListDescriptor listDescriptor, Connector connector, ValueFactory vf)
43: throws SesameDriverException {
44: super(listDescriptor, connector, vf);
45: this.listDescriptor = listDescriptor;
46: this.currentProperty = hasListProperty;
47: init();
48: }
49:
50: private void init() throws SesameDriverException {
51: this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, context);
52: }
53:
54: @Override
55: public boolean hasNext() {
56: return !next.isEmpty();
57: }
58:
59: @Override
60: public Resource nextNode() throws SesameDriverException {
61: nextInternal();
62: assert current.getObject() instanceof Resource;
63:
64: return (Resource) current.getObject();
65: }
66:
67: private void nextInternal() throws SesameDriverException {
68: if (!hasNext()) {
69: throw new IllegalStateException();
70: }
71: checkSuccessorMax(next, currentProperty);
72: this.current = next.iterator().next();
73: this.currentProperty = current.getPredicate();
74: checkNodeIsResource(current);
75: final Resource elem = (Resource) current.getObject();
76: this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
77: }
78:
79: @Override
80: public Resource currentContent() {
81:• assert current.getObject() instanceof Resource;
82:
83: return (Resource) current.getObject();
84: }
85:
86: @Override
87: public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
88: nextInternal();
89: assert current.getObject() instanceof Resource;
90:
91: final Assertion assertion = current.getPredicate() == hasListProperty ? listDescriptor
92: .getListProperty() : listDescriptor.getNextNode();
93: return createAxiom(current.getSubject(), assertion, (Resource) current.getObject());
94: }
95:
96: @Override
97: public void replaceCurrentWith(NamedResource newNode) throws SesameDriverException {
98: assert current.getObject() instanceof Resource;
99: final Resource newNodeSesame = vf.createIRI(newNode.getIdentifier().toString());
100: final List<Statement> toAdd = new ArrayList<>(2);
101: final List<Statement> toRemove = new ArrayList<>(2);
102: toRemove.add(current);
103: final Statement newCurrent = vf.createStatement(current.getSubject(), currentProperty,
104: newNodeSesame, context);
105: // From the current subject to the new node
106: toAdd.add(newCurrent);
107: if (hasNext()) {
108: toRemove.addAll(next);
109: final Statement stmt = next.iterator().next();
110: checkNodeIsResource(stmt);
111: final Resource nextNode = (Resource) stmt.getObject();
112: if (!newNodeSesame.equals(nextNode)) {
113: // From the new node to the next node
114: final Statement newNext = vf.createStatement(newNodeSesame, hasNextProperty,
115: nextNode, context);
116: toAdd.add(newNext);
117: this.next = Collections.singletonList(newNext);
118: } else {
119: this.next = connector.findStatements(newNodeSesame, hasNextProperty, null,
120: includeInferred, context);
121: }
122: } else {
123: this.next = Collections.emptyList();
124: }
125: this.current = null;
126: connector.removeStatements(toRemove);
127: connector.addStatements(toAdd);
128: }
129:
130: @Override
131: public void remove() throws SesameDriverException {
132: assert current.getObject() instanceof Resource;
133: final Collection<Statement> toRemove = new ArrayList<>(next.size() + 1);
134: toRemove.add(current);
135: if (hasNext()) {
136: toRemove.addAll(next);
137: final Statement stmt = next.iterator().next();
138: checkNodeIsResource(stmt);
139: final Resource nextNode = (Resource) stmt.getObject();
140: // Here we use the current property, so that it can also be the
141: // hasList property
142: final Statement toAdd = vf.createStatement(current.getSubject(), currentProperty,
143: nextNode, context);
144: this.next = Collections.singletonList(toAdd);
145: this.current = null;
146:
147: connector.addStatements(next);
148: } else {
149: this.next = Collections.emptyList();
150: }
151: connector.removeStatements(toRemove);
152: }
153: }