iLand
activity.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
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#ifndef ACTIVITY_H
20#define ACTIVITY_H
21#include <QJSValue>
22#include <QVector>
23#include <QMap>
24
25class Expression; // forward
26
27namespace ABE {
28
29
30class FMStand;
31class FMSTP;
32
33
34class Schedule {
35public:
36 // setup and life cycle
38 Schedule(QJSValue &js_value) { clear(); setup(js_value); }
40 void setup(const QJSValue &js_value);
41 QString dump() const;
42 // functions
46 double value(const FMStand *stand, const int specific_year=-1);
48 double minValue(const double U=100.) const;
50 double maxValue(const double U=100.) const;
52 double optimalValue(const double U=100.) const;
53 // some stuffs
54 int tmin; int tmax; int topt;
55 double tminrel; double tmaxrel; double toptrel;
57 // repeating
60 bool repeat;
62};
63
64class Events {
65public:
66 Events() {}
68 void clear();
70 void setup(QJSValue &js_value, QStringList event_names);
72 QJSValue run(const QString event, FMStand *stand, QJSValueList *params=0);
74 bool hasEvent(const QString &event) const;
75 QString dump();
76private:
77 QJSValue mInstance;
78 QMap<QString, QJSValue> mEvents;
79};
80
84 DynamicExpression(): filter_type(ftInvalid), expr(0) {}
86 void setup(const QJSValue &js_value);
87 bool evaluate(FMStand *stand) const;
88 bool isValid() const { return filter_type!=ftInvalid;}
89 QString dump() const;
90private:
91 enum { ftInvalid, ftExpression, ftJavascript} filter_type;
92 Expression *expr;
93 QJSValue func;
94};
95
97public:
99 void setup(QJSValue &js_value);
100 double evaluate(FMStand *stand);
101 QStringList dump();
102private:
103 QList<DynamicExpression> mConstraints;
104};
105
106class Activity; //forward
110{
111public:
112 ActivityFlags(): mActivity(0), mFlags(0) {}
113 ActivityFlags(Activity *act): mActivity(act), mFlags(0) {}
114 Activity *activity() const {return mActivity; }
115
116 bool active() const {return flag(Active); }
117 bool enabled() const {return flag(Enabled);}
118 bool isRepeating() const {return flag(Repeater);}
119 bool isPending() const {return flag(Pending); }
120 bool isForcedNext() const {return flag(ExecuteNext); }
121 bool isFinalHarvest() const {return flag(FinalHarvest); }
122 bool isExecuteImmediate() const {return flag(ExecuteImmediate); }
123 bool isScheduled() const {return flag(IsScheduled);}
124 bool isDoSimulate() const {return flag(DoSimulate);}
125 bool isSalvage() const {return flag(IsSalvage);}
126
127
128 void setActive(const bool active) { setFlag(Active, active); }
129 void setEnabled(const bool enabled) { setFlag(Enabled, enabled); }
130 void setIsRepeating(const bool repeat) { setFlag(Repeater, repeat); }
131 void setIsPending(const bool pending) { setFlag(Pending, pending); }
132 void setForceNext(const bool isnext) { setFlag(ExecuteNext, isnext); }
133 void setFinalHarvest(const bool isfinal) { setFlag(FinalHarvest, isfinal); }
134 void setExecuteImmediate(const bool doexec) { setFlag(ExecuteImmediate, doexec);}
135 void setIsScheduled(const bool doschedule) {setFlag(IsScheduled, doschedule); }
136 void setDoSimulate(const bool dosimulate) {setFlag(DoSimulate, dosimulate); }
137 void setIsSalvage(const bool issalvage) {setFlag(IsSalvage, issalvage); }
138
139private:
141 enum Flags { Active=1, // if false, the activity has already been executed
142 Enabled=2, // if false, the activity can not be executed
143 Repeater=4, // if true, the activity is executed
144 ExecuteNext=8, // this activity should be executed next (kind of "goto"
145 ExecuteImmediate=16, // should be executed immediately by the scheduler (e.g. required sanitary cuttings)
146 Pending=32, // the activity is currently in the scheduling algorithm
147 FinalHarvest=64, // the management of the activity is a "endnutzung" (compared to "vornutzung")
148 IsScheduled=128, // the execution time of the activity is scheduled by the Scheduler component
149 DoSimulate=256, // the default operation mode of harvests (simulate or not)
150 IsSalvage=512 // the activity is triggered by tree mortality events
151 };
152 bool flag(const ActivityFlags::Flags flag) const { return mFlags & flag; }
153 void setFlag(const ActivityFlags::Flags flag, const bool value) { if (value) mFlags |= flag; else mFlags &= (flag ^ 0xffffff );}
154 Activity *mActivity;
155 int mFlags;
156};
157
158
159
162{
163public:
164 // life cycle
165 Activity(const FMSTP *parent);
166 virtual ~Activity();
168 static Activity *createActivity(const QString &type, FMSTP *stp);
169
170 // properties
172 const FMSTP *program() const { return mProgram; }
173 virtual QString type() const;
174 QString name() const {return mName; }
175 int index() const { return mIndex; }
177 int earliestSchedule(const double U=100.) const {return mSchedule.minValue(U); }
179 int latestSchedule(const double U=100.) const { return mSchedule.maxValue(U); }
181 int optimalSchedule(const double U=100.) const { return mSchedule.optimalValue(U); }
182 bool isRepeatingActivity() const { return mSchedule.repeat; }
183 // main actions
185 virtual void setup(QJSValue value);
187 virtual double scheduleProbability(FMStand *stand, const int specific_year=-1);
190 virtual double execeuteProbability(FMStand *stand);
192 virtual bool execute(FMStand *stand);
195 virtual bool evaluate(FMStand *stand);
197 virtual void evaluateDyanamicExpressions(FMStand *stand);
199 virtual QStringList info();
200protected:
201 Schedule &schedule() { return mSchedule; }
202 Constraints &constraints() { return mConstraints; }
203 Events &events() { return mEvents; }
205 ActivityFlags mBaseActivity; // base properties of the activity (that can be changed for each stand)
206 static QStringList mAllowedProperties; // list of properties (e.g. 'schedule') that are parsed by the base activity
207private:
208 void setIndex(const int index) { mIndex = index; } // used during setup
209 void setName(const QString &name) { mName = name; }
210 int mIndex;
211 QString mName;
212 const FMSTP *mProgram; // link to the management programme the activity is part of
213 Schedule mSchedule; // timing of activity
214 Constraints mConstraints; // constraining factors
215 Events mEvents; // action handlers such as "onExecute"
216 DynamicExpression mEnabledIf; // enabledIf property (dynamically evaluated)
217 friend class FMSTP; // allow access of STP class to internals
218 friend class FMStand; // allow access of the activity class (e.g for events)
219 friend class ActivityObj; // allow access to scripting function
220};
221
222} // namespace
223
224Q_DECLARE_TYPEINFO(ABE::ActivityFlags, Q_PRIMITIVE_TYPE); // declare as POD structure to allow more efficient copying
225
226#endif // ACTIVITY_H
Q_DECLARE_TYPEINFO(ABE::ActivityFlags, Q_PRIMITIVE_TYPE)
Activity meta data (enabled, active, ...) that need to be stored per stand.
Definition: activity.h:110
void setIsRepeating(const bool repeat)
Definition: activity.h:130
ActivityFlags(Activity *act)
Definition: activity.h:113
void setIsScheduled(const bool doschedule)
Definition: activity.h:135
bool isFinalHarvest() const
Definition: activity.h:121
bool isScheduled() const
Definition: activity.h:123
void setExecuteImmediate(const bool doexec)
Definition: activity.h:134
ActivityFlags()
Definition: activity.h:112
bool isDoSimulate() const
Definition: activity.h:124
Activity * activity() const
Definition: activity.h:114
bool isRepeating() const
Definition: activity.h:118
bool active() const
Definition: activity.h:116
void setForceNext(const bool isnext)
Definition: activity.h:132
void setEnabled(const bool enabled)
Definition: activity.h:129
void setFinalHarvest(const bool isfinal)
Definition: activity.h:133
bool isPending() const
Definition: activity.h:119
void setActive(const bool active)
Definition: activity.h:128
bool enabled() const
Definition: activity.h:117
void setIsPending(const bool pending)
Definition: activity.h:131
void setIsSalvage(const bool issalvage)
Definition: activity.h:137
bool isForcedNext() const
Definition: activity.h:120
bool isSalvage() const
Definition: activity.h:125
bool isExecuteImmediate() const
Definition: activity.h:122
void setDoSimulate(const bool dosimulate)
Definition: activity.h:136
Activity is the base class for management activities.
Definition: activity.h:162
QString name() const
name of the activity as provided by JS
Definition: activity.h:174
virtual void setup(QJSValue value)
setup of the activity (events, schedule, constraints). additional setup in derived classes.
Definition: activity.cpp:478
Phase
Definition: activity.h:171
@ Regeneration
Definition: activity.h:171
@ Tending
Definition: activity.h:171
@ Thinning
Definition: activity.h:171
@ All
Definition: activity.h:171
@ Invalid
Definition: activity.h:171
Schedule & schedule()
Definition: activity.h:201
int earliestSchedule(const double U=100.) const
get earlist possible scheduled year (relative to rotation begin)
Definition: activity.h:177
virtual bool execute(FMStand *stand)
executes the action (usually defined in derived classes) using the context of 'stand'.
Definition: activity.cpp:513
virtual double scheduleProbability(FMStand *stand, const int specific_year=-1)
returns a value > 0 if the activity coult be scheduled now
Definition: activity.cpp:501
Constraints & constraints()
Definition: activity.h:202
ActivityFlags & standFlags(FMStand *stand=0)
Definition: activity.cpp:547
virtual QString type() const
Definition: activity.cpp:473
virtual bool evaluate(FMStand *stand)
executes the evaluation of the forest stand.
Definition: activity.cpp:520
friend class FMSTP
Definition: activity.h:217
static QStringList mAllowedProperties
Definition: activity.h:206
Events & events()
Definition: activity.h:203
int optimalSchedule(const double U=100.) const
get optimal scheduled year (relative to rotation begin)
Definition: activity.h:181
int latestSchedule(const double U=100.) const
get latest possible scheduled year (relative to rotation begin)
Definition: activity.h:179
ActivityFlags mBaseActivity
Definition: activity.h:205
static Activity * createActivity(const QString &type, FMSTP *stp)
Activity factory - create activities for given 'type'.
Definition: activity.cpp:447
virtual QStringList info()
dumps some information for debugging
Definition: activity.cpp:536
bool isRepeatingActivity() const
Definition: activity.h:182
int index() const
index of the activity within the STP
Definition: activity.h:175
Activity(const FMSTP *parent)
Definition: activity.cpp:433
virtual void evaluateDyanamicExpressions(FMStand *stand)
function that evaluates "bound" dynamic expressions
Definition: activity.cpp:527
const FMSTP * program() const
Definition: activity.h:172
virtual ~Activity()
Definition: activity.cpp:442
virtual double execeuteProbability(FMStand *stand)
returns a probability for the activity to be executed (ie all constraints are fulfilled) return value...
Definition: activity.cpp:507
The ActivityObj class encapsulates the 'activity' object in JS.
Definition: fomescript.h:333
Definition: activity.h:96
double evaluate(FMStand *stand)
run the constraints
Definition: activity.cpp:306
Constraints()
Definition: activity.h:98
QStringList dump()
prints some debug info
Definition: activity.cpp:326
void setup(QJSValue &js_value)
setup from javascript
Definition: activity.cpp:285
Definition: activity.h:64
QString dump()
prints some debug info
Definition: activity.cpp:273
void clear()
clear the list of events
Definition: activity.cpp:225
Events()
Definition: activity.h:66
void setup(QJSValue &js_value, QStringList event_names)
setup events from the javascript object
Definition: activity.cpp:230
QJSValue run(const QString event, FMStand *stand, QJSValueList *params=0)
execute javascript event /if registered) in the context of the forest stand 'stand'.
Definition: activity.cpp:241
bool hasEvent(const QString &event) const
returns true, if the event 'event' is available.
Definition: activity.cpp:268
The FMSTP class encapsulates a stand treatment program, which is defined in Javascript.
Definition: fmstp.h:39
FMStand encapsulates a forest stand for the forest management engine.
Definition: fmstand.h:49
Definition: activity.h:34
double maxValue(const double U=100.) const
returns the latest possible execution time
Definition: activity.cpp:200
int repeat_interval
Definition: activity.h:58
int topt
Definition: activity.h:54
int tmin
Definition: activity.h:54
double optimalValue(const double U=100.) const
returns the "optimal" year, i.e. the first year when prob. to execute is highest.
Definition: activity.cpp:211
void setup(const QJSValue &js_value)
Definition: activity.cpp:56
int tmax
Definition: activity.h:54
void clear()
Definition: activity.h:39
bool repeat
Definition: activity.h:60
double tminrel
Definition: activity.h:55
Schedule()
Definition: activity.h:37
bool force_execution
Definition: activity.h:56
bool absolute
Definition: activity.h:61
QString dump() const
Definition: activity.cpp:94
int repeat_start
Definition: activity.h:59
double value(const FMStand *stand, const int specific_year=-1)
value() evaluates the schedule for the given 'stand'.
Definition: activity.cpp:103
double tmaxrel
Definition: activity.h:55
double minValue(const double U=100.) const
gives (fixed) earliest possible execution time
Definition: activity.cpp:189
double toptrel
Definition: activity.h:55
Schedule(QJSValue &js_value)
Definition: activity.h:38
An expression engine for mathematical expressions provided as strings.
Definition: expression.h:29
Definition: abegrid.h:22
DynamicExpression encapsulates an "expression" that can be either a iLand expression,...
Definition: activity.h:83
QString dump() const
Definition: activity.cpp:417
bool evaluate(FMStand *stand) const
Definition: activity.cpp:371
~DynamicExpression()
Definition: activity.cpp:336
bool isValid() const
Definition: activity.h:88
DynamicExpression()
Definition: activity.h:84
void setup(const QJSValue &js_value)
Definition: activity.cpp:342