
#include <Component/ModelComponent.H>
Inheritance diagram for ModelComponent:


The goal is to provide a unified interface by which tunable parameters can be set, read, or exported as command-line options or in parameter maps or files, so that executables using a variable collection of model components can easily be built and configured using command-line options and config files. To this end, ModelComponent provides a set of facilities that allow it to hold a number of ModelParamBase data members. A ModelComponent also holds a list of sub-components that are ModelComponent as well, and many of the ModelComponent functions will first apply to the component itself, then recursively to its subcomponents. The ModelParamBase are the tunable parameters that are persistent and may be associated with a command-line option; see ModelParam.H for details. A ModelComponent is attached to a OptionManager that will handle option parsing and ModelParamBase load/save. A ModelComponent may request its master to associate some of its ModelParamBase data members with command-line options. Command-line option definitions are typically given in one master file per source directory (e.g. Devices/DeviceOpts.C, Media/MediaOpts.C, Neuro/NeuroOpts.C), although some components may also define private command-line options internally in their own .C files. Command-line parsing will set the value of ALL ModelParamBase objects in all ModelComponent objects and their subcomponents that have requested the option to the value specified on the command line. So this mechanism should be used sparingly, for parameters that may be used across a variety of models and a variety of ModelComponent derivatives.
There is one trick to be aware of with respect to the basic operation and setup of ModelComponent. For speed reasons, we want our ModelParam parameters to be true data members, so that we can access them as efficiently as normal data members (rather than having to first look them up by name). On the other hand, the ModelComponent must also be able to produce a list of all its ModelParam members, so that they can be saved to a ParamMap. To resolve this, ModelComponent both holds each ModelParam as a regular data member, and a list of pointers to all its ModelParam members. In addition, this list keeps track of which model params are affiliated with command-line options, as well as which should actually be exported on the command-line. This list is automatically maintained when the ModelParam objects are constructed and destroyed, so that the user does not have to worry about it.
Definition at line 125 of file ModelComponent.H.
Public Member Functions | |
Constructors and Destructors | |
| ModelComponent (OptionManager &mgr, const std::string &descrName, const std::string &tagName) | |
| Constructor. | |
| ModelComponent (const std::string &descrName, const std::string &tagName) | |
| Constructor overload without an OptionManager. | |
| virtual | ~ModelComponent () |
| Virtual destructor ensures proper destruction of derived classes. | |
| void | start () |
| Start the component. | |
| bool | started () const |
| Did we already get a call to start()? | |
| void | stop () |
| Stop the component. | |
| void | managerDestroyed () |
| Signify that our ModelManager just got destroyed. | |
| OptionManager & | getManager () const |
| Get our manager. | |
Name functions | |
| std::string | descriptiveName () const |
| Get a human-readable descriptive name for this component. | |
| void | setDescriptiveName (const std::string &name) |
| Change this component's descriptive name. | |
| std::string | tagName () const |
| Get a programmer-usable tag name for this object. | |
| void | setTagName (const std::string &name) |
| Change this component's tag name. | |
Sub-Component management functions | |
| ModelComponent * | getParent () const |
| Get this component's parent ModelComponent, or NULL if it has no parent. | |
| ModelComponent * | getRootObject () |
| Get this component's root parent (the parent of all parents in our tree). | |
| const ModelComponent * | getRootObject () const |
| Get this component's root parent (the parent of all parents in our tree). | |
| uint | addSubComponent (const nub::ref< ModelComponent > &subc) |
| Add a subcomponent to our internal list; return its index. | |
| int | removeSubComponent (const ModelComponent &subc, bool removeall=false) |
| Remove a subcomponent from our internal list, by address. | |
| void | removeSubComponent (const nub::ref< ModelComponent > &subc) |
| Remove a subcomponent by nub:ref. | |
| void | removeSubComponent (const uint idx) |
| Remove a subcomponent from our internal list, by index. | |
| void | removeSubComponent (const std::string &tagname) |
| Remove a subcomponent from our internal list, by tagName. | |
| void | removeAllSubComponents () |
| Remove all our subcomponents. | |
| nub::ref< ModelComponent > | subComponent (const uint idx) const |
| Access a sub-component by index. | |
| nub::ref< ModelComponent > | subComponent (const std::string &tagname, const ModelFlag flags=0) const |
| Access a sub-component by tagname. | |
| uint | numSubComp () const |
| Get current number of subcomponents. | |
| bool | hasSubComponent (const std::string &tagname, const ModelFlag flags=0) const |
| Return true if we have a subcomponent by that tagname. | |
| bool | hasSubComponent (const nub::soft_ref< ModelComponent > &c, const ModelFlag flags=0) const |
| Return true if we have the pointee as subcomponent. | |
| void | printout (std::ostream &s, const std::string &prefix="") const |
| Show our ModelParam internals and those of our subcomponents. | |
| void | reset (const ModelFlag flags) |
| Reset a model component and propagate the request to the sub-components. | |
| void | save (const ModelComponentSaveInfo &sinfo, const ModelFlag flags=MC_RECURSE) |
| Save/display our current state. | |
Access to tunable ModelParam's | |
| void | exportOptions (const ModelFlag flags) |
| Export some of our ModelParam parameters as command-line-options. | |
| bool | hasModelParam (const std::string &name, const ModelFlag flags=0) const |
| Check for named param in this component (and possibly its subcomponents). | |
| void | setModelParamString (const std::string &name, const std::string &value, const ModelFlag flags=0) |
| Parse a parameter value given as string and set the param. | |
| template<class T> | |
| void | setModelParamVal (const std::string &name, const T &val, const ModelFlag flags=0) |
| Set a parameter value. | |
| void | setModelParamValAux (const std::string &name, const RefHolder &val, const ModelFlag flags) |
| Auxiliary implementation function for setModelParamVal(). | |
| std::string | getModelParamString (const std::string &name, const ModelFlag flags=0) const |
| Get a parameter value. | |
| template<class T> | |
| T | getModelParamVal (const std::string &name, const ModelFlag flags=0) const |
| Get a parameter value. | |
| void | getModelParamValAux (const std::string &name, RefHolder &val, const ModelFlag flags) const |
| Auxiliary implementation function for getModelParamVal(). | |
| bool | doRequestOption (const ModelOptionDef *opt, const bool useMyVal=false, const bool recurse=true, const bool warn=true) |
| External request that one of our ModelParam becomes an option. | |
| size_t | getNumModelParams () const |
| Return the number of params that we have. | |
| const ModelParamBase * | getModelParam (size_t i) const |
| Get non-const access to the i'th model parameter. | |
| ModelParamBase * | getModelParam (size_t i) |
| Get non-const access to the i'th model parameter. | |
ParamMap-based configuration functions | |
| void | readParamsFrom (const ParamMap &pmap, const bool noerr=true) |
| Read params from the ParamMap. | |
| void | writeParamsTo (ParamMap &pmap) const |
| Write params to the ParamMap. | |
ParamClient interface | |
| virtual void | registerParam (ModelParamBase *mp) |
| our parameters will register with us upon construction | |
| virtual void | registerOptionedParam (OptionedModelParam *mp, const ParamFlag flags) |
| Our parameters will register with us upon construction. | |
| virtual void | unregisterParam (const ModelParamBase *mp) |
| our parameters will un-register with us upon destruction | |
| virtual void | paramChanged (ModelParamBase *param, const bool valueChanged, ParamClient::ChangeStatus *status) |
| Called whenever a ModelParamBase has its value changed. | |
| void | forgetExports () |
| Clear our list of command-line options to be exported. | |
Protected Member Functions | |
| void | setManager (OptionManager &mgr) |
| Set our manager. | |
| bool | hasBeenExported () const |
| Check if our options have already been exported. | |
| 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. | |
| virtual void | stop2 () |
| This is called from within stop() after the subcomponents have stopped. | |
| virtual void | reset1 () |
| This is called from within reset() before the subcomponents are reset. | |
| virtual void | reset2 () |
| This is called from within reset() after the subcomponents are reset. | |
| virtual void | save1 (const ModelComponentSaveInfo &sinfo) |
| This is called from within save() before the subcomponents save. | |
| virtual void | save2 (const ModelComponentSaveInfo &sinfo) |
| This is called from within save() after the subcomponents have saved. | |
| ModelComponent () | |
| Default constructor without arguments. DO NOT USE! | |
| void | init (OptionManager &mgr, const std::string &descrName, const std::string &tagName) |
| Make sure you call this if you have used the default constructor. | |
Classes | |
| struct | Impl |
| Private "pimpl" implementation class for ModelComponent. More... | |
|
||||||||||||||||
|
Constructor. Typically, in the constructor you should perform all initializations that are independent of the values of your ModelParam data members. Since the command-line usually has not been parsed yet at the time you construct your various ModelComponent objects, however, the start() function below is provided to allow you to perform additional initializations after the command-line has been parsed and the ModelParam data members have been configured.
Definition at line 241 of file ModelComponent.C. References dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::PtrChecker::addPtr(), CLDEBUG, rutz::full_object_cast(), and dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::getChecker(). |
|
||||||||||||
|
Constructor overload without an OptionManager. In this case, you must call setManager() before using any other functions. In fact, if you aren't ModelManager, you probably shouldn't be using this constructor! (ModelManager needs it due to an implementation quirk related to multiple inheritance). Definition at line 252 of file ModelComponent.C. References dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::PtrChecker::addPtr(), rutz::full_object_cast(), and dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::getChecker(). |
|
|
Virtual destructor ensures proper destruction of derived classes.
Definition at line 274 of file ModelComponent.C. References CLDEBUG, dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::PtrChecker::forgetPtr(), rutz::full_object_cast(), dummy_namespace_to_avoid_gcc411_bug_ModelComponent_C::getChecker(), ModelComponent::Impl::pinfos, ModelComponent::Impl::started, stop(), and ModelComponent::Impl::subComps. |
|
|
Default constructor without arguments. DO NOT USE! This constructor exists only so that we don't have to explicitly call the parameterized ModelComponent constructor in each and every object that has a ModelComponent virtual base somewhere in its inheritance hierarchy. This constructor, however, will leave the object uninitialized and you must call init() before you can use the object. Here is what you should do when building an inheritance hierarchy that has ModelComponent as virtual base: 1) in the constructor of the class that virtually derives from ModelComponent, call init(). See for example Channels/ChannelBase; 2) in classes that concretely derive from the latter one, just construct as usual; see for example Channels/SingleChannel, Channels/BlueChannel, etc. Definition at line 260 of file ModelComponent.C. |
|
|
|
||||||||||||||||||||
|
External request that one of our ModelParam becomes an option. This is to allow external callers to selectively decide that some of our internal ModelParam parameters should be exported as command-line options. If recurse is true, will look for the ModelParam in all our subcomponents, and will link all matching ModelParam in us and our subcomponents to the option (so that if a command-line value is provided, it will affect all matching ModelParam objects in us and all our subcomponents). Will return false (and generate a warning message if warn is true) if we (or our subcomponents if recurse is true) don't have a ModelParam of the specified name. Will generate a fatal error if we have the ModelParam but no known command-line option exists for that name in ModelOptionDefs.C. Returns true if the ModelParam was found and added as a command-line option. Throws a fatal error is we are started. Definition at line 779 of file ModelComponent.C. References CLERROR, CLFATAL, ModelComponent::Impl::mgr, ModelOptionDef::name, ModelComponent::Impl::pinfos, OptionManager::requestOption(), ModelComponent::Impl::started, and ModelComponent::Impl::subComps. Referenced by main(). |
|
|
Export some of our ModelParam parameters as command-line-options. This should be called once on the model manager to decide which sets of ModelParam paremeters should become command-line options; the call will then recurse through all ModelComponent objects that have been registered with the manager. This is provided so that external meta-knowledge about what a collection of ModelComponent objects forming a computational model can be used to decide on which command-line options are relevant: for instance, if you build a model that has no OutputFrameSeries, then you will not be able to call saveResults() on your Brain or other objects, so you would not want to export command-line options that relate to which results should be saved. This is handled by the exportFlag member of each ModelOptionDef, which must match the ModelManager's current export mask (set by ModelManager::allowOptions()) in order for that option to actually be exported. HISTORICAL NOTE: It used to be that every subclass of ModelComponent would need to override exportOptions(), and explicitly request that each model param be exported. This is no longer the case -- instead, when each OModelParam is constructed in the subclass's constructor, it ends up calling registerOptionedParam() on the ModelComponent, which adds the param to our list of params, but also marks the param for command-line export whenever exportOptions() is finally called. For those rare cases where you do not want an OModelParam exported to the command-line, you can call forgetExports(), and then explicitly re-export any desired options using doRequestOption(). Definition at line 673 of file ModelComponent.C. References CLDEBUG, ModelComponent::Impl::hasBeenExported, MC_RECURSE, ModelComponent::Impl::mgr, ModelComponent::Impl::pinfos, OptionManager::requestOption(), and ModelComponent::Impl::subComps. Referenced by StereoChannel::buildSubChans(), ModelManager::parseCommandLine(), and MexModelManager::parseMexCommandLine(). |
|
|
Clear our list of command-line options to be exported.
Definition at line 935 of file ModelComponent.C. References ModelComponent::Impl::pinfos. |
|
|
|
Get non-const access to the i'th model parameter.
Definition at line 834 of file ModelComponent.C. References LFATAL, and ModelComponent::Impl::pinfos. |
|
|
Get non-const access to the i'th model parameter.
Definition at line 824 of file ModelComponent.C. References LFATAL, and ModelComponent::Impl::pinfos. Referenced by PrefsWindow::addPrefsForComponent(). |
|
||||||||||||
|
Get a parameter value. Because the parameter may exist both in us and in several of our subcomponents, we will stop and return the value of the first instance encountered, searching first in us, then recursively in our subcomponents (depth-first search, if flags contains MC_RECURSE), in the order in which they were added. If the parameter is not found in us (or in any of our subcomponents, if flags contains MC_RECURSE), this function will not return (will abort or throw an exception) -- to avoid such an error, call hasModelParam() first to check that the particular param exists. It is ok to call this on a started ModelComponent. Definition at line 755 of file ModelComponent.C. References ASSERT, CLFATAL, ModelComponent::Impl::findMatchingParams(), GVX_ERR_CONTEXT, MC_IGNORE_MISSING, and rutz::sfmt(). Referenced by ModelParamBatch::installValues(), ModelManagerControl::loadConfig(), and ModelManagerControl::saveConfig(). |
|
||||||||||||||||
|
Get a parameter value. This is slow, as it will search for the ModelParam by name. If you need efficient access to ModelParam values, you should define a wrapper method in your ModelComponent derivative. In normal operation, indeed, only the methods of the ModelComponent derivative should directly access the ModelParam, not external people. Because the parameter may exist both in us and in several of our subcomponents, we will stop and return the value of the first instance encountered, searching first in us, then recursively in our subcomponents (depth-first search, if flags contains MC_RECURSE), in the order in which they were added. If the parameter is not found in us (or in any of our subcomponents, if flags contains MC_RECURSE), this function will not return (will abort or throw an exception) -- to avoid such an error, call hasModelParam() first to check that the particular param exists. It is ok to call this on a started ModelComponent. Definition at line 683 of file ModelComponent.H. References getModelParamValAux(), and val. Referenced by DescriptorVec::buildDV(), DescriptorVec::buildParticleCountDV(), DescriptorVec::buildRawDV(), DescriptorVec::buildSingleChannelFV(), SimulationViewerCompress::getTraj(), and main(). |
|
||||||||||||||||
|
Auxiliary implementation function for getModelParamVal().
Definition at line 1031 of file ModelComponent.C. References ASSERT, CLFATAL, ModelComponent::Impl::findMatchingParams(), GVX_ERR_CONTEXT, MC_IGNORE_MISSING, and rutz::sfmt(). Referenced by getModelParamVal(). |
|
|
Return the number of params that we have.
Definition at line 818 of file ModelComponent.C. References ModelComponent::Impl::pinfos. Referenced by PrefsWindow::addPrefsForComponent(). |
|
|
Get this component's parent ModelComponent, or NULL if it has no parent. Internally, each component stores a weak smart pointer to its parent, while the parent stores a strong smart pointer to the child. This avoids unbreakable reference-counting cycles, but still lets us traverse the parent-child in both directions component tree using getParent() and subComponent(). Definition at line 496 of file ModelComponent.C. References nub::soft_ref< T >::get_weak(), and ModelComponent::Impl::parent. Referenced by getRootObject(), and modelcomponent_xx_root_object_xx_1(). |
|
|
Get this component's root parent (the parent of all parents in our tree). If this component has no parent, then getRootObject() returns a pointer to this component itself. Definition at line 512 of file ModelComponent.C. References getParent(), and p. |
|
|
Get this component's root parent (the parent of all parents in our tree). If this component has no parent, then getRootObject() returns a pointer to this component itself. Definition at line 503 of file ModelComponent.C. References getParent(), and p. Referenced by SimEventQueue::evolve(), modelcomponent_xx_root_object_xx_1(), and SimulationViewerStats::start2(). |
|
|
Check if our options have already been exported.
Definition at line 1000 of file ModelComponent.C. References ModelComponent::Impl::hasBeenExported. |
|
||||||||||||
|
Check for named param in this component (and possibly its subcomponents). Use this to verify that a param exists before doing a getModelParamVal() or setModelParamVal(), since those functions will throw an exception if the named param is non-existent. Definition at line 723 of file ModelComponent.C. References ModelComponent::Impl::findMatchingParams(). |
|
||||||||||||
|
Return true if we have the pointee as subcomponent.
Definition at line 654 of file ModelComponent.C. References nub::soft_ref< T >::getWeak(), MC_RECURSE, and ModelComponent::Impl::subComps. |
|
||||||||||||
|
Return true if we have a subcomponent by that tagname.
Definition at line 635 of file ModelComponent.C. References MC_RECURSE, and ModelComponent::Impl::subComps. Referenced by MbariResultViewer::MbariResultViewer(), and Modelcomponent_Init(). |
|
||||||||||||||||
|
Make sure you call this if you have used the default constructor.
Definition at line 265 of file ModelComponent.C. |