Skip to contentMethod: replaceCurrentWith(NamedResource)
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.rdf4j.list;
19:
20: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import cz.cvut.kbss.ontodriver.model.Value;
26: import cz.cvut.kbss.ontodriver.rdf4j.connector.RepoConnection;
27: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
28: import org.eclipse.rdf4j.model.IRI;
29: import org.eclipse.rdf4j.model.Resource;
30: import org.eclipse.rdf4j.model.Statement;
31: import org.eclipse.rdf4j.model.ValueFactory;
32:
33: import java.util.ArrayList;
34: import java.util.Collection;
35: import java.util.Collections;
36: import java.util.List;
37:
38: class SimpleListIterator extends AbstractListIterator<NamedResource> {
39:
40: private final SimpleListDescriptor listDescriptor;
41:
42: private IRI currentProperty;
43:
44: private Statement current;
45: private Collection<Statement> next;
46:
47: public SimpleListIterator(SimpleListDescriptor listDescriptor, RepoConnection connector, ValueFactory vf)
48: throws Rdf4jDriverException {
49: super(listDescriptor, connector, vf);
50: this.listDescriptor = listDescriptor;
51: this.currentProperty = hasListProperty;
52: init();
53: }
54:
55: private void init() throws Rdf4jDriverException {
56: this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, contexts());
57: }
58:
59: @Override
60: public boolean hasNext() {
61: return !next.isEmpty();
62: }
63:
64: @Override
65: public Resource nextNode() throws Rdf4jDriverException {
66: nextInternal();
67: assert current.getObject().isResource();
68:
69: return (Resource) current.getObject();
70: }
71:
72: private void nextInternal() throws Rdf4jDriverException {
73: if (!hasNext()) {
74: throw new IllegalStateException();
75: }
76: checkSuccessorMax(next, currentProperty);
77: this.current = next.iterator().next();
78: this.currentProperty = current.getPredicate();
79: checkObjectIsResource(current);
80: final Resource elem = (Resource) current.getObject();
81: this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, contexts());
82: }
83:
84: @Override
85: public NamedResource currentContent() {
86: assert current.getObject().isResource();
87:
88: return NamedResource.create(current.getObject().stringValue());
89: }
90:
91: @Override
92: public Axiom<NamedResource> nextAxiom() throws Rdf4jDriverException {
93: nextInternal();
94: assert current.getObject().isResource();
95:
96: final Assertion assertion = current.getPredicate() == hasListProperty ? listDescriptor
97: .getListProperty() : listDescriptor.getNextNode();
98: return createAxiom(current.getSubject(), assertion, (Resource) current.getObject());
99: }
100:
101: private static Axiom<NamedResource> createAxiom(Resource subject, Assertion assertion, Resource value) {
102: return new AxiomImpl<>(NamedResource.create(subject.stringValue()), assertion, new Value<>(NamedResource.create(value.stringValue())));
103: }
104:
105: @Override
106: public void replaceCurrentWith(NamedResource newNode) throws Rdf4jDriverException {
107:• assert current.getObject() instanceof Resource;
108: final Resource newNodeRdf4j = vf.createIRI(newNode.getIdentifier().toString());
109: final List<Statement> toAdd = new ArrayList<>(2);
110: final List<Statement> toRemove = new ArrayList<>(2);
111: toRemove.add(current);
112: final Statement newCurrent = vf.createStatement(current.getSubject(), currentProperty,
113: newNodeRdf4j, context);
114: // From the current subject to the new node
115: toAdd.add(newCurrent);
116:• if (hasNext()) {
117: toRemove.addAll(next);
118: final Statement stmt = next.iterator().next();
119: checkObjectIsResource(stmt);
120: final Resource nextNode = (Resource) stmt.getObject();
121:• if (!newNodeRdf4j.equals(nextNode)) {
122: // From the new node to the next node
123: final Statement newNext = vf.createStatement(newNodeRdf4j, hasNextProperty,
124: nextNode, context);
125: toAdd.add(newNext);
126: this.next = Collections.singletonList(newNext);
127: } else {
128: this.next = connector.findStatements(newNodeRdf4j, hasNextProperty, null,
129: includeInferred, contexts());
130: }
131: } else {
132: this.next = Collections.emptyList();
133: }
134: this.current = null;
135: connector.removeStatements(toRemove);
136: connector.addStatements(toAdd);
137: }
138:
139: @Override
140: public void remove() throws Rdf4jDriverException {
141: assert current.getObject().isResource();
142: final Collection<Statement> toRemove = new ArrayList<>(next.size() + 1);
143: toRemove.add(current);
144: if (hasNext()) {
145: toRemove.addAll(next);
146: final Statement stmt = next.iterator().next();
147: checkObjectIsResource(stmt);
148: final Resource nextNode = (Resource) stmt.getObject();
149: // Here we use the current property, so that it can also be the
150: // hasList property
151: final Statement toAdd = vf.createStatement(current.getSubject(), currentProperty,
152: nextNode, context);
153: this.next = Collections.singletonList(toAdd);
154: this.current = null;
155:
156: connector.addStatements(next);
157: } else {
158: this.next = Collections.emptyList();
159: }
160: connector.removeStatements(toRemove);
161: }
162: }