Skip to content

Package: SimpleListHandler

SimpleListHandler

nameinstructionbranchcomplexitylinemethod
SimpleListHandler(Connector, ValueFactory)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
appendNewNodes(SimpleListValueDescriptor, ListHandler.MergeResult)
M: 0 C: 60
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
clearList(SimpleListValueDescriptor)
M: 0 C: 55
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
createIterator(SimpleListDescriptor)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createListHead(SimpleListValueDescriptor, Collection)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createListRest(IRI, SimpleListValueDescriptor)
M: 0 C: 49
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
iterator(SimpleListValueDescriptor)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
mergeWithOriginalList(SimpleListValueDescriptor, ListIterator)
M: 0 C: 44
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
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.SimpleListDescriptor;
18: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.rdf4j.connector.Connector;
21: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
22: import org.eclipse.rdf4j.model.IRI;
23: import org.eclipse.rdf4j.model.Resource;
24: import org.eclipse.rdf4j.model.Statement;
25: import org.eclipse.rdf4j.model.ValueFactory;
26:
27: import java.util.*;
28:
29: class SimpleListHandler extends ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> {
30:
31: SimpleListHandler(Connector connector, ValueFactory vf) {
32: super(connector, vf);
33: }
34:
35: @Override
36: ListIterator createIterator(SimpleListDescriptor listDescriptor) throws Rdf4jDriverException {
37: return new SimpleListIterator(listDescriptor, connector, vf);
38: }
39:
40: @Override
41: IRI createListHead(SimpleListValueDescriptor listValueDescriptor, Collection<Statement> listStatements) {
42: final IRI firstNode = toRdf4jIri(listValueDescriptor.getValues().get(0).getIdentifier());
43: listStatements.add(vf.createStatement(owner(listValueDescriptor), hasList(listValueDescriptor),
44: firstNode, context(listValueDescriptor)));
45: return firstNode;
46: }
47:
48: @Override
49: List<Statement> createListRest(IRI head, SimpleListValueDescriptor listValueDescriptor) {
50: final List<Statement> statements = new ArrayList<>(listValueDescriptor.getValues().size());
51: IRI previous = head;
52: final IRI nextNodeProp = hasNext(listValueDescriptor);
53: final IRI context = context(listValueDescriptor);
54: final Iterator<NamedResource> it = listValueDescriptor.getValues().iterator();
55: it.next();
56:• while (it.hasNext()) {
57: final IRI object = toRdf4jIri(it.next().getIdentifier());
58: statements.add(vf.createStatement(previous, nextNodeProp, object, context));
59: previous = object;
60: }
61: return statements;
62: }
63:
64: /**
65: * We are using this code instead of iterator.remove for performance
66: * reasons. The iterator has to reconnect the list for each removed node,
67: * which takes a lot of time.
68: */
69: @Override
70: void clearList(SimpleListValueDescriptor listValueDescriptor) throws Rdf4jDriverException {
71: final Set<IRI> contexts = contexts(listValueDescriptor);
72: final Collection<Statement> toRemove = new ArrayList<>();
73: IRI currentProperty = hasList(listValueDescriptor);
74: final IRI hasNext = hasNext(listValueDescriptor);
75: final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
76: Collection<Statement> stmts;
77: Resource subject = owner(listValueDescriptor);
78: do {
79: stmts = connector.findStatements(subject, currentProperty, null, includeInferred, contexts);
80:• if (!stmts.isEmpty()) {
81: subject = extractListNode(stmts, hasNext);
82: toRemove.addAll(stmts);
83: }
84: currentProperty = hasNext;
85:• } while (!stmts.isEmpty());
86: connector.removeStatements(toRemove);
87: }
88:
89: @Override
90: MergeResult mergeWithOriginalList(SimpleListValueDescriptor listDescriptor, ListIterator it) throws
91: Rdf4jDriverException {
92: int i = 0;
93: Resource node = null;
94:• while (it.hasNext() && i < listDescriptor.getValues().size()) {
95: node = it.nextNode();
96: final NamedResource newNode = listDescriptor.getValues().get(i);
97:• if (!node.stringValue().equals(newNode.getIdentifier().toString())) {
98: node = toRdf4jIri(newNode.getIdentifier());
99: it.replaceCurrentWith(newNode);
100: }
101: i++;
102: }
103: return new MergeResult(i, node);
104: }
105:
106: @Override
107: void appendNewNodes(SimpleListValueDescriptor listDescriptor, MergeResult mergeResult) throws
108: Rdf4jDriverException {
109: int i = mergeResult.i;
110: final Collection<Statement> toAdd = new ArrayList<>(listDescriptor.getValues().size() - i);
111: Resource previous = mergeResult.previous;
112: final IRI nextNode = toRdf4jIri(listDescriptor.getNextNode().getIdentifier());
113: final IRI context = context(listDescriptor);
114:• while (i < listDescriptor.getValues().size()) {
115: final Resource newNode = toRdf4jIri(listDescriptor.getValues().get(i).getIdentifier());
116: final Statement stmt = vf.createStatement(previous, nextNode, newNode, context);
117: toAdd.add(stmt);
118: previous = newNode;
119: i++;
120: }
121: connector.addStatements(toAdd);
122: }
123:
124: @Override
125: ListIterator iterator(SimpleListValueDescriptor listDescriptor) throws Rdf4jDriverException {
126: return new SimpleListIterator(listDescriptor, connector, vf);
127: }
128: }