-
Notifications
You must be signed in to change notification settings - Fork 14
/
SimpleListIterator.java
153 lines (134 loc) · 5.88 KB
/
SimpleListIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
class SimpleListIterator extends AbstractSesameIterator {
private final SimpleListDescriptor listDescriptor;
private IRI currentProperty;
private Statement current;
private Collection<Statement> next;
public SimpleListIterator(SimpleListDescriptor listDescriptor, Connector connector, ValueFactory vf)
throws SesameDriverException {
super(listDescriptor, connector, vf);
this.listDescriptor = listDescriptor;
this.currentProperty = hasListProperty;
init();
}
private void init() throws SesameDriverException {
this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, context);
}
@Override
public boolean hasNext() {
return !next.isEmpty();
}
@Override
public Resource nextNode() throws SesameDriverException {
nextInternal();
assert current.getObject() instanceof Resource;
return (Resource) current.getObject();
}
private void nextInternal() throws SesameDriverException {
if (!hasNext()) {
throw new IllegalStateException();
}
checkSuccessorMax(next, currentProperty);
this.current = next.iterator().next();
this.currentProperty = current.getPredicate();
checkNodeIsResource(current);
final Resource elem = (Resource) current.getObject();
this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
}
@Override
public Resource currentContent() throws SesameDriverException {
assert current.getObject() instanceof Resource;
return (Resource) current.getObject();
}
@Override
public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
nextInternal();
assert current.getObject() instanceof Resource;
final Assertion assertion = current.getPredicate() == hasListProperty ? listDescriptor
.getListProperty() : listDescriptor.getNextNode();
return createAxiom(current.getSubject(), assertion, (Resource) current.getObject());
}
@Override
public void replaceCurrentWith(NamedResource newNode) throws SesameDriverException {
assert current.getObject() instanceof Resource;
final Resource newNodeSesame = vf.createIRI(newNode.getIdentifier().toString());
final List<Statement> toAdd = new ArrayList<>(2);
final List<Statement> toRemove = new ArrayList<>(2);
toRemove.add(current);
final Statement newCurrent = vf.createStatement(current.getSubject(), currentProperty,
newNodeSesame, context);
// From the current subject to the new node
toAdd.add(newCurrent);
if (hasNext()) {
toRemove.addAll(next);
final Statement stmt = next.iterator().next();
checkNodeIsResource(stmt);
final Resource nextNode = (Resource) stmt.getObject();
if (!newNodeSesame.equals(nextNode)) {
// From the new node to the next node
final Statement newNext = vf.createStatement(newNodeSesame, hasNextProperty,
nextNode, context);
toAdd.add(newNext);
this.next = Collections.singletonList(newNext);
} else {
this.next = connector.findStatements(newNodeSesame, hasNextProperty, null,
includeInferred, context);
}
} else {
this.next = Collections.emptyList();
}
this.current = null;
connector.removeStatements(toRemove);
connector.addStatements(toAdd);
}
@Override
public void remove() throws SesameDriverException {
assert current.getObject() instanceof Resource;
final Collection<Statement> toRemove = new ArrayList<>(next.size() + 1);
toRemove.add(current);
if (hasNext()) {
toRemove.addAll(next);
final Statement stmt = next.iterator().next();
checkNodeIsResource(stmt);
final Resource nextNode = (Resource) stmt.getObject();
// Here we use the current property, so that it can also be the
// hasList property
final Statement toAdd = vf.createStatement(current.getSubject(), currentProperty,
nextNode, context);
this.next = Collections.singletonList(toAdd);
this.current = null;
connector.addStatements(next);
} else {
this.next = Collections.emptyList();
}
connector.removeStatements(toRemove);
}
}