Skip to content

Package: ListHandler

ListHandler

nameinstructionbranchcomplexitylinemethod
ListHandler(Connector, ValueFactory)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
context(ListDescriptor)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createForReferencedList(Connector, ValueFactory)
M: 8 C: 12
60%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
createForSimpleList(Connector, ValueFactory)
M: 8 C: 12
60%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 3
100%
M: 0 C: 1
100%
extractListNode(Collection, URI)
M: 22 C: 16
42%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 3 C: 4
57%
M: 0 C: 1
100%
hasList(ListDescriptor)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hasNext(ListDescriptor)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isOldListEmpty(Resource, URI, boolean, URI)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
loadList(ListDescriptor)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
mergeList(ListValueDescriptor)
M: 8 C: 31
79%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 8
100%
M: 0 C: 1
100%
owner(ListDescriptor)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
persistList(ListValueDescriptor)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
removeObsoletes(SesameIterator)
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
sesameUri(URI)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
updateList(ListValueDescriptor)
M: 0 C: 31
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

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.List;
20:
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
23: import org.openrdf.model.Resource;
24: import org.openrdf.model.Statement;
25: import org.openrdf.model.URI;
26: import org.openrdf.model.Value;
27: import org.openrdf.model.ValueFactory;
28:
29: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
30: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
31: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
32: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
33: import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
34: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
35: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
36: import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
37: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
38: import cz.cvut.kbss.ontodriver.model.Axiom;
39:
40: /**
41: * Base class for list handlers. </p>
42: *
43: * List handlers are responsible for loading and persisting lists.
44: *
45: * @author ledvima1
46: *
47: * @param <T>
48: * List descriptor type
49: * @param <V>
50: * List value descriptor type
51: */
52: abstract class ListHandler<T extends ListDescriptor, V extends ListValueDescriptor> {
53:
54:         protected final Connector connector;
55:         protected final ValueFactory vf;
56:
57:         ListHandler(Connector connector, ValueFactory vf) {
58:                 this.connector = connector;
59:                 this.vf = vf;
60:         }
61:
62:         /**
63:          * Loads axioms representing list described by the specified list
64:          * descriptor.
65:          *
66:          * @return Collection of axioms representing sequence values
67:          * @throws SesameDriverException
68:          */
69:         List<Axiom<NamedResource>> loadList(T listDescriptor) throws SesameDriverException {
70:                 final List<Axiom<NamedResource>> axioms = new ArrayList<>();
71:                 final SesameIterator it = createIterator(listDescriptor);
72:•                while (it.hasNext()) {
73:                         axioms.add(it.nextAxiom());
74:                 }
75:                 return axioms;
76:         }
77:
78:         abstract SesameIterator createIterator(T listDescriptor) throws SesameDriverException;
79:
80:         /**
81:          * Persists list values specified by the descriptor. </p>
82:          *
83:          * The values are saved in the order in which they appear in the descriptor.
84:          *
85:          * @param listValueDescriptor
86:          * Describes values to persist
87:          * @throws SesameDriverException
88:          */
89:         void persistList(V listValueDescriptor) throws SesameDriverException {
90:•                if (listValueDescriptor.getValues().isEmpty()) {
91:                         return;
92:                 }
93:                 final Collection<Statement> statements = new ArrayList<>(listValueDescriptor.getValues()
94:                                 .size());
95:                 final URI head = createListHead(listValueDescriptor, statements);
96:                 statements.addAll(createListRest(head, listValueDescriptor));
97:                 connector.addStatements(statements);
98:         }
99:
100:         abstract URI createListHead(V valueDescriptor, Collection<Statement> listStatements) throws SesameDriverException;
101:
102:         abstract List<Statement> createListRest(URI head, V valueDescriptor) throws SesameDriverException;
103:
104:         /**
105:          * Updates list with values specified by the descriptor. </p>
106:          *
107:          * @param listValueDescriptor
108:          * Describes the updated values
109:          * @throws SesameDriverException
110:          */
111:         void updateList(V listValueDescriptor) throws SesameDriverException {
112:•                if (listValueDescriptor.getValues().isEmpty()) {
113:                         clearList(listValueDescriptor);
114:•                } else if (isOldListEmpty(owner(listValueDescriptor), hasList(listValueDescriptor),
115:                                 listValueDescriptor.getListProperty().isInferred(), context(listValueDescriptor))) {
116:                         persistList(listValueDescriptor);
117:                 } else {
118:                         mergeList(listValueDescriptor);
119:                 }
120:         }
121:
122:         private boolean isOldListEmpty(Resource owner, URI hasListProperty, boolean includeInferred,
123:                         URI context) throws SesameDriverException {
124:                 final Collection<Statement> stmts = connector.findStatements(owner, hasListProperty, null,
125:                                 includeInferred, context);
126:                 return stmts.isEmpty();
127:         }
128:
129:         abstract void clearList(V listDescriptor) throws SesameDriverException;
130:
131:         private void mergeList(V listDescriptor) throws SesameDriverException {
132:                 final SesameIterator it = iterator(listDescriptor);
133:                 final MergeResult mergeResult = mergeWithOriginalList(listDescriptor, it);
134:                 removeObsoletes(it);
135:•                assert mergeResult.i > 0;
136:•                assert mergeResult.previous != null;
137:•                if (mergeResult.i < listDescriptor.getValues().size()) {
138:                         appendNewNodes(listDescriptor, mergeResult);
139:                 }
140:         }
141:
142:         abstract SesameIterator iterator(V listDescriptor) throws SesameDriverException;
143:
144:         abstract MergeResult mergeWithOriginalList(V listDescriptor, SesameIterator it) throws SesameDriverException;
145:
146:         abstract void appendNewNodes(V listDescriptor, MergeResult mergeResult) throws SesameDriverException;
147:
148:         void removeObsoletes(SesameIterator it) throws SesameDriverException {
149:•                while (it.hasNext()) {
150:                         it.nextNode();
151:                         it.remove();
152:                 }
153:         }
154:
155:         Resource extractListNode(Collection<Statement> stmts, URI nodeAssertion)
156:                         throws SesameDriverException {
157:•                if (stmts.size() > 1) {
158:                         throw new IntegrityConstraintViolatedException(
159:                                         "Invalid number of values found for assertion " + nodeAssertion
160:                                                         + ". Expected 1, got " + stmts.size());
161:                 }
162:                 final Value val = stmts.iterator().next().getObject();
163:•                if (!(val instanceof Resource)) {
164:                         throw new IntegrityConstraintViolatedException(
165:                                         "Invalid property value. Expected object property value, got literal.");
166:                 }
167:                 return (Resource) val;
168:         }
169:
170:         URI context(ListDescriptor listDescriptor) {
171:                 return sesameUri(listDescriptor.getContext());
172:         }
173:
174:         URI owner(ListDescriptor listDescriptor) {
175:                 return sesameUri(listDescriptor.getListOwner().getIdentifier());
176:         }
177:
178:         URI hasList(ListDescriptor listDescriptor) {
179:                 return sesameUri(listDescriptor.getListProperty().getIdentifier());
180:         }
181:
182:         URI hasNext(ListDescriptor listDescriptor) {
183:                 return sesameUri(listDescriptor.getNextNode().getIdentifier());
184:         }
185:
186:         URI sesameUri(java.net.URI uri) {
187:                 return SesameUtils.toSesameUri(uri, vf);
188:         }
189:
190:         /**
191:          * Creates handler for simple lists.
192:          *
193:          * @param connector
194:          * Storage connector
195:          * @param vf
196:          * Sesame value factory
197:          * @return List handler
198:          */
199:         static ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> createForSimpleList(
200:                         Connector connector, ValueFactory vf) {
201:•                assert connector != null;
202:•                assert vf != null;
203:
204:                 return new SimpleListHandler(connector, vf);
205:         }
206:
207:         /**
208:          * Creates handler for referenced lists.
209:          *
210:          * @param connector
211:          * Storage connector
212:          * @param vf
213:          * Sesame value factory
214:          * @return List handler
215:          */
216:         static ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> createForReferencedList(
217:                         Connector connector, ValueFactory vf) {
218:•                assert connector != null;
219:•                assert vf != null;
220:
221:                 return new ReferencedListHandler(connector, vf);
222:         }
223:
224:         static final class MergeResult {
225: protected int i;
226: protected Resource previous;
227: protected MergeResult(int i, Resource node) {
228: this.i = i;
229: this.previous = node;
230: }
231: }
232: }