Skip to contentMethod: SimpleListHandler(StorageConnector)
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.SimpleListDescriptor;
21: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
22: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import org.apache.jena.rdf.model.Property;
26: import org.apache.jena.rdf.model.Resource;
27: import org.apache.jena.rdf.model.ResourceFactory;
28: import org.apache.jena.rdf.model.Statement;
29:
30: import java.net.URI;
31: import java.util.ArrayList;
32: import java.util.List;
33:
34: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
35:
36: public class SimpleListHandler {
37:
38: private final StorageConnector connector;
39:
40: public SimpleListHandler(StorageConnector connector) {
41: this.connector = connector;
42: }
43:
44: List<Axiom<NamedResource>> loadList(SimpleListDescriptor descriptor) {
45: final List<Axiom<NamedResource>> result = new ArrayList<>();
46: final SimpleListIterator it = new SimpleListIterator(descriptor, connector);
47: while (it.hasNext()) {
48: result.add(it.nextAxiom());
49: }
50: return result;
51: }
52:
53: void persistList(SimpleListValueDescriptor descriptor) {
54: final List<NamedResource> values = descriptor.getValues();
55: if (values.isEmpty()) {
56: return;
57: }
58: Resource owner = createResource(descriptor.getListOwner().getIdentifier().toString());
59: appendNewNodes(descriptor, 0, owner);
60: }
61:
62: void appendNewNodes(SimpleListValueDescriptor descriptor, int index, Resource lastNode) {
63: final Property hasList = ResourceFactory
64: .createProperty(descriptor.getListProperty().getIdentifier().toString());
65: final Property hasNext = ResourceFactory.createProperty(descriptor.getNextNode().getIdentifier().toString());
66: final List<Statement> toAdd = new ArrayList<>(descriptor.getValues().size() - index);
67: for (; index < descriptor.getValues().size(); index++) {
68: lastNode = appendNode(lastNode, index == 0 ? hasList : hasNext, descriptor.getValues().get(index), toAdd);
69: }
70: final URI context = descriptor.getContext();
71: connector.add(toAdd, context != null ? context.toString() : null);
72: }
73:
74: private static Resource appendNode(Resource previous, Property property, NamedResource value,
75: List<Statement> statements) {
76: final Resource node = createResource(value.getIdentifier().toString());
77: statements.add(ResourceFactory.createStatement(previous, property, node));
78: return node;
79: }
80:
81: void updateList(SimpleListValueDescriptor descriptor) {
82: final SimpleListIterator it = new SimpleListIterator(descriptor, connector);
83: int i = 0;
84: while (it.hasNext() && i < descriptor.getValues().size()) {
85: final NamedResource update = descriptor.getValues().get(i);
86: final NamedResource existing = it.nextValue();
87: if (!existing.equals(update)) {
88: it.replace(update);
89: }
90: i++;
91: }
92: removeObsoleteNodes(it);
93: if (i < descriptor.getValues().size()) {
94: appendNewNodes(descriptor, i, it.getCurrentNode());
95: }
96: }
97:
98: private static void removeObsoleteNodes(SimpleListIterator it) {
99: while (it.hasNext()) {
100: it.nextValue();
101: it.removeWithoutReconnect();
102: }
103: }
104: }