Skip to contentMethod: clearList(SimpleListValueDescriptor)
1: /**
2: * Copyright (C) 2020 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: @Override
44: IRI createListHead(SimpleListValueDescriptor listValueDescriptor, Collection<Statement> listStatements) {
45: final IRI firstNode = sesameIri(listValueDescriptor.getValues().get(0).getIdentifier());
46: listStatements.add(vf.createStatement(owner(listValueDescriptor), hasList(listValueDescriptor),
47: firstNode, context(listValueDescriptor)));
48: return firstNode;
49: }
50:
51: @Override
52: List<Statement> createListRest(IRI head, SimpleListValueDescriptor listValueDescriptor) {
53: final List<Statement> statements = new ArrayList<>(listValueDescriptor.getValues().size());
54: IRI previous = head;
55: final IRI nextNodeProp = hasNext(listValueDescriptor);
56: final IRI context = context(listValueDescriptor);
57: final Iterator<NamedResource> it = listValueDescriptor.getValues().iterator();
58: it.next();
59: while (it.hasNext()) {
60: final IRI object = sesameIri(it.next().getIdentifier());
61: statements.add(vf.createStatement(previous, nextNodeProp, object, context));
62: previous = object;
63: }
64: return statements;
65: }
66:
67: /**
68: * We are using this code instead of iterator.remove for performance
69: * reasons. The iterator has to reconnect the list for each removed node,
70: * which takes a lot of time.
71: */
72: @Override
73: void clearList(SimpleListValueDescriptor listValueDescriptor) throws SesameDriverException {
74: final IRI context = context(listValueDescriptor);
75: final Collection<Statement> toRemove = new ArrayList<>();
76: IRI currentProperty = hasList(listValueDescriptor);
77: final IRI hasNext = hasNext(listValueDescriptor);
78: final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
79: Collection<Statement> stmts;
80: Resource subject = owner(listValueDescriptor);
81: do {
82: stmts = connector.findStatements(subject, currentProperty, null, includeInferred, context);
83:• if (!stmts.isEmpty()) {
84: subject = extractListNode(stmts, hasNext);
85: toRemove.addAll(stmts);
86: }
87: currentProperty = hasNext;
88:• } while (!stmts.isEmpty());
89: connector.removeStatements(toRemove);
90: }
91:
92: @Override
93: MergeResult mergeWithOriginalList(SimpleListValueDescriptor listDescriptor, SesameIterator it) throws
94: SesameDriverException {
95: int i = 0;
96: Resource node = null;
97: while (it.hasNext() && i < listDescriptor.getValues().size()) {
98: node = it.nextNode();
99: final NamedResource newNode = listDescriptor.getValues().get(i);
100: if (!node.stringValue().equals(newNode.getIdentifier().toString())) {
101: node = sesameIri(newNode.getIdentifier());
102: it.replaceCurrentWith(newNode);
103: }
104: i++;
105: }
106: return new MergeResult(i, node);
107: }
108:
109: @Override
110: void appendNewNodes(SimpleListValueDescriptor listDescriptor, MergeResult mergeResult) throws
111: SesameDriverException {
112: int i = mergeResult.i;
113: final Collection<Statement> toAdd = new ArrayList<>(listDescriptor.getValues().size() - i);
114: Resource previous = mergeResult.previous;
115: final IRI nextNode = sesameIri(listDescriptor.getNextNode().getIdentifier());
116: final IRI context = context(listDescriptor);
117: while (i < listDescriptor.getValues().size()) {
118: final Resource newNode = sesameIri(listDescriptor.getValues().get(i).getIdentifier());
119: final Statement stmt = vf.createStatement(previous, nextNode, newNode, context);
120: toAdd.add(stmt);
121: previous = newNode;
122: i++;
123: }
124: connector.addStatements(toAdd);
125: }
126:
127: @Override
128: SesameIterator iterator(SimpleListValueDescriptor listDescriptor) throws SesameDriverException {
129: return new SimpleListIterator(listDescriptor, connector, vf);
130: }
131: }