Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1033 | 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 | ********************************************************************************************/ |
||
908 | werner | 19 | #include "abe_global.h" |
891 | werner | 20 | #include "actscheduled.h" |
21 | |||
22 | #include "fmstp.h" |
||
23 | #include "fmstand.h" |
||
24 | #include "fomescript.h" |
||
25 | #include "fmtreelist.h" |
||
26 | |||
907 | werner | 27 | namespace ABE { |
891 | werner | 28 | |
1095 | werner | 29 | /** @class ActScheduled |
30 | @ingroup abe |
||
31 | The ActScheduled class is an all-purpose activity (similar to ActGeneral). The execution time of ActScheduled-activities, however, |
||
32 | is defined by the ABE Scheduler. |
||
891 | werner | 33 | |
1095 | werner | 34 | */ |
891 | werner | 35 | |
36 | |||
1095 | werner | 37 | |
891 | werner | 38 | ActScheduled::ActScheduled(FMSTP *parent): Activity(parent) |
39 | { |
||
40 | mBaseActivity.setIsScheduled(true); // use the scheduler |
||
41 | mBaseActivity.setDoSimulate(true); // simulate per default |
||
42 | } |
||
43 | |||
44 | void ActScheduled::setup(QJSValue value) |
||
45 | { |
||
46 | Activity::setup(value); |
||
47 | events().setup(value, QStringList() << "onEvaluate"); |
||
48 | |||
49 | if (!events().hasEvent(QStringLiteral("onEvaluate"))) |
||
50 | throw IException("activity %1 (of type 'scheduled') requires to have the 'onSchedule' event."); |
||
51 | |||
52 | } |
||
53 | |||
54 | bool ActScheduled::execute(FMStand *stand) |
||
55 | { |
||
56 | if (events().hasEvent(QStringLiteral("onExecute"))) { |
||
57 | // switch off simulation mode |
||
58 | stand->currentFlags().setDoSimulate(false); |
||
59 | // execute this event |
||
60 | bool result = Activity::execute(stand); |
||
61 | stand->currentFlags().setDoSimulate(true); |
||
62 | return result; |
||
63 | } else { |
||
64 | // default behavior: process all marked trees (harvest / cut) |
||
909 | werner | 65 | if (stand->trace()) qCDebug(abe) << stand->context() << "activity" << name() << "remove all marked trees."; |
891 | werner | 66 | FMTreeList trees(stand); |
67 | trees.removeMarkedTrees(); |
||
68 | return true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | bool ActScheduled::evaluate(FMStand *stand) |
||
73 | { |
||
74 | // this is called when it should be tested |
||
75 | stand->currentFlags().setDoSimulate(true); |
||
1061 | werner | 76 | QJSValue result = events().run(QStringLiteral("onEvaluate"), stand); |
891 | werner | 77 | if (stand->trace()) |
1063 | werner | 78 | qCDebug(abe) << stand->context() << "executed onEvaluate event of" << name() << "with result:" << FomeScript::JStoString(result); |
1061 | werner | 79 | |
80 | if (result.isNumber()) { |
||
81 | double harvest = result.toNumber(); |
||
82 | |||
891 | werner | 83 | // the return value is interpreted as scheduled harvest; if this value is 0, then no |
84 | if (harvest==0.) |
||
85 | return false; |
||
86 | stand->addScheduledHarvest(harvest); |
||
87 | if (stand->trace()) |
||
909 | werner | 88 | qCDebug(abe) << stand->context() << "scheduled harvest is now" << stand->scheduledHarvest(); |
891 | werner | 89 | return true; |
90 | } |
||
1061 | werner | 91 | bool bool_result = result.toBool(); |
891 | werner | 92 | return bool_result; |
93 | } |
||
94 | |||
95 | QStringList ActScheduled::info() |
||
96 | { |
||
97 | QStringList lines = Activity::info(); |
||
896 | werner | 98 | //lines << "this is an activity of type 'scheduled'."; |
891 | werner | 99 | return lines; |
100 | } |
||
101 | |||
102 | |||
103 | } // namespace |