/*
* File name: SimStateLoaderMaster.java (package eas.plugins.masterScheduler)
* Author(s): Lukas König
* Java version: 8.0 (at generation time)
* Generation date: 02.06.2014 (16:28:14)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.plugins.standard.storeAndLoad;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import eas.plugins.Plugin;
import eas.plugins.PluginFactory;
import eas.plugins.masterScheduler.MasterScheduler;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.simulation.EASRunnable;
import eas.simulation.SerializableSimulationState;
import eas.simulation.SimulationTime;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.simulation.standardEnvironments.DummyEnvironment;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.StarterMenuListener;
import eas.startSetup.parameterDatatypes.ArrayListString;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*/
public class SimStateLoaderMaster implements
MasterScheduler<EASRunnable> {
private static final long serialVersionUID = -604597667074272766L;
@Override
public List<String> getRequiredPlugins() {
return null;
}
@Override
public List<String> getSupportedPlugins() {
return null;
}
private static String filePath;
private static ArrayListString additionalPlugins;
public static final String ADDITIONAL_PLUGINS_TO_LOAD_PARNAME = "additionalPlugins";
private static ArrayListString pluginsToRemove;
public static final String PLUGINS_TO_REMOVE_PARNAME = "pluginsToRemove";
public static void setFilePath(String filePath) {
SimStateLoaderMaster.filePath = filePath;
}
public static void setAdditionalPlugins(ArrayListString additionalPlugins) {
SimStateLoaderMaster.additionalPlugins = additionalPlugins;
}
public static void setPluginsToRemove(ArrayListString pluginsToRemove) {
SimStateLoaderMaster.pluginsToRemove = pluginsToRemove;
}
@Override
public List<SingleParameter> getParameters() {
LinkedList<SingleParameter> list = new LinkedList<SingleParameter>();
list.add(new SingleParameter("filePath", Datatypes.STRING, "null", SimStateLoaderMaster.class));
ArrayListString stdAddPlugs = new ArrayListString();
stdAddPlugs.add(new AllroundVideoPlugin().id());
ArrayListString stdRemPlugs = new ArrayListString();
stdAddPlugs.add(new AllroundSimstateStorerPlugin().id());
list.add(new SingleParameter(ADDITIONAL_PLUGINS_TO_LOAD_PARNAME, Datatypes.STRING_ARR, stdAddPlugs, SimStateLoaderMaster.class));
list.add(new SingleParameter(PLUGINS_TO_REMOVE_PARNAME, Datatypes.STRING_ARR, stdRemPlugs, SimStateLoaderMaster.class));
return list;
}
@Override
public String id() {
return "simStateLoader";
}
@Override
public void runBeforeSimulation(EASRunnable env, ParCollection params) {
File f = new File(SimStateLoaderMaster.filePath);
SerializableSimulationState simState = new SerializableSimulationState(null, f);
SimulationTime<?> loadedSimTime = simState.load();
for (String plug : additionalPlugins) {
if (loadedSimTime.getPluginObject(plug) == null) {
loadedSimTime.registerPlugin(PluginFactory.getKonstPlug(plug, params));
}
}
for (String plug : pluginsToRemove) {
Plugin<?> registeredPlugin = loadedSimTime.getPluginObject(plug);
if (registeredPlugin != null) {
loadedSimTime.neverNotifyAgain(registeredPlugin);
}
}
StarterMenuListener.informAboutPausedSimulation();
loadedSimTime.resumeLoadedSimulation();
}
@Override
public void runAfterSimulation(EASRunnable env, ParCollection params) {
}
@Override
public void runDuringSimulation(EASRunnable env, Wink currentSimTime,
ParCollection params) {
}
@Override
public void handleEvent(EASEvent event, EASRunnable env, Wink lastSimTime,
ParCollection params) {
}
@Override
public void onSimulationResumed(EASRunnable env, Wink resumeTime,
ParCollection params) {
}
@Override
public EASRunnable[] generateRunnables(ParCollection params) {
return new DummyEnvironment[] {new DummyEnvironment(-1, params)};
}
/**
* The simulation run is terminated right after initialization as this
* plugin only loads a stored simulation. That means that the actual
* simulation run is the loaded simulation while this run is a "dummy"
* run only.
*/
@Override
public boolean isTerminationRequested(EASRunnable runnable,
Wink currentTime, ParCollection params) {
return true;
}
@Override
public String pluginDescription() {
return "The '" + this.id() + "' plugin is active only before the start of the simulation. "
+ "It loads a given stored simulation state and resumes it, i.e., the actual "
+ "simulation run is the one given by the stored simulation state, while "
+ "this simulation run is terminated after loading.";
}
}