+ for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
+ const Literals& effects = action->effects(0);
+ for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+ cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
+ _actions[*effect] = *action;
+ }
+ }
+ cout << "Number of actions: " << _actions.size() << endl;
+ makePlan(_finish);
+}
+
+Planner::~Planner(){
+ for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+ cerr << "Deleting node " << node->second->action().name() << endl;
+ delete node->second;
+ }
+}
+
+
+void Planner::makePlan(Node* node){
+ addNode(node);
+
+ cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
+ const Preconditions& preconds = node->action().preconditions();
+ cerr << "done" << endl;
+
+
+ if (preconds.size() == 0){
+ cerr << "Found no preconds" << endl;
+ _start->addChild(node);
+ }else{
+ for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+ cerr << "Looking for: '" << precond->first << "'" << endl;
+ hash_map<string,Node*>::iterator addedNode = _addedNodes.find(precond->first);
+ if(addedNode != _addedNodes.end()){
+ cerr << "Using already added node" << endl;
+ addedNode->second->addChild(node);
+ }else {
+ hash_map<string, Action>::iterator action = _actions.find(precond->first);
+ if (action != _actions.end()){
+ cerr << "Adding new node" << endl;
+ Node* newnode = new Node(action->second);
+ newnode->addChild(node);
+ makePlan(newnode);
+ }else if (precond->second){
+ cerr << "Action with effect: " << precond->first << " not found!" << endl;
+ cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
+ return;
+ }else{
+ cerr << "Action with effect: " << precond->first << " not found!" << endl;
+ cerr << "This is a soft precondition, so we will continue" << endl;
+ }
+ }
+ }
+ }
+}
+
+void Planner::addNode(Node* node){
+ cerr << "Adding node for action: " << node->action().name() << endl;
+ const Literals& effects = node->action().effects(0);
+ cerr << "Number of effects: " << effects.size() << endl;
+
+ for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
+ cout << "Adding node for effect: " << *effect << endl;
+ _addedNodes[*effect] = node;
+ }
+}
+
+
+void Planner::execute(){
+ _start->execute(true,Literals());
+ /*for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+ if (node->second->executed()){
+ }*/
+ _start->execute(false,Literals());
+}