Package: ReferencedListHandler
ReferencedListHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ReferencedListHandler(StorageConnector) |
|
|
|
|
|
||||||||||||||||||||
appendNewNodes(ReferencedListValueDescriptor, int, Resource) |
|
|
|
|
|
||||||||||||||||||||
appendNode(Resource, Object, Property, Property, String, List, ReferencedListValueDescriptor, int) |
|
|
|
|
|
||||||||||||||||||||
createNilTerminal(Resource, Property, ReferencedListDescriptor) |
|
|
|
|
|
||||||||||||||||||||
generateNewListNode(URI, String, int) |
|
|
|
|
|
||||||||||||||||||||
lambda$appendNode$0(Resource, Property, RDFNode) |
|
|
|
|
|
||||||||||||||||||||
loadList(ReferencedListDescriptor) |
|
|
|
|
|
||||||||||||||||||||
persistList(ReferencedListValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
removeObsoleteNodes(ReferencedListIterator, boolean) |
|
|
|
|
|
||||||||||||||||||||
removePreviousNilTerminal(Resource, Property, String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
updateList(ReferencedListValueDescriptor) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2024 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: import org.apache.jena.vocabulary.RDF;
28:
29: import java.net.URI;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Collections;
33: import java.util.List;
34: import java.util.Optional;
35:
36: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
37: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
38: import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
39:
40: public class ReferencedListHandler {
41:
42: private final StorageConnector connector;
43:
44: public ReferencedListHandler(StorageConnector connector) {
45: this.connector = connector;
46: }
47:
48: List<Axiom<?>> loadList(ReferencedListDescriptor descriptor) {
49: final List<Axiom<?>> result = new ArrayList<>();
50: final ReferencedListIterator<?> it = new ReferencedListIterator<>(descriptor, connector);
51:• while (it.hasNext()) {
52: result.add(it.nextAxiom());
53: }
54: return result;
55: }
56:
57: <V> void persistList(ReferencedListValueDescriptor<V> descriptor) {
58: final List<V> values = descriptor.getValues();
59:• if (values.isEmpty()) {
60: return;
61: }
62: Resource owner = createResource(descriptor.getListOwner().getIdentifier().toString());
63: appendNewNodes(descriptor, 0, owner);
64: }
65:
66: <V> void appendNewNodes(ReferencedListValueDescriptor<V> descriptor, int i, Resource lastNode) {
67:• assert lastNode != null;
68: final List<Statement> toAdd = new ArrayList<>((descriptor.getValues().size() - i) * 2);
69: final Property hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
70: final Property hasNext = createProperty(descriptor.getNextNode().getIdentifier().toString());
71: final Property hasContent = createProperty(descriptor.getNodeContent().getIdentifier().toString());
72:• final String context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
73:• for (; i < descriptor.getValues().size(); i++) {
74: lastNode =
75:• appendNode(lastNode, descriptor.getValues().get(i), i == 0 ? hasList : hasNext, hasContent, context,
76: toAdd, descriptor, i);
77: }
78:• if (i > 0) {
79: createNilTerminal(lastNode, hasNext, descriptor).ifPresent(toAdd::add);
80: connector.add(toAdd, context);
81: }
82: }
83:
84: private void removePreviousNilTerminal(Resource lastNode, Property hasNext, String context) {
85: connector.remove(lastNode, hasNext, RDF.nil, context);
86: }
87:
88: private <V> Resource appendNode(Resource previousNode, V value, Property link, Property hasContent,
89: String context, List<Statement> statements,
90: ReferencedListValueDescriptor<V> descriptor, int index) {
91: final Resource node = generateNewListNode(descriptor.getListOwner().getIdentifier(), context, index);
92: statements.add(createStatement(previousNode, link, node));
93: statements.addAll(ReferencedListHelper.toRdfNodes(value, descriptor.getNodeContent())
94: .map(n -> createStatement(node, hasContent, n))
95: .toList());
96: return node;
97: }
98:
99: private Resource generateNewListNode(URI baseUri, String context, int index) {
100: Resource node;
101: Collection<Statement> statements;
102: do {
103: node = createResource(baseUri.toString() + "-SEQ_" + index++);
104: statements = connector
105:• .find(node, null, null, context != null ? Collections.singleton(context) : Collections.emptySet());
106:• } while (!statements.isEmpty());
107: return node;
108: }
109:
110: private static Optional<Statement> createNilTerminal(Resource lastNode, Property hasNext,
111: ReferencedListDescriptor descriptor) {
112:• return descriptor.isTerminatedByNil() ? Optional.of(createStatement(lastNode, hasNext, RDF.nil)) : Optional.empty();
113: }
114:
115: <V> void updateList(ReferencedListValueDescriptor<V> descriptor) {
116: final ReferencedListIterator<V> it = new ReferencedListIterator<>(descriptor, connector);
117: int i = 0;
118: Resource lastNode = it.getCurrentNode();
119:• while (it.hasNext() && i < descriptor.getValues().size()) {
120: final V update = descriptor.getValues().get(i);
121: final V existing = it.nextValue();
122:• if (!existing.equals(update)) {
123: it.replace(update);
124: }
125: lastNode = it.getCurrentNode();
126: i++;
127: }
128: removeObsoleteNodes(it, descriptor.isTerminatedByNil());
129:• assert lastNode != null;
130: appendNewNodes(descriptor, i, lastNode);
131: }
132:
133: private void removeObsoleteNodes(ReferencedListIterator<?> it, boolean removeNilTerminal) {
134: Resource lastNode = it.getCurrentNode();
135:• while (it.hasNext()) {
136: it.nextValue();
137: lastNode = it.getCurrentNode();
138: it.removeWithoutReconnect();
139: }
140:• if (removeNilTerminal) {
141: removePreviousNilTerminal(lastNode, it.hasNextProperty, it.context);
142: }
143: }
144: }