5 using namespace __gnu_cxx;
7 extern "C" void* executeNode(void* arg);
9 struct ExecutionStuff {
13 queue<Node*>* execQueue;
16 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
19 _start = new StartNode(_init);
20 _finish = new EndNode(_goal);
23 for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
24 Action* act = new Action(*action);
25 _actions.push_back(act);
26 const Literals& effects = act->effects(0);
27 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
28 //cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
29 _actionEffects[*effect] = act;
32 //cerr << "Number of actions: " << _actions.size() << endl;
37 //cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
38 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
39 //cerr << "Deleting node " << (*node)->action()->name() << endl;
42 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
43 //cerr << "Deleting action " << (*action)->name() << endl;
49 void Planner::makePlan(Node* node){
52 //cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
53 const Preconditions& preconds = node->action()->preconditions();
54 //cerr << "done" << endl;
57 if (preconds.size() == 0){
58 //cerr << "Found no preconds" << endl;
59 _start->addChild(node);
61 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
62 //cerr << "Looking for: '" << precond->first << "'" << endl;
63 hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
64 if(addedNode != _addedEffects.end()){
65 //cerr << "Using already added node" << endl;
66 addedNode->second->addChild(node);
68 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
69 if (action != _actionEffects.end()){
70 //cerr << "Adding new node" << endl;
71 Node* newnode = new Node(action->second);
72 newnode->addChild(node);
74 }else if (precond->second){
75 //cerr << "Action with effect: " << precond->first << " not found!" << endl;
76 //cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
79 //cerr << "Action with effect: " << precond->first << " not found!" << endl;
80 //cerr << "This is a soft precondition, so we will continue" << endl;
81 node->satisfyCondition(precond->first);
82 _start->addChild(node);
89 void Planner::addNode(Node* node){
90 //cerr << "Adding node for action: " << node->action()->name() << endl;
91 const Literals& effects = node->action()->effects(0);
92 //cerr << "Number of effects: " << effects.size() << endl;
93 _addedNodes.push_back(node);
95 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
96 //cerr << "Adding node for effect: " << *effect << endl;
97 _addedEffects[*effect] = node;
102 void Planner::execute(){
103 _executedNodes.push(_start);
104 sem_init(&_nodes, 0, 1);
105 sem_init(&_list, 0, 1);
108 while (executions > 0){
109 cerr << "Waiting for a node to finish execution" << endl;
111 sem_getvalue(&_nodes, &retval);
112 cerr << "Semaphore before wait: " << retval << endl;
114 sem_getvalue(&_nodes, &retval);
115 cerr << "Semaphore after wait: " << retval << endl;
118 cerr << "Getting node: ";
120 cerr << "Number of nodes in queue: " << _executedNodes.size() << endl;
121 Node* node = _executedNodes.front();
122 _executedNodes.pop();
124 cerr << (int) node << endl;
125 cerr << node->action()->name() << ", and children.. ";
126 vector<Node*> children = node->children();
127 cerr << " done" << endl;
130 cerr << "Iterating over the children, number: " << children.size() << endl;
131 for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
132 if ((*child)->satisfyConditions(node->effects())){
134 cerr << "Creating new thread" << endl;
135 pthread_attr_t tattr;
137 pthread_attr_init(&tattr);
138 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
139 ExecutionStuff* es = new ExecutionStuff;
143 es->execQueue = &_executedNodes;
144 pthread_create(&tid, &tattr, executeNode, es);
150 cerr << "Number of nodes: " << _addedNodes.size() << endl;
151 back_insert_iterator<Literals> ii(_init);
153 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
154 cerr << "Deleting node " << (*node)->action()->name() << endl;
155 if ((*node)->executed()){
157 const Literals& effects = (*node)->effects();
158 copy(effects.begin(),effects.end(),ii);
159 cerr << "Finding action" << endl;
160 vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
161 if (action != _actions.end()){
162 cerr << "Removing executed action: " << (*action)->name() << endl;
164 _actions.erase(action);
170 _addedEffects.clear();
171 _actionEffects.clear();
172 copy(_init.begin(), _init.end(), ostream_iterator<string>(cerr, " "));
174 cerr << "Number of actions left: " << _actions.size() << endl;
175 //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan.
176 if (executions <= 1){
177 cerr << "Non of the remaining actions could be executed, quiting." << endl;
180 if (_actions.size() == 0){
181 cerr << "No remaining actions, quiting." << endl;
184 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
185 const Literals& effects = (*action)->effects(0);
186 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
187 cerr << "Adding effect: '" << *effect << "', action: " << (*action)->name() << endl;
188 _actionEffects[*effect] = *action;
191 _start = new StartNode(_init);
192 _finish = new EndNode(_goal);
195 if (_addedNodes.size() <= 2){
196 cerr << "No actions to execute, quiting." << endl;
202 void* executeNode(void* arg){
203 cerr << "Running new thred." << endl;
204 ExecutionStuff* es = (ExecutionStuff*)arg;
207 pthread_exit((void*)1);
212 cerr << "Adding pointer with value: " << (int)es->node << endl;
213 es->execQueue->push(es->node);
216 cerr << "Increasing semaphore" << endl;
219 pthread_exit((void*)0);