package at.oefai.aaa.agent.jam;

import at.oefai.aaa.agent.jam.types.Binding;

/**
 * Represents the runtime state of plan constructs.
 * @author Marc Huber
 * @author Jaeho Lee
 */
class PlanRuntimeSimpleState implements PlanRuntimeState {

    private final PlanSimpleConstruct thisConstruct;

    /**  */
    PlanRuntimeSimpleState(final PlanSimpleConstruct be) {
        this.thisConstruct = be;
    }

    // Member functions

    /**  */
    public State execute(final Binding b, final Goal thisGoal) {
        Action.Result actionReturnVal;
        AgentLogger log = AgentLogger.getStaticInstance();
        if (thisGoal != null) {
            log = thisGoal.getIntentionStructure().getLog();
        }
        Action a = this.thisConstruct.getAction();
        try {
            actionReturnVal = a.execute(b, thisGoal);
        } catch (RuntimeException e) {
            log.severe("**** Action in \"" + a.getTraceFile() + "\" at line#" + a.getTraceLine()
                       + " threw an exception!****", e);
            throw new RuntimeException("Plan action failed", e);
        }
        if (actionReturnVal == Action.Result.FAILED) {
            if (log.getShowActionFailure()) {
                log.warning("**** Action failed in \"" + a.getTraceFile() + "\", line#" + a.getTraceLine() + " ****");
            }
            return State.CONSTRUCT_FAILED;
        }
        return State.CONSTRUCT_COMPLETE;
    }
}
