GemaLib
The GeMA library
Public Types | Signals | Public Member Functions | Private Slots | Private Member Functions | Private Attributes | List of all members
GmSimulation Class Reference

Basic class for defining a GeMA physical Simulation, containing references to the model definition, solution defnition and results configuration definition. More...

#include <gmSimulation.h>

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

Public Types

enum  LogModes {
  QUIET_LOG_MODE = 0x001, VERBOSE_LOG_MODE = 0x002, NOCONSOLE_LOG_MODE = 0x004, REGRESSION_LOG_MODE = 0x008,
  MEMORY_LOG_MODE = 0x010, NOMEMORY_LOG_MODE = 0x020, TIME_LOG_MODE = 0x040, NOTIME_LOG_MODE = 0x080,
  SERVER_LOG_MODE = 0x100, NOSERVER_LOG_MODE = 0x200, PLUGININFO_LOG_MODE = 0x400, TIMESTATS_LOG_MODE = 0x800
}
 Predefined Log modes. Can be ored together. More...
 
enum  ServerMode { CONFIG_SERVER_MODE, ENABLE_SERVER_MODE, DISABLE_SERVER_MODE, LOCAL_SERVER_MODE }
 Server initialization modes. More...
 

Signals

void sectionParsed (QString section, LuaTable &data)
 Signal emmited whenever a model section has been parsed from the Lua file. More...
 

Public Member Functions

 GmSimulation ()
 Constructor. Simulation initialization should be done by calling init().
 
 ~GmSimulation ()
 Destructor.
 
bool initLibrary (QString configFile, QString logFile, int logMode, QString logPath, QString inputPath, QString outputPath, ServerMode serverMode, unsigned localServerPort, QString serverAddrFile, bool rsrcFiles)
 Initializes the library by reading the provided configuration file. More...
 
bool initSimulation (QString simulationFile, const QMap< QString, QString > &globalVars, int timeout=-1, lua_State *state=NULL)
 Initializes the simulation by reading the provided model file. More...
 
GmSimulationResult run ()
 Runs the simulation loaded by the call to init() More...
 
void clear ()
 Clears the current simulation environment, preparing the object for a new simulation: new calls to initSimulation() + run().
 
void printInfo ()
 Prints information about the simulation using the Info logger.
 
const GmLogCategorylogger ()
 Returns the basic logger for the GeMA library.
 
LuaEnvenv ()
 Returns the simulation environment.
 
GmSimulationDatadata () const
 Returns a reference to the simulation data.
 
GmPluginLoaderpluginLoader () const
 Returns a pointer to the plugin loader. Must be called AFTER a succesfull call to initLibrary().
 
const GmPathpaths () const
 Returns the object storing information for path macro susbstitution.
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Private Slots

void cancelRequested ()
 The slot called if the monitor server accepted a cancelation request. More...
 

Private Member Functions

bool setConfigFile (QString configFile, QString logFile, int logMode, ServerMode serverMode, unsigned localServerPort, QString serverAddrFile)
 Loads global configuration options for the library. Those include paths to loaded components, logging options, etc. More...
 
bool setSimulationFile (QString file, const QMap< QString, QString > &globalVars, lua_State *state)
 Loads data defining the model to be simulated. More...
 
bool setupLogging (LuaEnv &env, QString logFile, int logMode)
 Setup logging options. More...
 
bool setupMonitorServer (LuaEnv &env, ServerMode serverMode, unsigned localServerPort, bool logToServer, QString serverAddrFile)
 Initializes the monitor server depending on the configuration from serverMode and from the Lua config file. If the server is initialized, sets _monitorServer. More...
 
bool setupUnitManager (LuaEnv &env)
 Setup the unit manager. More...
 
bool loadConfigValueTables (LuaEnv &env)
 Loads from the given Lua environment the contents of the "modelConstants" and "modelOptions" tables, filling the _configConstants and _configOptions maps. More...
 
void setData (GmSimulationData *data)
 Changes the simulation data for this simulation. More...
 
void clearDataOnError (bool abort)
 Notifies the monitor server about the error / cancelation and sets the internal data to NULL. Also prints a cancelation message if appropriate. If abort is true, will always notify the server of an abort.
 
void printPluginVersions (GmLogCategory &logger)
 Print plugin versions to the specified logger.
 

Private Attributes

GmSimulationData_simulationData
 Object storing the complete Simulation information.
 
GmSimulationLoader_simulationLoader
 Simulation loader.
 
GmPath_paths
 Object storing the set of important simulation paths for macro translation.
 
QVariantMap _configConstants
 The set of constants defined on the config file.
 
QVariantMap _configOptions
 The set of options defined on the config file.
 
bool _rsrcFiles
 If set to true, the config file and the model file are allowed to be resource files.
 
GmLogCategory _logger
 Basic logger object for the library.
 
GmCommServer * _monitorServer
 The monitor server. Will be NULL if monitoring is not enabled.
 
QMutex _envMutex
 The mutex used to control the possible data race during the creation of the Lua environments and possible concurrent cancelation requests.
 
LuaEnv_env
 Lua environment used to read the Simulation. Reused during the execution step as it contains functions used by the orchestration.
 
bool _externalState
 Are we using an external Lua environment provided in initSimulation() ?
 
bool _redirectPrint
 Should we redirect Lua print() calls to the logger?
 
bool _loadCompleted
 A flag set to true when the model loading has finished successfully. Also protected by _envMutex.
 
QList< LuaEnv * > _workerEnvList
 A list with Lua environments for worker threads.
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

Basic class for defining a GeMA physical Simulation, containing references to the model definition, solution defnition and results configuration definition.

This is the core GeMA class. It should be initialized by successfully calling the initLibrary() and initSimulation() functions, passing as parameters, respectively, a global configuration file, and the simulation file.

After the initialization, the methods run() and saveFinalResults() complete the set of functions that are needed to execute a simulation.

Warnings and errors found by the classes methods are generally logged to the default logger, configured by the configuration file.

Member Enumeration Documentation

◆ LogModes

Predefined Log modes. Can be ored together.

Enumerator
QUIET_LOG_MODE 

Log only warnings and errors (exclusive with VERBOSE_LOG_MODE)

VERBOSE_LOG_MODE 

Log everything (exclusive with QUIET_LOG_MODE)

NOCONSOLE_LOG_MODE 

Don't log to the console.

REGRESSION_LOG_MODE 

Log will include tags for helping regression tests comparisson.

MEMORY_LOG_MODE 

Log will turn on memory messages (not to be used together with verbose & quiet)

NOMEMORY_LOG_MODE 

Log will turn off memory messages (not to be used together with verbose & quiet)

TIME_LOG_MODE 

Log will turn on time messages (not to be used together with verbose & quiet)

NOTIME_LOG_MODE 

Log will turn off time messages (not to be used together with verbose & quiet)

SERVER_LOG_MODE 

Log messages to the monitor server.

NOSERVER_LOG_MODE 

Don't log messages to the monitor server.

PLUGININFO_LOG_MODE 

Print log version / revision information.

TIMESTATS_LOG_MODE 

Print time statistics generated with GmTimeMsg, grouped by message, at the simulation end.

◆ ServerMode

Server initialization modes.

Enumerator
CONFIG_SERVER_MODE 

The monitor server will be turned on or off depending on the config file.

ENABLE_SERVER_MODE 

The monitor server will be turned on.

DISABLE_SERVER_MODE 

The monitor server will be turned off.

LOCAL_SERVER_MODE 

The monitor server will be turned on, listening for connections on the local host.

Member Function Documentation

◆ cancelRequested

void GmSimulation::cancelRequested ( )
privateslot

The slot called if the monitor server accepted a cancelation request.

IMPORTANT: This slot runs from the server thread, and so its execution happends in parallel with the main thread activity.

◆ initLibrary()

bool GmSimulation::initLibrary ( QString  configFile,
QString  logFile,
int  logMode,
QString  logPath,
QString  inputPath,
QString  outputPath,
ServerMode  serverMode,
unsigned  localServerPort,
QString  serverAddrFile,
bool  rsrcFiles 
)

Initializes the library by reading the provided configuration file.

Parameters
configFilePath for the configuration file containing global configuration options for the library. Those include paths to loaded components, logging options, etc.
logFilePath to the desired log file. If empty the value at the configuration file will be used instead.
logModeFlags used to override config file options for logging.
logPath,inputPathand outputPath: Paths uses by macro translations for, respectiveley, the following macros: $LOG, $INPUT and $OUTPUT. If empty default values will be the application path for $LOG and the simulation dir for the other two.
serverModeFlag defining if the monitor server should be started or not
localServerPortThe port where the server should listen for connections if serverMode equals LOCAL_SERVER_MODE. Can be 0 for any port. Ignored for other values of serverMode.
serverAddrFileThe name of the file that should be used to publish the monitor server address and port (if the server should be started). If empty, the name in the config file will be used instead.
rsrcFilesFlag stating whether the config file and later the model file can refernce a resource file or not (this distintion is made for better error messages only).
Returns
Returns false in case of fatal errors.

◆ initSimulation()

bool GmSimulation::initSimulation ( QString  simulationFile,
const QMap< QString, QString > &  globalVars,
int  timeout = -1,
lua_State *  state = NULL 
)

Initializes the simulation by reading the provided model file.

Parameters
modelFilePath for the simulation definition file
globalVarsUser supplied values for global variables that will be definied on the simulation environment. The map should be keyed by the variable name and its value should be equal the string needed to initialize the variable following the Lua syntax, such as that the statement key = value can be interpreted by Lua. This is used, for example, to fill the userParams global variable.
timeoutAn optional parameter thet when different from -1, asks the main thread to wait until the first client is connected to the monitor server. This can be important if the external monitor process needs to have access to every log message generated by GeMA. This value is the maximum time to wait in mili seconds. A value of zero waits for ever. A value of -1 disables waiting at all.
stateThis optional parameter should be almost always be set to NULL. When filled with a pointer to a Lua state, that state will be used when loading the simulation file and later during simulation execution. This option helps the integration of the GeMA environment inside other environments where the control loop is outside the GeMA library. IMPORTANT: The Lua state MUST have been compiled with LUAI_EXTRASPACE, as explained in luaEnv.h. We also expect the extra area to be filled with 0 when the state is created.
Returns
Returns false in case of fatal errors.

◆ loadConfigValueTables()

bool GmSimulation::loadConfigValueTables ( LuaEnv env)
private

Loads from the given Lua environment the contents of the "modelConstants" and "modelOptions" tables, filling the _configConstants and _configOptions maps.

Lua object references are removed from the map so that its contents remains valid after the disposal of the environment used to load the config file.

◆ run()

GmSimulationResult GmSimulation::run ( )

Runs the simulation loaded by the call to init()

< The simulation was cancelled by user request

< The simulation was aborted by a run time error

◆ sectionParsed

void GmSimulation::sectionParsed ( QString  section,
LuaTable data 
)
signal

Signal emmited whenever a model section has been parsed from the Lua file.

This signal is emmited while the model file is beeing parsed during the Lua file load.

Parameters
sectionThe name of the parsed section ('Simulation', 'Mesh', 'StateVar', ...)
dataA reference to the table storing the section data

◆ setConfigFile()

bool GmSimulation::setConfigFile ( QString  configFile,
QString  logFile,
int  logMode,
ServerMode  serverMode,
unsigned  localServerPort,
QString  serverAddrFile 
)
private

Loads global configuration options for the library. Those include paths to loaded components, logging options, etc.

Parameters
configFilePath for the configuration file
logFilePath to the desired log file. If empty the value at the configuration file will be used instead.
logModeFlags used to override config file options for logging.
serverModeFlag defining if the monitor server should be started or not
localServerPortThe port where the server should listen for connections if serverMode equals LOCAL_SERVER_MODE. Can be 0 for any port. Ignored for other values of serverMode.
serverAddrFileThe name of the file that should be used to publish the monitor server address and port (if the server should be started). If empty, the name in the config file will be used instead.
Returns
Returns false in case of fatal errors.

◆ setData()

void GmSimulation::setData ( GmSimulationData data)
private

Changes the simulation data for this simulation.

WARNING: You probably don't want to call this function. Use it only if really know what you are doing. For the bold only!!!

If called with NULL will clear the simulation data and also the Lua environment. When called with a pointer, replaces the simulation data with the new pointer and also CLEARS the Lua environment.

◆ setSimulationFile()

bool GmSimulation::setSimulationFile ( QString  file,
const QMap< QString, QString > &  globalVars,
lua_State *  state 
)
private

Loads data defining the model to be simulated.

Parameters
filePath for the model file
globalVarsUser supplied values for global variables that will be definied on the simulation environment. The map should be keyed by the variable name and its value should be equal the string needed to initialize the variable following the Lua syntax, such as that the statement key = value can be interpreted by Lua. This is used, for example, to fill the userParams global variable.
stateThis optional parameter should be almost always be set to NULL. When filled with a pointer to a Lua state, that state will be used when loading the simulation file and later during simulation execution. This option helps the integration of the GeMA environment inside other environments where the control loop is outside the GeMA library. IMPORTANT: The Lua state MUST have been compiled with LUAI_EXTRASPACE, as explained in luaEnv.h. We also expect the extra area to be filled with 0 when the state is created.
Returns
Returns false in case of fatal errors.

◆ setupLogging()

bool GmSimulation::setupLogging ( LuaEnv env,
QString  logFile,
int  logMode 
)
private

Setup logging options.

Parameters
envLua environment loaded with configuration options
logFileLog file name. If empty, config file value will be used
logModeLog options overriding the config file
Returns
Returns false in case of fatal errors that prevent model use

◆ setupMonitorServer()

bool GmSimulation::setupMonitorServer ( LuaEnv env,
ServerMode  serverMode,
unsigned  localServerPort,
bool  logToServer,
QString  serverAddrFile 
)
private

Initializes the monitor server depending on the configuration from serverMode and from the Lua config file. If the server is initialized, sets _monitorServer.

Parameters
envLua environment loaded with configuration options
serverModeFlag defining if the monitor server should be turned on, off or use the Lua configuration settings.
localServerPortThe port where the server should listen for connections if serverMode equals LOCAL_SERVER_MODE. Can be 0 for any port. Ignored for other values of serverMode.
logToServerIs the logger configured to emit signals that cn be connected to the server, so that it can forward log messages?
serverAddrFileOptional path to the file where the server should advertise its address. If empty, the value from the Lua config file will be used.
Returns
Returns false in case of fatal errors that prevent model use

◆ setupUnitManager()

bool GmSimulation::setupUnitManager ( LuaEnv env)
private

Setup the unit manager.

Parameters
envLua environment loaded with configuration options
Returns
Returns false in case of fatal errors that prevent model use

The documentation for this class was generated from the following files: