Skip to content

Package: ReferencedListIterator

ReferencedListIterator

nameinstructionbranchcomplexitylinemethod
ReferencedListIterator(ReferencedListDescriptor, Connector, ValueFactory)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
currentContent()
M: 4 C: 11
73%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getNodeContent(Resource)
M: 0 C: 44
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
hasNext()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
init()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
nextAxiom()
M: 4 C: 21
84%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
nextInternal()
M: 4 C: 49
92%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 9
90%
M: 0 C: 1
100%
nextNode()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove()
M: 4 C: 78
95%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 19
100%
M: 0 C: 1
100%
replaceCurrentWith(NamedResource)
M: 4 C: 37
90%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 7
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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.rdf4j;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
18: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
19: import cz.cvut.kbss.ontodriver.model.Axiom;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
22: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
23: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
24: import org.eclipse.rdf4j.model.IRI;
25: import org.eclipse.rdf4j.model.Resource;
26: import org.eclipse.rdf4j.model.Statement;
27: import org.eclipse.rdf4j.model.ValueFactory;
28:
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.Collections;
32:
33: class ReferencedListIterator extends AbstractListIterator {
34:
35: private final ReferencedListDescriptor listDescriptor;
36:
37: private final IRI hasContentProperty;
38:
39: private IRI currentProperty;
40:
41: private Statement currentNode;
42: private Statement currentContent;
43: private Collection<Statement> next;
44:
45: public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector, ValueFactory vf)
46: throws Rdf4jDriverException {
47: super(listDescriptor, connector, vf);
48: this.listDescriptor = listDescriptor;
49: this.hasContentProperty = Rdf4jUtils.toRdf4jIri(listDescriptor.getNodeContent().getIdentifier(), vf);
50: this.currentProperty = hasListProperty;
51: init();
52: }
53:
54: private void init() throws Rdf4jDriverException {
55: this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, contexts());
56: }
57:
58: @Override
59: public boolean hasNext() {
60:• return !next.isEmpty();
61: }
62:
63: @Override
64: public Resource nextNode() throws Rdf4jDriverException {
65: nextInternal();
66: return (Resource) currentNode.getObject();
67: }
68:
69: private void nextInternal() throws Rdf4jDriverException {
70:• if (!hasNext()) {
71: throw new IllegalStateException();
72: }
73: checkSuccessorMax(next, currentProperty);
74: this.currentNode = next.iterator().next();
75: this.currentProperty = currentNode.getPredicate();
76: checkNodeIsResource(currentNode);
77: final Resource elem = (Resource) currentNode.getObject();
78: this.currentContent = getNodeContent(elem);
79: this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, contexts());
80: }
81:
82: private Statement getNodeContent(Resource node) throws Rdf4jDriverException {
83: final Collection<Statement> elements = connector.findStatements(node, hasContentProperty,
84: null, includeInferred, contexts());
85: checkSuccessorMax(elements, hasContentProperty);
86:• if (elements.isEmpty()) {
87: throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
88: }
89: final Statement elem = elements.iterator().next();
90: checkNodeIsResource(elem);
91: return elem;
92: }
93:
94: @Override
95: public Resource currentContent() {
96:• assert currentContent.getObject() instanceof Resource;
97: return (Resource) currentContent.getObject();
98: }
99:
100: @Override
101: public Axiom<NamedResource> nextAxiom() throws Rdf4jDriverException {
102: nextInternal();
103:• assert currentContent.getObject() instanceof Resource;
104:
105: return createAxiom(currentContent.getSubject(), listDescriptor.getNodeContent(),
106: (Resource) currentContent.getObject());
107: }
108:
109: @Override
110: public void remove() throws Rdf4jDriverException {
111:• assert currentNode.getObject() instanceof Resource;
112: final Collection<Statement> toRemove = new ArrayList<>();
113: toRemove.add(currentNode);
114: toRemove.add(currentContent);
115:• if (!next.isEmpty()) {
116: toRemove.addAll(next);
117: final Statement stmt = next.iterator().next();
118: checkNodeIsResource(stmt);
119: final Resource nextNode = (Resource) stmt.getObject();
120: final Statement connectNext = vf
121: .createStatement(currentNode.getSubject(), currentProperty, nextNode, context);
122: this.next = Collections.singleton(connectNext);
123:
124: this.currentNode = null;
125: this.currentContent = null;
126: connector.addStatements(next);
127: } else {
128: next = Collections.emptyList();
129: }
130: connector.removeStatements(toRemove);
131: }
132:
133: @Override
134: public void replaceCurrentWith(NamedResource newContent) throws Rdf4jDriverException {
135:• assert currentNode.getObject() instanceof Resource;
136: // We just replace the original content statement with new one
137: connector.removeStatements(Collections.singleton(currentContent));
138: final Resource node = (Resource) currentNode.getObject();
139: final Statement stmt = vf
140: .createStatement(node, hasContentProperty, Rdf4jUtils.toRdf4jIri(newContent.getIdentifier(), vf),
141: context);
142: connector.addStatements(Collections.singleton(stmt));
143: }
144:
145: }