/**
*
*/
package eas.simulation.spatial.standardBrains.mdle;
import java.awt.image.BufferedImage;
import java.io.File;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.brain.AbstractBrain;
import eas.simulation.spatial.standardBrains.mdle.plan.MDL2eParser;
import eas.simulation.spatial.standardBrains.mdle.plan.MDL2ePlan;
import eas.simulation.spatial.standardBrains.mdle.utils.MDL2ePlanVisualizer;
import eas.simulation.spatial.standardBrains.mdle.utils.MDL2eStack;
/**
* @author Daniel Funke
*
*/
public class MDL2eBrain<AgentType extends AbstractAgent<?>> extends AbstractBrain<AgentType> {
/**
*
*/
private static final long serialVersionUID = -620467026354297185L;
private MDL2ePlan planRoot = null;
private MDL2eStack executionStack = new MDL2eStack();
private boolean active = true;
private MDL2eStack descheduleStack = new MDL2eStack();
private transient BufferedImage MRTimg = null;
/**
* @param brainsBody
*/
public MDL2eBrain(AgentType brainsBody, File mdl2ePlan) {
super(brainsBody);
planRoot = new MDL2eParser(this).parse(mdl2ePlan);
}
/*
* (non-Javadoc)
*
* @see
* eas.simulation.spatial.brains.AbstractBrainSpatial#decideAndReact(eas
* .simulation.Wink)
*/
@Override
public void decideAndReact(Wink time) {
descheduleStack.deschedule();
if (active) {
MDL2ePlan currentAtom = executionStack.pop();
if (currentAtom != null) {
if (currentAtom.checkInterrupt(time)) {
executionStack.push(currentAtom);
currentAtom.execute(time);
return;
} else { // currentAtom.checkInterrupt(time) == false
//queue for deschedule
descheduleStack.push(currentAtom);
//currentAtom.deschedule();
while ((currentAtom = executionStack.pop()) != null) {
MDL2ePlan schedulable = currentAtom.getSchedulable(
time, executionStack);
// schedulable atom found, its already on stack
if (schedulable != null) {
//if(schedulable.name.equals("MOVE")) System.out.println(schedulable);
schedulable.execute(time);
return;
}
// currentAtom has no schedulable children -->
// queue for deschedule
descheduleStack.push(currentAtom);
//currentAtom.deschedule();
}
// no schedulable atom found
active = false;
return;
}
} else { // currentAtom == null
currentAtom = planRoot.getSchedulable(time, executionStack);
// atom is already on stack
if (currentAtom != null) {
currentAtom.execute(time);
return;
}
}
// no schedulable atom found
active = false;
return;
}// end if(active)
}
public MDL2ePlan getPlan() {
return planRoot;
}
/* (non-Javadoc)
* @see eas.simulation.AbstractBrain#getMRTImage()
*/
@Override
public BufferedImage generateMRTImage() {
if(MRTimg == null)
MRTimg = new MDL2ePlanVisualizer(planRoot).visualize();
return MRTimg;
}
public boolean isActive() {
return active;
}
}