GemaLib
The GeMA library
gmSimulation.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_SIMULATION_H_
25 #define _GEMA_SIMULATION_H_
26 
27 #include "gmConfig.h"
28 #include <gmLog.h>
29 
30 #include <QString>
31 #include <QObject>
32 #include <QMutex>
33 
34 class GmSimulationData;
35 class GmSimulationLoader;
36 class GmPluginLoader;
37 class GmPath;
38 class GmThreadManager;
39 class GmTaskManager;
40 
41 class GmCommServer;
42 
43 class LuaEnv;
44 struct lua_State;
45 
48 {
52 };
53 
54 
58 class GM_API_EXPORT GmSimulation : public QObject
59 {
60  Q_OBJECT
61 
62 public:
63  GmSimulation();
64  ~GmSimulation();
65 
67  enum LogModes
68  {
69  QUIET_LOG_MODE = 0x001,
70  VERBOSE_LOG_MODE = 0x002,
71  NOCONSOLE_LOG_MODE = 0x004,
72  REGRESSION_LOG_MODE = 0x008,
73  MEMORY_LOG_MODE = 0x010,
74  NOMEMORY_LOG_MODE = 0x020,
75  TIME_LOG_MODE = 0x040,
76  NOTIME_LOG_MODE = 0x080,
77  SERVER_LOG_MODE = 0x100,
78  NOSERVER_LOG_MODE = 0x200,
79  PLUGININFO_LOG_MODE = 0x400,
80  TIMESTATS_LOG_MODE = 0x800,
81  };
82 
85  {
90  };
91 
92  bool initLibrary(QString configFile, QString logFile, int logMode,
93  QString logPath, QString inputPath, QString outputPath,
94  ServerMode serverMode, unsigned localServerPort,
95  QString serverAddrFile, bool rsrcFiles);
96 
97  bool initSimulation(QString simulationFile, const QMap<QString, QString>& globalVars,
98  int timeout = -1, lua_State* state = NULL);
99 
100  GmSimulationResult run();
101 
105  void clear() { setData(NULL); }
106 
107  void printInfo();
108 
110  const GmLogCategory& logger() { return _logger; }
111 
113  LuaEnv* env() { return _env; }
114 
116  GmSimulationData* data() const { return _simulationData; }
117 
118  GmPluginLoader* pluginLoader() const;
119 
121  const GmPath* paths() const { return _paths; }
122 
123 
124 signals:
133  void sectionParsed(QString section, LuaTable& data);
134 
135 private slots:
136  void cancelRequested();
137 
138 private:
139  bool setConfigFile(QString configFile, QString logFile, int logMode,
140  ServerMode serverMode, unsigned localServerPort, QString serverAddrFile);
141 
142  bool setSimulationFile(QString file, const QMap<QString, QString>& globalVars, lua_State* state);
143 
144  bool setupLogging (LuaEnv& env, QString logFile, int logMode);
145  bool setupMonitorServer (LuaEnv& env, ServerMode serverMode, unsigned localServerPort, bool logToServer, QString serverAddrFile);
146  bool setupUnitManager (LuaEnv& env);
147  bool loadConfigValueTables(LuaEnv& env);
148 
149  void setData(GmSimulationData* data);
150  void clearDataOnError(bool abort);
151 
152  void printPluginVersions(GmLogCategory& logger);
153 
157  QVariantMap _configConstants;
158  QVariantMap _configOptions;
159  bool _rsrcFiles;
160 
162 
163  GmCommServer* _monitorServer;
164 
169 
177 
179 };
180 
181 #endif
182 
Basic class for defining a GeMA physical Simulation, containing references to the model definition,...
Definition: gmSimulation.h:58
Declaration of usefull configuration definitions.
void clear()
Clears the current simulation environment, preparing the object for a new simulation: new calls to in...
Definition: gmSimulation.h:105
GmSimulationLoader * _simulationLoader
Simulation loader.
Definition: gmSimulation.h:155
bool _redirectPrint
Should we redirect Lua print() calls to the logger?
Definition: gmSimulation.h:175
LuaEnv * env()
Returns the simulation environment.
Definition: gmSimulation.h:113
Auxiliary class responsible for loading simulation information from a Lua File.
Definition: gmSimulationLoader.h:43
The monitor server will be turned on, listening for connections on the local host.
Definition: gmSimulation.h:89
const GmLogCategory & logger()
Returns the basic logger for the GeMA library.
Definition: gmSimulation.h:110
The simulation was cancelled by user request.
Definition: gmSimulation.h:50
The monitor server will be turned on or off depending on the config file.
Definition: gmSimulation.h:86
LuaEnv * _env
Lua environment used to read the Simulation. Reused during the execution step as it contains function...
Definition: gmSimulation.h:173
GmSimulationData * _simulationData
Object storing the complete Simulation information.
Definition: gmSimulation.h:154
ServerMode
Server initialization modes.
Definition: gmSimulation.h:84
The simulation completed successfully.
Definition: gmSimulation.h:49
LogModes
Predefined Log modes. Can be ored together.
Definition: gmSimulation.h:67
GmCommServer * _monitorServer
The monitor server. Will be NULL if monitoring is not enabled.
Definition: gmSimulation.h:163
GmLogCategory _logger
Basic logger object for the library.
Definition: gmSimulation.h:161
QMutex _envMutex
The mutex used to control the possible data race during the creation of the Lua environments and poss...
Definition: gmSimulation.h:168
bool _externalState
Are we using an external Lua environment provided in initSimulation() ?
Definition: gmSimulation.h:174
The monitor server will be turned off.
Definition: gmSimulation.h:88
GmPath * _paths
Object storing the set of important simulation paths for macro translation.
Definition: gmSimulation.h:156
QVariantMap _configConstants
The set of constants defined on the config file.
Definition: gmSimulation.h:157
const GmPath * paths() const
Returns the object storing information for path macro susbstitution.
Definition: gmSimulation.h:121
The monitor server will be turned on.
Definition: gmSimulation.h:87
bool _loadCompleted
A flag set to true when the model loading has finished successfully. Also protected by _envMutex.
Definition: gmSimulation.h:176
bool _rsrcFiles
If set to true, the config file and the model file are allowed to be resource files.
Definition: gmSimulation.h:159
GmSimulationResult
Enumeration describing the result of a simulation.
Definition: gmSimulation.h:47
The simulation was aborted by a run time error.
Definition: gmSimulation.h:51
QVariantMap _configOptions
The set of options defined on the config file.
Definition: gmSimulation.h:158
GmSimulationData * data() const
Returns a reference to the simulation data.
Definition: gmSimulation.h:116
QList< LuaEnv * > _workerEnvList
A list with Lua environments for worker threads.
Definition: gmSimulation.h:178