From dd7ee2fd1dcd61369ba6f721e2b5826e304f323a Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 20 Oct 2011 09:10:22 +0000 Subject: [PATCH] Controls for simulation playback git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22888 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../icons/control_fastforward.png | Bin 0 -> 607 bytes .../icons/control_fastforward_blue.png | Bin 0 -> 736 bytes .../icons/control_pause.png | Bin 0 -> 598 bytes .../icons/control_pause_blue.png | Bin 0 -> 721 bytes .../icons/control_play.png | Bin 0 -> 592 bytes .../icons/control_play_blue.png | Bin 0 -> 717 bytes .../icons/control_step.png | Bin 0 -> 720 bytes .../icons/control_step_blue.png | Bin 0 -> 694 bytes org.simantics.sysdyn.ui/plugin.xml | 106 ++++++++ .../profiles/SimulationPlaybackStyle.java | 4 +- .../simulationPlayback/FastSpeedHandler.java | 26 ++ .../NormalSpeedHandler.java | 26 ++ .../PlaybackExperimentHandler.java | 82 +++++++ .../PlaybackResetHandler.java | 63 +++++ .../simulationPlayback/SlowSpeedHandler.java | 26 ++ .../simulationPlayback/SpeedHandler.java | 50 ++++ .../listeners/SysdynExperimentListener.java | 4 + .../SysdynPlaybackExperimentListener.java | 76 ++++-- .../ui/menu/PlaybackSliderContribution.java | 61 ++++- .../sysdyn/ui/validation/ValidationUtils.java | 2 + org.simantics.sysdyn.ui/sysdyn.product | 4 +- .../manager/SysdynPlaybackExperiment.java | 229 +++++++++++++++++- 22 files changed, 722 insertions(+), 37 deletions(-) create mode 100644 org.simantics.sysdyn.ui/icons/control_fastforward.png create mode 100644 org.simantics.sysdyn.ui/icons/control_fastforward_blue.png create mode 100644 org.simantics.sysdyn.ui/icons/control_pause.png create mode 100644 org.simantics.sysdyn.ui/icons/control_pause_blue.png create mode 100644 org.simantics.sysdyn.ui/icons/control_play.png create mode 100644 org.simantics.sysdyn.ui/icons/control_play_blue.png create mode 100644 org.simantics.sysdyn.ui/icons/control_step.png create mode 100644 org.simantics.sysdyn.ui/icons/control_step_blue.png create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/FastSpeedHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/NormalSpeedHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackExperimentHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackResetHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SlowSpeedHandler.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SpeedHandler.java diff --git a/org.simantics.sysdyn.ui/icons/control_fastforward.png b/org.simantics.sysdyn.ui/icons/control_fastforward.png new file mode 100644 index 0000000000000000000000000000000000000000..31f7fd3adf22f2ae4e1162d1e438be7c5d9c5292 GIT binary patch literal 607 zcmV-l0-*hgP)VN9{w9DwJzd%Gm5%l6k(Srw1f=C@i5k%a|<)Grew%RTzD&|xaqcXf< z9*@@$bXSuRnUR_GMnrG_gFk+>+ij)QYU#~plQkL*gL7`wYBg4^R`qhZto$k<$f_Xo z<#I{K-n-q7Ow**(=_LO_v`{FR`Fvje zCh%1@e~?F`5v|v2G7LjvFc=W3dWbN_cqWrko&v(J{s`D?He|Qk1!*YBtJR9ERvYzt zJ(|zwGN#k%P%@d+p8}#;49X}KB%94HV55G&Pm9Gu2v{Y$-LC8hs*A_t>?u&M*9|ml zyWL6wWCWZ}Ck=-~vRJGF7W(*b$n)iLp-3cRJO#v!q9QpfKQN-6~U9q_TF$F*h9sL-j zNpc$weSXQZ6Gag%5kOy9HfA1@g zVA*&|VjFHeVhIV!`DzQM7F-a@NRTNvNlYYp4z%6NAQuJno^MeOJ!7dzy8e(=;etr6 z4q2h83Vb36o=G4!6u79$u)6u4LoN!;_)kItHmh9g5CP1HxVh+sSiS*Mlqx_uVfwL~ z+}p|}Y*;r1{g* z9DE4_W&SW~qUN5hWN@-RUJ#q3Ngxr&iJp;+4aEd;HM{A>I)B?7>yL4`Nc6h^we^ve z3YC6fwXTqAQWr8=Cd?emR={YnNptkhuSFVP{Q?6Lt^ zqF4A?)Arx1JpP&Pyh<4sUhXnam)AJ@xmkE-VuVGbg_|=vm|INb-=FD!zVlDxJ_uiv SXLQ>D0000yhD literal 0 HcmV?d00001 diff --git a/org.simantics.sysdyn.ui/icons/control_pause.png b/org.simantics.sysdyn.ui/icons/control_pause.png new file mode 100644 index 0000000000000000000000000000000000000000..2d9ce9c4ec4b787b77e2407809c8887b6252dc6e GIT binary patch literal 598 zcmV-c0;&CpP)U3VE{tZoOXQ3gborPd)C!*bfsFfgUA%b`K z{k54{z0H}ACRrHvW^d-rdyicUV+{VYtX~gCqfs0|(-=vN2o1PiuPW{>+#Atov}dlj zm>FQRf_YAoB-!b7g57TC=llI0*6TIQX0twmlwz@1Su_y<#c()O27`fy#f%p1x~?-# z)7Wme<7b=AhIKj}t=(=bMjvNzr~MuZg=Ct#TCD`Id5F*FgY9+;-ENndyrfd8-V+sI zk|x?lbD>axN~J2Ai^W%{R^I>_0Z9u6gEF#73lqsO`axO|3~qzj{hRS`0}LC%>-AD? zlFkeU5t@ED93Che0EA(j5rE6(g7f(t5ddbb)O7oPG}&BpJRae6I)RyiO6J&XwmSe5 z5%qypxm=P~KCwUk1<-E^%&v%&sfQfOPbAd^dMUz$2)JWFR zn(5E_apM_H=DRN?Tj1j{9obJa2Z=)=&L)N3y%h6eCr3~F>o%;x+TQ>p^I2c0Y>yvi zIld-=tn@%V9S}8IT_{!R5Z+_Ch0UxTYjubYFr%4GeU;lC74TEKeW=Sl5HvMAeX=Gi zUD%DrWY78$Ld*n!b75@ktedM+7b-uxz>7nb#SWC}9Rsqt73OZQ@HmbM0hR#sjmFrz z`Qn2R+;S%k^W^LfTz4f%8vIhZ??Cw@GT3qQ2$+f;Bm~?nEIi6bESWz7m0cmx39em$$Y&@l88-}q`@)p_|Tj# z*1_TN$urdYuZuK(mFK)s`?7zHi^MWe;3cvrICC;Dz(yzzbJPMi?R@k1RUUs#H=pHg zvrjkZN3JD?ns*D2owV@K;bCSR0s0c1`uAh{pXdA&89xkjaU%7W00000NkvXXu0mjf DW6MO* literal 0 HcmV?d00001 diff --git a/org.simantics.sysdyn.ui/icons/control_play.png b/org.simantics.sysdyn.ui/icons/control_play.png new file mode 100644 index 0000000000000000000000000000000000000000..0846555d0ca84cb99d4c70dad80144a232604041 GIT binary patch literal 592 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 literal 0 HcmV?d00001 diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index a9237d57..0f62b299 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -278,6 +278,46 @@ + + + + + + + + + + + + + + + + + + + + @@ -294,6 +334,25 @@ + + + + + + + + + + + + + + + + + + @@ -756,6 +837,31 @@ id="org.simantics.sysdyn.ui.newPlaybackExperiment" name="New Playback Experiment"> + + + + + + + + + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java index 1dd87165..8a68153a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java @@ -100,10 +100,10 @@ public class SimulationPlaybackStyle extends StyleBase ta[ta.length / 2] ) { index = ta.length - 1; - while(ta[index] > time) + while(ta[index] > time && index > 0) index--; } else { - while(ta[index] < time) + while(ta[index] < time && index < ta.length - 1) index++; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/FastSpeedHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/FastSpeedHandler.java new file mode 100644 index 00000000..d98a44ec --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/FastSpeedHandler.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; + +public class FastSpeedHandler extends SpeedHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + setPlaybackSpeed(SysdynPlaybackExperiment.DURATION_FAST); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/NormalSpeedHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/NormalSpeedHandler.java new file mode 100644 index 00000000..b2ece4d6 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/NormalSpeedHandler.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; + +public class NormalSpeedHandler extends SpeedHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + setPlaybackSpeed(SysdynPlaybackExperiment.DURATION_NORMAL); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackExperimentHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackExperimentHandler.java new file mode 100644 index 00000000..72ae6ec7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackExperimentHandler.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import java.util.Map; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.menus.UIElement; +import org.simantics.simulation.experiment.ExperimentState; +import org.simantics.simulation.experiment.IExperiment; +import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.ui.SimanticsUI; + +public class PlaybackExperimentHandler extends AbstractHandler implements IElementUpdater { + + public static final String COMMAND = "org.simantics.sysdyn.ui.playback"; + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) + return null; + SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; + if(!spe.isPlaybackRunning()) + spe.startPlayback(); + else + spe.stopPlayback(); + return null; + } + + @SuppressWarnings("rawtypes") + @Override + public void updateElement(UIElement element, Map parameters) { + IExperimentManager manager = + SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) + return; + SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; + + ExperimentState state = experiment.getState(); + if(state == ExperimentState.RUNNING && !spe.isPlaybackRunning()) { + // RUNNING == simulation, not playback + this.setBaseEnabled(false); + } else { + this.setBaseEnabled(true); + } + + if(spe.isPlaybackRunning()) { + element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_pause.png"))); + element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_pause_blue.png"))); + } else { + long duration = spe.getPlaybackDuration(); + if(duration == SysdynPlaybackExperiment.DURATION_SLOW) { + element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_step.png"))); + element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_step_blue.png"))); + } else if(duration == SysdynPlaybackExperiment.DURATION_FAST) { + element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_fastforward.png"))); + element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_fastforward_blue.png"))); + } else { + element.setIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_play.png"))); + element.setHoverIcon(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/control_play_blue.png"))); + } + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackResetHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackResetHandler.java new file mode 100644 index 00000000..e4155b67 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/PlaybackResetHandler.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import java.util.Map; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.ui.commands.IElementUpdater; +import org.eclipse.ui.menus.UIElement; +import org.simantics.simulation.experiment.ExperimentState; +import org.simantics.simulation.experiment.IExperiment; +import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; +import org.simantics.ui.SimanticsUI; + +public class PlaybackResetHandler extends AbstractHandler implements IElementUpdater { + + public static final String COMMAND = "org.simantics.sysdyn.ui.playbackReset"; + + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) + return null; + SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; + spe.resetPlayback(); + return null; + } + + @SuppressWarnings("rawtypes") + @Override + public void updateElement(UIElement element, Map parameters) { + IExperimentManager manager = + SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) + return; + SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; + + ExperimentState state = experiment.getState(); + if(state == ExperimentState.RUNNING && !spe.isPlaybackRunning()) { + // RUNNING == simulation, not playback + this.setBaseEnabled(false); + } else { + this.setBaseEnabled(true); + } + + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SlowSpeedHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SlowSpeedHandler.java new file mode 100644 index 00000000..6ebeba33 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SlowSpeedHandler.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; + +public class SlowSpeedHandler extends SpeedHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + setPlaybackSpeed(SysdynPlaybackExperiment.DURATION_SLOW); + return null; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SpeedHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SpeedHandler.java new file mode 100644 index 00000000..4fe96910 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/simulationPlayback/SpeedHandler.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.handlers.simulationPlayback; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.commands.ICommandService; +import org.simantics.simulation.experiment.IExperiment; +import org.simantics.simulation.project.IExperimentManager; +import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; +import org.simantics.ui.SimanticsUI; + +abstract class SpeedHandler extends AbstractHandler { + + protected void setPlaybackSpeed(long duration) { + + + IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); + IExperiment experiment = manager.getActiveExperiment(); + if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) + return; + final SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; + + if(spe.getPlaybackDuration() == duration) + return; + + + spe.setPlaybackDuration(duration); + + PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + ICommandService commandService = + (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class); + commandService.refreshElements(PlaybackExperimentHandler.COMMAND, null); + } + }); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynExperimentListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynExperimentListener.java index 44406261..04507781 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynExperimentListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynExperimentListener.java @@ -20,6 +20,8 @@ import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IExperimentListener; import org.simantics.sysdyn.ui.handlers.RunBasicExperiment; import org.simantics.sysdyn.ui.handlers.ToggleSimulation; +import org.simantics.sysdyn.ui.handlers.simulationPlayback.PlaybackExperimentHandler; +import org.simantics.sysdyn.ui.handlers.simulationPlayback.PlaybackResetHandler; public class SysdynExperimentListener implements IExperimentListener { @@ -43,6 +45,8 @@ public class SysdynExperimentListener implements IExperimentListener { } commandService.refreshElements(command.getId(), null); commandService.refreshElements(RunBasicExperiment.COMMAND, null); + commandService.refreshElements(PlaybackExperimentHandler.COMMAND, null); + commandService.refreshElements(PlaybackResetHandler.COMMAND, null); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynPlaybackExperimentListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynPlaybackExperimentListener.java index cb859ccc..50bd0480 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynPlaybackExperimentListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/listeners/SysdynPlaybackExperimentListener.java @@ -1,6 +1,16 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.listeners; -import java.util.Collection; import java.util.HashMap; import org.eclipse.ui.IEditorPart; @@ -11,18 +21,19 @@ import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.PossibleObjectWithType; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.util.Simantics; import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.diagram.profile.Profiles; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.layer0.Layer0; import org.simantics.project.IProject; import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.experiment.IExperimentListener; +import org.simantics.simulation.ontology.SimulationResource; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; @@ -32,6 +43,7 @@ import org.simantics.ui.SimanticsUI; public class SysdynPlaybackExperimentListener implements IExperimentListener { private Resource model; + private Resource previousProfile; public SysdynPlaybackExperimentListener(SysdynPlaybackExperiment experiment) { this.model = experiment.getModel(); @@ -96,11 +108,11 @@ public class SysdynPlaybackExperimentListener implements IExperimentListener { Layer0 l0 = Layer0.getInstance(graph); SysdynResource sr = SysdynResource.getInstance(graph); - Collection profiles = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.SimulationPlaybackProfile)); - if(profiles.isEmpty()) + Resource defaultProfile = graph.syncRequest(new PossibleObjectWithType(model, l0.ConsistsOf, sr.DefaultProfile)); + if(defaultProfile == null) return; - Resource defaultProfile = profiles.iterator().next(); + setProfileForModel(graph, model, previousProfile == null ? defaultProfile : previousProfile); HashMap infos = getRuntimesForModel(graph, runtimeDiagramsAndModelUris, model); for(Resource runtimeDiagram : infos.keySet()) { @@ -108,7 +120,7 @@ public class SysdynPlaybackExperimentListener implements IExperimentListener { if(previousRuntimeDiagramsAndModelUris.containsKey(runtimeDiagram)) { previous = previousRuntimeDiagramsAndModelUris.get(runtimeDiagram).previousProfile; } - setProfile(graph, model, runtimeDiagram, previous == null ? defaultProfile : previous); + setProfileForDiagram(graph, runtimeDiagram, previous == null ? defaultProfile : previous); } } }); @@ -170,30 +182,34 @@ public class SysdynPlaybackExperimentListener implements IExperimentListener { if(model == null) return; - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - - Resource profile = graph.syncRequest(new PossibleObjectWithType(model, l0.ConsistsOf, sr.SimulationPlaybackProfile)); + Resource profile = getSimulationPlaybackProfile(graph, model); if(profile == null) return; DiagramResource DIA = DiagramResource.getInstance(graph); + Resource current = graph.getPossibleObject(model, DIA.HasActiveProfile); + previousProfile = current; + + if(!profile.equals(current)) { + setProfileForModel(graph, model, profile); + } + HashMap infos = getRuntimesForModel(graph, allDiagramInfos, model); for(Resource runtimeDiagram : infos.keySet()) { DiagramInfo di = infos.get(runtimeDiagram); - Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile); + current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile); + di.previousProfile = current; if(profile.equals(current)) continue; - di.previousProfile = current; - setProfile(graph, model, runtimeDiagram, profile); + setProfileForDiagram(graph, runtimeDiagram, profile); } } }); } - private void setProfile(WriteGraph graph, Resource model, Resource runtimeDiagram, Resource profile) throws DatabaseException { + private void setProfileForDiagram(WriteGraph graph, Resource runtimeDiagram, Resource profile) throws DatabaseException { DiagramResource DIA = DiagramResource.getInstance(graph); Resource current = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile); @@ -202,14 +218,38 @@ public class SysdynPlaybackExperimentListener implements IExperimentListener { graph.deny(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, current); graph.claim(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile, null, profile); - // Set this profile as the default profile for this model - graph.deny(model, DIA.HasActiveProfile); - graph.claim(model, DIA.HasActiveProfile, profile); - // Set this profile as the default profile for this diagram Resource configuration = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration); graph.deny(configuration, DIA.HasActiveProfile); graph.claim(configuration, DIA.HasActiveProfile, profile); } + private void setProfileForModel(WriteGraph graph, Resource model, Resource profile) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + + // Set this profile as the default profile for this model + graph.deny(model, DIA.HasActiveProfile); + graph.claim(model, DIA.HasActiveProfile, profile); + } + + private Resource getSimulationPlaybackProfile(WriteGraph graph, Resource model) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + + Resource profile = graph.syncRequest(new PossibleObjectWithType(model, l0.ConsistsOf, sr.SimulationPlaybackProfile)); + if(profile == null) { + profile = Profiles.createProfile(graph, "Simulation Playback", sr.Profiles_SimulationPlaybackColours); + graph.deny(profile, l0.InstanceOf); + graph.claim(profile, l0.InstanceOf, null, sr.SimulationPlaybackProfile); + graph.claim(model, l0.ConsistsOf, profile); + } + + if(!graph.hasStatement(profile, SIMU.IsActive)) { + graph.claim(profile, SIMU.IsActive, sr.Profiles_SimulationPlaybackColours); + } + + return profile; + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/PlaybackSliderContribution.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/PlaybackSliderContribution.java index 04596c12..e589c31d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/PlaybackSliderContribution.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/menu/PlaybackSliderContribution.java @@ -23,6 +23,11 @@ import org.simantics.ui.SimanticsUI; public class PlaybackSliderContribution extends ToolBarContributionItem { + Runnable timeListener; + SysdynPlaybackExperiment spe; + Slider s; + Double startTime, endTime; + boolean ignoreChange = false; @Override public void fill(ToolBar parent, int index) @@ -33,8 +38,8 @@ public class PlaybackSliderContribution extends ToolBarContributionItem { IExperiment experiment = manager.getActiveExperiment(); if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment)) return; - final SysdynPlaybackExperiment spe = (SysdynPlaybackExperiment)experiment; - + spe = (SysdynPlaybackExperiment)experiment; + Double[] numbers = new Double[3]; try { numbers = SimanticsUI.getSession().syncRequest(new Read() { @@ -52,38 +57,37 @@ public class PlaybackSliderContribution extends ToolBarContributionItem { } catch (DatabaseException e1) { e1.printStackTrace(); } - + ToolItem ti = new ToolItem(parent, SWT.SEPARATOR); - + Composite composite = new Composite(parent, SWT.NONE); - GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, SWT.DEFAULT).numColumns(2).applyTo(composite); GridDataFactory.fillDefaults().applyTo(composite); - Slider s = new Slider(composite, SWT.NONE); + s = new Slider(composite, SWT.NONE); s.setMinimum(0); s.setMaximum(100); s.setIncrement(1); s.setPageIncrement(1); s.setThumb(1); - + final Label label = new Label(composite, SWT.NONE); GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(label); label.setText("0.0"); ti.setWidth(250); ti.setControl(composite); - - final Double startTime = numbers[0]; - final Double endTime = numbers[1]; - + startTime = numbers[0]; + endTime = numbers[1]; + s.addSelectionListener(new SelectionListener() { @Override public void widgetSelected(SelectionEvent e) { Slider s = (Slider)e.widget; Double time = s.getSelection() / 99.0 * (endTime - startTime) + startTime; - spe.setTime(time); + spe.setTimeAndContinue(time); if(!label.isDisposed()) { label.setText(time.toString()); } @@ -93,6 +97,39 @@ public class PlaybackSliderContribution extends ToolBarContributionItem { public void widgetDefaultSelected(SelectionEvent e) { } }); + + if(timeListener != null) { + spe.removeTimeListener(timeListener); + } + + timeListener = new Runnable() { + + @Override + public void run() { + s.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + int value = (int) ((spe.getTime() - startTime) / (endTime - startTime) * 99); + s.setSelection(value); + label.setText("" + spe.getTime()); + } + }); + + } + + }; + spe.addTimeListener(timeListener); + } + } + + @Override + public void dispose() { + if(this.timeListener != null && spe != null) { + spe.removeTimeListener(timeListener); + this.timeListener = null; } + super.dispose(); + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java index 7659e3d5..d81d186e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ValidationUtils.java @@ -27,6 +27,7 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.issues.ontology.IssueResource; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; @@ -39,6 +40,7 @@ public class ValidationUtils { public static void removeIssue(WriteGraph graph, Resource model, Resource source, Resource variable, Resource issue, Collection existing) throws DatabaseException { graph.deny(issue, Layer0.getInstance(graph).PartOf); graph.deny(source, IssueResource.getInstance(graph).Manages, issue); + RemoverUtil.remove(graph, issue); existing.remove(issue); } diff --git a/org.simantics.sysdyn.ui/sysdyn.product b/org.simantics.sysdyn.ui/sysdyn.product index 540ebbc5..8ab825b0 100644 --- a/org.simantics.sysdyn.ui/sysdyn.product +++ b/org.simantics.sysdyn.ui/sysdyn.product @@ -7,7 +7,9 @@ - -fixerrors + -fixerrors +--launcher.XXMaxPermSize +256m -ea -Xmx768M -Xshare:off -XstartOnFirstThread -Dorg.eclipse.swt.internal.carbon.smallFonts diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynPlaybackExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynPlaybackExperiment.java index 870418ea..615c7020 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynPlaybackExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynPlaybackExperiment.java @@ -1,28 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.manager; +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.Simantics; +import org.simantics.db.request.Read; import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IDynamicExperiment; import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.sysdyn.SysdynResource; public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynamicExperiment { - + + public static long DURATION_SLOW = 20000; + public static long DURATION_NORMAL = 10000; + public static long DURATION_FAST = 5000; + double time; public static final long VARIABLE_UPDATE_INTERVAL = 500000000; + private static final double UPDATES_PER_TIME_UNIT = 0.015; + private long playbackDuration = DURATION_NORMAL; + private Collection timeListeners = new ArrayList(); + ScheduledExecutorService playbackExecutionService; + PlaybackConfiguration playbackConfiguration; public SysdynPlaybackExperiment(Resource experiment, Resource model) { super(experiment, model); this.time = 0; } - volatile boolean updating = false; - - public void setTime(double time) { + /** + * Interrupts a possible ongoing playback + * + * @param time + */ + public void setTimeInterrupting(double time) { + stopPlayback(); + setTime(time); + } + + /** + * Sets a new time and continues playback from that point if + * playback was running + * @param time + */ + public void setTimeAndContinue(double time) { + if(isPlaybackRunning()) { + stopPlayback(); + setTime(time); + startPlayback(500); + } else { + setTime(time); + } + } + + private void setTime(double time) { this.time = time; fireValuesChanged(); } @@ -31,6 +84,18 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam return this.time; } + public void setPlaybackDuration(long duration) { + this.playbackDuration = duration; + if(isPlaybackRunning()) { + //Restart playback with different time settings + startPlayback(); + } + } + + public long getPlaybackDuration() { + return this.playbackDuration; + } + @Override public void init(ReadGraph g) { this.session = g.getSession(); @@ -46,4 +111,160 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam } }); } + + + // PLAYBACK CONTROLS + public void startPlayback() { + startPlayback(0); + } + public void startPlayback(long initialDelay) { + if(isPlaybackRunning()) { + stopPlayback(); + } + playbackConfiguration = getPlaybackConfiguration(); + playbackExecutionService = Executors.newScheduledThreadPool(1); + + if(time >= playbackConfiguration.endTime) { + setTime(playbackConfiguration.startTime); + playbackConfiguration = getPlaybackConfiguration(); + } + + Runnable playbackSimulationTask = new PlaybackSimulationTask(time, playbackConfiguration.simulationStepLength); + + long delay = (long) (playbackConfiguration.playbackDuration / playbackConfiguration.intervals); + ScheduledFuture stepper = playbackExecutionService.scheduleWithFixedDelay( + playbackSimulationTask, initialDelay, delay, TimeUnit.MILLISECONDS + ); + + Runnable stopSimulationTask = new StopSimulationTask(stepper, playbackConfiguration.endTime); + playbackExecutionService.schedule(stopSimulationTask, playbackConfiguration.playbackDuration + initialDelay, TimeUnit.MILLISECONDS); + + changeState(ExperimentState.RUNNING); + } + + public boolean isPlaybackRunning() { + return playbackExecutionService != null && !playbackExecutionService.isShutdown(); + } + + public void resetPlayback() { + double startTime = 0.0; + if(isPlaybackRunning() && playbackConfiguration != null) { + startTime = playbackConfiguration.startTime; + } else { + startTime = getPlaybackConfiguration().startTime; + } + setTimeInterrupting(startTime); + } + + public void stopPlayback() { + if(isPlaybackRunning()) { + playbackExecutionService.shutdownNow(); + playbackExecutionService.shutdown(); + if(playbackConfiguration != null) + playbackConfiguration = null; + changeState(ExperimentState.STOPPED); + } + } + + + private class PlaybackSimulationTask implements Runnable { + private int stepCount; + private double startTime, stepLength; + + public PlaybackSimulationTask(double startTime, double stepLength) { + this.startTime = startTime; + this.stepLength = stepLength; + } + + public void run() { + ++stepCount; + setTime(startTime + stepCount * stepLength); +// System.out.println("Playback step at time: " + (startTime + stepCount * stepLength) + " (step: " + stepCount + ")"); + } + } + + private class StopSimulationTask implements Runnable { + + private ScheduledFuture scheduledFuture; + private double endTime; + + public StopSimulationTask(ScheduledFuture aSchedFuture, double endTime){ + scheduledFuture = aSchedFuture; + this.endTime = endTime; + } + public void run() { +// System.out.println("Stopping playback"); + scheduledFuture.cancel(false); + stopPlayback(); + setTime(endTime); + } + + } + + private PlaybackConfiguration getPlaybackConfiguration() { + Double[] numbers = new Double[3]; + try { + numbers = Simantics.getSession().syncRequest(new Read() { + @Override + public Double[] perform(ReadGraph graph) throws DatabaseException { + Double[] numbers = new Double[3]; + Resource model = getModel(); + SysdynResource sr = SysdynResource.getInstance(graph); + numbers[0] = graph.getRelatedValue(model, sr.HasStartTime); + numbers[1] = graph.getRelatedValue(model, sr.HasStopTime); + numbers[2] = graph.getPossibleRelatedValue(model, sr.HasOutputInterval); + return numbers; + } + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + PlaybackConfiguration config = new PlaybackConfiguration(); + config.simulationDuration = numbers[1] - numbers[0] - time; + config.playbackDuration = (long) (config.simulationDuration / (config.simulationDuration + time) * playbackDuration); + config.intervals = config.playbackDuration * UPDATES_PER_TIME_UNIT; + config.simulationStepLength = config.simulationDuration / config.intervals; + config.endTime = numbers[1]; + config.startTime = numbers[0]; + return config; + } + + private class PlaybackConfiguration { + public double simulationDuration, simulationStepLength, intervals, endTime, startTime; + public long playbackDuration; + } + + protected void localStateChange() { + super.localStateChange(); + + ExperimentState state = getState(); + if(ExperimentState.DISPOSED.equals(state)) { + stopPlayback(); + } + } + + // TIME LISTENERS + public void addTimeListener(Runnable timeListener) { + if(!this.timeListeners.contains(timeListener)) + this.timeListeners.add(timeListener); + } + + public Collection getTimeListeners() { + return this.timeListeners; + } + + public void removeTimeListener(Runnable timeListener) { + this.timeListeners.remove(timeListener); + } + + @Override + public void fireValuesChanged() { + for(Runnable listener : timeListeners) { + listener.run(); + } + super.fireValuesChanged(); + + } + } -- 2.47.1