RLActionExecutor.cpp
Go to the documentation of this file.
00001 #include "RLActionExecutor.h"
00002 
00003 #include <actasp/AspKR.h>
00004 #include <actasp/AspRule.h>
00005 #include <actasp/ActionSelector.h>
00006 #include <actasp/Action.h>
00007 #include <actasp/execution_observer_utils.h>
00008 #include <actasp/action_utils.h>
00009 
00010 #include <algorithm>
00011 #include <iterator>
00012 
00013 using namespace std;
00014 
00015 namespace actasp {
00016 
00017 RLActionExecutor::RLActionExecutor(AspKR* kr, ActionSelector *selector, 
00018                       const std::map<std::string, Action * >& actionMap) :
00019                     
00020                     isGoalReached(false),
00021                     actionCounter(0),
00022                     newAction(true),
00023                     active(NULL),
00024                     kr(kr),
00025                     goalRules(),
00026                     selector(selector),
00027                     actionMap(),
00028                     executionObservers() {
00029 
00030   transform(actionMap.begin(),actionMap.end(),inserter(this->actionMap,this->actionMap.end()),ActionMapDeepCopy());
00031 }
00032 
00033 RLActionExecutor::~RLActionExecutor() {
00034   delete active;
00035   for_each(actionMap.begin(),actionMap.end(),ActionMapDelete());
00036 }
00037   
00038   
00039 void  RLActionExecutor::setGoal(const std::vector<actasp::AspRule>& goalRules) throw() {
00040 
00041   this->goalRules = goalRules;
00042 
00043   isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00044 
00045   delete active;
00046   active = NULL;
00047   actionCounter = 0;
00048   newAction = true;
00049 
00050 }
00051 
00052 bool RLActionExecutor::goalReached() const throw() {
00053   return isGoalReached;
00054 }
00055 
00056 bool RLActionExecutor::failed() const throw() {
00057   return false;
00058 }
00059 
00060 static Action *instantiateAction(const std::map<std::string, Action * >& actionMap, const AspFluent &actionFluent) {
00061   map<string, Action * >::const_iterator action = actionMap.find(actionFluent.getName());
00062   
00063   if(action == actionMap.end())
00064     throw logic_error("RLActionExecutor: no action with name " + actionFluent.getName());
00065   
00066   return action->second->cloneAndInit(actionFluent);
00067 }
00068 
00069 
00070 void RLActionExecutor::executeActionStep() {
00071   if (isGoalReached)
00072     return;
00073   
00074   if (active != NULL && !active->hasFinished()) {
00075     
00076   if (newAction) {
00077     for_each(executionObservers.begin(),executionObservers.end(),NotifyActionStart(active->toFluent(actionCounter)));
00078     newAction = false;
00079   } 
00080   
00081   active->run();
00082 
00083   } else {
00084     
00085 
00086     if (active != NULL) {
00087       for_each(executionObservers.begin(),executionObservers.end(),NotifyActionTermination(active->toFluent(actionCounter++)));
00088     }
00089 
00090     isGoalReached = kr->currentStateQuery(goalRules).isSatisfied();
00091 
00092     if (isGoalReached) //well done!
00093       return;
00094 
00095     //choose the next action
00096     
00097     ActionSet options = kr->availableActions();
00098     
00099     set<AspFluent>::const_iterator chosen = selector->choose(options);
00100 
00101     delete active;
00102     active = instantiateAction(actionMap,*chosen);
00103     actionCounter++;
00104     newAction = true;
00105 
00106   }
00107 
00108 }
00109 
00110 void RLActionExecutor::addExecutionObserver(ExecutionObserver *observer) throw() {
00111   executionObservers.push_back(observer);
00112 }
00113 
00114 void RLActionExecutor::removeExecutionObserver(ExecutionObserver *observer) throw() {
00115   executionObservers.remove(observer);
00116 }
00117 
00118 }


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