Subversion Repositories public iLand

Rev

Rev 780 | Rev 947 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 780 Rev 837
Line 61... Line 61...
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
    if (!QFile::exists(xml_name)) {
-
 
67
        qDebug() << "invalid XML project file: " << xml_name;
-
 
68
        return;
-
 
69
    }
-
 
70
    // get the number of years to run...
66
    // get the number of years to run...
71
    bool ok;
67
    bool ok;
72
    int years = QCoreApplication::arguments().at(2).toInt(&ok);
68
    int years = QCoreApplication::arguments().at(2).toInt(&ok);
73
    if (years<0 || !ok) {
69
    if (years<0 || !ok) {
74
        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!";
75
        return;
71
        return;
76
    }
72
    }
77
73
-
 
74
    if (!QFile::exists(xml_name)) {
-
 
75
        qDebug() << "invalid XML project file: " << xml_name;
-
 
76
        return;
-
 
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()) {
-
 
84
            qWarning() << "!!!! ERROR !!!!";
-
 
85
            qWarning() << iland_model.lastError();
-
 
86
            qWarning() << "!!!! ERROR !!!!";
-
 
87
            return;
-
 
88
        }
83
89
84
        setupLogging();
90
        setupLogging();
-
 
91
        mParams.clear();
-
 
92
        if (QCoreApplication::arguments().count()>3) {
-
 
93
            qWarning() << "set command line values:";
-
 
94
            for (int i=3;i<QCoreApplication::arguments().count();++i) {
-
 
95
                QString line = QCoreApplication::arguments().at(i);
-
 
96
                mParams.append(line);
-
 
97
                QString key = line.left(line.indexOf('='));
-
 
98
                QString value = line.mid(line.indexOf('=')+1);
-
 
99
                qWarning() << "set" << key << "to value:" << value;
-
 
100
                const_cast<XmlHelper&>(GlobalSettings::instance()->settings()).setNodeValue(key, value);
-
 
101
            }
85
102
        }
86
        qDebug() << "**************************************************";
103
        qDebug() << "**************************************************";
87
        qDebug() << "***********     iLand console session     ********";
104
        qDebug() << "***********     iLand console session     ********";
88
        qDebug() << "**************************************************";
105
        qDebug() << "**************************************************";
89
        qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate);
106
        qDebug() << "started at: " << QDateTime::currentDateTime().toString(Qt::ISODate);
90
        qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")";
107
        qDebug() << "iLand " << currentVersion() << " (" << svnRevision() << ")";
Line 92... Line 109...
92
109
93
        qWarning() << "*** creating model...";
110
        qWarning() << "*** creating model...";
94
        qWarning() << "**************************************************";
111
        qWarning() << "**************************************************";
95
112
96
        iland_model.create();
113
        iland_model.create();
-
 
114
        if (iland_model.hasError()) {
-
 
115
            qWarning() << "!!!! ERROR !!!!";
-
 
116
            qWarning() << iland_model.lastError();
-
 
117
            qWarning() << "!!!! ERROR !!!!";
-
 
118
            return;
-
 
119
        }
-
 
120
        runJavascript("onCreate");
97
        qWarning() << "**************************************************";
121
        qWarning() << "**************************************************";
98
        qWarning() << "*** running model for" << years << "years";
122
        qWarning() << "*** running model for" << years << "years";
99
        qWarning() << "**************************************************";
123
        qWarning() << "**************************************************";
100
124
101
        iland_model.run(years + 1);
125
        iland_model.run(years + 1);
-
 
126
        if (iland_model.hasError()) {
-
 
127
            qWarning() << "!!!! ERROR !!!!";
-
 
128
            qWarning() << iland_model.lastError();
-
 
129
            qWarning() << "!!!! ERROR !!!!";
-
 
130
            return;
-
 
131
        }
-
 
132
        runJavascript("onFinish");
102
133
103
        qWarning() << "**************************************************";
134
        qWarning() << "**************************************************";
104
        qWarning() << "*** model run finished.";
135
        qWarning() << "*** model run finished.";
105
        qWarning() << "*** " << QDateTime::currentDateTime();
136
        qWarning() << "*** " << QDateTime::currentDateTime();
106
        qWarning() << "**************************************************";
137
        qWarning() << "**************************************************";
Line 108... Line 139...
108
    } catch (const IException &e) {
139
    } catch (const IException &e) {
109
        qWarning() << "*** An exception occured ***";
140
        qWarning() << "*** An exception occured ***";
110
        qWarning() << e.message();
141
        qWarning() << e.message();
111
    }
142
    }
112
    catch (const std::exception &e) {
143
    catch (const std::exception &e) {
113
        qWarning() << "*** An exception occured ***";
144
        qWarning() << "*** An (std)exception occured ***";
114
        qWarning() << e.what();
145
        qWarning() << e.what();
115
    }
146
    }
116
    QCoreApplication::quit();
147
    QCoreApplication::quit();
117
148
118
149
Line 169... Line 200...
169
    } else {
200
    } else {
170
        qDebug() << "Log output is redirected to logfile" << fname;
201
        qDebug() << "Log output is redirected to logfile" << fname;
171
        mLogStream = new QTextStream(file);
202
        mLogStream = new QTextStream(file);
172
    }
203
    }
173
    qInstallMessageHandler(myMessageOutput);
204
    qInstallMessageHandler(myMessageOutput);
-
 
205
-
 
206
-
 
207
}
-
 
208
-
 
209
void ConsoleShell::runJavascript(const QString key)
-
 
210
{
-
 
211
    for (int i=0;i<mParams.count(); ++i) {
-
 
212
        QString line=mParams[i];
-
 
213
        QString pkey = line.left(line.indexOf('='));
-
 
214
        if (pkey == key) {
-
 
215
            QString command = line.mid(line.indexOf('=')+1);
-
 
216
            // execute the function
-
 
217
            qWarning() << "executing trigger" << key;
-
 
218
            qWarning() << GlobalSettings::instance()->executeJavascript(command);
-
 
219
        }
-
 
220
    }
174
221
175
222
176
}
223
}
177
224