Package eas.plugins.standard.eaPlugin

Source Code of eas.plugins.standard.eaPlugin.DefaultMasterEA

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

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import eas.plugins.masterScheduler.MasterScheduler;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.marbSimulation.EnvironmentEA;
import eas.simulation.spatial.sim2D.marbSimulation.RobEA;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Lukas König
*
*/
public class DefaultMasterEA implements MasterScheduler<EnvironmentEA> {

    /**
     *
     */
    private static final long serialVersionUID = 6313165497587658337L;

    /**
     * Constructor.
     *
     * @param params  The parameters.
     */
    @Override
    public EnvironmentEA[] generateRunnables(
            ParCollection params) {
        int i = params.getParValueInt("threadAnzahl");
        if (i == 0) {
            i = 1;
        }
        EnvironmentEA[] list = new EnvironmentEA[i];
        Random rand = new Random(params.getSeed());
       
        for (int j = 0; j < i; j++) {
            rand = new Random(rand.nextLong());
            list[j] = new EnvironmentEA(
                    rand,
                    j,
                    null,
                    params,
                    params.getPlugins(),
                    "Simulation " + j);
        }
       
        return list;
    }

    /**
     * @return  The master scheduler's id.
     */
    @Override
    public String id() {
        return eas.simulation.ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-ea";
    }

    /**
     * @return  The plugins required for <code>this</code>.
     */
    @Override
    public List<String> getRequiredPlugins() {
        LinkedList<String> list = new LinkedList<String>();
        list.add(new EAPlugin().id());
        return list;
    }

    /**
     * Returns the list of generic parameters consisting of all parameters of
     * the generated environments and agents by the method
     * generateEnvironments.
     *
     * Note that agents that are added later to the environment (i.e. are not
     * present in the environment given by the scheduler) do not get checked
     * for parameters. If parameters of these agents should be added to the
     * parameter collection, this has to be done manually.
     */
    @Override
    public List<SingleParameter> getParameters() {
        LinkedList<SingleParameter> list = new LinkedList<SingleParameter>();
//        ParCollection paramsZwisch = new ParCollection(new String[] {});
//        paramsZwisch.complete();

        list.add(new SingleParameter(
                "TimeToTermination",
                Datatypes.DOUBLE,
                100000.0,
                "Simulation time till termination.",
                this.id().toUpperCase()));

        EnvironmentEA[] environments = this.generateRunnables(GlobalVariables.getPrematureParameters());
        LinkedList<SingleParameter> wholeList = new LinkedList<SingleParameter>();
        for (EnvironmentEA e : environments) {
            wholeList.addAll(e.getParameters());
            for (AbstractAgent<?> a : e.getAgents()) {
                wholeList.addAll(a.getParameters());
            }
        }
       
        for (SingleParameter s1 : wholeList) {
            boolean contains = false;
            for (SingleParameter s2 : list) {
                if (s2.getParameterName().equals(s1.getParameterName())) {
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                list.add(s1);
            }
        }
       
        list.add(new SingleParameter(
                "threadAnzahl",
                Datatypes.INTEGER,
                1));

        return list;
    }

    /**
     * No standard functionality implemented.
     */
    @Override
    public void runAfterSimulation(
            final EnvironmentEA umg,
            final ParCollection params) {

    }

    /**
     * No standard functionality implemented.
     */
    @Override
    public void runBeforeSimulation(
            final EnvironmentEA umg,
            final ParCollection params) {

    }

    /**
     * Propagate time step to environment and all agents contained in it.
     */
    @Override
    public synchronized void runDuringSimulation(
            final EnvironmentEA env,
            final Wink simZyk,
            final ParCollection params) {
        env.step(simZyk);
        for (AbstractAgent<?> agent : env.getAgents()) {
            agent.step(simZyk);
        }
    }

    /**
     * Propagate event to environment and all agents contained in it.
     */
    @Override
    public void handleEvent(
            final EASEvent e,
            final EnvironmentEA env,
            final Wink lastTick,
            final ParCollection params) {
        env.handleEvent(e, lastTick);
        for (RobEA agent : env.getAgents()) {
            agent.handleEvent(e, lastTick);
        }
    }

    @Override
    public List<String> getSupportedPlugins() {
        return null;
    }

    @Override
    public boolean isTerminationRequested(EnvironmentEA runnable,
            Wink currentTime, ParCollection params) {
        return params.getParValueDouble("TimeToTermination") < currentTime.getCurrentTime();
    }

    @Override
    public void onSimulationResumed(EnvironmentEA env, Wink resumeTime,
            ParCollection params) {
       
    }
   
    @Override
    public String pluginDescription() {
        return "Default EA master scheduler implementation (override pluginDescription method to provide a specific description here).";
    }
}
TOP

Related Classes of eas.plugins.standard.eaPlugin.DefaultMasterEA

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.