/*
* File name: CarsScheduler.java (package eas.users.ocScenario.cars)
* Author(s): Lukas König
* Java version: 7.0
* Generation date: 09.05.2014 (14:54:30)
*
* (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.users.ocScenarios.traffic;
import java.util.List;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.standard.liveInteraction.AllroundLiveParameterSetterPlugin;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
/**
* The master scheduler implementation for the OC cars scenario. The scheduler
* provides a single CarsEnvironment object to simulate for the simulation
* engine (cf. method generateRunnables). Other than that, the behavior hardly
* differs from the default behavior given by AbstractDefaultMaster.
*
* @author Lukas König
*/
public class TrafficScheduler extends AbstractDefaultMaster<TrafficEnvironment> {
/**
* All serializable classes must have this.
*/
private static final long serialVersionUID = -2903906014491143313L;
/**
* Generates a list of Environments that are run simultaneously by the simulation engine
* in different threads. In this case, only one CarsEnvironment is generated
* which is filled with streets and (other) agents by a constructor call.
*
* @param params The program parameters of the run.
*
* @return A list of environments that contains at least (in this case exactly)
* one item.
*/
@Override
public TrafficEnvironment[] generateRunnables(ParCollection params) {
TrafficEnvironment env = new TrafficEnvironment(0, params);
return new TrafficEnvironment[] {env};
}
/**
* This method is invoked by the simulation engine during simulation
* time, specifically at cycle <code>currentSimTime</code>.
* <BR>
* Apart from invoking the method from the super class, the visualization
* speed is set to one tick per second.
*
* @param env The runnable that this plugin is called on.
* @param currentSimTime The current point in time.
* @param params The parameters.
*/
@Override
public synchronized void runDuringSimulation(TrafficEnvironment env, Wink currentSimTime, ParCollection params) {
super.runDuringSimulation(env, currentSimTime, params);
if (currentSimTime.getLastTick() == 1) {
try {
AllroundVideoPlugin vid = (AllroundVideoPlugin) env.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
vid.setPauseInterval(2000);
} catch (Exception e) {
}
}
}
/**
* This method is called by the simulation engine before the first simulation cycle.
* All initialization processes are completed at this time.
* <BR><BR>
* In this case, the parameter setter plugin is told which parameters to show
* during runtime.
*
* @param env The runnable that this plugin is called on.
* @param params The parameters.
*/
@Override
public void runBeforeSimulation(TrafficEnvironment env, ParCollection params) {
super.runBeforeSimulation(env, params);
List<SingleParameter> pars = TrafficParameters.getParameter();
String showThese = "";
for (SingleParameter par : pars) {
showThese += par.getParameterName() + ",";
}
params.setParValue(AllroundLiveParameterSetterPlugin.SHOW_THESE_PAR_NAME, showThese);
}
/**
* This method is invoked once when the simulation is resumed from a
* serialized state. This is usually the case after loading a simulation
* which has been stored to the disc. The method particularly has
* to handle all things that cannot be serialized. This includes:
* - Fields declared transient are not serialized,
* - Window frames' visibility has to be reset if desired,
* - Be careful when using anonymous inner classes (not quite sure why),
* - maybe more...
* Nearly all EAS classes are serializable, so there should be no
* problems in most cases. You will probably have most trouble with
* BufferedImage, so try to avoid fields of type BufferedImage.
*
* @param env The main runnable of the resumed simulation.
* @param resumeTime The time when the simulation was resumed.
* @param params The parameter collection.
*/
@Override
public void onSimulationResumed(TrafficEnvironment env, Wink resumeTime,
ParCollection params) {
super.onSimulationResumed(env, resumeTime, params); // Invoke super class.
TrafficParameters.resetRequested = false; // Suppress reset procedure.
}
/**
* A textual identification String of this plugin.
* The identification has to be unique, i.e., two different plugins have
* to have two different ids.
*
* @return An identification string of this plugin.
*/
@Override
public String id() {
/*
* As the scheduler is derived from AbstractDefaultMaster,
* A standard text given by the constant String below precedes the
* actual identification part "-cars".
*/
return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-cars";
}
}