1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2014 Association for Decentralized Information Management in
\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.sysdyn.ui.handlers;
\r
14 import java.util.concurrent.Semaphore;
\r
16 import org.eclipse.core.runtime.IProgressMonitor;
\r
17 import org.eclipse.core.runtime.IStatus;
\r
18 import org.eclipse.core.runtime.Status;
\r
19 import org.eclipse.core.runtime.SubMonitor;
\r
20 import org.eclipse.core.runtime.jobs.Job;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.RequestProcessor;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.common.utils.NameUtils;
\r
25 import org.simantics.db.exception.AdaptionException;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.request.Read;
\r
28 import org.simantics.message.MessageService;
\r
29 import org.simantics.project.IProject;
\r
30 import org.simantics.simulation.experiment.ExperimentState;
\r
31 import org.simantics.simulation.experiment.IExperiment;
\r
32 import org.simantics.simulation.model.ExperimentLoadingFailed;
\r
33 import org.simantics.simulation.project.IExperimentActivationListener;
\r
34 import org.simantics.simulation.project.IExperimentManager;
\r
35 import org.simantics.sysdyn.ui.listeners.SysdynExperimentManagerListener;
\r
36 import org.simantics.utils.DataContainer;
\r
37 import org.simantics.utils.ui.ErrorLogger;
\r
38 import org.simantics.utils.ui.ExceptionUtils;
\r
39 import org.simantics.utils.ui.dialogs.ShowMessage;
\r
41 public class SysdynExperimentActivator {
\r
44 * @param experimentManager
\r
47 public static void scheduleActivation(RequestProcessor processor, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {
\r
48 String jobName = "Activate Experiment";
\r
49 String experimentName = getName(processor, experiment);
\r
50 if (experimentName != null)
\r
51 jobName += " '" + experimentName + "'";
\r
53 // Shut down the previous active experiment
\r
54 IExperiment activeExperiment = experimentManager.getActiveExperiment();
\r
55 if (experiment != null)
\r
56 activeExperiment.shutdown();
\r
58 // Activate a new experiment
\r
59 scheduleActivation(jobName, project, experimentManager, experiment);
\r
64 * @param experimentManager
\r
67 public static void scheduleActivation(String jobName, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {
\r
70 protected IStatus run(final IProgressMonitor monitor) {
\r
72 return SysdynExperimentActivator.activate(monitor, project, experimentManager, experiment);
\r
80 public static IStatus activate(IProgressMonitor monitor, IProject project, IExperimentManager experimentManager, Resource experiment) {
\r
81 return new SysdynExperimentActivator().activateExperiment(monitor, project, experimentManager, experiment);
\r
84 private static String getName(RequestProcessor processor, final Resource resource) {
\r
86 return processor.syncRequest(new Read<String>() {
\r
88 public String perform(ReadGraph graph) throws DatabaseException {
\r
90 return graph.adapt(resource, String.class);
\r
91 } catch (AdaptionException e) {
\r
92 return NameUtils.getSafeName(graph, resource);
\r
96 } catch (DatabaseException e) {
\r
97 ErrorLogger.defaultLogWarning(e);
\r
102 private IStatus activateExperiment(final IProgressMonitor monitor, final IProject project, final IExperimentManager manager, final Resource experimentResource) {
\r
103 final SubMonitor mon = SubMonitor.convert(monitor, "Activating experiment", 100000);
\r
105 SysdynExperimentManagerListener.listenManager(manager);
\r
106 IExperiment[] experiments = manager.getExperiments();
\r
107 SubMonitor shutdownMon = mon.newChild(10000);
\r
108 int workPerExperiment;
\r
109 if (experiments.length > 0)
\r
110 workPerExperiment = 10000 / experiments.length;
\r
112 workPerExperiment = 10000;
\r
113 for(IExperiment e : experiments)
\r
114 if(e.getState() != ExperimentState.DISPOSED)
\r
115 e.shutdown(shutdownMon.newChild(workPerExperiment));
\r
116 mon.setWorkRemaining(90000);
\r
118 final Semaphore activated = new Semaphore(0);
\r
119 final DataContainer<Throwable> problem = new DataContainer<Throwable>();
\r
120 manager.startExperiment(experimentResource, new IExperimentActivationListener() {
\r
123 public void onExperimentActivated(final IExperiment experiment) {
\r
124 MessageService.defaultLog(new org.eclipse.core.runtime.Status(IStatus.INFO, "org.simantics.simulation.ui", 0, "Activated experiment " + experiment.getIdentifier() , null));
\r
125 activated.release();
\r
128 public void onFailure(Throwable e) {
\r
130 activated.release();
\r
133 public void onMessage(IStatus message) {
\r
134 MessageService.getDefault().log(message);
\r
135 /*ILogger logger = MessageService.getDefault();
\r
136 MultiStatus init = new MultiStatus(Activator.PLUGIN_ID, 0, "Activating experiment", null);
\r
137 for (String msg : messages) {
\r
138 init.add(new Status(IStatus.INFO, Activator.PLUGIN_ID, msg));
\r
140 logger.log(init);*/
\r
143 public IProgressMonitor getProgressMonitor() {
\r
148 activated.acquire();
\r
149 Throwable t = problem.get();
\r
151 if (t instanceof ExperimentLoadingFailed) {
\r
152 ErrorLogger.defaultLogError(t);
\r
153 ShowMessage.showError("Experiment Activation Failed", t.getMessage());
\r
155 ExceptionUtils.logAndShowError(t);
\r
159 return Status.OK_STATUS;
\r
160 //return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Experiment activation failed, see exception for details.", problem.get());
\r
161 } catch (InterruptedException e) {
\r
162 return Status.CANCEL_STATUS;
\r