ModuleFactory.h
Go to the documentation of this file.
1/*!
2 * \file ModuleFactory.h
3 * \brief Constructor of ModuleFactory from config file
4 *
5 * Changelog:
6 * - 1. 2017-05-30 - lj - Refactor and DeCoupling with Database I/O.
7 * - 2. 2022-08-19 - lj - Separate integer and floating point of parameter, input, output, and inoutput.
8 *
9 * \author Junzhi Liu, LiangJun Zhu
10 * \version 2.1
11 */
12#ifndef SEIMS_MODULE_FACTORY_H
13#define SEIMS_MODULE_FACTORY_H
14
15#include "invoke.h"
16#include "SEIMS_ModuleSetting.h"
17#include "SimulationModule.h"
18#include "ParamInfo.h"
19#include "SettingsInput.h"
20
21#include "tinyxml.h"
22
23#ifdef WIN32
24#define DLLINSTANCE HINSTANCE
25#else
26#define DLLINSTANCE void*
27#endif
28//! Simulation module instance
29typedef SimulationModule*(*InstanceFunction)();
30//! Simulation module metadata
31typedef const char*(*MetadataFunction)();
32
33using namespace bmps;
34
35/*!
36 * \class ModuleFactory
37 * \ingroup module_setting
38 * \brief Linking user-defined modules to create the modeling workflow.
39 */
41public:
42 /*!
43 * \brief Constructor
44 */
45 ModuleFactory(string model_name,
46 vector<string>& moduleIDs,
47 map<string, SEIMSModuleSetting *>& moduleSettings,
48 vector<DLLINSTANCE>& dllHandles,
49 map<string, InstanceFunction>& instanceFuncs,
50 map<string, MetadataFunction>& metadataFuncs,
51 map<string, vector<ParamInfo<FLTPT>*> >& moduleParams,
52 map<string, vector<ParamInfo<int>*> >& moduleParamsInt,
53 map<string, vector<ParamInfo<FLTPT>*> >& moduleInputs,
54 map<string, vector<ParamInfo<int>*> >& moduleInputsInt,
55 map<string, vector<ParamInfo<FLTPT>*> >& moduleOutputs,
56 map<string, vector<ParamInfo<int>*> >& moduleOutputsInt,
57 map<string, vector<ParamInfo<FLTPT>*> >& moduleInOutputs,
58 map<string, vector<ParamInfo<int>*> >& moduleInOutputsInt,
59 vector<ParamInfo<FLTPT> *>& tfValueInputs,
60 vector<ParamInfo<int>*>& tfValueInputsInt,
61 int mpi_rank = 0, int mpi_size = -1);
62 /*!
63 * \brief Initialization for exception-safe constructor
64 */
65 static ModuleFactory* Init(const string& module_path, InputArgs* input_args,
66 int mpi_rank = 0, int mpi_size = -1);
67
68 //! Destructor
70
71 //! Create a set of objects and set up the relationship among them. Return time-consuming.
72 void CreateModuleList(vector<SimulationModule *>& modules, int nthread = 1);
73
74 //! Get value from dependency modules
75 void GetValueFromDependencyModule(int iModule, vector<SimulationModule *>& modules);
76
77 //! Find outputID parameter's module. Return Module index iModule and its ParamInfo<FLTPT>
78 bool FindOutputParameter(string& outputID, int& iModule, ParamInfo<FLTPT>*& paraInfo);
79
80 //! Get Module ID by index
81 string GetModuleID(int i) const { return m_moduleIDs[i]; }
82
83 //! Get unique module IDs
84 vector<string>& GetModuleIDs() { return m_moduleIDs; }
85
86 //! Get map of module settings
87 map<string, SEIMSModuleSetting *>& GetModuleSettings() { return m_settings; }
88
89 //! Get Parameters of modules
90 map<string, vector<ParamInfo<FLTPT> *> >& GetModuleParams() { return m_moduleParams; }
91
92 //! Get integer parameters of modules
93 map<string, vector<ParamInfo<int>*> >& GetModuleParamsInt() { return m_moduleParamsInt; }
94
95 //! Get Input of modules, from other modules
96 map<string, vector<ParamInfo<FLTPT> *> >& GetModuleInputs() { return m_moduleInputs; }
97
98 //! Get integer input of modules, from other modules
99 map<string, vector<ParamInfo<int>*> >& GetModuleInputsInt() { return m_moduleInputsInt; }
100
101 //! Get Output of modules, out from current module
102 map<string, vector<ParamInfo<FLTPT>*> >& GetModuleOutputs() { return m_moduleOutputs; }
103
104 //! Get integer Output of modules, out from current module
105 map<string, vector<ParamInfo<int>*> >& GetModuleOutputsInt() { return m_moduleOutputsInt; }
106
107 //! Get InOutput of modules, in and out from current module
108 map<string, vector<ParamInfo<FLTPT> *> >& GetModuleInOutputs() { return m_moduleInOutputs; }
109
110 //! Get integer InOutput of modules, in and out from current module
111 map<string, vector<ParamInfo<int>*> >& GetModuleInOutputsInt() { return m_moduleInOutputsInt; }
112
113 //! Get transferred single value inputs across subbasins
114 vector<ParamInfo<FLTPT>*>& GetTransferredInputs() { return m_tfValueInputs; }
115
116 //! Get transferred single integer value inputs across subbasins
117 vector<ParamInfo<int>*>& GetTransferredInputsInt() { return m_tfValueInputsInt; }
118
119 //! Get the count of transferred single value inputs
120 int GetTransferredInputsCount() { return CVT_INT(m_tfValueInputs.size()); }
121
122 //! Get the count of transferred single integer value inputs
123 int GetTransferredInputsIntCount() { return CVT_INT(m_tfValueInputsInt.size()); }
124
125 //! Load modules setting from file
126 static bool LoadSettingsFromFile(const char* filename, vector<vector<string> >& settings);
127
128 /*!
129 * \brief Read configuration file
130 * \param[in] configFileName Configuration full file path
131 * \param[out] moduleIDs Unique module IDs (name)
132 * \param[out] moduleSettings Map of SEIMSModuleSetting
133 * \return True if succeed.
134 */
135 static bool ReadConfigFile(const char* configFileName, vector<string>& moduleIDs,
136 map<string, SEIMSModuleSetting *>& moduleSettings);
137
138 /*!
139 * \brief Load and parse module libraries
140 * \param module_path
141 * \param moduleIDs
142 * \param moduleSettings
143 * \param dllHandles
144 * \param instanceFuncs
145 * \param metadataFuncs
146 * \param moduleParams
147 * \param moduleParamsInt
148 * \param moduleInputs
149 * \param moduleInputsInt
150 * \param moduleOutputs
151 * \param moduleOutputsInt
152 * \param moduleInOutputs
153 * \param tfValueInputs
154 * \return True if succeed, else throw exception and return false.
155 */
156 static bool LoadParseLibrary(const string& module_path, vector<string>& moduleIDs,
157 map<string, SEIMSModuleSetting *>& moduleSettings,
158 vector<DLLINSTANCE>& dllHandles,
159 map<string, InstanceFunction>& instanceFuncs,
160 map<string, MetadataFunction>& metadataFuncs,
161 map<string, vector<ParamInfo<FLTPT>*> >& moduleParams,
162 map<string, vector<ParamInfo<int>*> >& moduleParamsInt,
163 map<string, vector<ParamInfo<FLTPT>*> >& moduleInputs,
164 map<string, vector<ParamInfo<int>*> >& moduleInputsInt,
165 map<string, vector<ParamInfo<FLTPT> *> >& moduleOutputs,
166 map<string, vector<ParamInfo<int>*> >& moduleOutputsInt,
167 map<string, vector<ParamInfo<FLTPT>*> >& moduleInOutputs,
168 map<string, vector<ParamInfo<int>*> >& moduleInOutputsInt,
169 vector<ParamInfo<FLTPT>*>& tfValueInputs,
170 vector<ParamInfo<int>*>& tfValueInputsInt);
171
172 //! Load function pointers from .DLL or .so
173 static void ReadDLL(const string& module_path, const string& id, const string& dllID,
174 vector<DLLINSTANCE>& dllHandles,
175 map<string, InstanceFunction>& instanceFuncs,
176 map<string, MetadataFunction>& metadataFuncs);
177
178 //! Get module instance by moduleID
179 SimulationModule* GetInstance(const string& moduleID) { return m_instanceFuncs[moduleID](); }
180
181 //! Match data type, e.g., 1D array
182 static dimensionTypes MatchType(const string &strType);
183
184 //! Match data transfer type, e.g., TF_SingleValue
185 static transferTypes MatchTransferType(const string& tfType);
186
187 //! Is constant input?
188 static bool IsConstantInputFromName(const string& name);
189
190 //! Read module's parameters setting from XML string
191 static void ReadParameterSetting(string& moduleID, TiXmlDocument& doc, SEIMSModuleSetting* setting,
192 map<string, vector<ParamInfo<FLTPT> *> >& moduleParams,
193 map<string, vector<ParamInfo<int>*> >& moduleParamsInt);
194
195 //! Read module's input, output, and in/output setting from XML string
196 static void ReadIOSetting(string& moduleID, TiXmlDocument& doc, SEIMSModuleSetting* setting,
197 const string& header, const string& title,
198 map<string, vector<ParamInfo<FLTPT>*> >& vars,
199 map<string, vector<ParamInfo<int>*> >& varsInt);
200
201 //! Get comparable name after underscore if necessary, e.g., T_PET => use PET
202 static string GetComparableName(string& paraName);
203
204 //! Find dependent parameters
205 static ParamInfo<FLTPT>* FindDependentParam(ParamInfo<FLTPT>* paramInfo, vector<string>& moduleIDs,
206 map<string, vector<ParamInfo<FLTPT> *> >& moduleOutputs);
207
208 //! Find dependent parameters
209 static ParamInfo<int>* FindDependentParam(ParamInfo<int>* paramInfo, vector<string>& moduleIDs,
210 map<string, vector<ParamInfo<int>*> >& moduleOutputs);
211
212public:
213 //! Rank ID for MPI, starts from 0 to mpi_size_ - 1
215 //! Rank size for MPI
217private:
218 //! Database name of the simulation model
219 string m_dbName;
220 //! Module IDs
221 vector<string> m_moduleIDs;
222 //! instance map of modules
223 map<string, InstanceFunction> m_instanceFuncs;
224 //! Metadata map of modules
225 map<string, MetadataFunction> m_metadataFuncs;
226 //! dynamic library handles (.dll in Windows, .so in Linux, and .dylib in macOS)
227 vector<DLLINSTANCE> m_dllHandles;
228 //! Module settings
229 map<string, SEIMSModuleSetting *> m_settings;
230 //! Parameters of modules, from database
231 map<string, vector<ParamInfo<FLTPT>*> > m_moduleParams;
232 //! Integer parameters of modules, from database
233 map<string, vector<ParamInfo<int>*> > m_moduleParamsInt;
234 //! Input of modules, from other modules
235 map<string, vector<ParamInfo<FLTPT>*> > m_moduleInputs;
236 //! Integer input of modules, from other modules
237 map<string, vector<ParamInfo<int>*> > m_moduleInputsInt;
238 //! Output of modules, out from current module
239 map<string, vector<ParamInfo<FLTPT>*> > m_moduleOutputs;
240 //! Integer output of modules, out from current module
241 map<string, vector<ParamInfo<int>*> > m_moduleOutputsInt;
242 //! InOutput of modules, out from current module, and from current module(i.e., other instance) meanwhile
243 map<string, vector<ParamInfo<FLTPT>*> > m_moduleInOutputs;
244 //! Integer InOutput of modules, out from current module, and from current module meanwhile
245 map<string, vector<ParamInfo<int>*> > m_moduleInOutputsInt;
246 //! transferred single value across subbasins
247 vector<ParamInfo<FLTPT> *> m_tfValueInputs;
248 //! transferred single integer value across subbasins
249 vector<ParamInfo<int>*> m_tfValueInputsInt;
250};
251#endif /* SEIMS_MODULE_FACTORY_H */
Class to store parameter item information.
User-defined module information in config.fig.
Setting Inputs for SEIMS.
Parent class for all modules in SEIMS.
#define CVT_INT(param)
A reference to the postfix of executable file for RELWITHDEBINFO mode.
Definition: basic.h:325
Base class for classes that cannot be copied.
Definition: basic.h:385
Class to store and manage parameter information from the parameter database.
Definition: ParamInfo.h:37
static dimensionTypes MatchType(const string &strType)
Match data type, e.g., 1D array.
map< string, vector< ParamInfo< int > * > > & GetModuleInOutputsInt()
Get integer InOutput of modules, in and out from current module.
Definition: ModuleFactory.h:111
map< string, vector< ParamInfo< FLTPT > * > > & GetModuleInOutputs()
Get InOutput of modules, in and out from current module.
Definition: ModuleFactory.h:108
static bool ReadConfigFile(const char *configFileName, vector< string > &moduleIDs, map< string, SEIMSModuleSetting * > &moduleSettings)
Read configuration file.
SimulationModule * GetInstance(const string &moduleID)
Get module instance by moduleID.
Definition: ModuleFactory.h:179
map< string, vector< ParamInfo< int > * > > & GetModuleInputsInt()
Get integer input of modules, from other modules.
Definition: ModuleFactory.h:99
static string GetComparableName(string &paraName)
Get comparable name after underscore if necessary, e.g., T_PET => use PET.
int GetTransferredInputsIntCount()
Get the count of transferred single integer value inputs.
Definition: ModuleFactory.h:123
void GetValueFromDependencyModule(int iModule, vector< SimulationModule * > &modules)
Get value from dependency modules.
vector< ParamInfo< int > * > & GetTransferredInputsInt()
Get transferred single integer value inputs across subbasins.
Definition: ModuleFactory.h:117
static ParamInfo< FLTPT > * FindDependentParam(ParamInfo< FLTPT > *paramInfo, vector< string > &moduleIDs, map< string, vector< ParamInfo< FLTPT > * > > &moduleOutputs)
Find dependent parameters.
static void ReadParameterSetting(string &moduleID, TiXmlDocument &doc, SEIMSModuleSetting *setting, map< string, vector< ParamInfo< FLTPT > * > > &moduleParams, map< string, vector< ParamInfo< int > * > > &moduleParamsInt)
Read module's parameters setting from XML string.
ModuleFactory(string model_name, vector< string > &moduleIDs, map< string, SEIMSModuleSetting * > &moduleSettings, vector< DLLINSTANCE > &dllHandles, map< string, InstanceFunction > &instanceFuncs, map< string, MetadataFunction > &metadataFuncs, map< string, vector< ParamInfo< FLTPT > * > > &moduleParams, map< string, vector< ParamInfo< int > * > > &moduleParamsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleInputs, map< string, vector< ParamInfo< int > * > > &moduleInputsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleOutputs, map< string, vector< ParamInfo< int > * > > &moduleOutputsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleInOutputs, map< string, vector< ParamInfo< int > * > > &moduleInOutputsInt, vector< ParamInfo< FLTPT > * > &tfValueInputs, vector< ParamInfo< int > * > &tfValueInputsInt, int mpi_rank=0, int mpi_size=-1)
Constructor.
int GetTransferredInputsCount()
Get the count of transferred single value inputs.
Definition: ModuleFactory.h:120
map< string, SEIMSModuleSetting * > & GetModuleSettings()
Get map of module settings.
Definition: ModuleFactory.h:87
vector< string > & GetModuleIDs()
Get unique module IDs.
Definition: ModuleFactory.h:84
map< string, vector< ParamInfo< FLTPT > * > > & GetModuleInputs()
Get Input of modules, from other modules.
Definition: ModuleFactory.h:96
static void ReadDLL(const string &module_path, const string &id, const string &dllID, vector< DLLINSTANCE > &dllHandles, map< string, InstanceFunction > &instanceFuncs, map< string, MetadataFunction > &metadataFuncs)
Load function pointers from .DLL or .so.
void CreateModuleList(vector< SimulationModule * > &modules, int nthread=1)
Create a set of objects and set up the relationship among them. Return time-consuming.
vector< ParamInfo< FLTPT > * > & GetTransferredInputs()
Get transferred single value inputs across subbasins.
Definition: ModuleFactory.h:114
static bool LoadSettingsFromFile(const char *filename, vector< vector< string > > &settings)
Load modules setting from file.
int m_mpi_size
Rank size for MPI.
Definition: ModuleFactory.h:216
static ModuleFactory * Init(const string &module_path, InputArgs *input_args, int mpi_rank=0, int mpi_size=-1)
Initialization for exception-safe constructor.
static void ReadIOSetting(string &moduleID, TiXmlDocument &doc, SEIMSModuleSetting *setting, const string &header, const string &title, map< string, vector< ParamInfo< FLTPT > * > > &vars, map< string, vector< ParamInfo< int > * > > &varsInt)
Read module's input, output, and in/output setting from XML string.
bool FindOutputParameter(string &outputID, int &iModule, ParamInfo< FLTPT > *&paraInfo)
Find outputID parameter's module. Return Module index iModule and its ParamInfo<FLTPT>
map< string, vector< ParamInfo< FLTPT > * > > & GetModuleParams()
Get Parameters of modules.
Definition: ModuleFactory.h:90
map< string, vector< ParamInfo< int > * > > & GetModuleParamsInt()
Get integer parameters of modules.
Definition: ModuleFactory.h:93
string GetModuleID(int i) const
Get Module ID by index.
Definition: ModuleFactory.h:81
map< string, vector< ParamInfo< FLTPT > * > > & GetModuleOutputs()
Get Output of modules, out from current module.
Definition: ModuleFactory.h:102
static transferTypes MatchTransferType(const string &tfType)
Match data transfer type, e.g., TF_SingleValue.
map< string, vector< ParamInfo< int > * > > & GetModuleOutputsInt()
Get integer Output of modules, out from current module.
Definition: ModuleFactory.h:105
int m_mpi_rank
Rank ID for MPI, starts from 0 to mpi_size_ - 1.
Definition: ModuleFactory.h:214
~ModuleFactory()
Destructor.
static bool LoadParseLibrary(const string &module_path, vector< string > &moduleIDs, map< string, SEIMSModuleSetting * > &moduleSettings, vector< DLLINSTANCE > &dllHandles, map< string, InstanceFunction > &instanceFuncs, map< string, MetadataFunction > &metadataFuncs, map< string, vector< ParamInfo< FLTPT > * > > &moduleParams, map< string, vector< ParamInfo< int > * > > &moduleParamsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleInputs, map< string, vector< ParamInfo< int > * > > &moduleInputsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleOutputs, map< string, vector< ParamInfo< int > * > > &moduleOutputsInt, map< string, vector< ParamInfo< FLTPT > * > > &moduleInOutputs, map< string, vector< ParamInfo< int > * > > &moduleInOutputsInt, vector< ParamInfo< FLTPT > * > &tfValueInputs, vector< ParamInfo< int > * > &tfValueInputsInt)
Load and parse module libraries.
static bool IsConstantInputFromName(const string &name)
Is constant input?
static ParamInfo< int > * FindDependentParam(ParamInfo< int > *paramInfo, vector< string > &moduleIDs, map< string, vector< ParamInfo< int > * > > &moduleOutputs)
Find dependent parameters.
Parse the input arguments of SEIMS.
Definition: invoke.h:29
Linking user-defined modules to create the modeling workflow.
Definition: ModuleFactory.h:40
Base module for all simulation modules in SEIMS.
Definition: SimulationModule.h:46
transferTypes
Float values be transferred across subbasins for MPI version.
Definition: MetadataInfoConst.h:86
dimensionTypes
enum of dimension data types
Definition: MetadataInfoConst.h:63
Parse the input arguments as a class which can be easily extended.
All BMPs scenario related data, classes, and functions.
Definition: BMPArealSourceFactory.h:22