base/RuntimeEnv.hpp
Go to the documentation of this file.
00001 /*
00002 
00003   This file is part of OpenFLUID software
00004   Copyright(c) 2007, INRA - Montpellier SupAgro
00005 
00006 
00007  == GNU General Public License Usage ==
00008 
00009   OpenFLUID is free software: you can redistribute it and/or modify
00010   it under the terms of the GNU General Public License as published by
00011   the Free Software Foundation, either version 3 of the License, or
00012   (at your option) any later version.
00013 
00014   OpenFLUID is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017   GNU General Public License for more details.
00018 
00019   You should have received a copy of the GNU General Public License
00020   along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
00021 
00022 
00023  == Other Usage ==
00024 
00025   Other Usage means a use of OpenFLUID that is inconsistent with the GPL
00026   license, and requires a written agreement between You and INRA.
00027   Licensees for Other Usage of OpenFLUID may use this file in accordance
00028   with the terms contained in the written agreement between You and INRA.
00029   
00030 */
00031 
00032 
00033 
00034 /**
00035   @file
00036 
00037   @author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
00038 */
00039 
00040 #ifndef __RUNTIMEENV_HPP__
00041 #define __RUNTIMEENV_HPP__
00042 
00043 #include <string>
00044 #include <boost/date_time/posix_time/posix_time.hpp>
00045 #include <boost/filesystem/path.hpp>
00046 
00047 #include <openfluid/dllexport.hpp>
00048 #include <openfluid/base/EnvProperties.hpp>
00049 #include <openfluid/core/DateTime.hpp>
00050 
00051 
00052 
00053 namespace openfluid { namespace base {
00054 
00055 
00056 // =====================================================================
00057 // =====================================================================
00058 
00059 
00060 /**
00061 
00062 */
00063 class DLLEXPORT RuntimeEnvironment
00064 {
00065   private:
00066 
00067     static RuntimeEnvironment* mp_Singleton;
00068 
00069     std::string m_Version;
00070     std::string m_FullVersion;
00071     std::string m_MajorMinorVersion;
00072 
00073     std::string m_OutputDir;
00074     std::string m_InputDir;
00075     std::string m_UserDataDir;
00076     std::string m_TempDir;
00077     std::string m_HomeDir;
00078     std::string m_MarketBagDir;
00079     std::string m_MarketBagVersionDir;
00080 
00081     std::string m_MarketBagSimVersionDir;
00082     std::string m_MarketBagObsVersionDir;
00083     std::string m_MarketBagBuildVersionDir;
00084     std::string m_MarketBagDataVersionDir;
00085     std::string m_MarketBagBinSubDir;
00086     std::string m_MarketBagSrcSubDir;
00087 
00088     std::string m_InstallPrefix;
00089 
00090     std::vector<std::string> m_DefaultSimulatorsPlugsDirs;
00091     std::vector<std::string> m_ExtraSimulatorsPlugsDirs;
00092 
00093     std::vector<std::string> m_DefaultObserversPlugsDirs;
00094     std::vector<std::string> m_ExtraObserversPlugsDirs;
00095 
00096 
00097     std::string m_ProvidedExamplesDir;
00098     std::string m_UserExamplesDir;
00099 
00100     std::string m_UserID;
00101     std::string m_HostName;
00102     std::string m_Arch;
00103 
00104     unsigned int m_SimulatorsMaxNumThreads;
00105 
00106     std::string m_DefaultConfigFilePath;
00107 
00108     bool m_ClearOutputDir;
00109 
00110     bool m_WriteResults;
00111 
00112     bool m_WriteSimReport;
00113 
00114     bool m_Profiling;
00115 
00116     unsigned int m_ValuesBufferSize;
00117 
00118     bool m_IsUserValuesBufferSize;
00119 
00120     openfluid::base::EnvironmentProperties* mp_WareEnv;
00121 
00122     openfluid::base::EnvironmentProperties m_ExtraProperties;
00123 
00124     boost::posix_time::ptime m_IgnitionDateTime;
00125 
00126     boost::posix_time::time_duration m_EffectiveSimulationDuration;
00127 
00128     openfluid::core::DateTime m_StartTime;
00129 
00130     openfluid::core::DateTime m_EndTime;
00131 
00132     // TODO set correct type for time step
00133     int m_TimeStep;
00134 
00135     bool m_IsLinkedToProject;
00136 
00137 
00138     /**
00139       Default constructor
00140     */
00141     RuntimeEnvironment();
00142 
00143   public:
00144 
00145     static RuntimeEnvironment* getInstance();
00146 
00147     /**
00148       Destructor
00149     */
00150     ~RuntimeEnvironment();
00151 
00152 
00153     /**
00154       Returns the version of OpenFLUID
00155       @return the version as a std::string
00156     */
00157     std::string getVersion() const { return m_Version; };
00158 
00159     /**
00160       Returns the full version of OpenFLUID, including status (alpha, beta, rc, ...)
00161       @return the full version as a std::string
00162     */
00163     std::string getFullVersion() const { return m_FullVersion; };
00164 
00165     /**
00166       Returns the version of OpenFLUID, composed of major and minor numbers
00167       @return the major.minor version as a std::string
00168     */
00169     std::string getMajorMinorVersion() const { return m_MajorMinorVersion; };
00170 
00171 
00172 
00173     /**
00174       Sets the input directory, overriding the default input dir
00175       @param[in] InputDir The input directory
00176     */
00177     void setInputDir(const std::string InputDir)
00178       { m_InputDir = InputDir; mp_WareEnv->setValue("dir.input",m_InputDir); };
00179 
00180     /**
00181       Returns the input directory
00182       @return the input directory
00183     */
00184     inline std::string getInputDir() const { return m_InputDir; };
00185 
00186     /**
00187       Sets the output directory, overriding the default output dir
00188       @param[in] OutputDir The output directory
00189     */
00190     void setOutputDir(const std::string OutputDir)
00191       { m_OutputDir = OutputDir; mp_WareEnv->setValue("dir.output",m_OutputDir); };
00192 
00193     /**
00194       Returns the output directory
00195       @return the output directory
00196     */
00197     inline std::string getOutputDir() const { return m_OutputDir; };
00198 
00199     /**
00200       Sets the output directory as a directory located in the user data directory, with a name based on the current date-time
00201     */
00202     void setDateTimeOutputDir();
00203 
00204     /**
00205       Returns the market bag directory (i.e. $HOME/.openfluid/market-bag)
00206       @return the market bag directory
00207     */
00208     inline std::string getMarketBagDir() const { return m_MarketBagDir; };
00209 
00210     /**
00211       @return the market bag directory for the current OpenFLUID version (i.e. $HOME/.openfluid/market-bag/2.0.0)
00212      */
00213     inline std::string getMarketBagVersionDir() const { return m_MarketBagVersionDir; };
00214 
00215     /**
00216       @return the market bag directory for simulators of the current version (i.e. $HOME/.openfluid/market-bag/2.0.0/simulators)
00217      */
00218     inline std::string getMarketBagSimVersionDir() const { return m_MarketBagSimVersionDir; };
00219 
00220     /**
00221       @return the market bag directory for observers of the current version (i.e. $HOME/.openfluid/market-bag/2.0.0/observers)
00222      */
00223     inline std::string getMarketBagObsVersionDir() const { return m_MarketBagObsVersionDir; };
00224 
00225     /**
00226       @return the market bag directory for builderexts of the current version (i.e. $HOME/.openfluid/market-bag/2.0.0/builderexts)
00227      */
00228     inline std::string getMarketBagBuildVersionDir() const { return m_MarketBagBuildVersionDir; };
00229 
00230     /**
00231       @return the market bag directory for datasets of the current version (i.e. $HOME/.openfluid/market-bag/2.0.0/datasets)
00232      */
00233     inline std::string getMarketBagDataVersionDir() const { return m_MarketBagDataVersionDir; };
00234 
00235     /**
00236       @return the binairies market bag subdirectory
00237      */
00238     inline std::string getMarketBagBinSubDir() const { return m_MarketBagBinSubDir; };
00239 
00240     /**
00241       @return the sources market bag subdirectory
00242      */
00243     inline std::string getMarketBagSrcSubDir() const { return m_MarketBagSrcSubDir; };
00244 
00245     /**
00246       Returns the default config file path (i.e. $HOME/.openfluid/openfluid.conf)
00247       @return the default config file path
00248     */
00249     inline std::string getDefaultConfigFile() const
00250       { return m_DefaultConfigFilePath; };
00251 
00252     /**
00253       Returns the path for a given config file (i.e. $HOME/.openfluid/Filename)
00254       @param[in] Filename the given config file name
00255       @return the path
00256     */
00257     std::string getConfigFilePath(std::string Filename) const
00258       { return boost::filesystem::path(m_UserDataDir + "/" + Filename).string(); };
00259 
00260 
00261     /**
00262       Returns the absolute path of the home directory
00263       @return the absolute path
00264     */
00265     std::string getUserHomeDir() const
00266       { return m_HomeDir; };
00267 
00268     /**
00269       Returns the absolute path for a given relative path in the user OpenFLUID directory (i.e. $HOME/.openfluid/RelativePath)
00270       @param[in] RelativePath the given relative path
00271       @return the absolute path
00272     */
00273     std::string getUserDataPath(std::string RelativePath) const
00274       { return boost::filesystem::path(m_UserDataDir + "/" + RelativePath).string(); };
00275 
00276     /**
00277       Returns the path of the temporary directory
00278       @return the path of the temporary directory
00279     */
00280     inline std::string getTempDir() const
00281       { return m_TempDir; };
00282 
00283     /**
00284       Returns the path for a given input file (i.e. InputDir/Filename)
00285       @param[in] Filename The given input file name
00286       @return the path for a given input file
00287     */
00288     std::string getInputFullPath(std::string Filename) const
00289       { return boost::filesystem::path(m_InputDir + "/" + Filename).string(); };
00290 
00291     /**
00292       Returns the path for a given output file (i.e. OutputDir/Filename)
00293       @param[in] Filename The given output file name
00294       @return the path for a given output file
00295     */
00296     inline std::string getOutputFullPath(std::string Filename) const
00297       { return boost::filesystem::path(m_OutputDir + "/" + Filename).string(); };
00298 
00299     /**
00300       Returns the path for a given simulator plugin file, taking into account the simulator plugins path search order
00301       @param[in] Filename The given simulator plugin file name
00302       @return the first path found for a given simulator plugin file
00303     */
00304     std::string getSimulatorPluginFullPath(std::string Filename);
00305 
00306     /**
00307       Adds search paths for plugins, separated by semicolon characters (i.e. /path/to/plugs:another/path/to/plugs).
00308       These paths are added at the top of the search paths list.
00309       @param[in] SemicolonSeparatedPaths a collection of paths separated by semicolons, as a std::string
00310     */
00311     void addExtraSimulatorsPluginsPaths(std::string SemicolonSeparatedPaths);
00312 
00313     inline void resetExtraSimulatorsPluginsPaths() { m_ExtraSimulatorsPlugsDirs.clear(); };
00314 
00315     inline std::vector<std::string> getDefaultSimulatorsPluginsPaths() const  { return m_DefaultSimulatorsPlugsDirs;  };
00316 
00317     inline std::vector<std::string> getExtraSimulatorsPluginsPaths() const  { return m_ExtraSimulatorsPlugsDirs;  };
00318 
00319     /**
00320       Returns the ordered list of paths used to search for simulator plugins
00321       @return the ordered list of paths
00322     */
00323     inline std::vector<std::string> getSimulatorsPluginsPaths() const
00324     {
00325       std::vector<std::string> ComposedPaths(m_ExtraSimulatorsPlugsDirs);
00326       ComposedPaths.insert(ComposedPaths.end(), m_DefaultSimulatorsPlugsDirs.begin(), m_DefaultSimulatorsPlugsDirs.end());
00327       return ComposedPaths;
00328     };
00329 
00330     /**
00331       Returns the path for a given observer plugin file, taking into account the observer plugins path search order
00332       @param[in] Filename The given observer plugin file name
00333       @return the first path found for a given observer plugin file
00334     */
00335     std::string getObserverPluginFullPath(std::string Filename);
00336 
00337     /**
00338       Adds search paths for plugins, separated by semicolon characters (i.e. /path/to/plugs:another/path/to/plugs).
00339       These paths are added at the top of the search paths list.
00340       @param[in] SemicolonSeparatedPaths a collection of paths separated by semicolons, as a std::string
00341     */
00342     void addExtraObserversPluginsPaths(std::string SemicolonSeparatedPaths);
00343 
00344     inline void resetExtraObserversPluginsPaths() { m_ExtraObserversPlugsDirs.clear(); };
00345 
00346     inline std::vector<std::string> getDefaultObserversPluginsPaths() const  { return m_DefaultObserversPlugsDirs;  };
00347 
00348     inline std::vector<std::string> getExtraObserversPluginsPaths() const  { return m_ExtraObserversPlugsDirs;  };
00349 
00350     /**
00351       Returns the ordered list of paths used to search for observer plugins
00352       @return the ordered list of paths
00353     */
00354     inline std::vector<std::string> getObserversPluginsPaths() const
00355     {
00356       std::vector<std::string> ComposedPaths(m_ExtraObserversPlugsDirs);
00357       ComposedPaths.insert(ComposedPaths.end(), m_DefaultObserversPlugsDirs.begin(), m_DefaultObserversPlugsDirs.end());
00358       return ComposedPaths;
00359     };
00360 
00361 
00362 
00363     /**
00364       Returns the install prefix path.
00365       The install prefix is given at compile time, but can be overriden by the OPENFLUID_INSTALL_PREFIX environment variable (recommended on windows systems).
00366       @return the install prefix path
00367     */
00368     inline std::string getInstallPrefix() const
00369       { return m_InstallPrefix; };
00370 
00371     /**
00372       Returns the path for common resources, taking into account the install prefix path
00373       @return the path for common resources
00374     */
00375     std::string getCommonResourcesDir() const;
00376 
00377     /**
00378       Returns the path for a given file, relative to the common resources path
00379       @param[in] RelativeFilePath The given file
00380       @return the path for a given file in the common resources
00381     */
00382     std::string getCommonResourceFilePath(std::string RelativeFilePath) const;
00383 
00384     /**
00385       Returns the path for resources of a given application, taking into account the install prefix path
00386       @param[in] AppName The given application name
00387       @return the path for resources of a given application
00388     */
00389     std::string getAppResourcesDir(std::string AppName) const;
00390 
00391     /**
00392       Returns the path for a given file, relative a given application resources path
00393       @param[in] AppName The given application name
00394       @param[in] RelativeFilePath The given file
00395       @return the path for a given file in the given application resources
00396     */
00397     std::string getAppResourceFilePath(std::string AppName, std::string RelativeFilePath) const;
00398 
00399     /**
00400       Returns the translations directory
00401       @return the path for translationslocale directory
00402     */
00403     std::string getTranslationsDir() const;
00404 
00405     /**
00406       Returns the path for provided examples, taking into account the install prefix path
00407       @return the path for provided examples
00408      */
00409     std::string getProvidedExamplesDir() const { return m_ProvidedExamplesDir; };
00410 
00411 
00412     /**
00413       Returns the path for examples in user's directory
00414       @return the path for user's examples
00415      */
00416     std::string getUserExamplesDir() const { return m_UserExamplesDir; };
00417 
00418 
00419     /**
00420       Returns the extra properties list
00421       @return the extra properties list
00422     */
00423     inline openfluid::base::EnvironmentProperties& getExtraProperties()
00424       { return m_ExtraProperties; };
00425 
00426     /**
00427       Returns the clear output directory flag
00428     */
00429     inline bool isClearOutputDir() const { return m_ClearOutputDir; };
00430 
00431     /**
00432       Sets the clear output directory flag
00433       @param[in] ClearDir The value of the flag
00434     */
00435     inline void setClearOutputDir(bool ClearDir)
00436       { m_ClearOutputDir = ClearDir; mp_WareEnv->setValue("mode.clearoutputdir",m_ClearOutputDir); };
00437 
00438     /**
00439       Returns the write results flag
00440       @return the flag as a boolean
00441     */
00442     inline bool isWriteResults() const
00443       { return m_WriteResults; };
00444 
00445     /**
00446       Sets the write results flag
00447       @param[in] WriteIt The value of the flag
00448     */
00449     void setWriteResults(bool WriteIt)
00450       { m_WriteResults = WriteIt; mp_WareEnv->setValue("mode.saveresults",m_WriteResults); };
00451 
00452     /**
00453       Returns the write simulation report flag
00454       @return the flag as a boolean
00455     */
00456     inline bool isWriteSimReport() const { return m_WriteSimReport; };
00457 
00458     /**
00459       Sets the write simulation report flag
00460       @param[in] WriteIt The value of the flag
00461     */
00462     void setWriteSimReport(bool WriteIt)
00463       { m_WriteSimReport = WriteIt; mp_WareEnv->setValue("mode.writereport",m_WriteSimReport); };
00464 
00465     openfluid::base::EnvironmentProperties* getWareEnvironment() const
00466       { return mp_WareEnv; };
00467 
00468 
00469     boost::posix_time::ptime getIgnitionDateTime() const
00470       { return m_IgnitionDateTime; };
00471 
00472 
00473     void resetIgnitionDateTime();
00474 
00475 
00476     boost::posix_time::time_duration getEffectiveSimulationDuration() const
00477       { return m_EffectiveSimulationDuration; };
00478 
00479 
00480     void setEffectiveSimulationDuration(const boost::posix_time::time_duration& TimeDuration)
00481       { m_EffectiveSimulationDuration = TimeDuration; };
00482 
00483     /**
00484       Returns the local host name (if could be determined)
00485       @return the local host name
00486     */
00487     std::string getHostName() const {return m_HostName; };
00488 
00489     /**
00490       Returns the local user ID (if could be determined)
00491       @return the local user ID
00492     */
00493     std::string getUserID() const {return m_UserID; };
00494 
00495     /**
00496       Returns the local system architecture (if could be determined)
00497       @return the local architecture
00498     */
00499     std::string getArch() const {return m_Arch; };
00500 
00501     unsigned int getSimulatorsMaxNumThreads() const { return m_SimulatorsMaxNumThreads; };
00502 
00503     void setSimulatorsMaxNumThreads(const unsigned int& MaxNumThreads)
00504       { if (MaxNumThreads > 0) m_SimulatorsMaxNumThreads = MaxNumThreads; };
00505 
00506 
00507     void setSimulationTimeInformation(openfluid::core::DateTime StartTime,
00508                                       openfluid::core::DateTime EndTime,
00509                                       int TimeStep);
00510 
00511     openfluid::core::DateTime getSimulationStartTime() const
00512       { return m_StartTime; };
00513 
00514     openfluid::core::DateTime getSimulationEndTime() const
00515       { return m_EndTime; };
00516 
00517     // TODO set correct type for time step
00518     int getSimulationTimeStep() const
00519       { return m_TimeStep; };
00520 
00521     void setValuesBufferSize(const unsigned int StepsNbr)
00522       { m_ValuesBufferSize = StepsNbr; m_IsUserValuesBufferSize = true; };
00523 
00524     inline unsigned int getValuesBufferSize() const
00525       { return m_ValuesBufferSize; };
00526 
00527     bool isUserValuesBufferSize() const { return m_IsUserValuesBufferSize; };
00528 
00529     inline void unsetUserValuesBufferSize()
00530       { m_IsUserValuesBufferSize = false; }
00531 
00532     void linkToProject();
00533 
00534     void detachFromProject();
00535 
00536     bool isLinkedToProject()
00537       { return m_IsLinkedToProject; };
00538 
00539     bool isSimulationProfilingEnabled() const { return m_Profiling; };
00540 
00541     void setSimulationProfilingEnabled(bool Profiling) { m_Profiling = Profiling; };
00542 
00543 };
00544 
00545 
00546 } } //namespaces
00547 
00548 #endif
00549 
00550 
00551 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines