1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.listeners;
\r
14 import java.util.HashMap;
\r
16 import org.eclipse.ui.IEditorPart;
\r
17 import org.eclipse.ui.IEditorReference;
\r
18 import org.eclipse.ui.IWorkbench;
\r
19 import org.eclipse.ui.IWorkbenchPage;
\r
20 import org.eclipse.ui.IWorkbenchWindow;
\r
21 import org.eclipse.ui.PlatformUI;
\r
22 import org.simantics.Simantics;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.WriteGraph;
\r
25 import org.simantics.db.common.request.PossibleObjectWithType;
\r
26 import org.simantics.db.common.request.WriteRequest;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.service.VirtualGraphSupport;
\r
29 import org.simantics.diagram.profile.Profiles;
\r
30 import org.simantics.diagram.stubs.DiagramResource;
\r
31 import org.simantics.layer0.Layer0;
\r
32 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
\r
33 import org.simantics.project.IProject;
\r
34 import org.simantics.simulation.experiment.ExperimentState;
\r
35 import org.simantics.simulation.experiment.IExperiment;
\r
36 import org.simantics.simulation.experiment.IExperimentListener;
\r
37 import org.simantics.simulation.ontology.SimulationResource;
\r
38 import org.simantics.simulation.project.IExperimentManager;
\r
39 import org.simantics.sysdyn.SysdynResource;
\r
40 import org.simantics.sysdyn.manager.SysdynPlaybackExperiment;
\r
41 import org.simantics.sysdyn.ui.editor.DiagramViewer;
\r
42 import org.simantics.ui.SimanticsUI;
\r
45 * Listener for playback simulations. This listener activates and reverts the playback
\r
46 * profile when a playback simlation is activated or disposed.
\r
47 * @author Teemu Lempinen
\r
50 public class SysdynPlaybackExperimentListener implements IExperimentListener {
\r
52 private Resource model;
\r
53 private Resource previousProfile;
\r
55 public SysdynPlaybackExperimentListener(SysdynPlaybackExperiment experiment) {
\r
56 this.model = experiment.getModel();
\r
57 activatePlaybackProfile();
\r
61 public void stateChanged(final ExperimentState state) {
\r
64 revertPlaybackProfiles();
\r
71 public Resource model = null;
\r
72 public Resource previousProfile = null;
\r
75 HashMap<Resource, DiagramInfo> previousRuntimeDiagramsAndModelUris = new HashMap<Resource, DiagramInfo>();
\r
77 private void activatePlaybackProfile() {
\r
78 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
82 previousRuntimeDiagramsAndModelUris.clear();
\r
83 previousRuntimeDiagramsAndModelUris = getCurrentRuntimeDiagramInfos();
\r
84 activatePlaybackProfileRequest(previousRuntimeDiagramsAndModelUris);
\r
89 private void revertPlaybackProfiles() {
\r
91 IProject project = SimanticsUI.getProject();
\r
94 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
95 IExperiment experiment = manager.getActiveExperiment();
\r
97 if(experiment != null && experiment instanceof SysdynPlaybackExperiment && this.model.equals(experiment.getModel()))
\r
100 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
\r
103 public void run() {
\r
104 final HashMap<Resource, DiagramInfo> runtimeDiagramsAndModelUris = getCurrentRuntimeDiagramInfos();
\r
106 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
\r
107 Simantics.getSession().asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
\r
110 public void perform(WriteGraph graph) throws DatabaseException {
\r
112 Resource model = SysdynPlaybackExperimentListener.this.model;
\r
116 Layer0 l0 = Layer0.getInstance(graph);
\r
117 SysdynResource sr = SysdynResource.getInstance(graph);
\r
119 Resource defaultProfile = graph.syncRequest(new PossibleObjectWithType(model, l0.ConsistsOf, sr.DefaultProfile));
\r
120 if(defaultProfile == null)
\r
123 setProfileForModel(graph, model, previousProfile == null ? defaultProfile : previousProfile);
\r
125 HashMap<Resource, DiagramInfo> infos = getRuntimesForModel(graph, runtimeDiagramsAndModelUris, model);
\r
126 for(Resource runtimeDiagram : infos.keySet()) {
\r
127 Resource previous = null;
\r
128 if(previousRuntimeDiagramsAndModelUris.containsKey(runtimeDiagram)) {
\r
129 previous = previousRuntimeDiagramsAndModelUris.get(runtimeDiagram).previousProfile;
\r
131 setProfileForDiagram(graph, runtimeDiagram, previous == null ? defaultProfile : previous);
\r
141 * Run in display thread.
\r
145 private HashMap<Resource, DiagramInfo> getCurrentRuntimeDiagramInfos() {
\r
146 HashMap<Resource, DiagramInfo> runtimeDiagramsAndModelUris = new HashMap<Resource, DiagramInfo>();
\r
147 IWorkbench workBench = PlatformUI.getWorkbench();
\r
148 IWorkbenchWindow window = workBench.getActiveWorkbenchWindow();
\r
149 IWorkbenchPage[] pages = window.getPages();
\r
150 for(IWorkbenchPage page : pages) {
\r
151 for(IEditorReference reference : page.getEditorReferences()) {
\r
152 IEditorPart iep = reference.getEditor(false);
\r
153 if(iep instanceof DiagramEditor) {
\r
154 DiagramEditor editor = (DiagramEditor) iep;
\r
155 if (editor.getViewer() instanceof DiagramViewer) {
\r
156 DiagramViewer viewer = (DiagramViewer) editor.getViewer();
\r
157 Resource runtime = viewer.getRuntime();
\r
158 Resource model = viewer.getResourceInput2().getModel(null);
\r
159 DiagramInfo info = new DiagramInfo();
\r
160 info.model = model;
\r
161 runtimeDiagramsAndModelUris.put(runtime, info);
\r
166 return runtimeDiagramsAndModelUris;
\r
170 private HashMap<Resource, DiagramInfo> getRuntimesForModel(WriteGraph graph, HashMap<Resource, DiagramInfo> allInfos, Resource model) throws DatabaseException {
\r
171 HashMap<Resource, DiagramInfo> runtimeDiagramsAndModelUris = new HashMap<Resource, DiagramInfo>();
\r
172 for(Resource runtimeDiagram : allInfos.keySet()) {
\r
173 DiagramInfo di = allInfos.get(runtimeDiagram);
\r
176 Resource m = di.model;
\r
177 if(m == null || !model.equals(m))
\r
180 runtimeDiagramsAndModelUris.put(runtimeDiagram, di);
\r
182 return runtimeDiagramsAndModelUris;
\r
185 private void activatePlaybackProfileRequest(final HashMap<Resource, DiagramInfo> allDiagramInfos) {
\r
186 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
\r
187 Simantics.getSession().asyncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
\r
190 public void perform(WriteGraph graph) throws DatabaseException {
\r
192 Resource model = SysdynPlaybackExperimentListener.this.model;
\r
196 Resource profile = getSimulationPlaybackProfile(graph, model);
\r
197 if(profile == null)
\r
200 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
202 Resource current = graph.getPossibleObject(model, DIA.HasActiveProfile);
\r
203 previousProfile = current;
\r
205 if(!profile.equals(current)) {
\r
206 setProfileForModel(graph, model, profile);
\r
209 HashMap<Resource, DiagramInfo> infos = getRuntimesForModel(graph, allDiagramInfos, model);
\r
210 for(Resource runtimeDiagram : infos.keySet()) {
\r
211 DiagramInfo di = infos.get(runtimeDiagram);
\r
212 current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
\r
213 di.previousProfile = current;
\r
214 if(profile.equals(current))
\r
216 setProfileForDiagram(graph, runtimeDiagram, profile);
\r
223 private void setProfileForDiagram(WriteGraph graph, Resource runtimeDiagram, Resource profile) throws DatabaseException {
\r
224 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
226 Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
\r
227 if(profile.equals(current)) return;
\r
229 if(current != null)
\r
230 graph.deny(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, current);
\r
231 graph.claim(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, profile);
\r
233 // Set this profile as the default profile for this diagram
\r
234 Resource configuration = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
\r
235 graph.deny(configuration, DIA.HasActiveProfile);
\r
236 graph.claim(configuration, DIA.HasActiveProfile, profile);
\r
239 private void setProfileForModel(WriteGraph graph, Resource model, Resource profile) throws DatabaseException {
\r
240 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
242 // Set this profile as the default profile for this model
\r
243 graph.deny(model, DIA.HasActiveProfile);
\r
244 graph.claim(model, DIA.HasActiveProfile, profile);
\r
247 private Resource getSimulationPlaybackProfile(WriteGraph graph, Resource model) throws DatabaseException {
\r
248 Layer0 l0 = Layer0.getInstance(graph);
\r
249 SysdynResource sr = SysdynResource.getInstance(graph);
\r
250 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
252 Resource profile = graph.syncRequest(new PossibleObjectWithType(model, l0.ConsistsOf, sr.SimulationPlaybackProfile));
\r
253 if(profile == null) {
\r
254 profile = Profiles.createProfile(graph, "Simulation Playback", sr.Profiles_SimulationPlaybackColours);
\r
255 graph.deny(profile, l0.InstanceOf);
\r
256 graph.claim(profile, l0.InstanceOf, null, sr.SimulationPlaybackProfile);
\r
257 graph.claim(model, l0.ConsistsOf, profile);
\r
260 if(!graph.hasStatement(profile, SIMU.IsActive)) {
\r
261 graph.claim(profile, SIMU.IsActive, sr.Profiles_SimulationPlaybackColours);
\r