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: 60
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, SesameIterator)
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) 2016 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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
18: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
19: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
20: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
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.ArrayList;
28: import java.util.Collection;
29: import java.util.Iterator;
30: import java.util.List;
31:
32: class SimpleListHandler extends ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> {
33:
34: SimpleListHandler(Connector connector, ValueFactory vf) {
35: super(connector, vf);
36: }
37:
38: @Override
39: SesameIterator createIterator(SimpleListDescriptor listDescriptor) throws SesameDriverException {
40: return new SimpleListIterator(listDescriptor, connector, vf);
41: }
42:
43: IRI createListHead(SimpleListValueDescriptor listValueDescriptor, Collection<Statement> listStatements) {
44: final IRI firstNode = sesameIri(listValueDescriptor.getValues().get(0).getIdentifier());
45: listStatements.add(vf.createStatement(owner(listValueDescriptor), hasList(listValueDescriptor),
46: firstNode, context(listValueDescriptor)));
47: return firstNode;
48: }
49:
50: List<Statement> createListRest(IRI head, SimpleListValueDescriptor listValueDescriptor) {
51: final List<Statement> statements = new ArrayList<>(listValueDescriptor.getValues().size());
52: IRI previous = head;
53: final IRI nextNodeProp = hasNext(listValueDescriptor);
54: final IRI context = context(listValueDescriptor);
55: final Iterator<NamedResource> it = listValueDescriptor.getValues().iterator();
56: it.next();
57:• while (it.hasNext()) {
58: final IRI object = sesameIri(it.next().getIdentifier());
59: statements.add(vf.createStatement(previous, nextNodeProp, object, context));
60: previous = object;
61: }
62: return statements;
63: }
64:
65: /**
66: * We are using this code instead of iterator.remove for performance
67: * reasons. The iterator has to reconnect the list for each removed node,
68: * which takes a lot of time.
69: */
70: @Override
71: void clearList(SimpleListValueDescriptor listValueDescriptor) throws SesameDriverException {
72: final IRI context = context(listValueDescriptor);
73: final Collection<Statement> toRemove = new ArrayList<>();
74: IRI currentProperty = hasList(listValueDescriptor);
75: final IRI hasNext = hasNext(listValueDescriptor);
76: final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
77: Collection<Statement> stmts;
78: Resource subject = owner(listValueDescriptor);
79: do {
80: stmts = connector.findStatements(subject, currentProperty, null, includeInferred, context);
81:• if (!stmts.isEmpty()) {
82: subject = extractListNode(stmts, hasNext);
83: toRemove.addAll(stmts);
84: }
85: currentProperty = hasNext;
86:• } while (!stmts.isEmpty());
87: connector.removeStatements(toRemove);
88: }
89:
90: @Override
91: MergeResult mergeWithOriginalList(SimpleListValueDescriptor listDescriptor, SesameIterator it) throws SesameDriverException {
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 = sesameIri(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 SesameDriverException {
108: int i = mergeResult.i;
109: final Collection<Statement> toAdd = new ArrayList<>(listDescriptor.getValues().size() - i);
110: Resource previous = mergeResult.previous;
111: final IRI nextNode = sesameIri(listDescriptor.getNextNode().getIdentifier());
112: final IRI context = context(listDescriptor);
113:• while (i < listDescriptor.getValues().size()) {
114: final Resource newNode = sesameIri(listDescriptor.getValues().get(i).getIdentifier());
115: final Statement stmt = vf.createStatement(previous, nextNode, newNode, context);
116: toAdd.add(stmt);
117: previous = newNode;
118: i++;
119: }
120: connector.addStatements(toAdd);
121: }
122:
123: @Override
124: SesameIterator iterator(SimpleListValueDescriptor listDescriptor) throws SesameDriverException {
125: return new SimpleListIterator(listDescriptor, connector, vf);
126: }
127: }