]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/editor/input/InputValidationCombinators.java
Multiple reader thread support for db client
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / editor / input / InputValidationCombinators.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.workbench.editor.input;
13
14 import java.util.Arrays;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ParametrizedRead;
19 import org.simantics.db.common.request.Queries;
20 import org.simantics.db.common.request.UnaryRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.request.combinations.Combinators;
23 import org.simantics.db.request.Read;
24 import org.simantics.ui.workbench.IResourceEditorInput;
25
26 /**
27  * Composable database read operations designed to be used for defining
28  * validation strategies for resource editor inputs through composition.
29  * 
30  * <p>
31  * For example one validation criterion might be that the editor input or one of
32  * its neighboring resources must have a proper URI.
33  * 
34  * @author Tuukka Lehtonen
35  */
36 public final class InputValidationCombinators {
37
38     /**
39      * Returns a function:
40      * <pre>
41      *   = subject -> exists(URI(subject))
42      *   | null    -> false
43      * </pre>.
44      */
45     public static ParametrizedRead<Resource, Boolean> hasURI() {
46         return HAS_URI;
47     }
48
49     /**
50      * Returns a function:
51      * <pre>
52      *   = input -> resource(input)
53      *   | null  -> null
54      * </pre>.
55      */
56     public static ParametrizedRead<IResourceEditorInput, Resource> extractInputResource() {
57         return EXTRACT_INPUT_RESOURCE;
58     }
59
60     /**
61      * Returns a function:
62      * <pre>
63      *   = subject -> singleObject(subject, resource(relationURI))
64      *   | null    -> null
65      * </pre>.
66      */
67     public static ParametrizedRead<Resource,Resource> completeFunction(String relationURI) {
68         return new CompleteFunctionURI(relationURI);
69     }
70
71     /**
72      * Returns a function:
73      * <pre>
74      *   = subject -> possibleObject(subject, resource(relationURI))
75      *   | null    -> null
76      * </pre>.
77      */
78     public static ParametrizedRead<Resource,Resource> partialFunction(String relationURI) {
79         return new PartialFunctionURI(relationURI);
80     }
81
82     /**
83      * Returns a function:
84      * <pre>
85      *   = subject -> true if any of conditions is true for subject, false otherwise
86      *   | null    -> true
87      * </pre>.
88      */
89     @SuppressWarnings("unchecked")
90     public static ParametrizedRead<Resource, Boolean> or(ParametrizedRead<Resource, Boolean> c1,
91             ParametrizedRead<Resource, Boolean> c2) {
92         return new FunctionOr(new ParametrizedRead[] { c1, c2 });
93     }
94
95     // ------------------------------------------------------------------------
96
97     private static class Or extends UnaryRead<Resource, Boolean> {
98         ParametrizedRead<Resource, Boolean>[] reads;
99         public Or(Resource resource, ParametrizedRead<Resource, Boolean>... reads) {
100             super(resource);
101             this.reads = reads;
102         }
103         @Override
104         public Boolean perform(ReadGraph graph) throws DatabaseException {
105             for (ParametrizedRead<Resource, Boolean> r : reads) {
106                 Read<Boolean> read = r.get(parameter);
107                 Boolean value = graph.syncRequest( read );
108                 if (value)
109                     return Boolean.TRUE;
110             }
111             return Boolean.FALSE;
112         }
113         @Override
114         public int hashCode() {
115             return super.hashCode() * 31 + Arrays.hashCode(reads);
116         }
117         @Override
118         public boolean equals(Object object) {
119             if (this == object) return true;
120             else if (object == null || getClass() != object.getClass()) return false;
121             Or other = (Or) object;
122             return super.equals(object) && Arrays.equals(reads, other.reads);
123         }
124     }
125
126     // ------------------------------------------------------------------------
127
128     private static class FunctionOr implements ParametrizedRead<Resource, Boolean> {
129         ParametrizedRead<Resource, Boolean>[] reads;
130         public FunctionOr(ParametrizedRead<Resource, Boolean>... reads) {
131             this.reads = reads;
132         }
133         @Override
134         public Read<Boolean> get(Resource subject) {
135             if (subject == null || reads.length == 0)
136                 return Combinators.constant(Boolean.TRUE);
137             return new Or(subject, reads);
138         }
139         @Override
140         public int hashCode() {
141             return getClass().hashCode() + 31 * Arrays.hashCode(reads);
142         }
143         @Override
144         public boolean equals(Object obj) {
145             if(obj == this) return true;
146             if(obj == null || obj.getClass() != getClass()) return false;
147             FunctionOr other = (FunctionOr)obj;
148             return Arrays.equals(reads, other.reads);
149         }
150     }
151
152     // ------------------------------------------------------------------------
153
154     private static class HasURI extends UnaryRead<Resource, Boolean> {
155         public HasURI(Resource resource) {
156             super(resource);
157         }
158         @Override
159         public Boolean perform(ReadGraph graph) throws DatabaseException {
160             if (parameter == null)
161                 return Boolean.FALSE;
162
163             String uri = graph.syncRequest(Queries.possibleUri(parameter));
164             //System.out.println("uri(" + parameter + "): " + uri);
165             if (uri == null)
166                 return Boolean.FALSE;
167
168             // FIXME: URI request will return invalid URIs, like
169             // null/Configuration/MyComposite after deleting the parenting model
170             // For this reason we try to reverse lookup the URI back into a
171             // resource which must be equal to the original parameter resource.
172
173             Resource reverseLookup = graph.getPossibleResource(uri);
174             //System.out.println("resource(" + uri + "): " + reverseLookup);
175             return parameter.equals(reverseLookup);
176         }
177     }
178
179     public static Read<Boolean> hasURI(Resource resource) {
180         return new HasURI(resource);
181     }
182
183     // ------------------------------------------------------------------------
184
185     private static class ExtractResource extends UnaryRead<IResourceEditorInput, Resource> {
186         public ExtractResource(IResourceEditorInput input) {
187             super(input);
188         }
189         @Override
190         public Resource perform(ReadGraph graph) throws DatabaseException {
191             return parameter != null ? parameter.getResource() : null;
192         }
193     }
194
195     public static Read<Resource> extractResource(IResourceEditorInput input) {
196         return new ExtractResource(input);
197     }
198
199     // ------------------------------------------------------------------------
200
201     private static class PossibleObjectURI implements Read<Resource> {
202         Resource subject;
203         String relationURI;
204         public PossibleObjectURI(Resource subject, String relationURI) {
205             this.subject = subject;
206             this.relationURI = relationURI;
207         }
208         @Override
209         public Resource perform(ReadGraph graph) throws DatabaseException {
210             Resource relation = graph.getResource(relationURI);
211             return graph.getPossibleObject(subject, relation);
212         }
213         @Override
214         public int hashCode() {
215             return subject.hashCode() + 31 * relationURI.hashCode();
216         }
217         @Override
218         public boolean equals(Object object) {
219             if (this == object) return true;
220             else if (object == null || getClass() != object.getClass()) return false;
221             PossibleObjectURI other = (PossibleObjectURI)object;
222             return subject.equals(other.subject) && relationURI.equals(other.relationURI);
223         }
224     }
225
226     /**
227      * Returns a read request that reads an object possibly connected to the subject by the relation.
228      */
229     public static Read<Resource> possibleObject(Resource subject, String relationURI) {
230         return new PossibleObjectURI(subject, relationURI);
231     }
232
233     // ------------------------------------------------------------------------
234
235     private static class PartialFunctionURI implements ParametrizedRead<Resource, Resource> {
236         String relationURI;
237         public PartialFunctionURI(String relationURI) {
238             this.relationURI = relationURI;
239         }
240         @Override
241         public Read<Resource> get(Resource subject) {
242             if (subject == null)
243                 return Combinators.constant(null);
244             return possibleObject(subject, relationURI);
245         }
246         @Override
247         public int hashCode() {
248             return getClass().hashCode() + 31 * relationURI.hashCode();
249         }
250         @Override
251         public boolean equals(Object obj) {
252             if(obj == this) return true;
253             if(obj == null || obj.getClass() != getClass()) return false;
254             PartialFunctionURI other = (PartialFunctionURI)obj;
255             return relationURI.equals(other.relationURI);
256         }
257     }
258
259     // ------------------------------------------------------------------------
260
261     private static class SingleObjectURI implements Read<Resource> {
262         Resource subject;
263         String relationURI;
264         public SingleObjectURI(Resource subject, String relationURI) {
265             this.subject = subject;
266             this.relationURI = relationURI;
267         }
268         @Override
269         public Resource perform(ReadGraph graph) throws DatabaseException {
270             Resource relation = graph.getResource(relationURI);
271             return graph.getSingleObject(subject, relation);
272         }
273         @Override
274         public int hashCode() {
275             return subject.hashCode() + 31 * relationURI.hashCode();
276         }
277         @Override
278         public boolean equals(Object object) {
279             if (this == object) return true;
280             else if (object == null || getClass() != object.getClass()) return false;
281             SingleObjectURI other = (SingleObjectURI) object;
282             return subject.equals(other.subject) && relationURI.equals(other.relationURI);
283         }
284     }
285
286     /**
287      * Returns a read request that reads a single object connected to the
288      * subject by the relation.
289      */
290     public static Read<Resource> singleObject(Resource subject, String relationURI) {
291         return new SingleObjectURI(subject, relationURI);
292     }
293
294     // ------------------------------------------------------------------------
295
296     private static class CompleteFunctionURI implements ParametrizedRead<Resource, Resource> {
297         String relationURI;
298         public CompleteFunctionURI(String relationURI) {
299             this.relationURI = relationURI;
300         }
301         @Override
302         public Read<Resource> get(Resource subject) {
303             if (subject == null)
304                 return Combinators.constant(null);
305             return singleObject(subject, relationURI);
306         }
307         @Override
308         public int hashCode() {
309             return getClass().hashCode() + 31 * relationURI.hashCode();
310         }
311         @Override
312         public boolean equals(Object obj) {
313             if(obj == this) return true;
314             if(obj == null || obj.getClass() != getClass()) return false;
315             CompleteFunctionURI other = (CompleteFunctionURI)obj;
316             return relationURI.equals(other.relationURI);
317         }
318     }
319
320     // ------------------------------------------------------------------------
321
322     private static final ParametrizedRead<IResourceEditorInput, Resource> EXTRACT_INPUT_RESOURCE = new ParametrizedRead<IResourceEditorInput, Resource>() {
323         @Override
324         public Read<Resource> get(IResourceEditorInput input) {
325             return extractResource(input);
326         }
327     };
328
329     // ------------------------------------------------------------------------
330
331     private static final ParametrizedRead<Resource, Boolean> HAS_URI = new ParametrizedRead<Resource, Boolean>() {
332         @Override
333         public Read<Boolean> get(Resource resource) {
334             return hasURI(resource);
335         }
336     };
337
338 }