FFSM++ 1.1.0
French Forest Sector Model ++
Loading...
Searching...
No Matches
ThreadManager.h
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3 * http://ffsm-project.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version, given the compliance with the *
9 * exceptions listed in the file COPYING that is distribued together *
10 * with this file. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22#ifndef THREAD_H
23#define THREAD_H
24
25#include <iostream>
26#include <string>
27#include <sstream>
28#include <random>
29#include <cmath>
30
31#include <QtCore>
32#include <QThread>
33#include <QString>
34#include <QColor>
35#include <QImage>
36#include <QMutex>
37
38#include "IpIpoptApplication.hpp"
39#include "IpSolveStatistics.hpp"
40#include "IpSmartPtr.hpp"
41
42// regmas includes..
43#include "BaseClass.h"
44
45class MainWindow;
46class ModelData;
47class Gis;
48class Init;
49class Scheduler;
50class Output;
51class ModelCore;
53class Opt;
54class Sandbox;
55class Carbon;
56
57using namespace std;
58
59/// Thread manager. Responsable to manage the main thread and "speak" with the GUI
60
61/**
62ThreadManager is responsable for the actions on the main thread (run/pause/resume/stop) and to speack with the GUI using the signal/slot tecniques.
63@author Antonello Lobianco
64*/
65class ThreadManager : public QThread, public BaseClass
66{
67 Q_OBJECT
68
69public:
71 // pointers..
72 ModelData* MD; ///< the model data object
73 Gis* GIS; ///< GIS information and methods
74 Init* INIT; ///< the Init object (pre-simulation scheduler)
75 Scheduler* SCD; ///< the scheduler object (simulation-loops scheduler)
76 Output* DO; ///< data output
77 ModelCore* CORE; ///< Core of the model
78 ModelCoreSpatial* SCORE; ///< Core of the model (spatial version)
79 Carbon* CBAL; ///< Module for the Carbon Balance
80 Sandbox* TEST; ///< Various debugging code for development
81 Ipopt::SmartPtr <Ipopt::TNLP> OPT; ///< Market optimisation
82 //std::random_device* randev; ///< used in the sampling from normal distribution 20150928: all random_device has been just be replaced with mt19937(time(0)), as largelly enought!
83 std::mt19937* gen; ///< used in the sampling from normal distribution
84
85 void setMessage(const QString &message);
86 void stop();
87 void deleteDeadOldPointers(); ///< Useful for several model running without leaving the GUI
88 void pauseOrResume();
89 void pause();
90 void resume();
91 void refreshGUI();
92 void msgOut(const int msgCode_h, const string message_h);
93 void addLayer(string layerName_h, string layerLabel_h);
94 void updatePixel(string layerName_h, int x_h, int y_h, QColor color);
95 void updateImage(string layerName_h, const QImage &image_h);
96 void upgradeMainSBLabel(const string message_h);
97 void upgradeYearSBLabel(int year);
98 string getBaseDirectory(){return baseDirectory.toStdString();};
99 string getInputFileName(){return inputFileName.toStdString();};
100 string getScenarioName() {return scenarioName.toStdString();};
101 void setScenarioName(const string &scenarioName_h){scenarioName=scenarioName_h.c_str();};
102 void setOutputDirName(string outputDirname_h);
103
104 /// the regional data object..
105 void setMDPointer(ModelData *MD_h){MD=MD_h;};
106 /// GIS information and methods..
107 void setGISPointer(Gis *GIS_h){GIS=GIS_h;};
108 /// the Init object, it schedule the pre-simulation phase..
109 void setINITPointer(Init *INIT_h){INIT=INIT_h;};
110 /// the sandbox object for within-development quick tests
111 void setTestPointer(Sandbox *TEST_h){TEST=TEST_h;};
112 /// the scheduler object. It manage the simulation loops..
113 void setSCDPointer(Scheduler *SCD_h){SCD=SCD_h;};
114 /// manage the printing of data needed for scenario-analisys. The "message output" (needed to see "what is it happening?" are instead simply printed with msgOut()..
115 void setDOPointer(Output *DO_h){DO=DO_h;};
116 /// Perform the algorithms of the model
117 void setCOREPointer(ModelCore* CORE_h){CORE=CORE_h;};
118 /// Perform the algorithms of the model
119 void setSCOREPointer(ModelCoreSpatial* SCORE_h){SCORE=SCORE_h;};
120 /// Perform the market optimisation
121 void setOPTPointer(Ipopt::SmartPtr<Ipopt::TNLP> OPT_h){OPT=OPT_h;};
122 /// Module that account for the Carbon Balance
123 void setCBALPointer(Carbon *CBAL_h){CBAL=CBAL_h;};
124
125 //public slots:
126 void setInputFileName(QString inputFileName_h);
127 //void setBaseDirectory(QString baseDirectory_h){baseDirectory = baseDirectory_h;};
128
129 // tree viewer operations...
130 /*
131 void treeViewerAddManager(string name){emit treeViewerAddItemToGui("Manager "+name, "manager_"+name, "managers");};
132 void treeViewerAddAgent(int uniqueID){emit treeViewerAddItemToGui(i2s(uniqueID), "agent_"+i2s(uniqueID), "agents"); };
133 void treeViewerAddManagerProperty(string managerName, string propertyName){
134 emit treeViewerAddItemToGui(propertyName, "manager_"+managerName+"_"+propertyName, "manager_"+managerName);};
135 void treeViewerAddAgentProperty(int uniqueID, string propertyName){
136 emit treeViewerAddItemToGui(propertyName, "agent_"+i2s(uniqueID)+"_"+propertyName, "agent_"+i2s(uniqueID));};
137 void treeViewerManagerPropertyChangeValue(string managerName, string propertyName, string newValue){
138 emit treeViewerItemChangeValueToGui("manager_"+managerName+"_"+propertyName, newValue);};
139 void treeViewerAgentPropertyChangeValue(int uniqueID, string propertyName, string newValue){
140 emit treeViewerItemChangeValueToGui("agent_"+i2s(uniqueID)+"_"+propertyName, newValue);};
141 void treeViewerRemoveManager(string name){emit treeViewerItemRemoveToGui("manager_"+name);};
142 void treeViewerRemoveAgent(int uniqueID){emit treeViewerItemRemoveToGui("agent_"+i2s(uniqueID));};
143 */
144 void treeViewerChangeGeneralPropertyValue(string propertyName, string newValue){
145 emit treeViewerItemChangeValueToGui("general_"+propertyName, newValue);};
146
147
148 void fitInWindow() {emit fitInWindowToGui();}; ///< Re-draw the map making it to fit (with the right proportions) to the widget
149 void runFromConsole(QString inputFileName_h, QString scenarioName_h);
150 bool usingGUI(){return GUI;};
151
152signals:
153 void upgradeLogArea(const QString &logMessage);
154 void upgradeMainSBLabelToGui(const QString &logMessage);
155 void upgradeYearSBLabelToGui(const QString &logMessage);
156 void addLayerToGui(QString layerName, QString layerLabel);
157 void updatePixelToGui(QString layerName_h, int x_h, int y_h, QColor color);
158 void updateImageToGui(QString layerName_h, QImage image_h);
159 void setOutputDirNameToGui(string outputDirname_h);
160 void setGUIUnsavedStatus(bool status_h);
161 void setGUIMapDimension(int x_h, int y_h);
162 void treeViewerItemChangeValueToGui(string itemID, string newValue);
163 void treeViewerItemRemoveToGui(string itemID);
164 void treeViewerAddItemToGui(string text, string itemID, string parentID);
166 void queryRequestOnPx(int px_ID, int currentLayerIndex);
167 void publishQueryResults(const QString &results);
168 void activateTab(int pos_h);
170 void sendScenarioOptionsToGUI(const QVector<QString> &scenarios_h);
171
172
173public slots:
174 /// Switch and control the access to pxQueryID and layerQueryPos members
175 void checkQuery(int px_ID, int currentLayerIndex, bool newRequest=true);
176 /// Compute the pixel query and return it to the GUI (with a signal)
177 void computeQuery(int px_ID, int currentLayerIndex);
178 void retrieveScenarioNameFromGUI(const QString &scenarioName_h);
179
180protected:
181 void run();
182
183private:
184 QString messageStr;
185 volatile bool stopped;
186 volatile bool running;
190 volatile int pxQueryID;
191 volatile int layerQueryPos;
192 QMutex mutex;
193 bool GUI;
194
195};
196
197
198
199#endif
200
This file is the header of BaseClass and it is included by ALL compiled code.
Base class for the regmas application.
Definition BaseClass.h:239
Class responsable to keep the logbook of the Carbon Balance.
Definition Carbon.h:50
Class to manage the spatial dimension.
Definition Gis.h:67
Init the environment, the objects and the agents of the model
Definition Init.h:45
Main GUI interface.
Definition MainWindow.h:50
The core of the model (spatial version).
Regional data, including macros and settings.
Definition ModelData.h:80
Definition Opt.h:52
Output methods
Definition Output.h:48
Manage the yearly loops.
Definition Scheduler.h:42
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
QString scenarioName
QString baseDirectory
void setCBALPointer(Carbon *CBAL_h)
Module that account for the Carbon Balance.
void activateTab(int pos_h)
void sendScenarioOptionsToGUI(const QVector< QString > &scenarios_h)
void setDOPointer(Output *DO_h)
manage the printing of data needed for scenario-analisys. The "message output" (needed to see "what i...
void setOPTPointer(Ipopt::SmartPtr< Ipopt::TNLP > OPT_h)
Perform the market optimisation.
void fitInWindowToGui()
void setScenarioName(const string &scenarioName_h)
Init * INIT
the Init object (pre-simulation scheduler)
void setGISPointer(Gis *GIS_h)
GIS information and methods..
QString inputFileName
void upgradeMainSBLabelToGui(const QString &logMessage)
volatile int pxQueryID
void treeViewerItemRemoveToGui(string itemID)
string getInputFileName()
string getBaseDirectory()
void resetGUIForNewSimulation()
void upgradeMainSBLabel(const string message_h)
void updateImage(string layerName_h, const QImage &image_h)
void updatePixel(string layerName_h, int x_h, int y_h, QColor color)
void runFromConsole(QString inputFileName_h, QString scenarioName_h)
Re-draw the map making it to fit (with the right proportions) to the widget.
Carbon * CBAL
Module for the Carbon Balance.
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
ModelCoreSpatial * SCORE
Core of the model (spatial version)
void publishQueryResults(const QString &results)
Sandbox * TEST
Various debugging code for development.
void addLayerToGui(QString layerName, QString layerLabel)
string getScenarioName()
void treeViewerItemChangeValueToGui(string itemID, string newValue)
void setSCDPointer(Scheduler *SCD_h)
the scheduler object. It manage the simulation loops..
void upgradeYearSBLabelToGui(const QString &logMessage)
void treeViewerChangeGeneralPropertyValue(string propertyName, string newValue)
volatile bool running
void msgOut(const int msgCode_h, const string message_h)
Gis * GIS
GIS information and methods.
volatile int layerQueryPos
volatile bool stopped
ModelData * MD
the model data object
Ipopt::SmartPtr< Ipopt::TNLP > OPT
Market optimisation.
void updateImageToGui(QString layerName_h, QImage image_h)
std::mt19937 * gen
used in the sampling from normal distribution
void setGUIUnsavedStatus(bool status_h)
void setTestPointer(Sandbox *TEST_h)
the sandbox object for within-development quick tests
void queryRequestOnPx(int px_ID, int currentLayerIndex)
void addLayer(string layerName_h, string layerLabel_h)
void deleteDeadOldPointers()
Useful for several model running without leaving the GUI.
Output * DO
data output
ModelCore * CORE
Core of the model.
void setCOREPointer(ModelCore *CORE_h)
Perform the algorithms of the model.
void setMDPointer(ModelData *MD_h)
the regional data object..
void upgradeYearSBLabel(int year)
void setInputFileName(QString inputFileName_h)
void setINITPointer(Init *INIT_h)
the Init object, it schedule the pre-simulation phase..
void setSCOREPointer(ModelCoreSpatial *SCORE_h)
Perform the algorithms of the model.
void setMessage(const QString &message)
void treeViewerAddItemToGui(string text, string itemID, string parentID)
void setGUIMapDimension(int x_h, int y_h)
void checkQuery(int px_ID, int currentLayerIndex, bool newRequest=true)
Switch and control the access to pxQueryID and layerQueryPos members.
void setOutputDirNameToGui(string outputDirname_h)
void upgradeLogArea(const QString &logMessage)
void retrieveScenarioNameFromGUI(const QString &scenarioName_h)
void updatePixelToGui(QString layerName_h, int x_h, int y_h, QColor color)
void computeQuery(int px_ID, int currentLayerIndex)
Compute the pixel query and return it to the GUI (with a signal)
void setOutputDirName(string outputDirname_h)