6 using namespace __gnu_cxx;
8 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
11 _start = new StartNode(_init);
12 _finish = new EndNode(_goal);
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;
25 for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
26 cerr << "Deleting node " << node->second->action().name() << endl;
32 void Planner::makePlan(Node* node){
35 cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
36 const Preconditions& preconds = node->action().preconditions();
37 cerr << "done" << endl;
40 if (preconds.size() == 0){
41 cerr << "Found no preconds" << endl;
42 _start->addChild(node);
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);
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);
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;
62 cerr << "Action with effect: " << precond->first << " not found!" << endl;
63 cerr << "This is a soft precondition, so we will continue" << endl;
70 void Planner::addNode(Node* node){
71 const Literals& effects = node->action().effects(0);
73 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
74 cout << "Adding node for effect: " << *effect << endl;
75 _addedNodes[*effect] = node;
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()){
85 _start->execute(false,Literals());