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