]> ruin.nu Git - popboot.git/blob - planner.cpp
not working
[popboot.git] / planner.cpp
1 #include "planner.h"
2 #include "node.h"
3 #include <iostream>
4 #include <vector>
5 using namespace std;
6 using namespace __gnu_cxx;
7
8 Planner::Planner(std::vector<Action> actions, literals init, literals goal){
9         _init = init;
10         _goal = goal;
11         _start = new StartNode(_init);
12         Node* finish = new EndNode(_goal);
13
14         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
15                 Action* act = new Action(*action);
16                 const literals& effects = act->effects(0);
17                 for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
18                         cerr << "Adding effect: " << *effect << endl;
19                         _actions[*effect] = act;
20                 }
21         }
22         makePlan(finish);
23 }
24
25 Planner::~Planner(){
26         for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
27                 cerr << "Deleting node " << node->second->action().name() << endl;
28                 delete node->second;
29         }
30 }
31
32
33 void Planner::makePlan(Node* node){
34         addNode(node);
35
36         cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
37         const literals& preconds = node->action().preconditions();
38         cerr << "done" << endl;
39
40
41         if (preconds.size() == 0){
42                 cerr << "Found no preconds" << endl;
43                 _start->addChild(node);
44         }else{
45                 for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
46                         cerr << "Looking for: " << *precond << endl;
47                         hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
48                         if(addedNode != _addedNodes.end()){
49                                 cerr << "Using already added node" << endl;
50                                 addedNode->second->addChild(node);
51                         }else {
52                                 hash_map<string, Action*>::iterator action = _actions.find(*precond);
53                                 if (action != _actions.end()){
54                                         cerr << "Adding new node" << endl;
55                                         Node* newnode = new Node(*action->second);
56                                         newnode->addChild(node);
57                                         makePlan(newnode);
58                                 }else{
59                                         cerr << "Action with effect: " << *precond << " not found!" << endl;
60                                 }
61                         }
62                 }
63         }
64 }
65
66 void Planner::addNode(Node* node){
67         const literals& effects = node->action().effects(0);
68
69         for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
70                 cout << "Adding node for effect: " << *effect << endl;
71                 _addedNodes[*effect] = node;
72         }
73 }
74
75
76 void Planner::execute(){
77         _start->execute(literals());
78         for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
79 }