Package eas.users.ocScenarios.traffic.lights

Source Code of eas.users.ocScenarios.traffic.lights.LightsPlugin

/*
* File name:        LightsPlugin.java (package eas.users.ocScenario.cars.lights)
* Author(s):        Lukas König
* Java version:     7.0
* Generation date:  10.05.2014 (21:35:41)
*
* (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.lights;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import eas.math.geometry.Vector2D;
import eas.plugins.AbstractDefaultPlugin;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
import eas.users.ocScenarios.traffic.TrafficEnvironment;
import eas.users.ocScenarios.traffic.TrafficParameters;

/**
* @author Lukas König
*/
public class LightsPlugin extends AbstractDefaultPlugin<TrafficEnvironment> {

    private static final long serialVersionUID = -1119533671607927212L;

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

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

    @Override
    public List<SingleParameter> getParameters() {
        ArrayList<SingleParameter> list = new ArrayList<>(1);
        list.add(new SingleParameter("LightsInterval", Datatypes.integerRange(0, 200), 75, TrafficParameters.class));
        return list;
    }

    @Override
    public String id() {
        return "cars-lights";
    }

    @Override
    public void runBeforeSimulation(TrafficEnvironment env, ParCollection params) {
        this.rand = new Random(params.getSeed());
    }

    @Override
    public void runAfterSimulation(TrafficEnvironment env, ParCollection params) {
       
    }

    private HashMap<Integer, CrossingLights> lightsCollection = new HashMap<>();
   
    @Override
    public void runDuringSimulation(TrafficEnvironment env, Wink currentSimTime,
            ParCollection params) {
        int lightInterval = TrafficParameters.lightsInterval;
       
        if (TrafficParameters.resetRequested) {
            this.reset(env, params);
        }
       
        if (lightInterval == 0) {
            return;
        }
       
        if (currentSimTime.getLastTick() % lightInterval == 0) {
            for (CrossingLights cl : this.lightsCollection.values()) {
                cl.toggle();
            }
        }
    }

    private void removeAllLights(TrafficEnvironment env) {
        for (CrossingLights cl : this.lightsCollection.values()) {
            for (int i = 0; i < 4; i++) {
                try {
                    env.removeAgent(cl.getLightAgent(i).id());
                } catch (Exception e) {
                }
            }
        }
    }
   
    public void reset(TrafficEnvironment env, ParCollection params) {
        int num = 0;

        this.removeAllLights(env);
        if (!params.getParValueBoolean("TwoLanes")) {
            return;
        }
       
        for (int i = 0; i < env.getWidth(); i++) {
            for (int j = 0; j < env.getHeight(); j++) {
                if (env.isRightLaneNtoS(i, j) && env.isLeftLaneWtoE(i, j) && env.isCrossing(i, j)) {
                    CrossingLights agents = new CrossingLights(env, params, new Vector2D(i, j), rand);
                    if (params.getParValueInt("LightsInterval") == 0) {
                        agents.setAllGreen();
                    } else {
                        agents.setRandDir();
                    }
                    lightsCollection.put(num, agents);
                    num++;
                }
            }
        }
    }

    private Random rand;
   
    @Override
    public void handleEvent(EASEvent event, TrafficEnvironment env,
            Wink lastSimTime, ParCollection params) {
       
    }

    @Override
    public void onSimulationResumed(TrafficEnvironment env, Wink resumeTime,
            ParCollection params) {
       
    }
}
TOP

Related Classes of eas.users.ocScenarios.traffic.lights.LightsPlugin

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.