Subversion Repositories public iLand

Rev

Rev 837 | Rev 984 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 837 Rev 947
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/ilandc/consoleshell.cpp':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/ilandc/consoleshell.cpp':
2
/********************************************************************************************
2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
6
**
7
**    This program is free software: you can redistribute it and/or modify
7
**    This program is free software: you can redistribute it and/or modify
8
**    it under the terms of the GNU General Public License as published by
8
**    it under the terms of the GNU General Public License as published by
9
**    the Free Software Foundation, either version 3 of the License, or
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (at your option) any later version.
10
**    (at your option) any later version.
11
**
11
**
12
**    This program is distributed in the hope that it will be useful,
12
**    This program is distributed in the hope that it will be useful,
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
**    GNU General Public License for more details.
15
**    GNU General Public License for more details.
16
**
16
**
17
**    You should have received a copy of the GNU General Public License
17
**    You should have received a copy of the GNU General Public License
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
19
********************************************************************************************/
20
20
21
#include "consoleshell.h"
21
#include "consoleshell.h"
22
22
23
#include <QtCore>
23
#include <QtCore>
24
#include <QKeyEvent>
24
#include <QKeyEvent>
25
25
26
#include "global.h"
26
#include "global.h"
27
#include "model.h"
27
#include "model.h"
28
#include "modelcontroller.h"
28
#include "modelcontroller.h"
29
#include "version.h"
29
#include "../iland/version.h"
30
30
31
QTextStream *ConsoleShell::mLogStream = 0;
31
QTextStream *ConsoleShell::mLogStream = 0;
32
32
33
// a try to really get keyboard strokes in console mode...
33
// a try to really get keyboard strokes in console mode...
34
// did not work.
34
// did not work.
35
class KeyboardTaker : public QThread
35
class KeyboardTaker : public QThread
36
 {
36
 {
37
 public:
37
 public:
38
     void run();
38
     void run();
39
     bool stop;
39
     bool stop;
40
 };
40
 };
41
41
42
 void KeyboardTaker::run()
42
 void KeyboardTaker::run()
43
 {
43
 {
44
     stop = false;
44
     stop = false;
45
     QTextStream qin(stdin, QFile::ReadOnly);
45
     QTextStream qin(stdin, QFile::ReadOnly);
46
     while (!stop) {
46
     while (!stop) {
47
         QString input = qin.read(1);
47
         QString input = qin.read(1);
48
         if (!input.isNull()) {
48
         if (!input.isNull()) {
49
             // .. process input
49
             // .. process input
50
             qWarning() << "input:" << input;
50
             qWarning() << "input:" << input;
51
         }
51
         }
52
     }
52
     }
53
 }
53
 }
54
54
55
ConsoleShell::ConsoleShell()
55
ConsoleShell::ConsoleShell()
56
{
56
{
57
}
57
}
58
58
59
/*
59
/*
60
*/
60
*/
61
61
62
void ConsoleShell::run()
62
void ConsoleShell::run()
63
{
63
{
64
64
65
    QString xml_name = QCoreApplication::arguments().at(1);
65
    QString xml_name = QCoreApplication::arguments().at(1);
66
    // get the number of years to run...
66
    // get the number of years to run...
67
    bool ok;
67
    bool ok;
68
    int years = QCoreApplication::arguments().at(2).toInt(&ok);
68
    int years = QCoreApplication::arguments().at(2).toInt(&ok);
69
    if (years<0 || !ok) {
69
    if (years<0 || !ok) {
70
        qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!";
70
        qDebug() << QCoreApplication::arguments().at(2) << "is an invalid number of years to run!";
71
        return;
71
        return;
72
    }
72
    }
73
73
74
    if (!QFile::exists(xml_name)) {
74
    if (!QFile::exists(xml_name)) {
75
        qDebug() << "invalid XML project file: " << xml_name;
75
        qDebug() << "invalid XML project file: " << xml_name;
76
        return;
76
        return;
77
    }
77
    }
78
    try {
78
    try {
79
79
80
        ModelController iland_model;
80
        ModelController iland_model;
81
        QObject::connect(&iland_model, SIGNAL(year(int)),SLOT(runYear(int)));
81
        QObject::connect(&iland_model, SIGNAL(year(int)),SLOT(runYear(int)));
82
        iland_model.setFileName(xml_name);
82
        iland_model.setFileName(xml_name);
83
        if (iland_model.hasError()) {
83
        if (iland_model.hasError()) {
84
            qWarning() << "!!!! ERROR !!!!";
84
            qWarning() << "!!!! ERROR !!!!";
85
            qWarning() << iland_model.lastError();
85
            qWarning() << iland_model.lastError();
86
            qWarning() << "!!!! ERROR !!!!";
86
            qWarning() << "!!!! ERROR !!!!";
87
            return;
87
            return;
88
        }
88
        }
89
89
90
        setupLogging();
90
        setupLogging();
91
        mParams.clear();
91
        mParams.clear();
92
        if (QCoreApplication::arguments().count()>3) {
92
        if (QCoreApplication::arguments().count()>3) {
93
            qWarning() << "set command line values:";
93
            qWarning() << "set command line values:";
94
            for (int i=3;i<QCoreApplication::arguments().count();++i) {
94
            for (int i=3;i<QCoreApplication::arguments().count();++i) {
95
                QString line = QCoreApplication::arguments().at(i);
95
                QString line = QCoreApplication::arguments().at(i);
96
                mParams.append(line);
96
                mParams.append(line);
97
                QString key = line.left(line.indexOf('='));
97
                QString key = line.left(line.indexOf('='));
98
                QString value = line.mid(line.indexOf('=')+1);
98
                QString value = line.mid(line.indexOf('=')+1);
99
                qWarning() << "set" << key << "to value:" << value;
99
                qWarning() << "set" << key << "to value:" << value;
100
                const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value);
100
                const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value);
101
            }
101
            }
102
        }
102
        }
103
        qDebug() << "**************************************************";
103
        qDebug() << "**************************************************";
104
        qDebug() << "***********     iLand console session     ********";
104
        qDebug() << "***********     iLand console session     ********";
105
        qDebug() << "**************************************************";
105
        qDebug() << "**************************************************";
106
        qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate);
106
        qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate);
107
        qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")";
107
        qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")";
108
        qDebug() << "**************************************************";
108
        qDebug() << "**************************************************";
109
109
110
        qWarning() << "*** creating model...";
110
        qWarning() << "*** creating model...";
111
        qWarning() << "**************************************************";
111
        qWarning() << "**************************************************";
112
112
113
        iland_model.create();
113
        iland_model.create();
114
        if (iland_model.hasError()) {
114
        if (iland_model.hasError()) {
115
            qWarning() << "!!!! ERROR !!!!";
115
            qWarning() << "!!!! ERROR !!!!";
116
            qWarning() << iland_model.lastError();
116
            qWarning() << iland_model.lastError();
117
            qWarning() << "!!!! ERROR !!!!";
117
            qWarning() << "!!!! ERROR !!!!";
118
            return;
118
            return;
119
        }
119
        }
120
        runJavascript("onCreate");
120
        runJavascript("onCreate");
121
        qWarning() << "**************************************************";
121
        qWarning() << "**************************************************";
122
        qWarning() << "*** running model for" << years << "years";
122
        qWarning() << "*** running model for" << years << "years";
123
        qWarning() << "**************************************************";
123
        qWarning() << "**************************************************";
124
124
125
        iland_model.run(years + 1);
125
        iland_model.run(years + 1);
126
        if (iland_model.hasError()) {
126
        if (iland_model.hasError()) {
127
            qWarning() << "!!!! ERROR !!!!";
127
            qWarning() << "!!!! ERROR !!!!";
128
            qWarning() << iland_model.lastError();
128
            qWarning() << iland_model.lastError();
129
            qWarning() << "!!!! ERROR !!!!";
129
            qWarning() << "!!!! ERROR !!!!";
130
            return;
130
            return;
131
        }
131
        }
132
        runJavascript("onFinish");
132
        runJavascript("onFinish");
133
133
134
        qWarning() << "**************************************************";
134
        qWarning() << "**************************************************";
135
        qWarning() << "*** model run finished.";
135
        qWarning() << "*** model run finished.";
136
        qWarning() << "*** " << QDateTime::currentDateTime();
136
        qWarning() << "*** " << QDateTime::currentDateTime();
137
        qWarning() << "**************************************************";
137
        qWarning() << "**************************************************";
138
138
139
    } catch (const IException &e) {
139
    } catch (const IException &e) {
140
        qWarning() << "*** An exception occured ***";
140
        qWarning() << "*** An exception occured ***";
141
        qWarning() << e.message();
141
        qWarning() << e.message();
142
    }
142
    }
143
    catch (const std::exception &e) {
143
    catch (const std::exception &e) {
144
        qWarning() << "*** An (std)exception occured ***";
144
        qWarning() << "*** An (std)exception occured ***";
145
        qWarning() << e.what();
145
        qWarning() << e.what();
146
    }
146
    }
147
    QCoreApplication::quit();
147
    QCoreApplication::quit();
148
148
149
149
150
}
150
}
151
151
152
void ConsoleShell::runYear(int year)
152
void ConsoleShell::runYear(int year)
153
{
153
{
154
    printf("simulating year %d ...\n", year-1);
154
    printf("simulating year %d ...\n", year-1);
155
}
155
}
156
156
157
QMutex qdebug_mutex;
157
QMutex qdebug_mutex;
158
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
158
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
159
 {
159
 {
160
    Q_UNUSED(context);
160
    Q_UNUSED(context);
161
    QMutexLocker m(&qdebug_mutex);
161
    QMutexLocker m(&qdebug_mutex);
162
162
163
    switch (type) {
163
    switch (type) {
164
     case QtDebugMsg:
164
     case QtDebugMsg:
165
        *ConsoleShell::logStream() << msg << endl;
165
        *ConsoleShell::logStream() << msg << endl;
166
         break;
166
         break;
167
     case QtWarningMsg:
167
     case QtWarningMsg:
168
        *ConsoleShell::logStream() << msg << endl;
168
        *ConsoleShell::logStream() << msg << endl;
169
        printf("Warning: %s\n", msg);
169
        printf("Warning: %s\n", msg);
170
170
171
         break;
171
         break;
172
     case QtCriticalMsg:
172
     case QtCriticalMsg:
173
        *ConsoleShell::logStream() << msg << endl;
173
        *ConsoleShell::logStream() << msg << endl;
174
        printf("Critical: %s\n", msg);
174
        printf("Critical: %s\n", msg);
175
         break;
175
         break;
176
     case QtFatalMsg:
176
     case QtFatalMsg:
177
        *ConsoleShell::logStream() << msg << endl;
177
        *ConsoleShell::logStream() << msg << endl;
178
        printf("Fatal: %s\n", msg);
178
        printf("Fatal: %s\n", msg);
179
     }
179
     }
180
 }
180
 }
181
181
182
182
183
void ConsoleShell::setupLogging()
183
void ConsoleShell::setupLogging()
184
{
184
{
185
    if (mLogStream) {
185
    if (mLogStream) {
186
        if (mLogStream->device())
186
        if (mLogStream->device())
187
            delete mLogStream->device();
187
            delete mLogStream->device();
188
        delete mLogStream;
188
        delete mLogStream;
189
        mLogStream = NULL;
189
        mLogStream = NULL;
190
    }
190
    }
191
191
192
    QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt");
192
    QString fname = GlobalSettings::instance()->settings().value("system.logging.logFile", "logfile.txt");
193
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
193
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
194
    fname.replace("$date$", timestamp);
194
    fname.replace("$date$", timestamp);
195
    fname = GlobalSettings::instance()->path(fname, "log");
195
    fname = GlobalSettings::instance()->path(fname, "log");
196
    QFile *file = new QFile(fname);
196
    QFile *file = new QFile(fname);
197
197
198
    if (!file->open(QIODevice::WriteOnly)) {
198
    if (!file->open(QIODevice::WriteOnly)) {
199
        qDebug() << "cannot open logfile" << fname;
199
        qDebug() << "cannot open logfile" << fname;
200
    } else {
200
    } else {
201
        qDebug() << "Log output is redirected to logfile" << fname;
201
        qDebug() << "Log output is redirected to logfile" << fname;
202
        mLogStream = new QTextStream(file);
202
        mLogStream = new QTextStream(file);
203
    }
203
    }
204
    qInstallMessageHandler(myMessageOutput);
204
    qInstallMessageHandler(myMessageOutput);
205
205
206
206
207
}
207
}
208
208
209
void ConsoleShell::runJavascript(const QString key)
209
void ConsoleShell::runJavascript(const QString key)
210
{
210
{
211
    for (int i=0;i<mParams.count(); ++i) {
211
    for (int i=0;i<mParams.count(); ++i) {
212
        QString line=mParams[i];
212
        QString line=mParams[i];
213
        QString pkey = line.left(line.indexOf('='));
213
        QString pkey = line.left(line.indexOf('='));
214
        if (pkey == key) {
214
        if (pkey == key) {
215
            QString command = line.mid(line.indexOf('=')+1);
215
            QString command = line.mid(line.indexOf('=')+1);
216
            // execute the function
216
            // execute the function
217
            qWarning() << "executing trigger" << key;
217
            qWarning() << "executing trigger" << key;
218
            qWarning() << GlobalSettings::instance()->executeJavascript(command);
218
            qWarning() << GlobalSettings::instance()->executeJavascript(command);
219
        }
219
        }
220
    }
220
    }
221
221
222
222
223
}
223
}
224
224
225
 
225