use of aima.core.search.nondeterministic.IfStateThenPlan in project aima-java by aimacode.
the class NondeterministicVacuumAgent method execute.
/**
* Execute an action from the contingency plan
*
* @param percept a percept.
* @return an action from the contingency plan.
*/
@Override
public Action execute(Percept percept) {
// check if goal state
VacuumEnvironmentState state = (VacuumEnvironmentState) this.getPerceptToStateFunction().apply(percept);
if (state.getLocationState(VacuumEnvironment.LOCATION_A) == VacuumEnvironment.LocationState.Clean && state.getLocationState(VacuumEnvironment.LOCATION_B) == VacuumEnvironment.LocationState.Clean) {
return NoOpAction.NO_OP;
}
// check stack size
if (this.stack.size() < 1) {
if (this.contingencyPlan.size() < 1) {
return NoOpAction.NO_OP;
} else {
this.stack.push(this.getContingencyPlan().removeFirst());
}
}
// pop...
Object currentStep = this.stack.peek();
// push...
if (currentStep instanceof Action) {
return (Action) this.stack.remove();
} else // case: next step is a plan
if (currentStep instanceof Plan) {
Plan newPlan = (Plan) currentStep;
if (newPlan.size() > 0) {
this.stack.push(newPlan.removeFirst());
} else {
this.stack.remove();
}
return this.execute(percept);
} else // case: next step is an if-then
if (currentStep instanceof IfStateThenPlan) {
IfStateThenPlan conditional = (IfStateThenPlan) this.stack.remove();
this.stack.push(conditional.ifStateMatches(percept));
return this.execute(percept);
} else // case: ignore next step if null
if (currentStep == null) {
this.stack.remove();
return this.execute(percept);
} else {
throw new RuntimeException("Unrecognized contingency plan step.");
}
}
Aggregations