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


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:37