]> ruin.nu Git - popboot.git/blob - planner.cpp
references instead of copies
[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("start",literals(), init));
10         addNode(_start);
11         Node* finish = new Node(Action("finish",goal,literals()));
12
13         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
14                 const literals& effects = action->effects();
15                 for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
16                         cerr << "Adding effect: " << *effect << endl;
17                         _actions[*effect] = *action;
18                 }
19         }
20         makePlan(finish);
21 }
22
23
24 void Planner::makePlan(Node* node){
25         cerr << "Fetching preconditions for action: " << node->action().executable() << ".. ";
26         const literals& preconds = node->action().preconditions();
27         cerr << "done" << endl;
28
29         if (preconds.size() == 0){
30                 cerr << "Found no preconds" << endl;
31                 _start->addChild(node);
32         }else{
33                 for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
34                         cerr << "Looking for: " << *precond << endl;
35                         hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
36                         if(addedNode != _addedNodes.end()){
37                                 cerr << "Using already added node" << endl;
38                                 addedNode->second->addChild(node);
39                         }else {
40                                 hash_map<string, Action>::iterator action = _actions.find(*precond);
41                                 if (action != _actions.end()){
42                                         cerr << "Adding new node" << endl;
43                                         Node* newnode = new Node(action->second);
44                                         newnode->addChild(node);
45                                         addNode(newnode);
46                                         makePlan(newnode);
47                                 }else{
48                                         cerr << "Action with effect: " << *precond << " not found!" << endl;
49                                 }
50                         }
51                 }
52         }
53 }
54
55 void Planner::addNode(Node* node){
56         const literals& effects = node->action().effects();
57
58         for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
59                 cout << "Adding node for effect: " << *effect << endl;
60                 _addedNodes[*effect] = node;
61         }
62 }
63
64
65 void Planner::execute(){
66         _start->execute(literals());
67 }