package at.oefai.aaa.agent.jam;

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

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

    private PlanConstruct thisConstruct = null;
    private PlanRuntimeState substate = null;

    /**  */
    PlanRuntimeWaitState(final PlanWaitConstruct be) {
        this.thisConstruct = be;
        this.substate = null;
    }

    // Member functions

    /** Check to see whether the action returns successfully or the goal has been accomplished. */
    public State execute(final Binding b, final Goal thisGoal) {
        // Check to see if the agent is waiting on action success
        if (((PlanWaitConstruct) this.thisConstruct).getAction() != null) {
            Action.Result returnVal = ((PlanWaitConstruct) this.thisConstruct).getAction().execute(b, thisGoal);
            if (returnVal == Action.Result.SUCCEEDED) {
                return State.CONSTRUCT_COMPLETE;
            }
            return State.CONSTRUCT_INCOMP;
        }
        // Agent must be waiting on a goal
        // Check for match of goal relation on world model
        /*
        log.info("PRWS::Waiting on goal achievement of relation:");
        (((PlanWaitConstruct)_thisConstruct).getRelation())
        log.info();
        */
        boolean matchFound;
        matchFound = thisGoal.getIntentionStructure().getWorldModel().match(((PlanWaitConstruct) this.thisConstruct).getRelation(), b);
        //log.info("  Match found: " + matchFound);
        return (matchFound) ? State.CONSTRUCT_COMPLETE : State.CONSTRUCT_INCOMP;
    }

}


