]> ruin.nu Git - popboot.git/blobdiff - planner.cpp
only execute an action when all preconditions are are true
[popboot.git] / planner.cpp
index 159cb528d68f15e5e0d5667facef30455042867d..a15028e29a3175e8fa316e2e6e6a37e2dc41194f 100644 (file)
@@ -1,3 +1,65 @@
 #include "planner.h"
+#include "node.h"
+#include <iostream>
+using namespace std;
+using namespace __gnu_cxx;
 
+Planner::Planner(std::vector<Action> actions, literals init, literals goal){
 
+       _start = new Node(Action("start",literals(), init));
+       addNode(_start);
+       Node* finish = new Node(Action("finish",goal,literals()));
+
+       for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
+               literals effects = action->effects();
+               for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
+                       cerr << "Adding effect: " << *effect << endl;
+                       _actions[*effect] = *action;
+               }
+       }
+       makePlan(finish);
+}
+
+
+void Planner::makePlan(Node* node){
+       literals preconds = node->action().preconditions();
+
+       if (preconds.size() == 0){
+               cerr << "Found no preconds" << endl;
+               _start->addChild(node);
+       }else{
+               for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+                       cerr << "Looking for: " << *precond << endl;
+                       hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
+                       if(addedNode != _addedNodes.end()){
+                               cerr << "Using already added node" << endl;
+                               addedNode->second->addChild(node);
+                       }else {
+                               hash_map<string, Action>::iterator action = _actions.find(*precond);
+                               if (action != _actions.end()){
+                                       cerr << "Adding new node" << endl;
+                                       Node* newnode = new Node(action->second);
+                                       newnode->addChild(node);
+                                       addNode(newnode);
+                                       makePlan(newnode);
+                               }else{
+                                       cerr << "Action with effect: " << *precond << " not found!" << endl;
+                               }
+                       }
+               }
+       }
+}
+
+void Planner::addNode(Node* node){
+       literals effects = node->action().effects();
+
+       for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
+               cout << "Adding node for effect: " << *effect << endl;
+               _addedNodes[*effect] = node;
+       }
+}
+
+
+void Planner::execute(){
+       _start->execute(literals());
+}