Skip to content

Package: ReferencedListHandler

ReferencedListHandler

nameinstructionbranchcomplexitylinemethod
ReferencedListHandler(StorageConnector)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
appendNewNodes(ReferencedListValueDescriptor, int, Resource)
M: 4 C: 72
95%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 11
100%
M: 0 C: 1
100%
appendNode(Resource, Object, Property, Property, String, List, ReferencedListValueDescriptor, int)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
generateNewListNode(URI, String, int)
M: 0 C: 25
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$appendNode$0(Resource, Property, RDFNode)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadList(ReferencedListDescriptor)
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
persistList(ReferencedListValueDescriptor)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
removeObsoleteNodes(ReferencedListIterator)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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%
updateList(ReferencedListValueDescriptor)
M: 0 C: 48
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 13
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.jena.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
22: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import org.apache.jena.rdf.model.Property;
25: import org.apache.jena.rdf.model.Resource;
26: import org.apache.jena.rdf.model.Statement;
27:
28: import java.net.URI;
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.Collections;
32: import java.util.List;
33: import java.util.stream.Collectors;
34:
35: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
36: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
37: import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
38:
39: public class ReferencedListHandler {
40:
41: private final StorageConnector connector;
42:
43: public ReferencedListHandler(StorageConnector connector) {
44: this.connector = connector;
45: }
46:
47: List<Axiom<?>> loadList(ReferencedListDescriptor descriptor) {
48: final List<Axiom<?>> result = new ArrayList<>();
49: final ReferencedListIterator<?> it = new ReferencedListIterator<>(descriptor, connector);
50:• while (it.hasNext()) {
51: result.add(it.nextAxiom());
52: }
53: return result;
54: }
55:
56: <V> void persistList(ReferencedListValueDescriptor<V> descriptor) {
57: final List<V> values = descriptor.getValues();
58:• if (values.isEmpty()) {
59: return;
60: }
61: Resource owner = createResource(descriptor.getListOwner().getIdentifier().toString());
62: appendNewNodes(descriptor, 0, owner);
63: }
64:
65: <V> void appendNewNodes(ReferencedListValueDescriptor<V> descriptor, int i, Resource lastNode) {
66:• assert lastNode != null;
67: final List<Statement> toAdd = new ArrayList<>((descriptor.getValues().size() - i) * 2);
68: final Property hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
69: final Property hasNext = createProperty(descriptor.getNextNode().getIdentifier().toString());
70: final Property hasContent = createProperty(descriptor.getNodeContent().getIdentifier().toString());
71:• final String context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
72:• for (; i < descriptor.getValues().size(); i++) {
73: lastNode =
74:• appendNode(lastNode, descriptor.getValues().get(i), i == 0 ? hasList : hasNext, hasContent, context,
75: toAdd, descriptor, i);
76: }
77: connector.add(toAdd, context);
78: }
79:
80: private <V> Resource appendNode(Resource previousNode, V value, Property link, Property hasContent,
81: String context, List<Statement> statements,
82: ReferencedListValueDescriptor<V> descriptor, int index) {
83: final Resource node = generateNewListNode(descriptor.getListOwner().getIdentifier(), context, index);
84: statements.add(createStatement(previousNode, link, node));
85: statements.addAll(ReferencedListHelper.toRdfNodes(value, descriptor.getNodeContent())
86: .map(n -> createStatement(node, hasContent, n))
87: .collect(Collectors.toList()));
88: return node;
89: }
90:
91: private Resource generateNewListNode(URI baseUri, String context, int index) {
92: Resource node;
93: Collection<Statement> statements;
94: do {
95: node = createResource(baseUri.toString() + "-SEQ_" + index++);
96: statements = connector
97:• .find(node, null, null, context != null ? Collections.singleton(context) : Collections.emptySet());
98:• } while (!statements.isEmpty());
99: return node;
100: }
101:
102: <V> void updateList(ReferencedListValueDescriptor<V> descriptor) {
103: final ReferencedListIterator<V> it = new ReferencedListIterator<>(descriptor, connector);
104: int i = 0;
105:• while (it.hasNext() && i < descriptor.getValues().size()) {
106: final V update = descriptor.getValues().get(i);
107: final V existing = it.nextValue();
108:• if (!existing.equals(update)) {
109: it.replace(update);
110: }
111: i++;
112: }
113: removeObsoleteNodes(it);
114:• if (i < descriptor.getValues().size()) {
115: appendNewNodes(descriptor, i, it.getCurrentNode());
116: }
117: }
118:
119: private static void removeObsoleteNodes(ReferencedListIterator<?> it) {
120:• while (it.hasNext()) {
121: it.nextValue();
122: it.removeWithoutReconnect();
123: }
124: }
125: }