Skip to contentMethod: appendNewNodes(ReferencedListValueDescriptor, int, Resource)
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.jena.list;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
18: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
19: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import org.apache.jena.rdf.model.Property;
22: import org.apache.jena.rdf.model.Resource;
23: import org.apache.jena.rdf.model.Statement;
24:
25: import java.net.URI;
26: import java.util.ArrayList;
27: import java.util.Collection;
28: import java.util.Collections;
29: import java.util.List;
30:
31: import static org.apache.jena.rdf.model.ResourceFactory.*;
32:
33: class ReferencedListHandler extends ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> {
34:
35: ReferencedListHandler(StorageConnector connector) {
36: super(connector);
37: }
38:
39: @Override
40: AbstractListIterator iterator(ReferencedListDescriptor descriptor) {
41: return new ReferencedListIterator(descriptor, connector);
42: }
43:
44: @Override
45: AbstractListIterator iterator(ReferencedListValueDescriptor descriptor) {
46: return iterator((ReferencedListDescriptor) descriptor);
47: }
48:
49: @Override
50: void appendNewNodes(ReferencedListValueDescriptor descriptor, int i, Resource lastNode) {
51:• assert lastNode != null;
52: final List<Statement> toAdd = new ArrayList<>((descriptor.getValues().size() - i) * 2);
53: final Property hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
54: final Property hasNext = createProperty(descriptor.getNextNode().getIdentifier().toString());
55: final Property hasContent = createProperty(descriptor.getNodeContent().getIdentifier().toString());
56:• final String context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
57:• for (; i < descriptor.getValues().size(); i++) {
58: lastNode =
59:• appendNode(lastNode, descriptor.getValues().get(i), i == 0 ? hasList : hasNext, hasContent, context,
60: toAdd);
61: }
62: connector.add(toAdd, context);
63: }
64:
65: private Resource appendNode(Resource previousNode, NamedResource value, Property link, Property hasContent,
66: String context, List<Statement> statements) {
67: final Resource node = generateNewListNode(value.getIdentifier(), context);
68: statements.add(createStatement(previousNode, link, node));
69: statements.add(createStatement(node, hasContent, createResource(value.getIdentifier().toString())));
70: return node;
71: }
72:
73: private Resource generateNewListNode(URI baseUri, String context) {
74: Resource node;
75: int index = 0;
76: Collection<Statement> statements;
77: do {
78: node = createResource(baseUri.toString() + "-SEQ_" + index++);
79: statements = connector
80: .find(node, null, null, context != null ? Collections.singleton(context) : Collections.emptySet());
81: } while (!statements.isEmpty());
82: return node;
83: }
84: }