-
Notifications
You must be signed in to change notification settings - Fork 14
/
ReferencedListIterator.java
147 lines (126 loc) · 5.17 KB
/
ReferencedListIterator.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
/**
* 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
class ReferencedListIterator extends AbstractSesameIterator {
private final ReferencedListDescriptor listDescriptor;
private final URI hasContentProperty;
private URI currentProperty;
private Statement currentNode;
private Statement currentContent;
private Collection<Statement> next;
public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector,
ValueFactory vf) throws SesameDriverException {
super(listDescriptor, connector, vf);
this.listDescriptor = listDescriptor;
this.hasContentProperty = SesameUtils.toSesameUri(listDescriptor.getNodeContent()
.getIdentifier(), vf);
this.currentProperty = hasListProperty;
init();
}
private void init() throws SesameDriverException {
this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred,
context);
}
@Override
public boolean hasNext() throws SesameDriverException {
return (!next.isEmpty());
}
@Override
public Resource nextNode() throws SesameDriverException {
nextInternal();
return (Resource) currentNode.getObject();
}
private void nextInternal() throws SesameDriverException {
if (!hasNext()) {
throw new IllegalStateException();
}
checkSuccessorMax(next, currentProperty);
this.currentNode = next.iterator().next();
this.currentProperty = currentNode.getPredicate();
checkNodeIsResource(currentNode);
final Resource elem = (Resource) currentNode.getObject();
this.currentContent = getNodeContent(elem);
this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
}
private Statement getNodeContent(Resource node) throws SesameDriverException {
final Collection<Statement> elems = connector.findStatements(node, hasContentProperty,
null, includeInferred, context);
checkSuccessorMax(elems, hasContentProperty);
if (elems.isEmpty()) {
throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
}
final Statement elem = elems.iterator().next();
checkNodeIsResource(elem);
return elem;
}
@Override
public Resource currentContent() throws SesameDriverException {
assert currentContent.getObject() instanceof Resource;
return (Resource) currentContent.getObject();
}
@Override
public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
nextInternal();
assert currentContent.getObject() instanceof Resource;
return createAxiom(currentContent.getSubject(), listDescriptor.getNodeContent(),
(Resource) currentContent.getObject());
}
@Override
public void remove() throws SesameDriverException {
assert currentNode.getObject() instanceof Resource;
final Collection<Statement> toRemove = new ArrayList<>();
toRemove.add(currentNode);
toRemove.add(currentContent);
if (!next.isEmpty()) {
toRemove.addAll(next);
final Statement stmt = next.iterator().next();
checkNodeIsResource(stmt);
final Resource nextNode = (Resource) stmt.getObject();
final Statement connectNext = vf.createStatement(currentNode.getSubject(),
currentProperty, nextNode, context);
this.next = Collections.singleton(connectNext);
this.currentNode = null;
this.currentContent = null;
connector.addStatements(next);
} else {
next = Collections.emptyList();
}
connector.removeStatements(toRemove);
}
@Override
public void replaceCurrentWith(NamedResource newContent) throws SesameDriverException {
assert currentNode.getObject() instanceof Resource;
// We just replace the original content statement with new one
connector.removeStatements(Collections.singleton(currentContent));
final Resource node = (Resource) currentNode.getObject();
final Statement stmt = vf.createStatement(node, hasContentProperty,
SesameUtils.toSesameUri(newContent.getIdentifier(), vf), context);
connector.addStatements(Collections.singleton(stmt));
}
}