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
16 import org.eclipse.core.expressions.PropertyTester;
\r
17 import org.simantics.DatabaseJob;
\r
18 import org.simantics.Simantics;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.common.request.UniqueRead;
\r
22 import org.simantics.db.common.utils.NameUtils;
\r
23 import org.simantics.db.common.utils.RequestUtil;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.exception.ResourceNotFoundException;
\r
26 import org.simantics.db.management.ISessionContext;
\r
27 import org.simantics.scl.reflection.OntologyVersions;
\r
28 import org.simantics.ui.SimanticsUI;
\r
29 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
32 * @author Tuukka Lehtonen
\r
34 public class CollectionResourcePropertyTester extends PropertyTester {
\r
36 private static final boolean DEBUG = false;
\r
39 * Tests if the received resource is an instance of any of the URIs
\r
40 * listed in the arguments.
\r
42 protected static final String RESOURCE_TYPES = "resourceTypes";
\r
44 private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
\r
47 public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
\r
49 System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
\r
51 final Resource[] resources = ResourceAdaptionUtils.toResources(receiver);
\r
52 if (resources == null)
\r
55 ISessionContext ctx = Simantics.getSessionContext();
\r
59 if (DatabaseJob.inProgress())
\r
63 return RequestUtil.trySyncRequest(
\r
65 SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
\r
66 SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
\r
68 new UniqueRead<Boolean>() {
\r
70 public Boolean perform(ReadGraph g) throws DatabaseException {
\r
71 return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));
\r
74 } catch (DatabaseException | InterruptedException e) {
\r
75 // Purposefully not logging these exceptions, there might be way too
\r
76 // many even under normal circumstances.
\r
77 // TODO: add debug tracing options controlling the printing of these exceptions
\r
82 protected boolean doTest(ReadGraph g, Resource[] resources, String property, Object[] args, Object expectedValue) throws DatabaseException {
\r
83 if (RESOURCE_TYPES.equals(property)) {
\r
84 loop: for(Resource r : resources) {
\r
86 System.out.println("** " + NameUtils.getSafeName(g, r));
\r
88 for (int i = 0; i < args.length; i++) {
\r
89 if (g.isInstanceOf(r, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
\r
91 System.out.println("IS INSTANCE OF " + args[i]);
\r
95 } catch (ResourceNotFoundException e) {
\r
96 /* This is a natural situation (database does not contain all resources
\r
97 referred in plugins). No error reporting.
\r
102 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));
\r