]> ruin.nu Git - popboot.git/blob - planner.h
Spelling fix in inittab
[popboot.git] / planner.h
1 #ifndef __PLANNER_H__
2 #define __PLANNER_H__
3
4 #include <ext/hash_map>
5 #include <vector>
6 #include <queue>
7 #include <pthread.h>
8 #include <semaphore.h>
9 #include "action.h"
10
11 class Node;
12
13 /**
14  * This class creates, holds and executes a plan.
15  */
16 class Planner {
17         public:
18                 /**
19                  * Creates a plan given the input actions, initial state and goal.
20                  *
21                  * @param actions A list of actions which will be used to reach the goal.
22                  * @param init The initial state which the plan will start from.
23                  * @param goal The goal state which the plan will try to reach.
24                  */
25                 Planner(std::vector<Action> actions, Literals init, Literals goal);
26
27                 /**
28                  * Deletes all the remaining nodes and actions.
29                  */
30                 ~Planner();
31
32                 /**
33                  * Executes the plan.
34                  * Creates new threads for execution of each action.
35                  *
36                  * If any action fail in some way, this method will try to replan and
37                  * recursively call itself.
38                  */
39                 void execute();
40
41
42         protected:
43
44                 /**
45                  * This method does the actual planning. Given the input node it will
46                  * try to find all preconditions and create links from parent nodes
47                  * to this node and, if the precondition did not have a node associated
48                  * with it, create a new and recursively call this method for the newly
49                  * created node.
50                  *
51                  * @param node The node which this method will find parent nodes for.
52                  */
53                 void makePlan(Node* node);
54
55                 /**
56                  * Adds this node to the internal map and vector.
57                  */
58                 void addNode(Node* node);
59
60                 /**
61                  * This method goes through the executed nodes and then calls 
62                  * executeChildren for these nodes.
63                  */
64                 void executePlan();
65
66                 /**
67                  * Iterates through the children of the input node and satisfies all
68                  * preconditions possible with this node. The children will be executed
69                  * if all preconditions have been satisfied.
70                  */
71                 int executeChildren(Node* node);
72
73                 /**
74                  * Addes the remaining actions to the actions map, creates new start and
75                  * finish nodes and runs makePlan.
76                  */
77                 void replan();
78                 
79                 /**
80                  * Deletes all nodes, updates _init and _goal, and clears the internal maps and vector.
81                  */
82                 int cleanupExecution();
83
84                 //! Semaphore which is used to signal if a new node has been executed and added to _executedNodes.
85                 sem_t _nodes;
86                 //! Mutex for access to _executedNodes.
87                 sem_t _list;
88                 //! Holds the nodes which have been executed, but the children haven't been taken care of yet.
89                 std::queue<Node*> _executedNodes;
90
91                 //! The start node in the plan graph.
92                 Node* _start;
93                 //! The end node in the plan graph.
94                 Node* _finish;
95                 //! Map from all the effects which can be achieved with the already added nodes.
96                 __gnu_cxx::hash_map<std::string,Node*> _addedEffects;
97                 //! Map with all effects available and their corresponding actions.
98                 __gnu_cxx::hash_map<std::string,Action*> _actionEffects;
99                 //! The initial state.
100                 Literals _init;
101                 //! The goal state
102                 Literals _goal;
103                 //! All the nodes added to the current plan.
104                 std::vector<Node*> _addedNodes;
105                 //! All the non-executed actions available in the current plan.
106                 std::vector<Action*> _actions;
107 };
108 #endif