/*
* Datei: Simulation.java
* Autor(en): Lukas König
* Java-Version: 6.0
* Erstellt (vor): 08.05.2008
*
* (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.startSetup.marbBuilder.traceBetrachter;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.FileNamePostfixFilter;
import eas.plugins.Plugin;
import eas.plugins.standard.eaPlugin.xmlRecording.XMLAufnLesen;
import eas.plugins.standard.eaPlugin.xmlRecording.XMLAufnahmePlugin;
import eas.simulation.spatial.sim2D.marbSimulation.endlAutomat.GlobaleMARBVariablen;
import eas.simulation.spatial.sim2D.marbSimulation.endlAutomat.conditions.Condition;
import eas.startSetup.ParCollection;
import eas.startSetup.marbBuilder.Vis;
import eas.startSetup.marbBuilder.traceBetrachter.Umgebung;
import eas.startSetup.marbBuilder.traceBetrachter.VisMantel;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
/**
* Implementiert eine Simulation als initiale Zuordnung einer Menge von
* Robotern zu einer Umgebung und einer anschließenden iterierten Ausf�hrung
* von Zustandswechseln der Roboter und evolutionären Operatoren.
*
* @author Lukas König
*/
public class Simulation implements Observer {
/**
* Liste von Robotern (im VisMantel), die verwaltet werden.
*/
private ArrayList<VisMantel> graphen;
/**
* Der Zufallsgenerator.
*/
private Random rand;
/**
* Die Umgebung, die durch <code>this</code> dargestellt wird.
*/
private Umgebung umgebung;
/**
* Ob evolviert wird.
*/
private boolean evolution;
/**
* Die Parameter.
*/
private ParCollection pars;
/**
* Der Observer für die Umgebung.
*/
private Observer observer;
/**
* Liste von graphischen Observern der Automaten der Simulation.
*/
private Vis[] visObserver;
/**
* Das zur Simulation gehörende Simulationszeit-Objekt.
*/
private SimulationsZeit simZ;
/**
* Die PLugins-Liste.
*/
private List<Plugin<?>> plugins;
/**
* Konstruktor, der die Attribute initialisiert.
*
* @param params Die Parameter.
* @param obs Ein möglicher Observer für die Umgebung.
* @param visObs Die Vis-Observer.
* @param plugs Plugins für diese Umgebung.
* @param simZeit Das zur Simulation gehörende Simulationszeit-Objekt.
*/
public Simulation(
final ParCollection params,
final Observer obs,
final Vis[] visObs,
final Plugin<?>[] plugs,
final SimulationsZeit simZeit,
final XMLAufnLesen aufnahme) {
GlobaleMARBVariablen.setCollCount(0);
this.pars = params;
this.observer = obs;
this.visObserver = visObs;
this.simZ = simZeit;
this.rand = new Random(this.pars.getSeed().longValue());
this.umgebung = new Umgebung(this.rand,
this.pars,
this.observer,
this,
plugs,
aufnahme);
this.plugins = new LinkedList<Plugin<?>>();
if (plugs != null) {
for (Plugin<?> p : plugs) {
this.plugins.add(p);
}
}
this.graphen = new ArrayList<VisMantel>();
this.ladeGraphen();
this.platziereRoboter();
StaticMethods.log(
StaticMethods.LOG_INFO,
"Roboter in Simulation: "
+ this.getUm().getAgenten().size(),
this.pars);
}
/**
* Konstruktor, der die Attribute initialisiert.
*
* @param params Die Parameter.
* @param obs Ein möglicher Observer für die Umgebung.
* @param visObs Die Vis-Observer.
* @param automaten Die Initialautomaten für die simulierten Roboter.
* @param condColl Die Bedingungen.
* @param ids Die Identit�tsnummern der Roboter.
* @param wandDyn Die Wanddynamik für diese Umgebung.
*/
public Simulation(final ParCollection params,
final Observer obs,
final Vis[] visObs,
final String[][] automaten,
final Condition[][] condColl,
final int[] ids,
final Plugin<?>[] wandDyn,
final XMLAufnLesen aufnahme) {
this.pars = params;
this.observer = obs;
this.visObserver = visObs;
this.rand = new Random(this.pars.getSeed().longValue());
this.umgebung = new Umgebung(this.rand,
this.pars,
this.observer,
this,
wandDyn,
aufnahme);
this.graphen = new ArrayList<VisMantel>();
// this.aufnahme = new XMLAufnSpeichern(this.pars, this.graphen.size());
this.zuordnen(visObs, automaten, condColl, ids);
this.platziereRoboter();
}
/**
* lädt gespeicherte Graphen aus dem im Parameterobjekt angegebenen
* Verzeichnis.
*/
private void ladeGraphen() {
File verz = new File(this.pars.getStdDirectory());
String endung = eas.startSetup.marbBuilder.ConstantsGraphVis.GRAPH_ENDUNG;
String[] gespGr = verz.list(new FileNamePostfixFilter(endung));
String grNam;
VisMantel visRob = null;
Vis obs = null;
if (gespGr != null) {
for (int i = 0; i < gespGr.length; i++) {
grNam = gespGr[i].substring(
0,
gespGr[i].length() - endung.length() - 1);
for (int j = 0; j < this.visObserver.length; j++) {
if (this.visObserver[j].getGraphName().equals(grNam)) {
obs = this.visObserver[j];
break;
}
}
visRob = new VisMantel(grNam,
this.rand,
this.pars,
obs);
this.graphen.add(visRob);
}
}
}
/**
* Erzeugt aus einer Automatenliste die zugehörigen Roboter und
* ordnet sie den entsprechenden Observern zu.
*
* @param visObs Die Vis-Observer.
* @param automaten Die Automaten.
* @param condColl Die Bedingungen.
* @param ids Die Identit�tsnummern der Automaten.
*/
private void zuordnen(final Vis[] visObs,
final String[][] automaten,
final Condition[][] condColl,
final int[] ids) {
String[] auts = new String[1];
Condition[] conds = new Condition[1];
if (visObs.length != automaten.length) {
throw new RuntimeException("Unterschiedliche Anzahl von Automaten "
+ "und Vis-Objekten.");
}
String grNam;
VisMantel visRob = null;
Vis obsVis = null;
boolean sel = false;
for (int i = 0; i < visObs.length; i++) {
grNam = visObs[i].getGraphName();
obsVis = this.visObserver[i];
sel = this.visObserver[i].isVisible();
auts = automaten[i];
conds = condColl[i];
visRob = new VisMantel(auts,
conds,
grNam,
this.umgebung,
this.rand,
ids[i],
this.pars,
sel,
obsVis,
this.plugins);
this.graphen.add(visRob);
}
}
/**
* Speichert alle Graphen in der Liste <code>this.graphen</code>.
*/
public void speichereAlleGraphen() {
Iterator<VisMantel> it = this.graphen.iterator();
VisMantel aktGr;
while (it.hasNext()) {
aktGr = it.next();
aktGr.speichereGraph();
}
}
/**
* Gibt die kleinste noch nicht von einem der aktuellen Graphen verwendete
* ID zurück.
*
* @return Die kleinste noch nicht verwendete ID.
*/
public int kleinsteFreieID() {
int kleinste = 0;
Iterator<VisMantel> it = this.graphen.iterator();
VisMantel aktGr;
while (it.hasNext()) {
aktGr = it.next();
if (kleinste <= aktGr.getRob().getId()) {
kleinste = aktGr.getRob().getId() + 1;
}
}
return kleinste;
}
/**
* führt einen einzelnen Simulationsschritt aus, der einem Zeitzyklus
* entspricht.
*/
private void step() {
}
/**
* Beendet die Simulation: speichert die Aufnahme.
*/
public void simulationEnde() {
if (this.getUm().existsPlugin(XMLAufnahmePlugin.class)) {
this.umgebung.setSimEndeFlag();
}
Iterator<VisMantel> it = this.graphen.iterator();
while (it.hasNext()) {
it.next().getRob().simBeendet();
}
}
/**
* Platziert die Roboter.
*/
public void platziereRoboter() {
Iterator<VisMantel> it;
Roboter rob;
it = this.graphen.iterator();
while (it.hasNext()) {
rob = it.next().getRob();
rob.zuruecksetzen();
this.umgebung.hinzuRobotRand(rob);
}
}
/**
* Die Update-Methode der beobachtenden Klasse.
*
* @param o Das beobachtete Objekt, von dem die Meldung ausging.
* @param arg Argument.
*/
@Override
public void update(final Observable o, final Object arg) {
this.step();
}
/**
* @return Returns the umgebung.
*/
public Umgebung getUm() {
return this.umgebung;
}
/**
* @return Returns the evolution.
*/
public boolean isEvolution() {
return this.evolution;
}
/**
* @param evol The evolution to set.
*/
public void setEvolution(final boolean evol) {
this.evolution = evol;
}
/**
* @return Das Simulationszeitobjekt.
*/
public SimulationsZeit getSimZeit() {
return this.simZ;
}
}