From: lempinen Date: Wed, 4 Jul 2012 12:18:19 +0000 (+0000) Subject: Functions for enabling/disabling simulation control buttons (refs #3511) X-Git-Tag: simantics-1.7~18 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ad233620b6bcefc6592629cd7d884717e8b28b7c;p=simantics%2Fsysdyn.git Functions for enabling/disabling simulation control buttons (refs #3511) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25274 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index 1c1852b1..aea336c8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -48,6 +48,7 @@ import org.simantics.simulation.data.Datasource; import org.simantics.simulation.experiment.Experiment; import org.simantics.simulation.experiment.ExperimentState; import org.simantics.simulation.experiment.IDynamicExperiment; +import org.simantics.simulation.experiment.IExperimentListener; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.adapter.VariableValueSubscription; @@ -89,7 +90,9 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { */ public void init(ReadGraph g) { this.session = g.getSession(); - changeState(ExperimentState.STOPPED); + state = ExperimentState.STOPPED; + for(IExperimentListener listener : listeners.getListeners()) + listener.stateChanged(state); session.asyncRequest(new ReadRequest() { @@ -100,7 +103,8 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { toggleActivation(graph, true); } }); - + + setStateToDatabase(ExperimentState.INITIALIZING); } @Override @@ -145,8 +149,10 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { @Override public void simulate(boolean enabled) { if(enabled && sysdynModel != null) { - changeState(ExperimentState.RUNNING); - startSimulationJob(); + if(!ExperimentState.RUNNING.equals(getState())) { + changeState(ExperimentState.RUNNING); + startSimulationJob(); + } } else if (!toggled){ changeState(ExperimentState.STOPPED); } @@ -562,32 +568,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { } protected void localStateChange() { - final ExperimentState state = getState(); - session.asyncRequest(new WriteRequest() { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - SimulationResource SR = SimulationResource.getInstance(graph); - graph.deny(model, SR.HasExperimentState); - Resource st = graph.newResource(); - switch(state) { - case INITIALIZING: - graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing); - break; - case RUNNING: - graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running); - break; - case STOPPED: - graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped); - break; - case DISPOSED: - graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed); - break; - } - - graph.claim(model, SR.HasExperimentState, st); - }}); - + setStateToDatabase(getState()); switch(state) { case DISPOSED: onExperimentDisposed(); @@ -596,6 +577,41 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { } + protected void setStateToDatabase(final ExperimentState state) { + session.asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + final Session session = graph.getSession(); + session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + SimulationResource SR = SimulationResource.getInstance(graph); + graph.deny(model, SR.HasExperimentState); + Resource st = graph.newResource(); + switch(state) { + case INITIALIZING: + graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing); + break; + case RUNNING: + graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running); + break; + case STOPPED: + graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped); + break; + case DISPOSED: + graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed); + break; + } + + graph.claim(model, SR.HasExperimentState, st); + }}); + } + }); + } + /** * Actions performed when experiment is disposed * @param graph