import org.simantics.simulation.experiment.Experiment;\r
import org.simantics.simulation.experiment.ExperimentState;\r
import org.simantics.simulation.experiment.IDynamicExperiment;\r
+import org.simantics.simulation.experiment.IExperimentListener;\r
import org.simantics.simulation.ontology.SimulationResource;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.adapter.VariableValueSubscription;\r
*/\r
public void init(ReadGraph g) {\r
this.session = g.getSession();\r
- changeState(ExperimentState.STOPPED);\r
+ state = ExperimentState.STOPPED;\r
+ for(IExperimentListener listener : listeners.getListeners())\r
+ listener.stateChanged(state);\r
\r
session.asyncRequest(new ReadRequest() {\r
\r
toggleActivation(graph, true);\r
}\r
});\r
-\r
+ \r
+ setStateToDatabase(ExperimentState.INITIALIZING);\r
}\r
\r
@Override\r
@Override\r
public void simulate(boolean enabled) {\r
if(enabled && sysdynModel != null) {\r
- changeState(ExperimentState.RUNNING);\r
- startSimulationJob();\r
+ if(!ExperimentState.RUNNING.equals(getState())) {\r
+ changeState(ExperimentState.RUNNING);\r
+ startSimulationJob();\r
+ }\r
} else if (!toggled){\r
changeState(ExperimentState.STOPPED);\r
}\r
}\r
\r
protected void localStateChange() {\r
- final ExperimentState state = getState();\r
- session.asyncRequest(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- SimulationResource SR = SimulationResource.getInstance(graph);\r
- graph.deny(model, SR.HasExperimentState);\r
- Resource st = graph.newResource();\r
- switch(state) {\r
- case INITIALIZING:\r
- graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing);\r
- break;\r
- case RUNNING:\r
- graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running);\r
- break;\r
- case STOPPED:\r
- graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped);\r
- break;\r
- case DISPOSED:\r
- graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed);\r
- break;\r
- }\r
-\r
- graph.claim(model, SR.HasExperimentState, st);\r
- }});\r
-\r
+ setStateToDatabase(getState());\r
switch(state) {\r
case DISPOSED:\r
onExperimentDisposed();\r
\r
}\r
\r
+ protected void setStateToDatabase(final ExperimentState state) {\r
+ session.asyncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+ final Session session = graph.getSession();\r
+ session.asyncRequest(new WriteRequest(support.getWorkspacePersistent("experiments")) {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ SimulationResource SR = SimulationResource.getInstance(graph);\r
+ graph.deny(model, SR.HasExperimentState);\r
+ Resource st = graph.newResource();\r
+ switch(state) {\r
+ case INITIALIZING:\r
+ graph.claim(st, L0.InstanceOf, SR.ExperimentState_Initializing);\r
+ break;\r
+ case RUNNING:\r
+ graph.claim(st, L0.InstanceOf, SR.ExperimentState_Running);\r
+ break;\r
+ case STOPPED:\r
+ graph.claim(st, L0.InstanceOf, SR.ExperimentState_Stopped);\r
+ break;\r
+ case DISPOSED:\r
+ graph.claim(st, L0.InstanceOf, SR.ExperimentState_Disposed);\r
+ break;\r
+ }\r
+\r
+ graph.claim(model, SR.HasExperimentState, st);\r
+ }}); \r
+ }\r
+ });\r
+ }\r
+ \r
/**\r
* Actions performed when experiment is disposed\r
* @param graph\r