]> ruin.nu Git - popboot.git/blob - planner.cpp
some done, need parser
[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         _finish = new EndNode(_goal);
13
14         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
15                 const Literals& effects = action->effects(0);
16                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
17                         cerr << "Adding effect: " << *effect << endl;
18                         _actions[*effect] = *action;
19                 }
20         }
21         makePlan(_finish);
22 }
23
24 Planner::~Planner(){
25         for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
26                 cerr << "Deleting node " << node->second->action().name() << endl;
27                 delete node->second;
28         }
29 }
30
31
32 void Planner::makePlan(Node* node){
33         addNode(node);
34
35         cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
36         const Preconditions& preconds = node->action().preconditions();
37         cerr << "done" << endl;
38
39
40         if (preconds.size() == 0){
41                 cerr << "Found no preconds" << endl;
42                 _start->addChild(node);
43         }else{
44                 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
45                         cerr << "Looking for: " << precond->first << endl;
46                         hash_map<string,Node*>::iterator addedNode = _addedNodes.find(precond->first);
47                         if(addedNode != _addedNodes.end()){
48                                 cerr << "Using already added node" << endl;
49                                 addedNode->second->addChild(node);
50                         }else {
51                                 hash_map<string, Action>::iterator action = _actions.find(precond->first);
52                                 if (action != _actions.end()){
53                                         cerr << "Adding new node" << endl;
54                                         Node* newnode = new Node(action->second);
55                                         newnode->addChild(node);
56                                         makePlan(newnode);
57                                 }else if (precond->second){
58                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
59                                         cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
60                                         return;
61                                 }else{
62                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
63                                         cerr << "This is a soft precondition, so we will continue" << endl;
64                                 }
65                         }
66                 }
67         }
68 }
69
70 void Planner::addNode(Node* node){
71         const Literals& effects = node->action().effects(0);
72
73         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
74                 cout << "Adding node for effect: " << *effect << endl;
75                 _addedNodes[*effect] = node;
76         }
77 }
78
79
80 void Planner::execute(){
81         _start->execute(true,Literals());
82         for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
83                 if (!node->second->executed())
84                         node->second->execute(false,Literals());
85         }
86 }