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%
checkSuccessorMax(Collection, IRI)
M: 6 C: 58
91%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 12
92%
M: 0 C: 1
100%
currentContent()
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
currentContentToMultilingualString()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
fromRdf4jValue(Value)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNodeContent(Resource)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
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%
lambda$currentContentToMultilingualString$0(MultilingualString, Statement)
M: 8 C: 23
74%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$replaceCurrentWith$1(Resource, Value)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
nextAxiom()
M: 0 C: 57
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
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(Object)
M: 4 C: 40
91%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 8
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: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
21: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
24: import cz.cvut.kbss.ontodriver.model.MultilingualString;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27: import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
28: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
29: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
30: import org.eclipse.rdf4j.model.IRI;
31: import org.eclipse.rdf4j.model.Literal;
32: import org.eclipse.rdf4j.model.Resource;
33: import org.eclipse.rdf4j.model.Statement;
34: import org.eclipse.rdf4j.model.ValueFactory;
35:
36: import java.util.ArrayList;
37: import java.util.Collection;
38: import java.util.Collections;
39: import java.util.HashSet;
40: import java.util.Set;
41: import java.util.stream.Collectors;
42:
43: class ReferencedListIterator<T> extends AbstractListIterator<T> {
44:
45: private final ReferencedListDescriptor listDescriptor;
46:
47: private final IRI hasContentProperty;
48:
49: private IRI currentProperty;
50:
51: private Statement currentNode;
52: private Collection<Statement> currentContent;
53: private Collection<Statement> next;
54:
55: public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector, ValueFactory vf)
56: throws Rdf4jDriverException {
57: super(listDescriptor, connector, vf);
58: this.listDescriptor = listDescriptor;
59: this.hasContentProperty = Rdf4jUtils.toRdf4jIri(listDescriptor.getNodeContent().getIdentifier(), vf);
60: this.currentProperty = hasListProperty;
61: init();
62: }
63:
64: private void init() throws Rdf4jDriverException {
65: this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, contexts());
66: }
67:
68: @Override
69: public boolean hasNext() {
70:• return !next.isEmpty();
71: }
72:
73: @Override
74: public Resource nextNode() throws Rdf4jDriverException {
75: nextInternal();
76: return (Resource) currentNode.getObject();
77: }
78:
79: private void nextInternal() throws Rdf4jDriverException {
80:• if (!hasNext()) {
81: throw new IllegalStateException();
82: }
83: super.checkSuccessorMax(next, currentProperty);
84: this.currentNode = next.iterator().next();
85: this.currentProperty = currentNode.getPredicate();
86: checkObjectIsResource(currentNode);
87: final Resource elem = (Resource) currentNode.getObject();
88: this.currentContent = getNodeContent(elem);
89: this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, contexts());
90: }
91:
92: private Collection<Statement> getNodeContent(Resource node) throws Rdf4jDriverException {
93: final Collection<Statement> elements = connector.findStatements(node, hasContentProperty,
94: null, includeInferred, contexts());
95: checkSuccessorMax(elements, hasContentProperty);
96:• if (elements.isEmpty()) {
97: throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
98: }
99: return elements;
100: }
101:
102: @Override
103: protected void checkSuccessorMax(Collection<Statement> stmts, IRI property) {
104: final Set<String> langs = new HashSet<>();
105: // Remove duplicates
106: final Set<Statement> statements = new HashSet<>(stmts);
107:• if (statements.size() == 1) {
108: return;
109: }
110:• for (Statement s : statements) {
111:• if (!s.getObject().isLiteral()) {
112: throw icViolatedException(property, statements.size());
113: }
114: final Literal literal = (Literal) s.getObject();
115:• if (literal.getLanguage().isPresent() && !langs.contains(literal.getLanguage().get())) {
116: langs.add(literal.getLanguage().get());
117: } else {
118: throw icViolatedException(property, statements.size());
119: }
120: }
121: }
122:
123: @Override
124: public T currentContent() {
125:• if (currentContent.size() == 1) {
126: return (T) fromRdf4jValue(currentContent.iterator().next().getObject());
127: } else {
128: final MultilingualString mls = currentContentToMultilingualString();
129: return (T) mls;
130: }
131: }
132:
133: private Object fromRdf4jValue(org.eclipse.rdf4j.model.Value value) {
134:• return value.isLiteral() ? Rdf4jUtils.getLiteralValue((Literal) value) : NamedResource.create(value.stringValue());
135: }
136:
137: @Override
138: public Axiom<T> nextAxiom() throws Rdf4jDriverException {
139: nextInternal();
140:
141:• if (currentContent.size() == 1) {
142: final org.eclipse.rdf4j.model.Value obj = currentContent.iterator().next().getObject();
143: return new AxiomImpl(NamedResource.create(currentContent.iterator().next().getSubject().stringValue()),
144: listDescriptor.getNodeContent(),
145: new Value<>(fromRdf4jValue(obj)));
146: }
147: final MultilingualString mls = currentContentToMultilingualString();
148: return new AxiomImpl(NamedResource.create(currentContent.iterator().next().getSubject().stringValue()),
149: listDescriptor.getNodeContent(), new Value<>(mls));
150: }
151:
152: private MultilingualString currentContentToMultilingualString() {
153: final MultilingualString mls = new MultilingualString();
154: currentContent.forEach(s -> {
155:• assert s.getObject().isLiteral();
156: final Literal literal = (Literal) s.getObject();
157:• assert literal.getLanguage().isPresent();
158: mls.set(literal.getLanguage().get(), literal.getLabel());
159: });
160: return mls;
161: }
162:
163: @Override
164: public void remove() throws Rdf4jDriverException {
165:• assert currentNode.getObject() instanceof Resource;
166: final Collection<Statement> toRemove = new ArrayList<>();
167: toRemove.add(currentNode);
168: toRemove.addAll(currentContent);
169:• if (!next.isEmpty()) {
170: toRemove.addAll(next);
171: final Statement stmt = next.iterator().next();
172: checkObjectIsResource(stmt);
173: final Resource nextNode = (Resource) stmt.getObject();
174: final Statement connectNext = vf
175: .createStatement(currentNode.getSubject(), currentProperty, nextNode, context);
176: this.next = Collections.singleton(connectNext);
177:
178: this.currentNode = null;
179: this.currentContent = null;
180: connector.addStatements(next);
181: } else {
182: next = Collections.emptyList();
183: }
184: connector.removeStatements(toRemove);
185: }
186:
187: @Override
188: public void replaceCurrentWith(T newContent) throws Rdf4jDriverException {
189:• assert currentNode.getObject() instanceof Resource;
190: // We just replace the original content statement with new one
191: connector.removeStatements(currentContent);
192: final Collection<org.eclipse.rdf4j.model.Value> contentValues = new ReferencedListHelper(valueConverter).toRdf4jValue(listDescriptor.getNodeContent(), newContent);
193: final Resource node = (Resource) currentNode.getObject();
194: connector.addStatements(contentValues.stream()
195: .map(v -> vf.createStatement(node, hasContentProperty, v, context))
196: .collect(Collectors.toList()));
197: }
198: }