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