AspFluent.cpp
Go to the documentation of this file.
00001 #include <actasp/AspFluent.h>
00002 
00003 #include <sstream>
00004 #include <cstdlib> //for atoi
00005 
00006 using namespace std;
00007 
00008 namespace actasp {
00009 
00010 
00011 AspFluent::AspFluent(const std::string& formula) throw (std::invalid_argument) :
00012       timeStep(),
00013                         cachedBase(){
00014         
00015   //this used to be nice, but it turned out to be a major bottleneck, so I had to reimplement it for efficiency.
00016    
00017    bool inName = true;   
00018    bool valid = false;
00019    string current;
00020    //current.reserve(100);
00021    size_t first_par = formula.find_first_of('(');
00022    size_t last_par = formula.find_last_of(')');
00023    size_t last_comma = formula.find_last_of(',');
00024    
00025    if(first_par == string::npos)
00026     throw std::invalid_argument("AspFluent: The string " + formula + " does not contain a '(', therefore is not a valid fluent");
00027    
00028    if(last_par == string::npos)
00029      throw std::invalid_argument("The string " + formula + " does not contain a ')', therefore is not a valid fluent");
00030    
00031    size_t time_begins = (last_comma == string::npos)? first_par+1 : last_comma+1;
00032    timeStep = atoi(&formula[time_begins]);
00033    cachedBase.assign(&formula[0],time_begins);
00034   
00035 }
00036 
00037 AspFluent::AspFluent(const std::string &name, const std::vector<std::string> &variables, unsigned int timeStep) throw () : 
00038                 timeStep(timeStep),
00039                 cachedBase() {
00040   stringstream ss;
00041 
00042   ss << name << "(";
00043 
00044   int i=0;
00045   for (int size = variables.size(); i<size; ++i)
00046     ss << variables[i] << ",";
00047 
00048   cachedBase = ss.str();
00049 
00050 }
00051 
00052 unsigned int AspFluent::arity() const  throw() {
00053         return this->getParameters().size() + 1;
00054 }
00055 
00056 void AspFluent::setTimeStep(unsigned int timeStep) throw() {
00057         
00058 
00059         this->timeStep = timeStep;
00060         
00061 }
00062 
00063 unsigned int AspFluent::getTimeStep() const throw() {
00064         return this->timeStep;
00065 }
00066 
00067 string AspFluent::getName() const throw() {
00068         return cachedBase.substr(0,cachedBase.find_first_of('('));
00069 }
00070 
00071 vector<string> AspFluent::getParameters() const throw() {
00072   
00073   size_t start = cachedBase.find_first_of('(')+1;
00074   
00075   vector<string> params;
00076   
00077   size_t comma = cachedBase.find_first_of(',',start);
00078   
00079   while(comma != string::npos) {
00080     params.push_back(cachedBase.substr(start,comma-start));
00081     start = comma+1;
00082     comma = cachedBase.find_first_of(',',comma+1);
00083   }
00084   
00085         return params;
00086 }
00087 
00088 bool AspFluent::operator<(const AspFluent& other) const throw(){
00089         if(this->timeStep < other.timeStep)
00090                 return true;
00091 
00092         if(this->timeStep > other.timeStep)
00093                 return false;
00094 
00095         return  this->cachedBase < other.cachedBase;
00096 }
00097 
00098 bool AspFluent::operator==(const AspFluent& other) const throw() {
00099         if(this->timeStep != other.timeStep)
00100                 return false;
00101         
00102         return this->cachedBase == other.cachedBase;
00103 }
00104 
00105 std::string AspFluent::toString(unsigned int timeStep) const throw() {
00106     
00107   stringstream ss;
00108   ss << timeStep << ")";
00109   return cachedBase + ss.str();
00110 }
00111 
00112 std::string AspFluent::toString(const string& timeStepVar) const throw() {   
00113   return cachedBase + timeStepVar + ")";
00114 }
00115 
00116 std::string AspFluent::toString() const throw () {
00117         return this->toString(this->timeStep);
00118 }
00119 
00120 
00121 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:36