bpp-core3  3.0.0
bpp::ApplicationTools Class Reference

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

#include <Bpp/App/ApplicationTools.h>

Public Member Functions

 ApplicationTools ()
 
virtual ~ApplicationTools ()
 

Static Public Member Functions

static bool parameterExists (const std::string &parameterName, const std::map< std::string, std::string > &params)
 Tells if a parameter have been specified. More...
 
static bool parameterExists (const std::string &parameterName, std::vector< std::string > &params)
 
static std::vector< std::string > matchingParameters (const std::string &pattern, const std::map< std::string, std::string > &params)
 Returns a vector of parameter names that match a given pattern. More...
 
static std::vector< std::string > matchingParameters (const std::string &pattern, std::vector< std::string > &params)
 
static double getDoubleParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, double defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a double parameter. More...
 
static int getIntParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, int defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get an integer parameter. More...
 
static std::string getStringParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a string parameter. More...
 
static bool getBooleanParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, bool defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a boolean parameter. More...
 
template<class T >
static T getParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, T defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a parameter. More...
 
static std::string getAFilePath (const std::string &parameter, const std::map< std::string, std::string > &params, bool isRequired=true, bool mustExist=true, const std::string &suffix="", bool suffixIsOptional=false, const std::string &defaultPath="none", int warn=0)
 Get a file path. More...
 
template<class T >
static std::vector< T > getVectorParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a vector. More...
 
template<class T >
static std::vector< std::vector< T > > getVectorOfVectorsParameter (const std::string &parameterName, const std::map< std::string, std::string > &params, char separator, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
 Get a vector of vectors. More...
 
template<class T >
static std::vector< T > getVectorParameter (const std::string &parameterName, const 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. More...
 
template<class T >
static RowMatrix< T > getMatrixParameter (const std::string &parameterName, const 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 RowMatrix. The input is made of embedded parenthesis, such as ((1,2),(3,4)), where the matrix is filled by lines. Here, the matrix would be:

\[ \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix} \]

. More...

 
static void startTimer ()
 Starts the timer. More...
 
static void displayTime (const std::string &msg)
 Display the current timer value to the 'message' stream. More...
 
static double getTime ()
 Get the current timer value. More...
 
Output methods.
static void displayMessage (const std::string &text)
 Print a message. More...
 
static void displayError (const std::string &text)
 Print an error message. More...
 
static void displayWarning (const std::string &text)
 Print a warning message. More...
 
static void displayTask (const std::string &text, bool eof=false)
 Print a task message. More...
 
static void displayTaskDone ()
 Print a task ended message. More...
 
template<class T >
static void displayResult (const std::string &text, const T &result)
 Print a result message. More...
 
static void displayBooleanResult (const std::string &text, bool result)
 Print a boolean result message ("yes" or "no"). More...
 
static void displayGauge (size_t iter, size_t total, char symbol='>', const std::string &mes="")
 Display a gauge. More...
 
static void displayUnlimitedGauge (size_t iter, const std::string &mes="")
 Display a gauge for unefined amount of iterations. More...
 

Static Public Attributes

static std::shared_ptr< OutputStreamerror
 The output stream where errors have to be displayed. More...
 
static std::shared_ptr< OutputStreammessage
 The output stream where messages have to be displayed. More...
 
static std::shared_ptr< OutputStreamwarning
 The output stream where warnings have to be displayed. More...
 
static time_t startTime
 Timer variable. More...
 
static size_t terminalWidth = 80
 The width of the output terminal (in character). More...
 
static float terminalSplit = 0.5
 The fraction of terminal width dedicated to messages. More...
 
static bool interactive = true
 Tell if the program is interactive (typically run in foreground). Default to yes. More...
 
static int warningLevel = 0
 Specify the amount of warning to display. More...
 

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 55 of file ApplicationTools.h.

Constructor & Destructor Documentation

◆ ApplicationTools()

bpp::ApplicationTools::ApplicationTools ( )
inline

Definition at line 97 of file ApplicationTools.h.

◆ ~ApplicationTools()

virtual bpp::ApplicationTools::~ApplicationTools ( )
inlinevirtual

Definition at line 98 of file ApplicationTools.h.

Member Function Documentation

◆ displayBooleanResult()

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

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 580 of file ApplicationTools.h.

References displayGauge(), displayResult(), and displayUnlimitedGauge().

◆ displayError()

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

Print an error message.

Parameters
textThe text of the message.

Definition at line 239 of file ApplicationTools.cpp.

Referenced by getMatrixParameter().

◆ displayGauge()

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++)
{
//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 271 of file ApplicationTools.cpp.

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

Referenced by bpp::FunctionTools::computeGrid(), and displayBooleanResult().

◆ displayMessage()

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

◆ displayResult()

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

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 568 of file ApplicationTools.h.

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

Referenced by bpp::AbstractParameterAliasable::aliasParameters(), bpp::BppApplication::BppApplication(), displayBooleanResult(), bpp::NumCalcApplicationTools::getParameterGrid(), bpp::BppODiscreteDistributionFormat::initialize_(), and bpp::BppODiscreteDistributionFormat::readDiscreteDistribution().

◆ displayTask()

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 251 of file ApplicationTools.cpp.

Referenced by getMatrixParameter().

◆ displayTaskDone()

void ApplicationTools::displayTaskDone ( )
static

Print a task ended message.

Print "Done." and go to next line.

Definition at line 263 of file ApplicationTools.cpp.

Referenced by getMatrixParameter().

◆ displayTime()

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 341 of file ApplicationTools.cpp.

Referenced by bpp::BppApplication::done(), and startTimer().

◆ displayUnlimitedGauge()

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++)
{
//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 317 of file ApplicationTools.cpp.

References bpp::TextTools::toString().

Referenced by displayBooleanResult(), and bpp::AbstractOptimizer::optimize().

◆ displayWarning()

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

◆ getAFilePath()

string ApplicationTools::getAFilePath ( const std::string &  parameter,
const std::map< std::string, std::string > &  params,
bool  isRequired = true,
bool  mustExist = true,
const std::string &  suffix = "",
bool  suffixIsOptional = false,
const std::string &  defaultPath = "none",
int  warn = 0 
)
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.
defaultPathPath to use if no argument is provided.
warnTell if a warning must be sent in case the parameter is not found.
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 98 of file ApplicationTools.cpp.

References bpp::FileTools::fileExists().

Referenced by getParameter().

◆ getBooleanParameter()

bool ApplicationTools::getBooleanParameter ( const std::string &  parameterName,
const std::map< std::string, std::string > &  params,
bool  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
int  warn = 0 
)
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 179 of file ApplicationTools.cpp.

References bpp::TextTools::toString().

Referenced by bpp::BppApplication::BppApplication(), and getStringParameter().

◆ getDoubleParameter()

double ApplicationTools::getDoubleParameter ( const std::string &  parameterName,
const std::map< std::string, std::string > &  params,
double  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
int  warn = 0 
)
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 126 of file ApplicationTools.cpp.

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

Referenced by bpp::BppODiscreteDistributionFormat::initialize_(), and parameterExists().

◆ getIntParameter()

int ApplicationTools::getIntParameter ( const std::string &  parameterName,
const std::map< std::string, std::string > &  params,
int  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
int  warn = 0 
)
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 152 of file ApplicationTools.cpp.

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

Referenced by bpp::BppApplication::BppApplication(), and parameterExists().

◆ getMatrixParameter()

template<class T >
static RowMatrix<T> bpp::ApplicationTools::getMatrixParameter ( const std::string &  parameterName,
const std::map< std::string, std::string > &  params,
char  separator,
const std::string &  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
bool  warn = true 
)
inlinestatic

Get a RowMatrix. The input is made of embedded parenthesis, such as ((1,2),(3,4)), where the matrix is filled by lines. Here, the matrix would be:

\[ \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ \end{pmatrix} \]

.

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 472 of file ApplicationTools.h.

References bpp::RowMatrix< Scalar >::addRow(), displayError(), displayMessage(), displayTask(), displayTaskDone(), displayWarning(), getStringParameter(), bpp::StringTokenizer::hasMoreToken(), bpp::TextTools::isEmpty(), bpp::StringTokenizer::nextToken(), bpp::StringTokenizer::numberOfRemainingTokens(), and bpp::TextTools::toString().

◆ getParameter()

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

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 246 of file ApplicationTools.h.

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

◆ getStringParameter()

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

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 189 of file ApplicationTools.h.

References displayWarning(), getBooleanParameter(), and bpp::TextTools::isEmpty().

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

◆ getTime()

double ApplicationTools::getTime ( )
static

Get the current timer value.

Returns
The number of seconds from when timer was started.

Definition at line 365 of file ApplicationTools.cpp.

Referenced by startTimer().

◆ getVectorOfVectorsParameter()

template<class T >
static std::vector< std::vector<T> > bpp::ApplicationTools::getVectorOfVectorsParameter ( const std::string &  parameterName,
const std::map< std::string, std::string > &  params,
char  separator,
const std::string &  defaultValue,
const std::string &  suffix = "",
bool  suffixIsOptional = true,
int  warn = 0 
)
inlinestatic

Get a vector of vectors.

Parameters
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
separatorThe character used to delimit values (cannot be a space character).
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.
Exceptions
ExceptionIf a space character is used as separator.
Returns
The corresponding value.

Definition at line 352 of file ApplicationTools.h.

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

◆ getVectorParameter() [1/2]

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

Get a vector.

Parameters
parameterNameThe name of the corresponding parameter.
paramsThe attribute map where options may be found.
separatorThe character used to delimit values (cannot be a space character).
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.
Exceptions
ExceptionIf a space character is used as separator.
Returns
The corresponding value.

Definition at line 311 of file ApplicationTools.h.

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

◆ getVectorParameter() [2/2]

template<class T >
static std::vector<T> bpp::ApplicationTools::getVectorParameter ( const std::string &  parameterName,
const 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 
)
inlinestatic

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 412 of file ApplicationTools.h.

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

◆ matchingParameters() [1/2]

vector< string > ApplicationTools::matchingParameters ( const std::string &  pattern,
const std::map< std::string, std::string > &  params 
)
static

Returns a vector of parameter names that match a given pattern.

Only "*" wildcard is implemented now.

Parameters
patternThe pattern.
paramsThe parameter list.
Returns
a vector of matching names.

Definition at line 28 of file ApplicationTools.cpp.

References bpp::StringTokenizer::hasMoreToken(), and bpp::StringTokenizer::nextToken().

Referenced by parameterExists().

◆ matchingParameters() [2/2]

vector< string > ApplicationTools::matchingParameters ( const std::string &  pattern,
std::vector< std::string > &  params 
)
static

◆ parameterExists() [1/2]

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

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 108 of file ApplicationTools.h.

References bpp::TextTools::isEmpty().

Referenced by getParameter().

◆ parameterExists() [2/2]

static bool bpp::ApplicationTools::parameterExists ( const std::string &  parameterName,
std::vector< std::string > &  params 
)
inlinestatic

Definition at line 116 of file ApplicationTools.h.

References getDoubleParameter(), getIntParameter(), and matchingParameters().

◆ startTimer()

static void bpp::ApplicationTools::startTimer ( )
inlinestatic

Starts the timer.

Definition at line 640 of file ApplicationTools.h.

References displayTime(), getTime(), and startTime.

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

Member Data Documentation

◆ error

std::shared_ptr< OutputStream > ApplicationTools::error
static

The output stream where errors have to be displayed.

Definition at line 61 of file ApplicationTools.h.

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

◆ interactive

bool ApplicationTools::interactive = true
static

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

Definition at line 89 of file ApplicationTools.h.

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

◆ message

std::shared_ptr< OutputStream > ApplicationTools::message
static

The output stream where messages have to be displayed.

Definition at line 65 of file ApplicationTools.h.

Referenced by bpp::MetaOptimizer::doStep(), and bpp::BppApplication::help().

◆ startTime

time_t ApplicationTools::startTime
static

Timer variable.

Definition at line 74 of file ApplicationTools.h.

Referenced by startTimer().

◆ terminalSplit

float ApplicationTools::terminalSplit = 0.5
static

The fraction of terminal width dedicated to messages.

Definition at line 84 of file ApplicationTools.h.

Referenced by displayResult().

◆ terminalWidth

size_t ApplicationTools::terminalWidth = 80
static

The width of the output terminal (in character).

Definition at line 79 of file ApplicationTools.h.

Referenced by displayResult().

◆ warning

std::shared_ptr< OutputStream > ApplicationTools::warning
static

The output stream where warnings have to be displayed.

Definition at line 69 of file ApplicationTools.h.

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

◆ warningLevel

int ApplicationTools::warningLevel = 0
static

Specify the amount of warning to display.

Definition at line 94 of file ApplicationTools.h.

Referenced by bpp::BppApplication::BppApplication(), and getParameter().


The documentation for this class was generated from the following files: