ToPS
ProbabilisticModelParameter.cpp
00001 /*
00002  *       ProbabilisticModelParameter.cpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      Ígor Bonádio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #include "ProbabilisticModelParameter.hpp"
00026 #include "util.hpp"
00027 
00028 namespace tops {
00029 
00030   void ProbabilisticModelParameters::add(const char * name, ProbabilisticModelParameterValuePtr value) {
00031     std::string s(name);
00032     add(s, value);
00033   }
00034 
00036   void ProbabilisticModelParameters::add(std::string name, ProbabilisticModelParameterValuePtr value)
00037   {
00038     if(_parameters.find(name) == _parameters.end())
00039       _parameters[name] = value;
00040     else
00041       {
00042         std::cerr << "ERROR: " << name << "  already defined !" << std::endl;
00043       }
00044   }
00045 
00047   void ProbabilisticModelParameters::set(std::string name, ProbabilisticModelParameterValuePtr value)
00048   {
00049     _parameters[name] = value;
00050   }
00051 
00052 
00054   ProbabilisticModelParameterValuePtr ProbabilisticModelParameters::getMandatoryParameterValue(const std::string & name) const {
00055     ProbabilisticModelParameterValuePtr null;
00056     std::map <std::string, ProbabilisticModelParameterValuePtr>::const_iterator it = _parameters.find(name) ;
00057     if(it == _parameters.end()) {
00058       std::cerr<< "ERROR: Mandatory parameter, " << name << ", was not defined !\n";
00059       return null;
00060     }
00061     return  it->second;
00062   }
00064   ProbabilisticModelParameterValuePtr ProbabilisticModelParameters::getOptionalParameterValue(const std::string & name) const {
00065     ProbabilisticModelParameterValuePtr null;
00066     std::map <std::string, ProbabilisticModelParameterValuePtr>::const_iterator it = _parameters.find(name) ;
00067     if(it == _parameters.end()) {
00068       return null;
00069     }
00070     return  it->second;
00071   }
00072 
00073   std::map <std::string, ProbabilisticModelParameterValuePtr>  ProbabilisticModelParameters::parameters() const {
00074     return _parameters;
00075   }
00076 
00077   void ProbabilisticModelParameterValue::setIsRoot(bool root) {
00078   }
00079 
00080   std::string ProbabilisticModelParameterValue::parameter_type () const {
00081     std::string type("ProbabilisticModelParameterValue");
00082     return type;
00083   }
00084   std::map<std::string,double> & ProbabilisticModelParameterValue::getDoubleMap()  {
00085     return p;
00086   }
00087   StringVector & ProbabilisticModelParameterValue::getStringVector() {
00088     return s;
00089   }
00090   DoubleVector & ProbabilisticModelParameterValue::getDoubleVector()  {
00091     return d;
00092   }
00093   IntVector & ProbabilisticModelParameterValue::getIntVector() {
00094     return i;
00095   }
00096 
00097   ProbabilisticModelParameters & ProbabilisticModelParameterValue::getParameters()  {
00098     return _parameters;
00099   }
00100 
00101   std::string ProbabilisticModelParameterValue::getString() const {
00102     return _str;
00103   }
00104   std::map<std::string,std::string> &  ProbabilisticModelParameterValue::getStringMap()  {
00105     return str_map;
00106   }
00107   int ProbabilisticModelParameterValue::getInt() const {
00108     int i = 0;
00109     std::cerr << "WARNING: parameter not specified as a int !\n"  << std::endl;
00110     return i;
00111   }
00112   double ProbabilisticModelParameterValue::getDouble()  const {
00113     double d = 0.0;
00114     std::cerr << "WARNING: parameter not specified as a double !\n"  << std::endl;
00115     return d;
00116   }
00117   std::string ProbabilisticModelParameterValue::str() const {
00118     std::string s;
00119     std::cerr << "WARNING: not implemented " << parameter_type() << "::str()" << std::endl;
00120     return s;
00121   }
00122 
00123   void ProbabilisticModelParameterListValue::initialize(ProbabilisticModelParameters p) {
00124     _parameters = p;
00125     _root = false;
00126   }
00127   ProbabilisticModelParameters & ProbabilisticModelParameterListValue::getParameters()  {
00128     return _parameters;
00129   }
00130   std::string ProbabilisticModelParameterListValue::parameter_type () const {
00131     std::string type("ProbabilisticModelParameterListValue");
00132     return type;
00133   }
00134   void ProbabilisticModelParameterListValue::setIsRoot(bool root) {
00135     _root = root;
00136   }
00137 
00138   std::string ProbabilisticModelParameterListValue::getString() const {
00139     std::stringstream out;
00140     std::map <std::string, ProbabilisticModelParameterValuePtr>::const_iterator it;
00141     std::map<std::string, ProbabilisticModelParameterValuePtr> p = _parameters.parameters();
00142     out << "[" <<std::endl;
00143     for(it = p.begin(); it != p.end(); it++)
00144       {
00145         out<< it->first << " = " << (it->second)->str() << std::endl;
00146       }
00147     out << "]";
00148     return out.str();
00149   }
00150 
00151   std::string ProbabilisticModelParameterListValue::str () const {
00152     std::stringstream out;
00153     std::map <std::string, ProbabilisticModelParameterValuePtr>::const_iterator it;
00154     std::map<std::string, ProbabilisticModelParameterValuePtr> p = _parameters.parameters();
00155     if(_root != true) {
00156       out << "[" <<std::endl;
00157       for(it = p.begin(); it != p.end(); it++)
00158         {
00159           out<< it->first << " = " << (it->second)->str() << std::endl;
00160         }
00161       out << "]" << std::endl;
00162     } else
00163       for(it = p.begin(); it != p.end(); it++)
00164         {
00165           out<< it->first << " = " << (it->second)->str() << std::endl;
00166         }
00167 
00168     return out.str();
00169   }
00170 
00171 
00172   void DoubleParameterValue::initialize(double d){
00173     _d = d;
00174   }
00175 
00176   std::string DoubleParameterValue::parameter_type () const {
00177     std::string type("DoubleParameterValue");
00178     return type;
00179   }
00180 
00181   double DoubleParameterValue::getDouble() const {
00182     return _d;
00183   }
00184   int DoubleParameterValue::getInt() const {
00185     return (int)_d;
00186   }
00187 
00188   std::string DoubleParameterValue::str() const {
00189     std::stringstream out;
00190     out << _d ;
00191     return out.str();
00192   }
00193 
00194   std::string IntParameterValue::parameter_type () const {
00195     std::string type("IntParameterValue");
00196     return type;
00197   }
00198 
00199 
00200   void IntParameterValue::initialize(int v) {
00201     _v = v;
00202   }
00203 
00204   int IntParameterValue::getInt() const {
00205     return _v;
00206   }
00207   double IntParameterValue::getDouble() const {
00208     return (double) _v;
00209   }
00210 
00211   std::string IntParameterValue::str() const {
00212     std::stringstream out;
00213     out << _v;
00214     return out.str();
00215   }
00216 
00217 
00218   std::string StringParameterValue::parameter_type () const {
00219     std::string type("StringParameterValue");
00220     return type;
00221   }
00222   void StringParameterValue::initialize(std::string v) {
00223     _v = v;
00224   }
00225 
00226   std::string StringParameterValue::getString() const {
00227     return _v;
00228   }
00229 
00230   std::string StringParameterValue::str() const {
00231     std::stringstream out;
00232     out << "\"" << _v << "\"" ;
00233     return out.str();
00234   }
00235 
00236   std::string IntVectorParameterValue::parameter_type () const {
00237     std::string type("IntVectorParameterValue");
00238     return type;
00239   }
00240   void IntVectorParameterValue::initialize(IntVector v) {
00241     _v = v;
00242   }
00243 
00244   IntVector & IntVectorParameterValue::getIntVector()  {
00245     return _v;
00246   }
00247   std::string IntVectorParameterValue::str() const {
00248     std::stringstream out;
00249     out << "(";
00250     if(_v.size() > 0)
00251       out << _v[0] ;
00252     for(int i = 1; i <(int) _v.size(); i++)
00253       out << "," << _v[i];
00254     out << ")";
00255     return out.str();
00256   }
00257 
00258   void DoubleMapParameterValue::initialize(const std::map<std::string, double > & values) {
00259     _v = values;
00260   }
00261 
00262   std::string DoubleMapParameterValue::parameter_type () const {
00263     std::string type("DoubleMapParameterValue");
00264     return type;
00265   }
00266 
00267   std::map<std::string, double> & DoubleMapParameterValue::getDoubleMap()   {
00268     return _v;
00269   }
00270 
00271   std::string DoubleMapParameterValue::str() const {
00272     std::map<std::string, double>::const_iterator it;
00273     std::stringstream out;
00274     out << "(";
00275     for(it = _v.begin(); it != _v.end(); it++)
00276       {
00277         std::vector <std::string> splited;
00278         boost::regex separator("\\|");
00279         split_regex(it->first, splited, separator);
00280         if(splited.size() == 1) {
00281           splited.push_back("");
00282         }
00283         std::string context (splited[1]);
00284         std::string symbol ( splited[0]);
00285         if(context.size() > 0)
00286           out<< "\"" << symbol << "\"|\"" << context << "\": " << (it->second) << ";" << std::endl;
00287         else
00288           out<< "\"" << symbol << "\": " << (it->second) << ";" << std::endl;
00289       }
00290     out << ")";
00291     return out.str();
00292   }
00293 
00294   void DoubleVectorParameterValue::initialize(DoubleVector v) {
00295     _v = v;
00296   }
00297 
00298   DoubleVector & DoubleVectorParameterValue::getDoubleVector()  {
00299     return _v;
00300   }
00301   std::string DoubleVectorParameterValue::parameter_type () const {
00302     std::string type("DoubleVectorParameterValue");
00303     return type;
00304   }
00305 
00306   std::string DoubleVectorParameterValue::str() const {
00307     std::stringstream out;
00308     out << "(";
00309     if(_v.size() > 0)
00310       out << _v[0] ;
00311     for(int i = 1; i < (int)_v.size(); i++)
00312       out << ", " << _v[i];
00313     out << ")";
00314     return out.str();
00315   }
00316 
00317 
00318   void StringMapParameterValue::initialize(std::map<std::string,std::string> m)
00319   {
00320     _str_map = m;
00321   }
00322 
00323   StringMap & StringMapParameterValue::getStringMap() {
00324     return  _str_map;
00325   }
00326   std::string StringMapParameterValue::parameter_type () const {
00327     std::string type("StringMapParameterValue");
00328     return type;
00329   }
00330   std::string StringMapParameterValue::str() const {
00331     std::map<std::string, std::string>::const_iterator it;
00332     std::stringstream out;
00333     out << "(";
00334     for(it = _str_map.begin(); it != _str_map.end(); it++)
00335       {
00336         out << "\"" <<it->first << "\": \"" <<  it->second << "\";" << std::endl;;
00337       }
00338     out << ")";
00339     return out.str();
00340   }
00341 
00342 
00343 
00344   StringVector &  StringVectorParameterValue::getStringVector() {
00345     return  _v;
00346   }
00347   void StringVectorParameterValue::initialize(StringVector v) {
00348     _v = v;
00349   }
00350   std::string StringVectorParameterValue::parameter_type () const {
00351     std::string type("StringVectorParameterValue");
00352     return type;
00353   }
00354 
00355   std::string StringVectorParameterValue::str() const {
00356     std::stringstream out;
00357     out << "(";
00358     if(_v.size() > 0)
00359       out << "\""<< _v[0] << "\"";
00360     for(int i = 1; i < (int)_v.size(); i++)
00361       out << ", \"" << _v[i] << "\"";
00362     out << ")";
00363     return out.str();
00364   }
00365 }