bpp-core  2.1.0
bpp::ApplicationTools Class Reference

This class provides some common tools for developping applications. More...

#include <Bpp/App/ApplicationTools.h>

+ Collaboration diagram for bpp::ApplicationTools:

List of all members.

Public Member Functions

 ApplicationTools ()
virtual ~ApplicationTools ()

Static Public Member Functions

static bool parameterExists (const std::string &parameterName, std::map< std::string, std::string > &params)
 Tells if a parameter have been specified.
static double getDoubleParameter (const std::string &parameterName, std::map< std::string, std::string > &params, double defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a double parameter.
static int getIntParameter (const std::string &parameterName, std::map< std::string, std::string > &params, int defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get an integer parameter.
static std::string getStringParameter (const std::string &parameterName, std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a string parameter.
static bool getBooleanParameter (const std::string &parameterName, std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a boolean parameter.
template<class T >
static T getParameter (const std::string &parameterName, std::map< std::string, std::string > &params, T defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a parameter.
static std::string getAFilePath (const std::string &parameter, std::map< std::string, std::string > &params, bool isRequired=true, bool mustExist=true, const std::string &suffix="", bool suffixIsOptional=false) throw (Exception)
 Get a file path.
template<class T >
static std::vector< T > getVectorParameter (const std::string &parameterName, std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a vector.
template<class T >
static std::vector< T > getVectorParameter (const std::string &parameterName, std::map< std::string, std::string > &params, char separator, char rangeOperator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
 Get a vector.
static void startTimer ()
 Starts the timer.
static void displayTime (const std::string &msg)
 Display the current timer value to the 'message' stream.
static double getTime ()
 Get the current timer value.
Output methods.
static void displayMessage (const std::string &text)
 Print a message.
static void displayError (const std::string &text)
 Print an error message.
static void displayWarning (const std::string &text)
 Print a warning message.
static void displayTask (const std::string &text, bool eof=false)
 Print a task message.
static void displayTaskDone ()
 Print a task ended message.
template<class T >
static void displayResult (const std::string &text, const T &result)
 Print a result message.
static void displayBooleanResult (const std::string &text, bool result)
 Print a boolean result message ("yes" or "no").
static void displayGauge (size_t iter, size_t total, char symbol='>', const std::string &mes="")
 Display a gauge.
static void displayUnlimitedGauge (size_t iter, const std::string &mes="")
 Display a gauge for unefined amount of iterations.

Static Public Attributes

static OutputStreamerror = new StdErr()
 The output stream where errors have to be displayed.
static OutputStreammessage = new StdOut()
 The output stream where messages have to be displayed.
static OutputStreamwarning = new StdOut()
 The output stream where warnings have to be displayed.
static time_t startTime
 Timer variable.
static size_t terminalWidth = 80
 The width of the output terminal (in character).
static float terminalSplit = 0.5
 The fraction of terminal width dedicated to messages.
static bool interactive = true
 Tell if the program is interactive (typically run in foreground). Default to yes.

Detailed Description

This class provides some common tools for developping applications.

These functions are designed for helping to parse an option file.

The option files are supposed to follow this simple format:

 parameterName = parameterContent

with one parameter per line.

In files, shell comments:

 # my comment line here

C comments:

 / * my comment block here * /

and C++ comments:

 // my comment line here

are allowed, and ignored while parsing.

Some methods for displaying information (messages, errors, warnings...) are also provided.

Methods dealing with parameters takes as argument a map<string, string> object containing the parameters (names are the keys of the map, and values are... the values of the map!). These map objects may be obtained from the AttributesTools utilitary class.

Definition at line 90 of file ApplicationTools.h.


Constructor & Destructor Documentation

Definition at line 128 of file ApplicationTools.h.

virtual bpp::ApplicationTools::~ApplicationTools ( ) [inline, virtual]

Definition at line 129 of file ApplicationTools.h.


Member Function Documentation

static void bpp::ApplicationTools::displayBooleanResult ( const std::string &  text,
bool  result 
) [inline, static]

Print a boolean result message ("yes" or "no").

Result will be aligned to 30 character from the begining of the message.

Parameters:
textThe text of the message.
resultThe result.

Definition at line 431 of file ApplicationTools.h.

References displayResult().

void ApplicationTools::displayError ( const std::string &  text) [static]

Print an error message.

Parameters:
textThe text of the message.

Definition at line 217 of file ApplicationTools.cpp.

void ApplicationTools::displayGauge ( size_t  iter,
size_t  total,
char  symbol = '>',
const std::string &  mes = "" 
) [static]

Display a gauge.

Show progress status.

 for(size_t i = 0; i < 1000; i++)
 {
   ApplicationTools::displayGauge(i, 999, '*');
   //Perform time consuming task...
 }

will result in something like:

     * [************************************]
     * 
Parameters:
iterThe current iteration number.
totalThe total number of iteration.
symbolThe character to display in the gauge.
mesA message to print before the gauge.

Definition at line 235 of file ApplicationTools.cpp.

References bpp::TextTools::resizeLeft(), bpp::TextTools::resizeRight(), and bpp::TextTools::toString().

Referenced by bpp::FunctionTools::computeGrid().

void ApplicationTools::displayMessage ( const std::string &  text) [static]

Print a message.

Parameters:
textThe text of the message.

Definition at line 215 of file ApplicationTools.cpp.

Referenced by bpp::FunctionTools::computeGrid(), displayResult(), and bpp::ReparametrizationFunctionWrapper::init_().

template<class T >
static void bpp::ApplicationTools::displayResult ( const std::string &  text,
const T &  result 
) [inline, static]

Print a result message.

Result will be aligned to 30 character from the begining of the message. ex: text = "Here is what you get:" and result = "THAT" gives "Here is what you get: THAT".

Parameters:
textThe text of the message.
resultThe result.

Definition at line 419 of file ApplicationTools.h.

References displayMessage(), bpp::TextTools::resizeRight(), terminalSplit, and terminalWidth.

Referenced by displayBooleanResult(), bpp::BppODiscreteDistributionFormat::initialize_(), and bpp::BppODiscreteDistributionFormat::read().

void ApplicationTools::displayTask ( const std::string &  text,
bool  eof = false 
) [static]

Print a task message.

Display the message and flush the buffer, but do not end the current line.

Parameters:
textThe text of the message.
eofInsert a carriage return after displaying the message.

Definition at line 221 of file ApplicationTools.cpp.

Print a task ended message.

Print "Done." and go to next line.

Definition at line 231 of file ApplicationTools.cpp.

void ApplicationTools::displayTime ( const std::string &  msg) [static]

Display the current timer value to the 'message' stream.

Parameters:
msgMessage to display before time.

Definition at line 297 of file ApplicationTools.cpp.

Referenced by bpp::BppApplication::done().

void ApplicationTools::displayUnlimitedGauge ( size_t  iter,
const std::string &  mes = "" 
) [static]

Display a gauge for unefined amount of iterations.

Show progress status.

 for(size_t i = 0; i < 1000; i++)
 {
   ApplicationTools::displayUnlimitedGauge(i);
   //Perform time consuming task...
 }

will result in something like:

     * - 1
     * / 2
     * - 3
     * \ 4
     * - 5
     * etc
     * 
Parameters:
iterThe current iteration number.
mesA message to print before the gauge.

Definition at line 274 of file ApplicationTools.cpp.

References bpp::TextTools::toString().

Referenced by bpp::AbstractOptimizer::optimize().

void ApplicationTools::displayWarning ( const std::string &  text) [static]

Print a warning message.

Parameters:
textThe text of the message.

Definition at line 219 of file ApplicationTools.cpp.

Referenced by bpp::DualityDiagram::compute_(), bpp::ContingencyTableTest::ContingencyTableTest(), getParameter(), and bpp::ReparametrizationFunctionWrapper::init_().

std::string ApplicationTools::getAFilePath ( const std::string &  parameter,
std::map< std::string, std::string > &  params,
bool  isRequired = true,
bool  mustExist = true,
const std::string &  suffix = "",
bool  suffixIsOptional = false 
) throw (Exception) [static]

Get a file path.

Parameters:
parameterThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
isRequiredTell if this path is strictly required or is optional (in the first case, if the parameter is not found, the programm will send an error and exit).
mustExistTell if the corresponding file must already exist.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
Exceptions:
ExceptionIf no file path is specified and isRequired is true, or the file does not exist and mustExist is set to true.

Definition at line 71 of file ApplicationTools.cpp.

References bpp::FileTools::fileExists().

bool ApplicationTools::getBooleanParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
bool  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [static]

Get a boolean parameter.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 163 of file ApplicationTools.cpp.

References bpp::TextTools::toString().

Referenced by bpp::BppApplication::BppApplication().

double ApplicationTools::getDoubleParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
double  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [static]

Get a double parameter.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 95 of file ApplicationTools.cpp.

References bpp::TextTools::toDouble(), and bpp::TextTools::toString().

Referenced by bpp::BppODiscreteDistributionFormat::initialize_().

int ApplicationTools::getIntParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
int  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [static]

Get an integer parameter.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 121 of file ApplicationTools.cpp.

References bpp::TextTools::toInt(), and bpp::TextTools::toString().

template<class T >
static T bpp::ApplicationTools::getParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [inline, static]

Get a parameter.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 229 of file ApplicationTools.h.

References displayWarning(), parameterExists(), and bpp::TextTools::toString().

std::string ApplicationTools::getStringParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
const std::string &  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [static]

Get a string parameter.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 142 of file ApplicationTools.cpp.

Referenced by bpp::NumCalcApplicationTools::getParameterGrid(), and getVectorParameter().

double ApplicationTools::getTime ( ) [static]

Get the current timer value.

Returns:
The number of seconds from when timer was started.

Definition at line 321 of file ApplicationTools.cpp.

template<class T >
static std::vector<T> bpp::ApplicationTools::getVectorParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
char  separator,
const std::string &  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [inline, static]

Get a vector.

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
separatorThe character used to delimit values.
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 288 of file ApplicationTools.h.

References getStringParameter(), bpp::TextTools::isEmpty(), bpp::NestedStringTokenizer::nextToken(), bpp::StringTokenizer::numberOfRemainingTokens(), and bpp::TextTools::toString().

template<class T >
static std::vector<T> bpp::ApplicationTools::getVectorParameter ( const std::string &  parameterName,
std::map< std::string, std::string > &  params,
char  separator,
char  rangeOperator,
const std::string &  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
) [inline, static]

Get a vector.

Similar to getVectorParameter, but dedicated to numerical values. It allows the possibility to set range of values, which will be incremented by 1 (like the : operator in R).

Parameters:
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
separatorThe character used to delimit values.
rangeOperatorThe character used to delimit ranges (the + 1 operator must be available for T).
defaultValueThe default value to use if the parameter is not found.
suffixA suffix to be applied to the parameter name.
suffixIsOptionalTell if the suffix is absolutely required.
warnTell if a warning must be sent in case the parameter is not found.
Returns:
The corresponding value.

Definition at line 330 of file ApplicationTools.h.

References getStringParameter(), bpp::StringTokenizer::nextToken(), bpp::StringTokenizer::numberOfRemainingTokens(), and bpp::TextTools::toString().

bool ApplicationTools::parameterExists ( const std::string &  parameterName,
std::map< std::string, std::string > &  params 
) [static]

Tells if a parameter have been specified.

Parameters:
parameterNameThe name of the parameter.
paramsThe parameter list.
Returns:
True is the parameter of specified name is in the list.

Definition at line 62 of file ApplicationTools.cpp.

References bpp::TextTools::isEmpty().

Referenced by getParameter().

static void bpp::ApplicationTools::startTimer ( ) [inline, static]

Starts the timer.

Definition at line 491 of file ApplicationTools.h.

References startTime.


Member Data Documentation

The output stream where errors have to be displayed.

Definition at line 97 of file ApplicationTools.h.

Referenced by bpp::ReparametrizationFunctionWrapper::fireParameterChanged().

bool ApplicationTools::interactive = true [static]

Tell if the program is interactive (typically run in foreground). Default to yes.

Definition at line 125 of file ApplicationTools.h.

Referenced by bpp::BppApplication::BppApplication().

The output stream where messages have to be displayed.

Definition at line 101 of file ApplicationTools.h.

Referenced by bpp::MetaOptimizer::doStep().

time_t ApplicationTools::startTime [static]

Timer variable.

Definition at line 110 of file ApplicationTools.h.

Referenced by startTimer().

float ApplicationTools::terminalSplit = 0.5 [static]

The fraction of terminal width dedicated to messages.

Definition at line 120 of file ApplicationTools.h.

Referenced by displayResult().

size_t ApplicationTools::terminalWidth = 80 [static]

The width of the output terminal (in character).

Definition at line 115 of file ApplicationTools.h.

Referenced by displayResult().

The output stream where warnings have to be displayed.

Definition at line 105 of file ApplicationTools.h.

Referenced by bpp::RescaledHmmLikelihood::computeForward_(), and bpp::BppODiscreteDistributionFormat::initialize_().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Friends