/*
* Datei: AreaUsageTracker.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt: 2010_10_29
*
* (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.other;
import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.miscellaneous.StaticMethods;
import eas.plugins.AbstractDefaultPlugin;
import eas.plugins.PluginProperties;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
@PluginProperties(pluginIsHidden = true)
public class AllroundAreaUsageTrackerPlugin extends AbstractDefaultPlugin<AbstractEnvironment2D<AbstractAgent2D<?>>> {
/**
*
*/
private static final long serialVersionUID = 8669292960948575126L;
@Override
public List<String> getRequiredPlugins() {
return null;
}
@Override
public List<SingleParameter> getParameters() {
final LinkedList<SingleParameter> params = new LinkedList<SingleParameter>();
params.add(new SingleParameter(
"gridPoints",
Datatypes.VECTOR2D,
new Vector2D(10, 10),
"",
id()));
return params;
}
@Override
public String id() {
return AbstractDefaultPlugin.ALLROUND_PLUGIN_PREFIX + "-areaUsagePlugin";
}
private Point gridPoints;
private double[][] matrix;
private Rectangle2D boundingBox;
@Override
public void runBeforeSimulation(final AbstractEnvironment2D<AbstractAgent2D<?>> env, final ParCollection params) {
gridPoints = params.getParValueVector2D("gridPoints").toPoint();
boundingBox = new Rectangle2D(new Vector2D(env.getBoundingBox(false)
.upperLeftCorner()), new Vector2D(env.getBoundingBox(false).lowerRightCorner()));
matrix = new double[gridPoints.x][gridPoints.y];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 1;
}
}
}
private ArrayList<String> output() {
final ArrayList<String> list = new ArrayList<String>(matrix.length);
String s;
for (final double[] array : matrix) {
s = "";
for (final double element : array) {
s += element + " ";
}
list.add(s);
}
return list;
}
private void storeResults(final ParCollection params) {
StaticMethods.speichereTextAusArray("./sharedDirectory", "plot.txt", output(), params);
}
@Override
public void runAfterSimulation(final AbstractEnvironment2D<AbstractAgent2D<?>> env, final ParCollection params) {
storeResults(params);
}
@Override
public void runDuringSimulation(
final AbstractEnvironment2D<AbstractAgent2D<?>> env,
final Wink simZyk,
final ParCollection params) {
final List<AbstractAgent2D<?>> agentList = env.getAgents();
Vector2D agentPosition;
int xField, yField;
final double xFieldWidth = boundingBox.getWidth() / gridPoints.x;
final double yFieldWidth = boundingBox.getHeight() / gridPoints.y;
for (final AbstractAgent<?> agent : agentList) {
// if (!agent.getClass().isInstance(new ObstacleAgent(0, null, null))) {
agentPosition = env.getAgentPosition(agent.id());
xField = (int) ((agentPosition.x - boundingBox.upperLeftCorner().x) / (xFieldWidth));
yField = (int) ((agentPosition.y - boundingBox.upperLeftCorner().y) / (yFieldWidth));
if (xField < matrix.length && yField < matrix[0].length
&& xField >= 0 && yField >= 0) {
matrix[xField][yField] *= 0.9;
}
// }
}
}
@Override
public void handleEvent(final EASEvent e, final AbstractEnvironment2D<AbstractAgent2D<?>> env,
final Wink lastTimeStep, final ParCollection params) {
}
@Override
public List<String> getSupportedPlugins() {
return null;
}
@Override
public void onSimulationResumed(AbstractEnvironment2D<AbstractAgent2D<?>> env, Wink resumeTime,
ParCollection params) {
}
}