]> ruin.nu Git - popboot.git/blobdiff - planner.cpp
small change
[popboot.git] / planner.cpp
index 6d70ae40df2114d32e6cf4f3ab611155e96b6aa6..d49fb911beb267aa1b7005a8bdd9a729b84f2e22 100644 (file)
@@ -1,58 +1,68 @@
 #include "planner.h"
 #include "node.h"
 #include <iostream>
+#include <vector>
 using namespace std;
 using namespace __gnu_cxx;
 
-Planner::Planner(std::vector<Action> actions, literals init, literals goal){
-
-       _start = new Node(Action("start",literals(), init));
+Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
+       _init = init;
+       _goal = goal;
+       _start = new StartNode(_init);
+       _finish = new EndNode(_goal);
        addNode(_start);
-       Node* finish = new Node(Action("finish",goal,literals()));
 
        for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
-               const literals& effects = action->effects();
-               for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
-                       cerr << "Adding effect: " << *effect << endl;
+               const Literals& effects = action->effects(0);
+               for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+                       cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
                        _actions[*effect] = *action;
                }
        }
-       makePlan(finish);
+       cout << "Number of actions: " << _actions.size() << endl;
+       makePlan(_finish);
 }
 
 Planner::~Planner(){
        for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               cerr << "Deleting node " << node->second->action().executable() << endl;
+               cerr << "Deleting node " << node->second->action().name() << endl;
                delete node->second;
        }
 }
 
 
 void Planner::makePlan(Node* node){
-       cerr << "Fetching preconditions for action: " << node->action().executable() << ".. ";
-       const literals& preconds = node->action().preconditions();
+       addNode(node);
+
+       cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
+       const Preconditions& preconds = node->action().preconditions();
        cerr << "done" << endl;
 
+
        if (preconds.size() == 0){
                cerr << "Found no preconds" << endl;
                _start->addChild(node);
        }else{
-               for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
-                       cerr << "Looking for: " << *precond << endl;
-                       hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
+               for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+                       cerr << "Looking for: '" << precond->first << "'" <<  endl;
+                       hash_map<string,Node*>::iterator addedNode = _addedNodes.find(precond->first);
                        if(addedNode != _addedNodes.end()){
                                cerr << "Using already added node" << endl;
                                addedNode->second->addChild(node);
                        }else {
-                               hash_map<string, Action>::iterator action = _actions.find(*precond);
+                               hash_map<string, Action>::iterator action = _actions.find(precond->first);
                                if (action != _actions.end()){
                                        cerr << "Adding new node" << endl;
                                        Node* newnode = new Node(action->second);
                                        newnode->addChild(node);
-                                       addNode(newnode);
                                        makePlan(newnode);
+                               }else if (precond->second){
+                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
+                                       return;
                                }else{
-                                       cerr << "Action with effect: " << *precond << " not found!" << endl;
+                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       cerr << "This is a soft precondition, so we will continue" << endl;
                                }
                        }
                }
@@ -60,9 +70,11 @@ void Planner::makePlan(Node* node){
 }
 
 void Planner::addNode(Node* node){
-       const literals& effects = node->action().effects();
+       cerr << "Adding node for action: " << node->action().name() << endl;
+       const Literals& effects = node->action().effects(0);
+       cerr << "Number of effects: " << effects.size() << endl;
 
-       for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+       for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                cout << "Adding node for effect: " << *effect << endl;
                _addedNodes[*effect] = node;
        }
@@ -70,5 +82,9 @@ void Planner::addNode(Node* node){
 
 
 void Planner::execute(){
-       _start->execute(literals());
+       _start->execute(Literals());
+       /*for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+               if (node->second->executed()){
+       }*/
+       _start->execute(Literals());
 }