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