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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.tester;
\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
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
41 * @author Tuukka Lehtonen
\r
43 public class BasicResourcePropertyTester extends PropertyTester {
\r
45 private static final boolean DEBUG = false;
\r
48 * Tests if the received resource is an instance of any of the URIs
\r
49 * listed in the arguments.
\r
51 protected static final String RESOURCE_TYPE = "resourceType";
\r
54 * Tests if the received resource(s) are all instances of any of the URIs
\r
55 * listed in the arguments.
\r
57 protected static final String ALL_RESOURCES_OF_TYPE = "allResourcesOfType";
\r
60 * Tests if the received resource inherits any of the URIs
\r
61 * listed in the arguments.
\r
63 protected static final String INHERITS = "inherits";
\r
66 * Tests if all the received resource(s) inherit any of the URIs
\r
67 * listed in the arguments.
\r
69 protected static final String ALL_INHERIT = "allInherit";
\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
75 protected static final String NAME_MATCHES = "nameMatches";
\r
78 * Tests if the received resource has any of the tags listed as URIs in the
\r
81 protected static final String HAS_TAG = "hasTag";
\r
84 * Tests if the received resource has any of the tags listed as URIs in the
\r
87 protected static final String HAS_PARENT = "hasParent";
\r
90 * Tests if the received resource has any of the statements with the predicate type listed as URI in the
\r
93 protected static final String HAS_STATEMENT = "hasStatement";
\r
95 private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
\r
98 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
\r
100 System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
\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
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
113 if (DatabaseJob.inProgress())
\r
116 ISessionContext ctx = Simantics.getSessionContext();
\r
121 return RequestUtil.trySyncRequest(
\r
123 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
124 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
126 new UniqueRead<Boolean>() {
\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
132 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
\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
144 protected boolean doTest(ReadGraph g, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
\r
145 if (RESOURCE_TYPE.equals(property)) {
\r
147 System.out.println("** " + NameUtils.getSafeName(g, resource));
\r
149 for (int i = 0; i < args.length; i++) {
\r
150 if (g.isInstanceOf(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
\r
152 System.out.println("IS INSTANCE OF " + args[i]);
\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
163 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));
\r
165 } else if (INHERITS.equals(property)) {
\r
167 System.out.println("** " + NameUtils.getSafeName(g, resource));
\r
169 for (int i = 0; i < args.length; i++) {
\r
170 if (g.isInheritedFrom(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
\r
172 System.out.println("INHERITS " + args[i]);
\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
183 System.out.println("DOES NOT INHERIT ANY OF: " + Arrays.toString(args));
\r
185 } else if (NAME_MATCHES.equals(property)) {
\r
186 if (args.length == 0)
\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
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
198 for (Pattern p : patterns) {
\r
199 Matcher m = p.matcher(name);
\r
204 } catch (ValueTypeMismatchException e) {
\r
205 e.printStackTrace();
\r
206 } catch (BindingException e) {
\r
207 e.printStackTrace();
\r
210 } else if (HAS_TAG.equals(property)) {
\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
216 System.out.println("HAS TAG " + args[i]);
\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
226 } else if (HAS_PARENT.equals(property)) {
\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
233 System.out.println("HAS PARENT " + args[i]);
\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
243 } else if (HAS_STATEMENT.equals(property)) {
\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
249 System.out.println("HAS STATEMENT " + args[i]);
\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
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
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
277 if (!argTypesResolved) {
\r
279 System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
\r
283 for (Resource resource : resources) {
\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
291 if (rts.contains(argTypes[t])) {
\r
294 System.out.println("IS INSTANCE OF " + args[t]);
\r
299 System.out.println("IS NOT AN INSTANCE OF ANY ARGUMENT TYPE");
\r
304 System.out.println("ALL RESOURCES ARE INSTANCES OF ONE OF: " + Arrays.toString(args));
\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
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
319 if (!argTypesResolved) {
\r
321 System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
\r
325 for (Resource resource : resources) {
\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
332 if (g.isInheritedFrom(resource, argTypes[t])) {
\r
333 inheritsArgType = true;
\r
335 System.out.println("INHERITS " + args[t]);
\r
338 if (!inheritsArgType) {
\r
340 System.out.println("DOES NOT INHERIT ANY ARGUMENT TYPE");
\r
345 System.out.println("ALL RESOURCES INHERIT ONE OF: " + Arrays.toString(args));
\r