1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.tester;
14 import java.util.Arrays;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
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;
41 * @author Tuukka Lehtonen
43 public class BasicResourcePropertyTester extends PropertyTester {
45 private static final boolean DEBUG = false;
48 * Tests if the received resource is an instance of any of the URIs
49 * listed in the arguments.
51 protected static final String RESOURCE_TYPE = "resourceType";
54 * Tests if the received resource(s) are all instances of any of the URIs
55 * listed in the arguments.
57 protected static final String ALL_RESOURCES_OF_TYPE = "allResourcesOfType";
60 * Tests if the received resource inherits any of the URIs
61 * listed in the arguments.
63 protected static final String INHERITS = "inherits";
66 * Tests if all the received resource(s) inherit any of the URIs
67 * listed in the arguments.
69 protected static final String ALL_INHERIT = "allInherit";
72 * Tests if the received resource has a name property that matches any of
73 * the regular expressions specified in the arguments.
75 protected static final String NAME_MATCHES = "nameMatches";
78 * Tests if the received resource has any of the tags listed as URIs in the
81 protected static final String HAS_TAG = "hasTag";
84 * Tests if the received resource has any of the tags listed as URIs in the
87 protected static final String HAS_PARENT = "hasParent";
90 * Tests if the received resource has any of the statements with the predicate type listed as URI in the
93 protected static final String HAS_STATEMENT = "hasStatement";
95 private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
98 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
100 System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
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)
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)
113 if (DatabaseJob.inProgress())
116 ISessionContext ctx = Simantics.getSessionContext();
121 return RequestUtil.trySyncRequest(
123 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
124 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
126 new UniqueRead<Boolean>() {
128 public Boolean perform(ReadGraph g) throws DatabaseException {
129 if (multiResourceTest) {
130 return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));
132 return Boolean.valueOf(doTest(g, resource, property, args, expectedValue));
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
144 protected boolean doTest(ReadGraph g, Resource resource, String property, Object[] args, Object expectedValue) throws DatabaseException {
145 if (RESOURCE_TYPE.equals(property)) {
147 System.out.println("** " + NameUtils.getSafeName(g, resource));
149 for (int i = 0; i < args.length; i++) {
150 if (g.isInstanceOf(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
152 System.out.println("IS INSTANCE OF " + args[i]);
156 } catch (ResourceNotFoundException e) {
157 /* This is a natural situation (database does not contain all resources
158 referred in plugins). No error reporting.
163 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));
165 } else if (INHERITS.equals(property)) {
167 System.out.println("** " + NameUtils.getSafeName(g, resource));
169 for (int i = 0; i < args.length; i++) {
170 if (g.isInheritedFrom(resource, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
172 System.out.println("INHERITS " + args[i]);
176 } catch (ResourceNotFoundException e) {
177 /* This is a natural situation (database does not contain all resources
178 referred in plugins). No error reporting.
183 System.out.println("DOES NOT INHERIT ANY OF: " + Arrays.toString(args));
185 } else if (NAME_MATCHES.equals(property)) {
186 if (args.length == 0)
188 Pattern[] patterns = new Pattern[args.length];
189 for (int i = 0; i < args.length; i++) {
190 patterns[i] = Pattern.compile((String) args[i]);
193 Layer0 L0 = Layer0.getInstance(g);
194 for (Resource r : g.getObjects(resource, L0.HasName)) {
195 String name = g.getPossibleValue(r, Bindings.STRING);
198 for (Pattern p : patterns) {
199 Matcher m = p.matcher(name);
204 } catch (ValueTypeMismatchException e) {
206 } catch (BindingException e) {
210 } else if (HAS_TAG.equals(property)) {
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)) {
216 System.out.println("HAS TAG " + args[i]);
220 } catch (ResourceNotFoundException e) {
221 /* This is a natural situation (database does not contain all resources
222 referred in plugins). No error reporting.
226 } else if (HAS_PARENT.equals(property)) {
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));
233 System.out.println("HAS PARENT " + args[i]);
237 } catch (ResourceNotFoundException e) {
238 /* This is a natural situation (database does not contain all resources
239 referred in plugins). No error reporting.
243 } else if (HAS_STATEMENT.equals(property)) {
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)) {
249 System.out.println("HAS STATEMENT " + args[i]);
253 } catch (ResourceNotFoundException e) {
254 /* This is a natural situation (database does not contain all resources
255 referred in plugins). No error reporting.
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++) {
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.
277 if (!argTypesResolved) {
279 System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
283 for (Resource resource : resources) {
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)
291 if (rts.contains(argTypes[t])) {
294 System.out.println("IS INSTANCE OF " + args[t]);
299 System.out.println("IS NOT AN INSTANCE OF ANY ARGUMENT TYPE");
304 System.out.println("ALL RESOURCES ARE INSTANCES OF ONE OF: " + Arrays.toString(args));
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++) {
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.
319 if (!argTypesResolved) {
321 System.out.println("(WW) NO ARGUMENTS RESOLVED INTO RESOURCES!");
325 for (Resource resource : resources) {
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)
332 if (g.isInheritedFrom(resource, argTypes[t])) {
333 inheritsArgType = true;
335 System.out.println("INHERITS " + args[t]);
338 if (!inheritsArgType) {
340 System.out.println("DOES NOT INHERIT ANY ARGUMENT TYPE");
345 System.out.println("ALL RESOURCES INHERIT ONE OF: " + Arrays.toString(args));