Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
19
 
641 werner 20
#include "global.h"
21
#include "modules.h"
22
#include "plugin_interface.h"
23
 
24
#include "globalsettings.h"
808 werner 25
#include "debugtimer.h"
769 werner 26
#include "exception.h"
780 werner 27
#include <QtPlugin>
641 werner 28
 
29
// include the static modules here in the code:
781 werner 30
#if QT_VERSION >= 0x050000
780 werner 31
Q_IMPORT_PLUGIN(FirePlugin)
32
Q_IMPORT_PLUGIN(WindPlugin)
959 werner 33
Q_IMPORT_PLUGIN(BarkBeetlePlugin)
781 werner 34
#else
35
Q_IMPORT_PLUGIN(iland_fire)
36
Q_IMPORT_PLUGIN(iland_wind)
959 werner 37
Q_IMPORT_PLUGIN(iland_barkbeetle)
781 werner 38
#endif
641 werner 39
 
40
Modules::Modules()
41
{
42
    init();
43
}
44
 
45
// load the static plugins
46
void Modules::init()
47
{
48
    foreach (QObject *plugin, QPluginLoader::staticInstances()) {
49
        DisturbanceInterface *di = qobject_cast<DisturbanceInterface *>(plugin);
50
        if (di) {
51
            qDebug() << di->name();
52
            // check xml file
53
            if (GlobalSettings::instance()->settings().valueBool(QString("modules.%1.enabled").arg(di->name()))) {
54
                // plugin is enabled: store in list of active modules
55
                mInterfaces.append(di);
646 werner 56
                // check for other interfaces
57
                SetupResourceUnitInterface *si=qobject_cast<SetupResourceUnitInterface *>(plugin);
58
                if (si)
59
                    mSetupRUs.append(si);
60
                WaterInterface *wi = qobject_cast<WaterInterface *>(plugin);
61
                if (wi)
62
                    mWater.append(wi);
1044 werner 63
                TreeDeathInterface *td = qobject_cast<TreeDeathInterface*>(plugin);
64
                if (td)
65
                    mTreeDeath.append(td);
641 werner 66
            }
67
        }
68
    }
69
 
1065 werner 70
    // fix the order of modules: make sure that "barkbeetle" is after "wind"
71
    DisturbanceInterface *wind = module(QStringLiteral("wind"));
72
    DisturbanceInterface *bb = module(QStringLiteral("barkbeetle"));
73
    if (wind && bb) {
74
        int iw = mInterfaces.indexOf(wind), ib = mInterfaces.indexOf(bb);
75
        if (ib<iw)
76
            mInterfaces.swap(iw, ib);
77
    }
78
 
79
 
641 werner 80
}
646 werner 81
 
652 werner 82
DisturbanceInterface * Modules::module(const QString &module_name)
83
{
84
    foreach(DisturbanceInterface *di, mInterfaces)
85
        if (di->name() == module_name)
86
            return di;
87
    return 0;
88
}
89
 
646 werner 90
void Modules::setupResourceUnit(const ResourceUnit *ru)
91
{
92
    foreach(SetupResourceUnitInterface *si, mSetupRUs)
93
        si->setupResourceUnit(ru);
94
}
95
 
96
void Modules::setup()
97
{
767 werner 98
 
646 werner 99
    foreach(DisturbanceInterface *di, mInterfaces)
100
        di->setup();
101
 
961 werner 102
    // set up the scripting (i.e., Javascript)
793 werner 103
    QJSEngine *engine = GlobalSettings::instance()->scriptEngine();
674 werner 104
    foreach(DisturbanceInterface *di, mInterfaces)
105
        di->setupScripting(engine);
106
}
107
 
108
 
646 werner 109
void Modules::calculateWater(const ResourceUnit *resource_unit, const WaterCycleData *water_data)
110
{
111
    foreach(WaterInterface *wi, mWater)
112
        wi->calculateWater(resource_unit, water_data);
113
}
649 werner 114
 
1044 werner 115
void Modules::treeDeath(const Tree *tree, int removal_type)
116
{
1050 werner 117
    if (mTreeDeath.isEmpty())
118
        return;
119
 
1044 werner 120
    for (QList<TreeDeathInterface*>::const_iterator it=mTreeDeath.constBegin(); it!=mTreeDeath.constEnd(); ++it)
121
        (*it)->treeDeath(tree, removal_type);
122
 
123
}
124
 
649 werner 125
void Modules::run()
126
{
127
    DebugTimer t("modules");
128
 
1065 werner 129
    // *** run in fixed order ***
130
    foreach(DisturbanceInterface *di, mInterfaces) {
769 werner 131
        try {
1065 werner 132
            di->run();
769 werner 133
        } catch (const IException &e) {
1065 werner 134
            qWarning() << "ERROR: uncaught exception in module '" << di->name() << "':";
769 werner 135
            qWarning() << "ERROR:" << e.message();
136
            qWarning() << " **************************************** ";
137
        }
1065 werner 138
    }
769 werner 139
 
1065 werner 140
 
141
    // *** run in random order ****
142
    //    QList<DisturbanceInterface*> run_list = mInterfaces;
143
 
144
    //    // execute modules in random order
145
    //    while (!run_list.isEmpty()) {
146
    //        int idx = irandom(0, run_list.size()-1);
147
    //        if (logLevelDebug())
148
    //            qDebug() << "executing disturbance module: " << run_list[idx]->name();
149
 
150
    //        try {
151
    //            run_list[idx]->run();
152
    //        } catch (const IException &e) {
153
    //            qWarning() << "ERROR: uncaught exception in module '" << run_list[idx]->name() << "':";
154
    //            qWarning() << "ERROR:" << e.message();
155
    //            qWarning() << " **************************************** ";
156
    //        }
157
 
158
    //        // remove from list
159
    //        run_list.removeAt(idx);
160
    //    }
649 werner 161
}
162
 
163
void Modules::yearBegin()
164
{
165
    foreach(DisturbanceInterface *di, mInterfaces)
166
        di->yearBegin();
167
 
168
}
652 werner 169
 
170
 
674 werner 171