/*
* File name: ObserverControllerPlugin.java (package eas.plugins.standard)
* Author(s): Lukas König
* Java version: 8.0 (at generation time)
* Generation date: 21.05.2014 (11:38:50)
*
* (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.liveInteraction;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import eas.miscellaneous.system.windowFrames.GeneralDialog;
import eas.plugins.AbstractDefaultPlugin;
import eas.plugins.standard.visualization.AllroundVideoPlugin;
import eas.plugins.standard.visualization.chartPlugin.AllroundChartPlugin;
import eas.plugins.standard.visualization.chartPlugin.ChartEvent;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.GlobalVariables;
import eas.startSetup.ParCollection;
/**
* @author Lukas König
*/
public class AllroundObserverControllerPlugin extends AbstractDefaultPlugin<AbstractEnvironment<?>> {
private static final long serialVersionUID = -6018284982065421572L;
private OCWindow frame;
private AllroundVideoPlugin vid;
private int markedID;
@Override
public String id() {
return AbstractDefaultPlugin.ALLROUND_PLUGIN_PREFIX + "-observerAndController";
}
@Override
public void runBeforeSimulation(AbstractEnvironment<?> env, ParCollection params) {
frame = new OCWindow("Observe and control single agents");
try {
this.vid = (AllroundVideoPlugin) env.getSimTime().getPluginObject(new AllroundVideoPlugin().id());
} catch (Exception e) {
}
this.reset();
}
@Override
public void runAfterSimulation(AbstractEnvironment<?> env, ParCollection params) {
}
private AbstractAgent<?> currentlyObservedAgent = null;
private transient AllroundChartPlugin chartPlugin;
@Override
public void runDuringSimulation(AbstractEnvironment<?> env, Wink currentSimTime, ParCollection params) {
if (vid == null) {
env.getSimTime().neglectTicks(this);
return;
}
Integer vidMarkedID = vid.getMarkedAgentId();
if (vidMarkedID != null && currentlyObservedAgent != env.getAgent(vidMarkedID)) {
this.reset();
}
AbstractAgent<?> agt = null;
if (vidMarkedID != null && markedID != vidMarkedID) {
markedID = vidMarkedID;
agt = env.getAgent(vidMarkedID);
}
if (vidMarkedID == null && markedID != -1) {
markedID = -1;
agt = env;
}
if (agt != null) {
this.frame.setMethods(agt);
frame.setVisible(true);
currentlyObservedAgent = agt;
}
// Invoking.
if (this.frame.invokeNext != null) {
try {
this.frame.invokeNext.invoke(this.frame.invokeOnNext, this.frame.invokeWithNext);
} catch (Exception e) {
GlobalVariables.getPrematureParameters().logWarning(
"Invokation failed: " + e.toString() + " - " + this.frame.invokeNext.getName());
}
this.frame.invokeNext = null;
this.frame.invokeOnNext = null;
this.frame.invokeWithNext = null;
}
// Chart events.
for (ChartEvent event : this.frame.getEvents().keySet()) {
// Try finding chart plugin.
if (chartPlugin == null) {
try {
chartPlugin = (AllroundChartPlugin) env.getSimTime().getPluginObject(new AllroundChartPlugin().id());
} catch (Exception e) {
}
if (chartPlugin == null) {
this.chartPlugin = new AllroundChartPlugin();
if (GeneralDialog.yesNoAnswer(
"Chartplugin not found",
"Plugin '" + new AllroundChartPlugin().id() + "' is required to draw charts. Should I register it?")) {
env.getSimTime().registerPlugin(this.chartPlugin);
}
}
}
// If there is a chart plugin.
if (this.chartPlugin != null) {
Object[] pars = this.frame.getEvents().get(event);
Method m = this.frame.getChartMethods().get(event);
AbstractAgent<?> agent = this.frame.getChartAgents().get(event);
try {
Object returnValue = m.invoke(agent, pars);
if (returnValue == null) {
returnValue = new Double(0);
}
if (returnValue.getClass().equals(Boolean.class) || returnValue.getClass().equals(Boolean.TYPE)) {
if ((Boolean) returnValue) {
event.setValue(1);
} else {
event.setValue(0);
}
} else {
event.setValue(Double.parseDouble(returnValue.toString()));
}
} catch (Exception e) {
GlobalVariables.getPrematureParameters().logWarning(
"Invokation failed: " + e.toString() + " - " + m.getName());
}
env.getSimTime().broadcastEvent(event);
}
}
}
public void reset() {
this.markedID = Integer.MIN_VALUE;
this.currentlyObservedAgent = null;
this.frame.setVisible(true);
}
@Override
public List<String> getSupportedPlugins() {
List<String> supp = super.getSupportedPlugins();
if (supp == null) {
supp = new LinkedList<>();
}
supp.add(new AllroundChartPlugin().id());
return supp;
}
@Override
public void onSimulationResumed(AbstractEnvironment<?> env,
Wink resumeTime, ParCollection params) {
frame = new OCWindow("Observe and control single agents");
this.reset();
}
@Override
public List<String> getRequiredPlugins() {
List<String> plugins = super.getRequiredPlugins();
plugins.add(new AllroundVideoPlugin().id());
return plugins;
}
}