]> ruin.nu Git - popboot.git/commitdiff
splitup of execution
authorMichael Andreen <harv@ruin.nu>
Fri, 3 Jun 2005 09:13:36 +0000 (09:13 +0000)
committerMichael Andreen <harv@ruin.nu>
Fri, 3 Jun 2005 09:13:36 +0000 (09:13 +0000)
planner.cpp
planner.h

index 02d477233d3534727790404178f4c3b0c97cbc84..9d845702f93f114d916feadf1acc9e421dbe930d 100644 (file)
@@ -99,6 +99,35 @@ void Planner::addNode(Node* node){
 
 
 void Planner::execute(){
+       executePlan();
+
+       if (cleanupExecution() <= 1){
+               cerr << "Non of the remaining actions could be executed, quiting." << endl;
+               return;
+       }
+
+       cout << "Effects achieved so far: " << _init.size() << ": ";
+       copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
+       cout << endl;
+
+       if (_actions.size() == 0){
+               //Nothing left to do, quitting.
+               return;
+       }
+       //REPLANNING
+       cout << "Replanning..." << endl;
+       
+       replan();
+
+       if (_addedNodes.size() <= 2){
+               cerr << "No actions to execute, quiting." << endl;
+               return;
+       }
+       execute();
+       
+}
+
+void Planner::executePlan(){
        sem_init(&_nodes, 0, 1);
        sem_init(&_list, 0, 1);
 
@@ -121,32 +150,42 @@ void Planner::execute(){
                //We don't need to continue if the end node was executed.
                if (node == _finish)
                        return;
+               executions += executeChildren(node);
 
-               vector<Node*> children = node->children();
-
-               //Iterate over the children for this node
-               for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
-                       //Satisfy the preconditions the current node had as effect.
-                       if ((*child)->satisfyConditions(node->effects())){
-                               //If all preconditions were satisified we can create a new thread
-                               //and execute this child.
-                               ++executions;
-                               pthread_attr_t tattr;
-                               pthread_t tid;
-                               pthread_attr_init(&tattr);
-                               pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
-                               //Create a struct object for the thread and add the
-                               //needed members to it.
-                               ExecutionStuff* es = new ExecutionStuff;
-                               es->nodes = &_nodes;
-                               es->list = &_list;
-                               es->node = *child;
-                               es->execQueue = &_executedNodes;
-                               pthread_create(&tid, &tattr, executeNode, es);
-                       }
-               }
+       }
+}
+
+
+int Planner::executeChildren(Node* node){
+
+       vector<Node*> children = node->children();
 
+       int executions = 0;
+       //Iterate over the children for this node
+       for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
+               //Satisfy the preconditions the current node had as effect.
+               if ((*child)->satisfyConditions(node->effects())){
+                       //If all preconditions were satisified we can create a new thread
+                       //and execute this child.
+                       ++executions;
+                       pthread_attr_t tattr;
+                       pthread_t tid;
+                       pthread_attr_init(&tattr);
+                       pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
+                       //Create a struct object for the thread and add the
+                       //needed members to it.
+                       ExecutionStuff* es = new ExecutionStuff;
+                       es->nodes = &_nodes;
+                       es->list = &_list;
+                       es->node = *child;
+                       es->execQueue = &_executedNodes;
+                       pthread_create(&tid, &tattr, executeNode, es);
+               }
        }
+       return executions;
+}
+
+int Planner::cleanupExecution(){
        //Clearing the init and goal vectors, new effects will be added below.
        _init.clear();
        _goal.clear();
@@ -162,6 +201,7 @@ void Planner::execute(){
        //iterator for inserting effects at the end of _init
        back_insert_iterator<Literals> ii(_init);
 
+       int executions = 0;
        //Iterate throu the nodes, to see what's been done
        for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
                if ((*node)->executed()){
@@ -184,22 +224,11 @@ void Planner::execute(){
        _addedNodes.clear();
        _addedEffects.clear();
        _actionEffects.clear();
+       return executions;
+}
 
-       cout << "Effects achieved so far: " << _init.size() << ": ";
-       copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
-       cout << endl;
+void Planner::replan(){
 
-       if (executions <= 1){
-               cerr << "Non of the remaining actions could be executed, quiting." << endl;
-               return;
-       }
-       if (_actions.size() == 0){
-               //Nothing left to do, quitting.
-               return;
-       }
-       //REPLANNING
-       cout << "Replanning..." << endl;
-       
        //Adding the effecs for the remaining actions.
        for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
                const Literals& effects = (*action)->effects(0);
@@ -211,12 +240,6 @@ void Planner::execute(){
        _finish = new EndNode(_goal);
        addNode(_start);
        makePlan(_finish);
-       if (_addedNodes.size() <= 2){
-               cerr << "No actions to execute, quiting." << endl;
-               return;
-       }
-       execute();
-       
 }
 void* executeNode(void* arg){
        ExecutionStuff* es = (ExecutionStuff*)arg;
index 8d116933f14a6d0122aaa0182ca22f21686ca277..55d572877e61641cbdce15d0186bee9b4da9c235 100644 (file)
--- a/planner.h
+++ b/planner.h
@@ -22,11 +22,15 @@ class Planner {
 
                void makePlan(Node* node);
                void addNode(Node* node);
+               void executePlan();
+               void replan();
+               int cleanupExecution();
+               int executeChildren(Node* node);
 
                sem_t _nodes;
                sem_t _list;
                std::queue<Node*> _executedNodes;
-               
+
                Node* _start;
                Node* _finish;
                __gnu_cxx::hash_map<std::string,Node*> _addedEffects;