]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
7d6906019c6bf96ea8b3fcd2cef10fcdc07898ca
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2014 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.handlers;\r
13 \r
14 import java.util.concurrent.Semaphore;\r
15 \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
40 \r
41 public class SysdynExperimentActivator {\r
42     /**\r
43      * @param project\r
44      * @param experimentManager\r
45      * @param experiment\r
46      */\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
52         /*\r
53         // Shut down the previous active experiment\r
54         IExperiment activeExperiment = experimentManager.getActiveExperiment();\r
55         if (experiment != null)\r
56             activeExperiment.shutdown();\r
57          */\r
58         // Activate a new experiment\r
59         scheduleActivation(jobName, project, experimentManager, experiment);\r
60     }\r
61 \r
62     /**\r
63      * @param project\r
64      * @param experimentManager\r
65      * @param experiment\r
66      */\r
67     public static void scheduleActivation(String jobName, final IProject project, final IExperimentManager experimentManager, final Resource experiment) {\r
68         new Job(jobName) {\r
69             @Override\r
70             protected IStatus run(final IProgressMonitor monitor) {\r
71                 try {\r
72                     return SysdynExperimentActivator.activate(monitor, project, experimentManager, experiment);\r
73                 } finally {\r
74                     monitor.done();\r
75                 }\r
76             }\r
77         }.schedule();\r
78     }\r
79 \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
82     }\r
83 \r
84     private static String getName(RequestProcessor processor, final Resource resource) {\r
85         try {\r
86             return processor.syncRequest(new Read<String>() {\r
87                 @Override\r
88                 public String perform(ReadGraph graph) throws DatabaseException {\r
89                     try {\r
90                         return graph.adapt(resource, String.class);\r
91                     } catch (AdaptionException e) {\r
92                         return NameUtils.getSafeName(graph, resource);\r
93                     }\r
94                 }\r
95             });\r
96         } catch (DatabaseException e) {\r
97             ErrorLogger.defaultLogWarning(e);\r
98             return null;\r
99         }\r
100     }\r
101 \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
104 \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
111         else\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
117 \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
121 \r
122             @Override\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
126             }\r
127             @Override\r
128             public void onFailure(Throwable e) {\r
129                 problem.set(e);\r
130                 activated.release();\r
131             }\r
132             @Override\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
139                     }\r
140                     logger.log(init);*/\r
141             }\r
142             @Override\r
143             public IProgressMonitor getProgressMonitor() {\r
144                 return mon;\r
145             }\r
146         }, true);\r
147         try {\r
148             activated.acquire();\r
149             Throwable t = problem.get();\r
150             if (t != null) {\r
151                 if (t instanceof ExperimentLoadingFailed) {\r
152                     ErrorLogger.defaultLogError(t);\r
153                     ShowMessage.showError("Experiment Activation Failed", t.getMessage());\r
154                 } else {\r
155                     ExceptionUtils.logAndShowError(t);\r
156                 }\r
157             }\r
158 \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
163         }\r
164     }\r
165 \r
166 }\r