Go to the documentation of this file.00001
00002
00003 #include <actasp/executors/ReplanningActionExecutor.h>
00004
00005 #include <actasp/AspKR.h>
00006 #include <actasp/AnswerSet.h>
00007 #include <actasp/Planner.h>
00008 #include <actasp/Action.h>
00009 #include <actasp/action_utils.h>
00010 #include <actasp/ExecutionObserver.h>
00011 #include <actasp/PlanningObserver.h>
00012 #include <actasp/execution_observer_utils.h>
00013
00014 #include <list>
00015 #include <algorithm>
00016 #include <iterator>
00017
00018 using namespace std;
00019
00020 namespace actasp {
00021
00022 ReplanningActionExecutor::ReplanningActionExecutor(actasp::AspKR* reasoner,
00023 actasp::Planner *planner,
00024 const std::map<std::string, Action * > &actionMap
00025 ) throw (std::invalid_argument) :
00026 goalRules(),
00027 isGoalReached(true),
00028 hasFailed(false),
00029 actionMap(),
00030 plan(),
00031 actionCounter(0),
00032 newAction(true),
00033 kr(reasoner),
00034 planner(planner),
00035 executionObservers(){
00036 if (reasoner == NULL)
00037 throw invalid_argument("ReplanningActionExecutor: reasoner is NULL");
00038
00039 if (planner == NULL)
00040 throw invalid_argument("ReplanningActionExecutor: planner is NULL");
00041
00042 transform(actionMap.begin(),actionMap.end(),inserter(this->actionMap,this->actionMap.begin()),ActionMapDeepCopy());
00043 }
00044
00045 ReplanningActionExecutor::~ReplanningActionExecutor() {
00046 for_each(actionMap.begin(),actionMap.end(),ActionMapDelete());
00047 }
00048
00049 struct NotifyNewPlan {
00050
00051 NotifyNewPlan(const AnswerSet& plan) : plan(plan) {}
00052
00053 void operator()(PlanningObserver* observer) {
00054 observer->planChanged(plan);
00055 }
00056
00057 AnswerSet plan;
00058
00059 };
00060
00061 void ReplanningActionExecutor::computePlan() {
00062 isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00063
00064 if (!isGoalReached) {
00065 plan = planner->computePlan(goalRules).instantiateActions(actionMap);
00066 actionCounter = 0;
00067 }
00068
00069 hasFailed = plan.empty();
00070
00071 if(!hasFailed)
00072 for_each(planningObservers.begin(),planningObservers.end(),NotifyNewPlan(planToAnswerSet(plan)));
00073
00074 }
00075
00076 void ReplanningActionExecutor::setGoal(const std::vector<actasp::AspRule>& goalRules) throw() {
00077 this->goalRules = goalRules;
00078
00079 computePlan();
00080 }
00081
00082
00083
00084
00085 void ReplanningActionExecutor::executeActionStep() {
00086
00087 if (isGoalReached || hasFailed)
00088 return;
00089
00090
00091 Action *current = plan.front();
00092
00093 if(newAction) {
00094 for_each(executionObservers.begin(),executionObservers.end(),NotifyActionStart(current->toFluent(actionCounter)));
00095 newAction = false;
00096 }
00097
00098
00099 current->run();
00100
00101 if (current->hasFinished()) {
00102
00103
00104 for_each(executionObservers.begin(),executionObservers.end(),NotifyActionTermination(current->toFluent(actionCounter++)));
00105
00106 delete current;
00107 plan.pop_front();
00108
00109 newAction = true;
00110
00111 if (plan.empty() || !kr->isPlanValid(planToAnswerSet(plan),goalRules)) {
00112
00113
00114 for_each(plan.begin(),plan.end(),ActionDeleter());
00115 plan.clear();
00116
00117 computePlan();
00118
00119 }
00120
00121 }
00122
00123
00124
00125 }
00126
00127 void ReplanningActionExecutor::addExecutionObserver(ExecutionObserver *observer) throw() {
00128 executionObservers.push_back(observer);
00129 }
00130
00131 void ReplanningActionExecutor::removeExecutionObserver(ExecutionObserver *observer) throw() {
00132 executionObservers.remove(observer);
00133 }
00134
00135 void ReplanningActionExecutor::addPlanningObserver(PlanningObserver *observer) throw() {
00136 planningObservers.push_back(observer);
00137 }
00138
00139 void ReplanningActionExecutor::removePlanningObserver(PlanningObserver *observer) throw() {
00140 planningObservers.remove(observer);
00141 }
00142
00143 }