iLand
bitecellscript.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 BITECELLSCRIPT_H
20#define BITECELLSCRIPT_H
21
22#include <QObject>
23#include <QJSValue>
24#include <QMap>
25#include "expression.h"
26#include "bitecell.h"
27#include "scripttree.h"
28
29namespace ABE {
30class FMTreeList;
31class FMSaplingList;
32}
33
34namespace BITE {
35
36class BiteCellScript : public QObject
37{
38 Q_OBJECT
39 Q_PROPERTY(bool active READ active WRITE setActive)
40 Q_PROPERTY(bool spreading READ spreading WRITE setSpreading)
41 Q_PROPERTY(int yearsLiving READ yearsLiving)
42 Q_PROPERTY(int cumYearsLiving READ cumYearsLiving)
43 Q_PROPERTY(int outbreakYears READ outbreakYears)
44 Q_PROPERTY(ABE::FMTreeList* trees READ trees)
45 Q_PROPERTY(ABE::FMSaplingList* saplings READ saplings)
46 Q_PROPERTY(BiteAgent* agent READ agent)
47public:
48 explicit BiteCellScript(QObject *parent = nullptr);
50 void setCell(BiteCell *c) { mCell = c; }
51 BiteCell *cell() const {return mCell; }
52 void setAgent(BiteAgent *a) { mAgent = a; }
53 BiteAgent *agent() const { return mAgent; }
54
55 bool active() const { return mCell->isActive(); }
56 void setActive(bool a) { mCell->setActive(a); }
57
58 bool spreading() const { return mCell->isSpreading(); }
59 void setSpreading(bool a) { mCell->setSpreading(a); }
60
61 int yearsLiving() const { return mCell->yearsLiving(); }
62 int cumYearsLiving() const { return mCell->cumYearsLiving(); }
63 int outbreakYears() const;
64
67
68
69signals:
70
71public slots:
72 QString info();
73 // access to variables of the cell
74 bool hasValue(QString variable_name);
75 double value(QString variable_name);
76 void setValue(QString var_name, double value);
77
78 void die() { mCell->die(); }
79
80 void reloadTrees();
81 void reloadSaplings();
82private:
83 BiteCell *mCell;
84 BiteAgent *mAgent;
85};
86
87
88class Events {
89public:
90 Events() {}
92 void clear();
94 void setup(QJSValue &js_value, QStringList event_names, BiteAgent *agent);
96 QString run(const QString event, BiteCell *cell=nullptr, QJSValueList *params=nullptr);
98 bool hasEvent(const QString &event) const;
99 QJSValue eventFunction(const QString &event) { return mEvents[event].property(event); }
100 QString dump();
101private:
102 QJSValue mInstance;
103 QMap<QString, QJSValue> mEvents;
104 BiteCellScript mCell;
105 QJSValue mScriptCell;
106 BiteAgent *mAgent;
107};
108
114 DynamicExpression(): wrapper_type(CellWrap), filter_type(ftInvalid), expr(nullptr), mAgent(nullptr), mTree(nullptr) {}
117 void setup(const QJSValue &js_value, EWrapperType type, BiteAgent *agent);
118 EFilterType type() const {return filter_type; }
119 double evaluate(BiteCell *cell) const;
120 double evaluate(Tree* tree) const;
121 double evaluate(SaplingTree* sap, ResourceUnit *ru) const;
122
123 bool evaluateBool(BiteCell *cell) const { return evaluate(cell) > 0.; }
124 bool evaluateBool(Tree *tree) const { return evaluate(tree) > 0.; }
125 bool evaluateBool(SaplingTree *sap, ResourceUnit *ru) const { return evaluate(sap, ru) > 0.; }
126
127 bool isValid() const { return filter_type!=ftInvalid;}
128 bool isConst() const { return filter_type == ftConstant; }
129 QString dump() const;
130private:
131 EWrapperType wrapper_type;
132 EFilterType filter_type;
133 Expression *expr;
134 QJSValue func;
135 BiteAgent *mAgent;
136 double mConstValue;
137
138 // value for trees
139 QJSValue mTreeValue;
140 ScriptTree *mTree;
141
142 // value for cells
143 BiteCellScript mCell;
144 QJSValue mScriptCell;
145
146
147};
148
150public:
152 ~Constraints();
153 void setup(QJSValue &js_value, DynamicExpression::EWrapperType wrap, BiteAgent *agent);
154 double evaluate(BiteCell *cell);
155 double evaluate(ABE::FMTreeList *treelist);
156 double evaluate(ABE::FMSaplingList *saplinglist);
157 bool isConst();
158 QStringList dump();
159private:
160 QList<DynamicExpression*> mConstraints;
161 BiteAgent *mAgent;
162};
163
164
165} // end namespace
166#endif // BITECELLSCRIPT_H
Definition: fmsaplinglist.h:12
The FMTreeList class implements low-level functionality for selecting and harvesting of trees.
Definition: fmtreelist.h:34
Definition: biteagent.h:71
Definition: bitecell.h:38
void setActive(bool activate)
Definition: bitecell.h:50
int yearsLiving() const
Definition: bitecell.h:62
bool isActive() const
Definition: bitecell.h:49
int cumYearsLiving() const
Definition: bitecell.h:64
void setSpreading(bool activate)
Definition: bitecell.h:53
bool isSpreading() const
Definition: bitecell.h:52
void die()
Definition: bitecell.cpp:79
Definition: bitecellscript.h:37
void die()
Definition: bitecellscript.h:78
void setSpreading(bool a)
Definition: bitecellscript.h:59
int cumYearsLiving
Definition: bitecellscript.h:42
bool active() const
Definition: bitecellscript.h:55
~BiteCellScript()
Definition: bitecellscript.h:49
BiteCellScript(QObject *parent=nullptr)
Definition: bitecellscript.cpp:34
void reloadTrees()
Definition: bitecellscript.cpp:92
bool hasValue(QString variable_name)
Definition: bitecellscript.cpp:63
void setValue(QString var_name, double value)
Definition: bitecellscript.cpp:80
int yearsLiving() const
Definition: bitecellscript.h:61
BiteCell * cell() const
Definition: bitecellscript.h:51
BiteAgent * agent
Definition: bitecellscript.h:46
bool active
Definition: bitecellscript.h:39
QString info()
Definition: bitecellscript.cpp:57
int cumYearsLiving() const
Definition: bitecellscript.h:62
ABE::FMTreeList * trees()
double value(QString variable_name)
Definition: bitecellscript.cpp:69
void setCell(BiteCell *c)
Definition: bitecellscript.h:50
ABE::FMTreeList * trees
Definition: bitecellscript.h:44
int yearsLiving
Definition: bitecellscript.h:41
BiteAgent * agent() const
Definition: bitecellscript.h:53
int outbreakYears
Definition: bitecellscript.h:43
void setAgent(BiteAgent *a)
Definition: bitecellscript.h:52
void reloadSaplings()
Definition: bitecellscript.cpp:99
ABE::FMSaplingList * saplings
Definition: bitecellscript.h:45
bool spreading
Definition: bitecellscript.h:40
ABE::FMSaplingList * saplings()
bool spreading() const
Definition: bitecellscript.h:58
void setActive(bool a)
Definition: bitecellscript.h:56
Definition: bitecellscript.h:149
bool isConst()
return true if no dynamic evaluation happens
Definition: bitecellscript.cpp:423
~Constraints()
Definition: bitecellscript.cpp:338
Constraints()
Definition: bitecellscript.h:151
void setup(QJSValue &js_value, DynamicExpression::EWrapperType wrap, BiteAgent *agent)
setup from javascript
Definition: bitecellscript.cpp:343
double evaluate(BiteCell *cell)
run the constraints
Definition: bitecellscript.cpp:365
QStringList dump()
prints some debug info
Definition: bitecellscript.cpp:433
Definition: bitecellscript.h:88
Events()
Definition: bitecellscript.h:90
void setup(QJSValue &js_value, QStringList event_names, BiteAgent *agent)
setup events from the javascript object
Definition: bitecellscript.cpp:452
QString run(const QString event, BiteCell *cell=nullptr, QJSValueList *params=nullptr)
execute javascript event /if registered) in the context of the forest stand 'stand'.
Definition: bitecellscript.cpp:466
QString dump()
prints some debug info
Definition: bitecellscript.cpp:508
QJSValue eventFunction(const QString &event)
Definition: bitecellscript.h:99
void clear()
clear the list of events
Definition: bitecellscript.cpp:447
bool hasEvent(const QString &event) const
returns true, if the event 'event' is available.
Definition: bitecellscript.cpp:503
An expression engine for mathematical expressions provided as strings.
Definition: expression.h:29
ResourceUnit is the spatial unit that encapsulates a forest stand and links to several environmental ...
Definition: resourceunit.h:49
Definition: scripttree.h:10
A tree is the basic simulation entity of iLand and represents a single tree.
Definition: tree.h:44
Definition: abegrid.h:22
Definition: biteagent.cpp:32
DynamicExpression encapsulates an "expression" that can be either a iLand expression,...
Definition: bitecellscript.h:111
bool isConst() const
Definition: bitecellscript.h:128
bool isValid() const
Definition: bitecellscript.h:127
void setup(const QJSValue &js_value, EWrapperType type, BiteAgent *agent)
Definition: bitecellscript.cpp:131
EWrapperType
Definition: bitecellscript.h:112
@ CellWrap
Definition: bitecellscript.h:112
@ SaplingWrap
Definition: bitecellscript.h:112
@ TreeWrap
Definition: bitecellscript.h:112
bool evaluateBool(SaplingTree *sap, ResourceUnit *ru) const
Definition: bitecellscript.h:125
EFilterType type() const
Definition: bitecellscript.h:118
DynamicExpression()
Definition: bitecellscript.h:114
bool evaluateBool(BiteCell *cell) const
Definition: bitecellscript.h:123
~DynamicExpression()
Definition: bitecellscript.cpp:123
EFilterType
Definition: bitecellscript.h:113
@ ftJavascript
Definition: bitecellscript.h:113
@ ftInvalid
Definition: bitecellscript.h:113
@ ftExpression
Definition: bitecellscript.h:113
@ ftConstant
Definition: bitecellscript.h:113
bool evaluateBool(Tree *tree) const
Definition: bitecellscript.h:124
QString dump() const
Definition: bitecellscript.cpp:321
double evaluate(BiteCell *cell) const
Definition: bitecellscript.cpp:176
Definition: saplings.h:29