Skip to contentMethod: replaceCurrentWith(NamedResource)
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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
18: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
19: import cz.cvut.kbss.ontodriver.model.Axiom;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
22: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
23: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
24: import org.eclipse.rdf4j.model.IRI;
25: import org.eclipse.rdf4j.model.Resource;
26: import org.eclipse.rdf4j.model.Statement;
27: import org.eclipse.rdf4j.model.ValueFactory;
28:
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.Collections;
32:
33: class ReferencedListIterator extends AbstractSesameIterator {
34:
35: private final ReferencedListDescriptor listDescriptor;
36:
37: private final IRI hasContentProperty;
38:
39: private IRI currentProperty;
40:
41: private Statement currentNode;
42: private Statement currentContent;
43: private Collection<Statement> next;
44:
45: public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector connector, ValueFactory vf)
46: throws SesameDriverException {
47: super(listDescriptor, connector, vf);
48: this.listDescriptor = listDescriptor;
49: this.hasContentProperty = SesameUtils.toSesameIri(listDescriptor.getNodeContent().getIdentifier(), vf);
50: this.currentProperty = hasListProperty;
51: init();
52: }
53:
54: private void init() throws SesameDriverException {
55: this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, contexts());
56: }
57:
58: @Override
59: public boolean hasNext() {
60: return !next.isEmpty();
61: }
62:
63: @Override
64: public Resource nextNode() throws SesameDriverException {
65: nextInternal();
66: return (Resource) currentNode.getObject();
67: }
68:
69: private void nextInternal() throws SesameDriverException {
70: if (!hasNext()) {
71: throw new IllegalStateException();
72: }
73: checkSuccessorMax(next, currentProperty);
74: this.currentNode = next.iterator().next();
75: this.currentProperty = currentNode.getPredicate();
76: checkNodeIsResource(currentNode);
77: final Resource elem = (Resource) currentNode.getObject();
78: this.currentContent = getNodeContent(elem);
79: this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, contexts());
80: }
81:
82: private Statement getNodeContent(Resource node) throws SesameDriverException {
83: final Collection<Statement> elements = connector.findStatements(node, hasContentProperty,
84: null, includeInferred, contexts());
85: checkSuccessorMax(elements, hasContentProperty);
86: if (elements.isEmpty()) {
87: throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
88: }
89: final Statement elem = elements.iterator().next();
90: checkNodeIsResource(elem);
91: return elem;
92: }
93:
94: @Override
95: public Resource currentContent() {
96: assert currentContent.getObject() instanceof Resource;
97: return (Resource) currentContent.getObject();
98: }
99:
100: @Override
101: public Axiom<NamedResource> nextAxiom() throws SesameDriverException {
102: nextInternal();
103: assert currentContent.getObject() instanceof Resource;
104:
105: return createAxiom(currentContent.getSubject(), listDescriptor.getNodeContent(),
106: (Resource) currentContent.getObject());
107: }
108:
109: @Override
110: public void remove() throws SesameDriverException {
111: assert currentNode.getObject() instanceof Resource;
112: final Collection<Statement> toRemove = new ArrayList<>();
113: toRemove.add(currentNode);
114: toRemove.add(currentContent);
115: if (!next.isEmpty()) {
116: toRemove.addAll(next);
117: final Statement stmt = next.iterator().next();
118: checkNodeIsResource(stmt);
119: final Resource nextNode = (Resource) stmt.getObject();
120: final Statement connectNext = vf
121: .createStatement(currentNode.getSubject(), currentProperty, nextNode, context);
122: this.next = Collections.singleton(connectNext);
123:
124: this.currentNode = null;
125: this.currentContent = null;
126: connector.addStatements(next);
127: } else {
128: next = Collections.emptyList();
129: }
130: connector.removeStatements(toRemove);
131: }
132:
133: @Override
134: public void replaceCurrentWith(NamedResource newContent) throws SesameDriverException {
135:• assert currentNode.getObject() instanceof Resource;
136: // We just replace the original content statement with new one
137: connector.removeStatements(Collections.singleton(currentContent));
138: final Resource node = (Resource) currentNode.getObject();
139: final Statement stmt = vf
140: .createStatement(node, hasContentProperty, SesameUtils.toSesameIri(newContent.getIdentifier(), vf),
141: context);
142: connector.addStatements(Collections.singleton(stmt));
143: }
144:
145: }