Skip to content

Method: replaceCurrentWith(NamedResource)

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