]> ruin.nu Git - popboot.git/blob - planner.cpp
compiles again
[popboot.git] / planner.cpp
1 #include "planner.h"
2 #include "node.h"
3 #include <iostream>
4 using namespace std;
5 using namespace __gnu_cxx;
6
7 Planner::Planner(std::vector<Action> actions, literals init, literals goal){
8
9         _start = new Node(Action("",init, literals()));
10         Node* finish = new Node(Action("",literals(),goal));
11
12         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
13                 literals preconds = action->preconditions();
14                 for (literals::iterator effect = preconds.begin(); effect != preconds.end(); ++effect){
15                         _actions[*effect] = *action;
16                 }
17         }
18         makePlan(finish);
19 }
20
21
22 void Planner::makePlan(Node* node){
23         literals preconds = node->action().preconditions();
24
25         if (preconds.size() == 0){
26                 _start->addChild(node);
27         }else{
28                 for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
29                         hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
30                         if(addedNode != _addedNodes.end()){
31                                 addedNode->second->addChild(node);
32                         }else {
33                                 hash_map<string, Action>::iterator action = _actions.find(*precond);
34                                 if (action != _actions.end()){
35                                         Node* newnode = new Node(action->second);
36                                         newnode->addChild(node);
37                                         makePlan(newnode);
38                                 }else{
39                                         cerr << "Action with effect: " << *precond << " not found!";
40                                 }
41                         }
42                 }
43         }
44 }
45
46 void Planner::addNode(Node* node){
47         literals effects = node->action().effects();
48
49         for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
50                 _addedNodes[*effect] = node;
51         }
52 }