FFSM++  1.1.0
French Forest Sector Model ++
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 
45 class MainWindow;
46 class ModelData;
47 class Gis;
48 class Init;
49 class Scheduler;
50 class Output;
51 class ModelCore;
52 class ModelCoreSpatial;
53 class Opt;
54 class Sandbox;
55 class Carbon;
56 
57 using namespace std;
58 
59 /// Thread manager. Responsable to manage the main thread and "speak" with the GUI
60 
61 /**
62 ThreadManager 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 */
65 class ThreadManager : public QThread, public BaseClass
66 {
67  Q_OBJECT
68 
69 public:
70  ThreadManager();
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 
152 signals:
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);
165  void fitInWindowToGui();
166  void queryRequestOnPx(int px_ID, int currentLayerIndex);
167  void publishQueryResults(const QString &results);
168  void activateTab(int pos_h);
169  void resetGUIForNewSimulation();
170  void sendScenarioOptionsToGUI(const QVector<QString> &scenarios_h);
171 
172 
173 public 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 
180 protected:
181  void run();
182 
183 private:
184  QString messageStr;
185  volatile bool stopped;
186  volatile bool running;
187  QString inputFileName;
188  QString baseDirectory;
189  QString scenarioName;
190  volatile int pxQueryID;
191  volatile int layerQueryPos;
192  QMutex mutex;
193  bool GUI;
194 
195 };
196 
197 
198 
199 #endif
200 
volatile bool stopped
QString scenarioName
void setSCDPointer(Scheduler *SCD_h)
the scheduler object. It manage the simulation loops..
Manage the yearly loops.
Definition: Scheduler.h:42
def text(cat, text_h)
volatile bool running
volatile int layerQueryPos
ModelData * MD
the model data object
Definition: ThreadManager.h:72
Init * INIT
the Init object (pre-simulation scheduler)
Definition: ThreadManager.h:74
Init the environment, the objects and the agents of the model
Definition: Init.h:45
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
Definition: ThreadManager.h:75
STL namespace.
Gis * GIS
GIS information and methods.
Definition: ThreadManager.h:73
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
void setSCOREPointer(ModelCoreSpatial *SCORE_h)
Perform the algorithms of the model.
Regional data, including macros and settings.
Definition: ModelData.h:80
void setScenarioName(const string &scenarioName_h)
ModelCore * CORE
Core of the model.
Definition: ThreadManager.h:77
Output methods
Definition: Output.h:48
ModelCoreSpatial * SCORE
Core of the model (spatial version)
Definition: ThreadManager.h:78
void setCOREPointer(ModelCore *CORE_h)
Perform the algorithms of the model.
Output * DO
data output
Definition: ThreadManager.h:76
void setTestPointer(Sandbox *TEST_h)
the sandbox object for within-development quick tests
Ipopt::SmartPtr< Ipopt::TNLP > OPT
Market optimisation.
Definition: ThreadManager.h:81
Carbon * CBAL
Module for the Carbon Balance.
Definition: ThreadManager.h:79
MainWindow()
Constructor.
Definition: MainWindow.cpp:39
void setINITPointer(Init *INIT_h)
the Init object, it schedule the pre-simulation phase..
string getScenarioName()
Base class for the regmas application.
Definition: BaseClass.h:239
Class to manage the spatial dimension.
Definition: Gis.h:67
void setCBALPointer(Carbon *CBAL_h)
Module that account for the Carbon Balance.
void treeViewerChangeGeneralPropertyValue(string propertyName, string newValue)
string getInputFileName()
Definition: ThreadManager.h:99
Class responsable to keep the logbook of the Carbon Balance.
Definition: Carbon.h:50
std::mt19937 * gen
used in the sampling from normal distribution
Definition: ThreadManager.h:83
void setGISPointer(Gis *GIS_h)
GIS information and methods..
Definition: Opt.h:52
QString messageStr
string getBaseDirectory()
Definition: ThreadManager.h:98
volatile int pxQueryID
The core of the model (spatial version).
void setMDPointer(ModelData *MD_h)
the regional data object..
void setOPTPointer(Ipopt::SmartPtr< Ipopt::TNLP > OPT_h)
Perform the market optimisation.
QString baseDirectory
QString inputFileName
This file is the header of BaseClass and it is included by ALL compiled code.
void fitInWindow()
void setDOPointer(Output *DO_h)
manage the printing of data needed for scenario-analisys. The "message output" (needed to see "what i...
Sandbox * TEST
Various debugging code for development.
Definition: ThreadManager.h:80