asp_formatter.cpp
Go to the documentation of this file.
00001 
00002 #include "actasp/AnswerSet.h"
00003 #include <actasp/Action.h>
00004 
00005 #include <iostream>
00006 #include <vector>
00007 #include <sstream>
00008 #include <algorithm>
00009 #include <iterator>
00010 
00011 using namespace std;
00012 using namespace actasp;
00013 
00014 
00016 
00017 class SimpleAction : public actasp::Action {
00018 public:
00019 
00020         SimpleAction() : done(false) {}
00021 
00022         int paramNumber() const {
00023                 return 0;
00024         }
00025 
00026         std::string getName() const {
00027                 return name;
00028         }
00029 
00030         void run() {
00031                 std::cout << "running " << name << std::endl;
00032                 done = true;
00033         }
00034 
00035         bool hasFinished() const {
00036                 return done;
00037         }
00038 
00039         virtual actasp::Action *cloneAndInit(const actasp::AspFluent &f) const {
00040                 SimpleAction *newAction = new SimpleAction(*this);
00041                 newAction->name = f.getName();
00042                 newAction->params = f.getParameters();
00043                 return newAction;
00044         }
00045         
00046         virtual actasp::Action *clone() const {
00047                 return new SimpleAction(*this);
00048         }
00049 
00050 private:
00051         std::string name;
00052         std::vector<std::string> params;
00053         bool done;
00054 
00055         std::vector<std::string> getParameters() const {
00056                 return params;
00057         }
00058 
00059 };
00060 
00061 struct DeleteAction {
00062         
00063         void operator()(Action *act) {
00064                 delete act;
00065         }
00066 };
00067 
00068 struct CreateFluent {
00069         AspFluent operator()(const std::string & fluentDescription) {
00070                 return AspFluent(fluentDescription);
00071         }
00072 };
00073 
00074 
00075 static std::set<actasp::AspFluent> parseAnswerSet(const std::string& answerSetContent) {
00076 
00077   stringstream predicateLine(answerSetContent);
00078 
00079   set<AspFluent> predicates;
00080 
00081   //split the line based on spaces
00082   transform(istream_iterator<string>(predicateLine),
00083             istream_iterator<string>(),
00084             inserter(predicates,predicates.begin()),
00085             CreateFluent());
00086 
00087   return predicates;
00088 
00089 }
00090 
00091 static std::vector<actasp::AnswerSet> readAnswerSets(istream &input) {
00092         
00093         string line;
00094         string answerSetContent;
00095         while (input) {
00096                 getline(input, line);
00097                 answerSetContent += line;
00098                 answerSetContent += "\n";
00099         }
00100 
00101         bool satisfiable = answerSetContent.find("UNSATISFIABLE") == string::npos;
00102 
00103         vector<AnswerSet> allSets;
00104 
00105         if (satisfiable) {
00106 
00107                 stringstream content(answerSetContent);
00108 
00109                 string firstLine;
00110                 string eachAnswerset;
00111 
00112                 while (content) {
00113 
00114                         getline(content,firstLine);
00115                         if (firstLine.find("Answer") != string::npos) {
00116                                 getline(content,eachAnswerset);
00117 
00118                                 set<AspFluent> fluents = parseAnswerSet(eachAnswerset);
00119 
00120                                 allSets.push_back(AnswerSet(fluents.begin(), fluents.end()));
00121                         }
00122                 }
00123         }
00124 
00125         return allSets;
00126         
00127 }
00128 
00129 int main() {
00130         
00131         std::map<std::string, actasp::Action *> actionMap;
00132 
00133         actionMap.insert(std::make_pair(std::string("approach"), new SimpleAction()));
00134         actionMap.insert(std::make_pair(std::string("gothrough"), new SimpleAction()));
00135         actionMap.insert(std::make_pair(std::string("opendoor"), new SimpleAction()));
00136         actionMap.insert(std::make_pair(std::string("searchroom"), new SimpleAction()));
00137         actionMap.insert(std::make_pair(std::string("askperson"), new SimpleAction())); 
00138         actionMap.insert(std::make_pair(std::string("changefloor"), new SimpleAction()));
00139   actionMap.insert(std::make_pair(std::string("callelevator"), new SimpleAction()));
00140         
00141         vector<AnswerSet> sets = readAnswerSets(cin);
00142         
00143         cout << endl << "Number of answer sets: " << sets.size() << endl;
00144         
00145         for(int i=0; i<sets.size(); ++i) {
00146                 
00147                 cout <<  "------------------------------" << endl;
00148                 
00149                 cout << "Plan" << endl;
00150                 
00151                 list<Action*> plan = sets[i].instantiateActions(actionMap);
00152                 list<Action*>::iterator pIt = plan.begin();
00153                 
00154                 for(int t=0; pIt != plan.end(); ++pIt, ++t)
00155                         if ((*pIt) != NULL)
00156                                 cout << (*pIt)->toASP(t) << " ";
00157                 
00158                 for_each(plan.begin(),plan.end(),DeleteAction());
00159                 
00160                 cout << endl << endl << "Fluents" << endl;
00161                 
00162                 
00163                 //fluents in an answer set are ordered by time step.
00164 
00165                 unsigned int lastTimeStep = sets[i].maxTimeStep();
00166                 
00167                 for(int t = 0; t <= lastTimeStep; ++t) {
00168                         
00169                 set<AspFluent> fluentsAtTimeT = sets[i].getFluentsAtTime(t);
00170                 set<AspFluent>::const_iterator flu = fluentsAtTimeT.begin();
00171 
00172                 
00173                 
00174                 for(; flu != fluentsAtTimeT.end(); ++flu)
00175                         cout << flu->toString() << " ";
00176                 
00177                 cout << endl << endl;
00178                 }
00179                 
00180                 cout << endl;
00181                 
00182         }
00183         
00184         
00185         
00186         return 0;
00187 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:46