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);
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;
22 cout << "Number of actions: " << _actions.size() << endl;
27 for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
28 cerr << "Deleting node " << node->second->action().name() << endl;
34 void Planner::makePlan(Node* node){
37 cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
38 const Preconditions& preconds = node->action().preconditions();
39 cerr << "done" << endl;
42 if (preconds.size() == 0){
43 cerr << "Found no preconds" << endl;
44 _start->addChild(node);
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);
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);
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;
64 cerr << "Action with effect: " << precond->first << " not found!" << endl;
65 cerr << "This is a soft precondition, so we will continue" << endl;
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;
77 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
78 cout << "Adding node for effect: " << *effect << endl;
79 _addedNodes[*effect] = node;
84 void Planner::execute(){
85 _start->execute(true,Literals());
86 /*for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
87 if (node->second->executed()){
89 _start->execute(false,Literals());