ModelManager Class Reference

Manages a collection of ModelComponent objects forming a model. More...

#include <Component/ModelManager.H>

Inheritance diagram for ModelManager:
Inheritance graph
[legend]
Collaboration diagram for ModelManager:
Collaboration graph
[legend]

List of all members.

Classes

struct  Impl
 This is the internal implementation struct for ModelManager. More...

Public Member Functions

virtual void paramChanged (ModelParamBase *const param, const bool valueChanged, ParamClient::ChangeStatus *status)
 Intercept people changing our ModelParam's.
Constructors and Destructors

 ModelManager (const std::string &descrName="The Model", const std::string &tagName="model", const bool loadCfgOpt=true, const bool saveCfgOpt=true, const bool autoLoadCfg=true)
 Default constructor.
virtual ~ModelManager ()
 Virtual destructor ensures proper destruction of derived classes.
bool debugMode () const
 Returns true if we are running in debug mode.
Model option database

void allowOptions (const int mask)
 Only allow ModelOptionDef's that match the given mask.
virtual void requestOption (OptionedModelParam &p, const bool useMyVal=false)
 ModelComponent objects may request a command-line option here.
virtual void unRequestOption (OptionedModelParam &p)
 Request the removal of a param from the command-line options.
virtual void requestOptionAlias (const ModelOptionDef *def)
 Users may request model option aliases.
virtual void setOptionValString (const ModelOptionDef *def, const std::string &val)
 Set an option value.
virtual std::string getOptionValString (const ModelOptionDef *def)
 Get an option value.
virtual bool isOptionRequested (const ModelOptionDef *def) const
 Check if anybody has requested the given option.
Alternative access to internal options

These functions may be useful for alternative methods of displaying the help for command-line options (e.g. in the Qt ModelManagerWizard).

virtual uint numOptionDefs () const
 Get the number of ModelOptionDef objects in our list.
virtual uint getOptionDefs (const ModelOptionDef **arr, uint narr)
 Get ModelOptionDef objects from our list, up to the size of the given array.
virtual const ModelOptionDeffindOptionDef (const char *name) const
 Get the ModelOptionDef for the given name.
virtual bool isOptionDefUsed (const ModelOptionDef *def) const
 Query if the given ModelOptionDef is used in this program.
Command-line parsing

bool parseCommandLine (const int argc, const char **argv, const char *usage, const int minarg, const int maxarg)
 Parse all command-line options and set our params.
bool parseCommandLine (const int argc, char *const *argv, const char *usage, const int minarg, const int maxarg)
 Overload that takes a argv array of non-const strings.
bool parseCommandLineCore (const int argc, const char **argv)
 The core of parseCommandLine().
bool parseCommandLineCore (const char *args)
uint numExtraArgs () const
 Get the number of available extra args.
std::string getExtraArg (const uint num) const
 Get an extra arg, by number.
template<class T >
getExtraArgAs (const uint num) const
 Get an extra arg converted to type T.
void clearExtraArgs ()
 Clear our list of leftover command-line arguments.
Persistent ModelParamBase management

void loadConfig ()
 Load all our ModelParamBase values from file.
void loadConfig (const std::string &fname)
 Explicitly load all our ModelParams from file.
void saveConfig () const
 Save all our ModelParams to file.
void saveConfig (const std::string &fname) const
 Explicitly save all our ModelParams to file.

Protected Member Functions

void setFPE (bool val)
void unRequestTestMode ()
void unRequestUsingFPE ()
virtual void start1 ()
 This is called from within start() before the subcomponents start.
virtual void start2 ()
 This is called from within start() after the subcomponents have started.
virtual void stop1 ()
 This is called from within stop() before the subcomponents stop.

Detailed Description

Manages a collection of ModelComponent objects forming a model.

ModelManager should be implemented once in each executable, and will facilitate the management of a collection of ModelComponent objects, load/save of tunable ModelParamBase parameters that may be in those objects, and generation/parsing of command-line options. ModelComponent objects must manually register with the ModelManager to fall under its management. Typically, when a ModelComponent has subcomponents, only the top-level component should be registered with the ModelManager, as all ModelParamBase access functions will recurse through the ModelComponent's subcomponents. See test-model.C and test-audioGrab.C for simple examples of how to write executables that are managed by ModelManager.

Definition at line 62 of file ModelManager.H.


Constructor & Destructor Documentation

ModelManager::ModelManager ( const std::string descrName = "The Model",
const std::string tagName = "model",
const bool  loadCfgOpt = true,
const bool  saveCfgOpt = true,
const bool  autoLoadCfg = true 
)

Default constructor.

Parameters:
descrName descriptine name
tagName tag name (see ModelComponent.H)
loadCfgOpt create a --load-config-from=X option if true
saveCfgOpt create a --save-config-to=X option if true
autoLoadCfg try to load config from ~/.execname if true

Definition at line 132 of file ModelManager.C.

References ModelManager::Impl::autoLoadConfig, ModelManager::Impl::didSave, OModelParam< T >::getOptionDef(), invt_install_fancy_terminate(), ModelManager::Impl::paramDebugMode, ModelManager::Impl::paramFpuPrecision, ModelManager::Impl::paramFpuRoundingMode, ModelManager::Impl::paramLoadConfigFname, ModelManager::Impl::paramSaveConfigFname, requestOption(), ModelComponent::setManager(), and setOptionValString().

ModelManager::~ModelManager (  )  [virtual]

Virtual destructor ensures proper destruction of derived classes.

Definition at line 194 of file ModelManager.C.

References ModelComponent::managerDestroyed(), ModelComponent::started(), and ModelComponent::stop().


Member Function Documentation

void ModelManager::allowOptions ( const int  mask  ) 

Only allow ModelOptionDef's that match the given mask.

This mask is used to filter requestOption() calls -- if the ModelOptionDef's filterFlag isn't part of ModelManager's current mask, then the requestOption() is ignored.

Definition at line 219 of file ModelManager.C.

References CmdlineOptionManager::allowOptions(), and ASSERT.

Referenced by main().

void ModelManager::clearExtraArgs (  ) 

Clear our list of leftover command-line arguments.

Definition at line 615 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::clearExtraArgs().

Referenced by MexModelManager::parseMexCommandLine().

bool ModelManager::debugMode (  )  const

Returns true if we are running in debug mode.

Debug mode can be invoked via the --debug command-line option. Programs can use this function to do additional debug outputs if the manager is in debug mode. In addition, we will generate a debug printout of all model parameters just before start() and we will force the log level to LOG_DEBUG (see log.H) if debug mode is on.

Definition at line 601 of file ModelManager.C.

References ASSERT, OModelParam< T >::getVal(), and ModelManager::Impl::paramDebugMode.

const ModelOptionDef * ModelManager::findOptionDef ( const char *  name  )  const [virtual]

Get the ModelOptionDef for the given name.

Aborts if there is no such ModelOptionDef.

Implements OptionManager.

Definition at line 373 of file ModelManager.C.

References CmdlineOptionManager::findOptionDef().

string ModelManager::getExtraArg ( const uint  num  )  const

Get an extra arg, by number.

Definition at line 352 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::getExtraArg().

Referenced by getExtraArgAs(), main(), MexModelManager::parseMexCommandLine(), and submain().

template<class T >
T ModelManager::getExtraArgAs ( const uint  num  )  const [inline]

Get an extra arg converted to type T.

Definition at line 326 of file ModelManager.H.

References convertFromString(), and getExtraArg().

Referenced by main(), and submain().

uint ModelManager::getOptionDefs ( const ModelOptionDef **  arr,
uint  narr 
) [virtual]

Get ModelOptionDef objects from our list, up to the size of the given array.

Returns the number of array entries that were actually filled with valid ModelOptionDef pointers.

Implements OptionManager.

Definition at line 366 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::getOptionDefs().

string ModelManager::getOptionValString ( const ModelOptionDef def  )  [virtual]

Get an option value.

The value returned here is the value currently held by the ModelManager. This value is updated in three cases: 1) when a new component requests the option using requestOption() with useMyVal true; 2) when setOptionValString() is called, and 3) when the command-line arguments are parsed by ParseCommandLine, if a value for this option has been specified in the command line. If ModelComponent objects modify their local ModelParam values for their local parameter with this name, the value returned here will not be affected.

Implements OptionManager.

Definition at line 263 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::getOptionValString().

Referenced by main().

bool ModelManager::isOptionDefUsed ( const ModelOptionDef def  )  const [virtual]

Query if the given ModelOptionDef is used in this program.

Implements OptionManager.

Definition at line 379 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::isOptionDefUsed().

bool ModelManager::isOptionRequested ( const ModelOptionDef def  )  const [virtual]

Check if anybody has requested the given option.

Implements OptionManager.

Definition at line 270 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::isOptionRequested().

void ModelManager::loadConfig ( const std::string fname  ) 

Explicitly load all our ModelParams from file.

This function will always load the params from the given file, even is --load-config-from=X was not specified.

Definition at line 395 of file ModelManager.C.

References ModelComponent::readParamsFrom().

void ModelManager::loadConfig (  ) 

Load all our ModelParamBase values from file.

This function is automatically called during parseCommandLine() if --load-config-from=XX option is encountered or autoload was specified in our constructor (thus ensuring that command-line options encountered after the option specifying which config file to load are applied after the config file has been loaded). It may also be called manually. This function is a no-op if no filename is in our loadConfigFile internal ModelParam (this parameter can be set by the --load-config-from=X command-line option). Otherwise, load the file (which should be a ParamMap) and configure all our parameters.

Definition at line 386 of file ModelManager.C.

References ASSERT, rutz::shared_ptr< T >::get(), and ModelManager::Impl::paramLoadConfigFname.

Referenced by main(), and paramChanged().

uint ModelManager::numExtraArgs (  )  const

Get the number of available extra args.

Definition at line 345 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::numExtraArgs().

Referenced by main(), and MexModelManager::parseMexCommandLine().

uint ModelManager::numOptionDefs (  )  const [virtual]

Get the number of ModelOptionDef objects in our list.

Implements OptionManager.

Definition at line 359 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::numOptionDefs().

void ModelManager::paramChanged ( ModelParamBase *const   param,
const bool  valueChanged,
ParamClient::ChangeStatus status 
) [virtual]
bool ModelManager::parseCommandLine ( const int  argc,
char *const *  argv,
const char *  usage,
const int  minarg,
const int  maxarg 
)

Overload that takes a argv array of non-const strings.

bool ModelManager::parseCommandLine ( const int  argc,
const char **  argv,
const char *  usage,
const int  minarg,
const int  maxarg 
)

Parse all command-line options and set our params.

This function will create a set of command-line options from our current collection of OptionedModelParam objects. It will then parse the command-line arguments and set the various option values accordingly, using a call to setOptionValString(). Any left-over args (i.e., not formatted as options) will be available through getExtraArg(), and if the number of such extra args is not within the specified minarg/maxarg bounds, a usage message and optionlist will be printed out (also printed out if a '--help' option is encountered). To decide which options should be exported by your various ModelComponent objects, typically you will call exportOptions() on the manager, which will propagate the call to all registered components. Here we have an additional default behavior that if exportOptions() has not been called on us, we will call it with an order to export all options, just before we start parsing the command-line. So, if you want to export all options, you don't have to do anything; if you want to only export special classes of options, then you will need to call exportOptions() manually some time before you parse the command-line. Returns true upon fully successful parse.

Parameters:
argc the number of command-line args
argv the command-line args
usage a textual description of the expected usage format, describing only what the non-option arguments should be (e.g., "<input.ppm> <output.ppm>")
minarg the minimum number of non-optional arguments
maxarg the maximum number of non-optional args, or -1 if unbounded

Definition at line 277 of file ModelManager.C.

References ASSERT, ModelManager::Impl::autoLoadConfig, ModelComponent::exportOptions(), ModelComponent::hasBeenExported(), MC_RECURSE, CmdlineOptionManager::parseCommandLine(), and ModelComponent::readParamsFrom().

Referenced by main(), lobot::App::parse_command_line(), and submain().

bool ModelManager::parseCommandLineCore ( const int  argc,
const char **  argv 
)

The core of parseCommandLine().

This may be called recursively to handle command-line aliases. Note: beware that the first element of argv will be ignored (as it usually is the program name).

Definition at line 331 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::parseCommandLineCore().

Referenced by MexModelManager::parseMexCommandLine().

void ModelManager::requestOption ( OptionedModelParam p,
const bool  useMyVal = false 
) [virtual]

ModelComponent objects may request a command-line option here.

Parameters:
p the ModelParam data member of c corresponding to the option
useMyVal if true, we will do a setOptionValString() using the current value of the parameter, so that this value will be propagated to all OptionedModelParam objects that have requested this option and will become the new command-line default value. If false, the ModelParam p will be updated to the current value of the option. So, use useMyVal=true if your component wishes to push its current parameter value as the default for everyone using this option, and use false if your component wishes to use the current default instead.

Implements OptionManager.

Definition at line 226 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::requestOption().

Referenced by ModelManager().

void ModelManager::requestOptionAlias ( const ModelOptionDef def  )  [virtual]

Users may request model option aliases.

Parameters:
name the name of the option alias, from ModelOptionDefs.C

Implements OptionManager.

Definition at line 248 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::requestOptionAlias().

void ModelManager::saveConfig ( const std::string fname  )  const

Explicitly save all our ModelParams to file.

This function will always save the params to the given file, even is --save-config-to=X was not specified.

Definition at line 412 of file ModelManager.C.

References ModelComponent::writeParamsTo().

void ModelManager::saveConfig (  )  const

Save all our ModelParams to file.

This function is a no-op if no filename is in our saveConfigFile internal ModelParam (this parameter can be set by the --save-config-to=X command-line option). Otherwise, save all our params to the file as a ParamMap. Returns true on success. You should typically call this either some time before you start() your model, or after you have stop()'ed it. NOTE: There is a default behavior which is to call this as the first thing in start(), i.e., just before all our registered components will be started, unless you have manually called it yourself earlier, in which case it is not called again.

Definition at line 403 of file ModelManager.C.

References ASSERT, rutz::shared_ptr< T >::get(), and ModelManager::Impl::paramSaveConfigFname.

Referenced by start1().

void ModelManager::setOptionValString ( const ModelOptionDef def,
const std::string val 
) [virtual]

Set an option value.

All ModelParam objects associated this ModelOptionDef will be updated with the new value. Will throw a fatal error if the model has been started (see ModelComponent::start()).

Implements OptionManager.

Definition at line 255 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::setOptionValString().

Referenced by main(), ModelManager(), paramChanged(), lobot::App::parse_command_line(), and submain().

void ModelManager::start1 (  )  [protected, virtual]
void ModelManager::start2 (  )  [protected, virtual]

This is called from within start() after the subcomponents have started.

Reimplemented from ModelComponent.

Definition at line 454 of file ModelManager.C.

References ASSERT, and CpuTimer::reset().

void ModelManager::stop1 (  )  [protected, virtual]

This is called from within stop() before the subcomponents stop.

Reimplemented from ModelComponent.

Definition at line 462 of file ModelManager.C.

References ASSERT, CpuTimer::mark(), and CpuTimer::report().

void ModelManager::unRequestOption ( OptionedModelParam p  )  [virtual]

Request the removal of a param from the command-line options.

Parameters:
p the ModelParam data member of c corresponding to the option

Implements OptionManager.

Definition at line 234 of file ModelManager.C.

References ASSERT, and CmdlineOptionManager::unRequestOption().


The documentation for this class was generated from the following files:
Generated on Sun May 8 08:43:37 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3