Package eas.simulation

Source Code of eas.simulation.SimulationStarter

/*
* Datei: SimulationStarter.java
* Autor(en):        Lukas König
* Java-Version:     6.0
* Erstellt:         21.05.2010
*
* (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.simulation;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;

import eas.miscellaneous.StaticMethods;
import eas.plugins.Plugin;
import eas.plugins.masterScheduler.MasterScheduler;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;

/**
*
* @author Lukas König
*/
public class SimulationStarter implements Serializable {

    private static final long serialVersionUID = -3118453692238795898L;

    /**
     * The mapping from environments to the corresponding simulation timers.
     */
    private HashMap<EASRunnable, SimulationTime<?>> timers;
   
    /**
     * The parameters.
     */
    private ParCollection pars;
   
    /**
     * The schedulers.
     */
    private LinkedList<Plugin<?>> plugins;
   
    /**
     * The master scheduler (also first plugin in list above).
     */
    private MasterScheduler<?> masterScheduler;
   
   /**
    * Constructor that initializes and starts the simultanous simulation
    * of a list of environments given by the master scheduler.
    *
    * @param params  The parameters.
    */
    public SimulationStarter(final ParCollection params) {
        if (params == null) {
            throw new RuntimeException("No parameters given.");
        }
       
        this.pars = params;
        this.timers = new HashMap<EASRunnable, SimulationTime<?>>();
        this.masterScheduler = params.getMasterScheduler();
        this.plugins = new LinkedList<Plugin<?>>();
        this.plugins.add(this.masterScheduler);
        this.plugins.addAll(params.getPlugins());
        EASRunnable[] envs = this.masterScheduler.generateRunnables(params);
       
        if (envs != null) {
            for (EASRunnable e : envs) {
                LinkedList<Plugin<?>> pluginsCopy = new LinkedList<Plugin<?>>();
                pluginsCopy.add(params.getMasterScheduler());
                pluginsCopy.addAll(params.getPlugins());
               
                @SuppressWarnings({ "unchecked", "rawtypes" })
                SimulationTime<?> simT = new SimulationTime(e, pluginsCopy, params);
                e.setSimTime(simT);
                this.timers.put(e, simT);
                simT.timeStart();
//                LiveWindow.gogo(null);
            }
        } else {
            throw new RuntimeException("No environments to simulate.");
        }
       
        StaticMethods.log(
                StaticMethods.LOG_INFO,
                "<" + this.getClass().getSimpleName() + "> Simulation started - simulating "
                + envs.length
                + " runnable(s).",
                this.pars);
       
//        SonstMeth.log(
//                SonstMeth.LOG_INFO,
//                "Simulation terminated - "
//                + envs.length
//                + " runnable(s) simulated.",
//                this.pars);
    }
   
    public static final String ERROR_LOG_FILE_NAME = "Error" + "_" + System.currentTimeMillis() + ".log";
   
    /**
     * @param args  Parameters for the simulation formatted as alternating
     *              name / value pairs.
     */
    public static void main(String[] args) {
        SimulationStarter starter;
        ParCollection params = GlobalVariables.getPrematureParameters();
        params.overwriteParameterList(args);
       
        StaticMethods.log(
                StaticMethods.LOG_DEBUG,
                params.toString(),
                params,
                "plain",
                null);

//        try {
//            File errorDir = new File(params.getStdDirectory() + "/" + "errorLogging");
//            errorDir.mkdir();
//            File errorFile = new File(errorDir
//                    + "/"
//                    + ERROR_LOG_FILE_NAME);
//            System.setErr(new PrintStream(errorFile));
//            params.logInfo("Error messages are henceforth printed to: " + errorFile);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }

        starter = new SimulationStarter(params);
        starter.toString(); // Dummy-instruction.
    }
}
TOP

Related Classes of eas.simulation.SimulationStarter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.