#include <iostream>
+#include <sstream>
+#include <fstream>
using namespace std;
+#include "planner.h"
+#include "action.h"
+
+vector<string> stringToVector(string str){
+ vector<string> strings;
+
+ istringstream ist(str);
+ while (ist >> str){
+ strings.push_back(str);
+ }
+
+ return strings;
+}
+
int main(int argc, char** argv){
+
+ if (argc != 4){
+ cout << "Syntax: " << argv[0] << " <file> \"init state\" \"goal state\"" << endl;
+ return 1;
+ }
+
+ ifstream file(argv[1]);
+ if (!file){
+ cerr << "Cannot open input file: " << argv[1] << endl;
+ exit(2);
+ }
+ vector<Action> actions;
+ while (!file.eof()){
+ string exec;
+ string precond;
+ string effects;
+ getline(file,exec);
+ getline(file,effects);
+ getline(file,precond);
+ cout << exec << ":" << effects << ":" << precond << endl;
+ actions.push_back(Action(exec, stringToVector(precond), stringToVector(effects)));
+ }
+ Planner p(actions, stringToVector(argv[2]), stringToVector(argv[3]));
return 0;
}
Planner::Planner(std::vector<Action> actions, literals init, literals goal){
- _start = new Node(Action("",init, literals()));
- Node* finish = new Node(Action("",literals(),goal));
+ _start = new Node(Action("",literals(), init));
+ Node* finish = new Node(Action("",goal,literals()));
for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
- literals preconds = action->preconditions();
- for (literals::iterator effect = preconds.begin(); effect != preconds.end(); ++effect){
+ literals effects = action->effects();
+ for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
+ cerr << "Adding effect: " << *effect << endl;
_actions[*effect] = *action;
}
}
literals preconds = node->action().preconditions();
if (preconds.size() == 0){
+ cerr << "Found no preconds" << endl;
_start->addChild(node);
}else{
for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+ cerr << "Looking for: " << *precond << endl;
hash_map<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
if(addedNode != _addedNodes.end()){
+ cerr << "Using already added node" << endl;
addedNode->second->addChild(node);
}else {
hash_map<string, Action>::iterator action = _actions.find(*precond);
if (action != _actions.end()){
+ cerr << "Adding new node" << endl;
Node* newnode = new Node(action->second);
newnode->addChild(node);
+ addNode(newnode);
makePlan(newnode);
}else{
- cerr << "Action with effect: " << *precond << " not found!";
+ cerr << "Action with effect: " << *precond << " not found!" << endl;
}
}
}
literals effects = node->action().effects();
for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){
+ cout << "Adding node for effect: " << *effect << endl;
_addedNodes[*effect] = node;
}
}