]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/tester/BasicResourcePropertyTester.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / tester / BasicResourcePropertyTester.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.tester;
13
14 import java.util.Arrays;
15 import java.util.Set;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.eclipse.core.expressions.PropertyTester;
20 import org.simantics.DatabaseJob;
21 import org.simantics.Simantics;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.PossibleTypedParent;
26 import org.simantics.db.common.request.Queries;
27 import org.simantics.db.common.request.UniqueRead;
28 import org.simantics.db.common.utils.NameUtils;
29 import org.simantics.db.common.utils.RequestUtil;
30 import org.simantics.db.exception.BindingException;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.exception.ResourceNotFoundException;
33 import org.simantics.db.exception.ValueTypeMismatchException;
34 import org.simantics.db.management.ISessionContext;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.scl.reflection.OntologyVersions;
37 import org.simantics.ui.SimanticsUI;
38 import org.simantics.ui.utils.ResourceAdaptionUtils;
39
40 /**
41  * @author Tuukka Lehtonen
42  */
43 public class BasicResourcePropertyTester extends PropertyTester {
44
45     private static final boolean DEBUG = false;
46
47     /**
48      * Tests if the received resource is an instance of any of the URIs
49      * listed in the arguments.
50      */
51     protected static final String RESOURCE_TYPE = "resourceType";
52
53     /**
54      * Tests if the received resource(s) are all instances of any of the URIs
55      * listed in the arguments.
56      */
57     protected static final String ALL_RESOURCES_OF_TYPE = "allResourcesOfType";
58
59     /**
60      * Tests if the received resource inherits any of the URIs
61      * listed in the arguments.
62      */
63     protected static final String INHERITS = "inherits";
64
65     /**
66      * Tests if all the received resource(s) inherit any of the URIs
67      * listed in the arguments.
68      */
69     protected static final String ALL_INHERIT = "allInherit";
70
71     /**
72      * Tests if the received resource has a name property that matches any of
73      * the regular expressions specified in the arguments.
74      */
75     protected static final String NAME_MATCHES = "nameMatches";
76
77     /**
78      * Tests if the received resource has any of the tags listed as URIs in the
79      * arguments.
80      */
81     protected static final String HAS_TAG = "hasTag";
82
83     /**
84      * Tests if the received resource has any of the tags listed as URIs in the
85      * arguments.
86      */
87     protected static final String HAS_PARENT = "hasParent";
88     
89     /**
90      * Tests if the received resource has any of the statements with the predicate type listed as URI in the
91      * arguments.
92      */
93     protected static final String HAS_STATEMENT = "hasStatement";
94     
95     private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
96
97     @Override
98     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
99         if (DEBUG)
100             System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
101
102         // Receiver sanity check related to tested property
103         final Resource resource = ResourceAdaptionUtils.toSingleResource(receiver);
104         final Resource[] resources = ResourceAdaptionUtils.toResources(receiver);
105         if (resources.length == 0)
106             return false;
107
108         // Don't test with multi-selection and single resource test.
109         final boolean multiResourceTest = ALL_RESOURCES_OF_TYPE.equals(property) || ALL_INHERIT.equals(property);
110         if (!multiResourceTest && resource == null)
111             return false;
112
113         if (DatabaseJob.inProgress())
114             return false;
115
116         ISessionContext ctx = Simantics.getSessionContext();
117         if (ctx == null)
118             return false;
119
120         try {
121             return RequestUtil.trySyncRequest(
122                     ctx.getSession(),
123                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
124                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
125                     false,
126                     new UniqueRead<Boolean>() {
127                 @Override
128                 public Boolean perform(ReadGraph g) throws DatabaseException {
129                     if (multiResourceTest) {
130                         return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));
131                     } else {
132                         return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
133                     }
134                 }
135             });
136         } catch (DatabaseException | InterruptedException e) {
137             // Purposefully not logging these exceptions, there might be way too
138             // many even under normal circumstances.
139             // TODO: add debug tracing options controlling the printing of these exceptions
140             return false;
141         }
142     }
143
144     protected boolean doTest(ReadGraph g, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
145         if (RESOURCE_TYPE.equals(property)) {
146             if (DEBUG)
147                 System.out.println("** " + NameUtils.getSafeName(g, resource));
148             try {
149                 for (int i = 0; i < args.length; i++) {
150                     if (g.isInstanceOf(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
151                         if (DEBUG)
152                             System.out.println("IS INSTANCE OF " + args[i]);
153                         return true;
154                     }
155                 }
156             } catch (ResourceNotFoundException e) {
157                 /* This is a natural situation (database does not contain all resources
158                    referred in plugins). No error reporting.
159                  */
160                 return false;
161             }
162             if (DEBUG)
163                 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));
164             return false;
165         } else if (INHERITS.equals(property)) {
166             if (DEBUG)
167                 System.out.println("** " + NameUtils.getSafeName(g, resource));
168             try {
169                 for (int i = 0; i < args.length; i++) {
170                     if (g.isInheritedFrom(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
171                         if (DEBUG)
172                             System.out.println("INHERITS " + args[i]);
173                         return true;
174                     }
175                 }
176             } catch (ResourceNotFoundException e) {
177                 /* This is a natural situation (database does not contain all resources
178                    referred in plugins). No error reporting.
179                  */
180                 return false;
181             }
182             if (DEBUG)
183                 System.out.println("DOES NOT INHERIT ANY OF: " + Arrays.toString(args));
184             return false;
185         } else if (NAME_MATCHES.equals(property)) {
186             if (args.length == 0)
187                 return false;
188             Pattern[] patterns = new Pattern[args.length];
189             for (int i = 0; i < args.length; i++) {
190                 patterns[i] = Pattern.compile((String) args[i]);
191             }
192             try {
193                 Layer0 L0 = Layer0.getInstance(g);
194                 for (Resource r : g.getObjects(resource, L0.HasName)) {
195                     String name = g.getPossibleValue(r, Bindings.STRING);
196                     if (name == null)
197                         continue;
198                     for (Pattern p : patterns) {
199                         Matcher m = p.matcher(name);
200                         if (m.matches())
201                             return true;
202                     }
203                 }
204             } catch (ValueTypeMismatchException e) {
205                 e.printStackTrace();
206             } catch (BindingException e) {
207                 e.printStackTrace();
208             }
209             return false;
210         } else if (HAS_TAG.equals(property)) {
211             try {
212                 for (int i = 0; i < args.length; i++) {
213                     Resource tag = g.syncRequest(Queries.resource((String) args[i]));
214                     if (g.hasStatement(resource, tag)) {
215                         if (DEBUG)
216                             System.out.println("HAS TAG " + args[i]);
217                         return true;
218                     }
219                 }
220             } catch (ResourceNotFoundException e) {
221                 /* This is a natural situation (database does not contain all resources
222                    referred in plugins). No error reporting.
223                  */
224                 return false;
225             }
226         } else if (HAS_PARENT.equals(property)) {
227             try {
228                 for (int i = 0; i < args.length; i++) {
229                     Resource type = g.syncRequest(Queries.resource((String) args[i]));
230                     Resource parent = g.syncRequest(new PossibleTypedParent(resource, type));
231                     if(parent != null) {
232                         if (DEBUG)
233                             System.out.println("HAS PARENT " + args[i]);
234                         return true;
235                     }
236                 }
237             } catch (ResourceNotFoundException e) {
238                 /* This is a natural situation (database does not contain all resources
239                    referred in plugins). No error reporting.
240                  */
241                 return false;
242             }
243         } else if (HAS_STATEMENT.equals(property)) {
244             try {
245                 for (int i = 0; i < args.length; i++) {
246                     Resource predicate = g.getResource(VERSIONS.currentVersion((String) args[i]));
247                     if (g.hasStatement(resource, predicate)) {
248                         if (DEBUG)
249                             System.out.println("HAS STATEMENT " + args[i]);
250                         return true;
251                     }
252                 }
253             } catch (ResourceNotFoundException e) {
254                 /* This is a natural situation (database does not contain all resources
255                    referred in plugins). No error reporting.
256                  */
257                 return false;
258             }
259         }
260         return false;
261     }
262
263     protected boolean doTest(ReadGraph g, Resource[] resources, String property, Object[] args, Object expectedValue) throws DatabaseException {
264         if (ALL_RESOURCES_OF_TYPE.equals(property)) {
265             Resource[] argTypes = new Resource[args.length];
266             boolean argTypesResolved = false;
267             for (int i = 0; i < args.length; i++) {
268                 try {
269                     argTypes[i] = g.getResource(VERSIONS.currentVersion((String) args[i]));
270                     argTypesResolved = true;
271                 } catch (ResourceNotFoundException e) {
272                     /* This is a natural situation (database does not contain all resources
273                            referred in plugins). No error reporting.
274                      */
275                 }
276             }
277             if (!argTypesResolved) {
278                 if (DEBUG)
279                     System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
280                 return false;
281             }
282
283             for (Resource resource : resources) {
284                 if (DEBUG)
285                     System.out.println("** " + NameUtils.getSafeName(g, resource));
286                 Set<Resource> rts = g.getTypes(resource);
287                 boolean hasArgType = false;
288                 for (int t = 0; t < argTypes.length; ++t) {
289                     if (argTypes[t] == null)
290                         continue;
291                     if (rts.contains(argTypes[t])) {
292                         hasArgType = true;
293                         if (DEBUG)
294                             System.out.println("IS INSTANCE OF " + args[t]);
295                     }
296                 }
297                 if (!hasArgType) {
298                     if (DEBUG)
299                         System.out.println("IS NOT AN INSTANCE OF ANY ARGUMENT TYPE");
300                     return false;
301                 }
302             }
303             if (DEBUG)
304                 System.out.println("ALL RESOURCES ARE INSTANCES OF ONE OF: " + Arrays.toString(args));
305             return true;
306         } else if (ALL_INHERIT.equals(property)) {
307             Resource[] argTypes = new Resource[args.length];
308             boolean argTypesResolved = false;
309             for (int i = 0; i < args.length; i++) {
310                 try {
311                     argTypes[i] = g.getResource(VERSIONS.currentVersion((String) args[i]));
312                     argTypesResolved = true;
313                 } catch (ResourceNotFoundException e) {
314                     /* This is a natural situation (database does not contain all resources
315                            referred in plugins). No error reporting.
316                      */
317                 }
318             }
319             if (!argTypesResolved) {
320                 if (DEBUG)
321                     System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
322                 return false;
323             }
324
325             for (Resource resource : resources) {
326                 if (DEBUG)
327                     System.out.println("** " + NameUtils.getSafeName(g, resource));
328                 boolean inheritsArgType = false;
329                 for (int t = 0; t < argTypes.length; ++t) {
330                     if (argTypes[t] == null)
331                         continue;
332                     if (g.isInheritedFrom(resource, argTypes[t])) {
333                         inheritsArgType = true;
334                         if (DEBUG)
335                             System.out.println("INHERITS " + args[t]);
336                     }
337                 }
338                 if (!inheritsArgType) {
339                     if (DEBUG)
340                         System.out.println("DOES NOT INHERIT ANY ARGUMENT TYPE");
341                     return false;
342                 }
343             }
344             if (DEBUG)
345                 System.out.println("ALL RESOURCES INHERIT ONE OF: " + Arrays.toString(args));
346             return true;
347         }
348         return false;
349     }
350
351 }