-
Notifications
You must be signed in to change notification settings - Fork 14
/
ReferencedListIterator.java
76 lines (64 loc) · 2.85 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
package cz.cvut.kbss.ontodriver.jena.list;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.model.*;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import java.util.Collection;
import java.util.Collections;
import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
class ReferencedListIterator extends AbstractListIterator {
private final Property hasContent;
private final Assertion hasContentAssertion;
ReferencedListIterator(ReferencedListDescriptor descriptor, StorageConnector connector) {
super(descriptor, connector);
this.hasContentAssertion = descriptor.getNodeContent();
this.hasContent = createProperty(hasContentAssertion.getIdentifier().toString());
}
@Override
Axiom<NamedResource> nextAxiom() {
final NamedResource value = nextValue();
final NamedResource node = NamedResource.create(currentNode.getURI());
return new AxiomImpl<>(node, hasContentAssertion, new Value<>(value));
}
@Override
NamedResource nextValue() {
resolveNextListNode();
return NamedResource.create(resolveNodeContent().getURI());
}
private Resource resolveNodeContent() {
final Collection<Statement> contentStatements;
if (context != null) {
contentStatements = connector.find(currentNode, hasContent, null, context);
} else {
contentStatements = connector.find(currentNode, hasContent, null);
}
verifyContentValueCount(contentStatements);
final Statement statement = contentStatements.iterator().next();
assert statement.getObject().isResource();
return statement.getObject().asResource();
}
private void verifyContentValueCount(Collection<Statement> contentStatements) {
if (contentStatements.isEmpty()) {
throw new IntegrityConstraintViolatedException("No content found for list node " + currentNode.getURI());
}
if (contentStatements.size() > 1) {
throw new IntegrityConstraintViolatedException(
"Encountered multiple content values of list node " + currentNode.getURI());
}
}
@Override
void removeWithoutReconnect() {
super.removeWithoutReconnect();
remove(currentNode, hasContent, null);
}
@Override
void replace(Resource replacement) {
remove(currentNode, hasContent, null);
final Statement toAdd = createStatement(currentNode, hasContent, replacement);
connector.add(Collections.singletonList(toAdd));
}
}