iLand
expression.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
20#ifndef LOGICEXPRESSION_H
21#define LOGICEXPRESSION_H
22#include <QtCore/QString>
23#include <QtCore/QStringList>
24#include <QtCore/QMutexLocker>
25#include <QtCore/QVector>
26
29{
30public:
32 Expression();
33 Expression(const QString &aExpression) { m_expr=nullptr; m_execList=nullptr; setExpression(aExpression); }
34 Expression(const QString &expression, ExpressionWrapper *wrapper) { m_expr=nullptr; m_execList=nullptr; setExpression(expression); mModelObject = wrapper; }
35 // intialization
36 void setExpression(const QString &aExpression);
37 void setAndParse(const QString &expr);
38 void setModelObject(ExpressionWrapper *wrapper) { mModelObject = wrapper; }
39 const QString &expression() const { return m_expression; }
40 void parse(ExpressionWrapper *wrapper=nullptr);
41
43 void linearize(const double low_value, const double high_value, const int steps=1000);
45 void linearize2d(const double low_x, const double high_x, const double low_y, const double high_y, const int stepsx=50, const int stepsy=50);
46
48 static void setLinearizationEnabled(const bool enable) {mLinearizationAllowed = enable; }
49 // calculations
50 double execute(double *varlist=nullptr, ExpressionWrapper *object=nullptr) const;
51 bool executeBool(double *varlist=nullptr, ExpressionWrapper *object=nullptr) const { return execute(varlist, object) != 0.; }
52 double executeLocked() { QMutexLocker m(&m_execMutex); return execute(); }
57 double calculate(const double Val1=0., const double Val2=0., const bool forceExecution=false) const;
58 bool calculateBool(const double Val1=0., const double Val2=0., const bool forceExecution=false) const { return calculate(Val1, Val2, forceExecution) != 0.; }
61 double calculate(ExpressionWrapper &object, const double variable_value1=0., const double variable_value2=0.) const;
62 double calculateBool(ExpressionWrapper &object, const double variable_value1=0., const double variable_value2=0.) const { return calculate(object,variable_value1, variable_value2)!=0.; }
63
64 //variables
66 void setVar(const QString& Var, double Value);
68 double *addVar(const QString& VarName);
70 double * getVarAdress(const QString& VarName);
71
72
73 bool isConstExpression() const { return m_constExpression; }
74 bool isEmpty() const { return m_empty; }
75 const QString &lastError() const { return m_errorMsg; }
80 bool isStrict() { return m_strict;}
81 void setStrict(bool str) { m_strict=str; }
82 void setCatchExceptions(bool docatch=true) { m_catchExceptions = docatch; }
83 void setExternalVarSpace(const QStringList& ExternSpaceNames, double* ExternSpace);
84 void enableIncSum();
85 // other maintenance
86 static void addConstant(const QString const_name, const double const_value);
87private:
88 enum ETokType {etNumber, etOperator, etVariable, etFunction, etLogical, etCompare, etStop, etUnknown, etDelimeter};
89 enum EValueClasses {evcBHD, evcHoehe, evcAlter};
90 struct ExtExecListItem {
91 ETokType Type;
92 double Value;
93 int Index;
94 };
95 enum EDatatype {edtInfo, edtNumber, edtString, edtObject, edtVoid, edtObjVar, edtReference, edtObjectReference};
96 bool m_catchExceptions;
97 QString m_errorMsg;
98
99 bool m_parsed;
100 mutable bool m_strict;
101 bool m_empty; // empty expression
102 bool m_constExpression;
103 QString m_tokString;
104 QString m_expression;
105 Expression::ExtExecListItem *m_execList;
106 int m_execListSize; // size of buffer
107 int m_execIndex;
108 double m_varSpace[10];
109 QStringList m_varList;
110 QStringList m_externVarNames;
111 double *m_externVarSpace;
112 Expression::ETokType m_state;
113 Expression::ETokType m_lastState;
114 char *m_pos;
115 char *m_expr;
116 QString m_token;
117 QString m_prepStr;
118 int m_tokCount;
119 Expression::ETokType next_token();
120 void atom();
121 void parse_levelL0();
122 void parse_levelL1();
123 void parse_level0();
124 void parse_level1();
125 void parse_level2();
126 void parse_level3();
127 void parse_level4();
128 int getFuncIndex(const QString& functionName);
129 int getVarIndex(const QString& variableName);
130 inline double getModelVar(const int varIdx, ExpressionWrapper *object=nullptr) const ;
131
132 // link to external model variable
133 ExpressionWrapper *mModelObject;
134
135 double getExternVar(const int Index) const;
136 // inc-sum
137 mutable double m_incSumVar;
138 bool m_incSumEnabled;
139 double udfPolygon(double Value, double* Stack, int ArgCount) const;
140 double udfInList(double value, double* stack, int argCount) const;
141 double udfSigmoid(double Value, double sType, double p1, double p2) const;
142 double udfRandom(int type, double p1, double p2) const;
143
144 void checkBuffer(int Index);
145 QMutex m_execMutex;
146 // linearization
147 inline double linearizedValue(const double x) const;
148 inline double linearizedValue2d(const double x, const double y) const;
149 int mLinearizeMode;
150 QVector<double> mLinearized;
151 double mLinearLow, mLinearHigh;
152 double mLinearStep;
153 double mLinearLowY, mLinearHighY;
154 double mLinearStepY;
155 int mLinearStepCountY;
156 static bool mLinearizationAllowed;
157};
158
159#endif // LOGICEXPRESSION_H
An expression engine for mathematical expressions provided as strings.
Definition: expression.h:29
void linearize2d(const double low_x, const double high_x, const double low_y, const double high_y, const int stepsx=50, const int stepsy=50)
lineraize2d works with two variables
Definition: expression.cpp:884
double executeLocked()
thread safe version
Definition: expression.h:52
void setVar(const QString &Var, double Value)
set the value of the variable named "Var". Note: using addVar to obtain a pointer may be more efficie...
Definition: expression.cpp:476
~Expression()
Definition: expression.cpp:180
void linearize(const double low_value, const double high_value, const int steps=1000)
call linearize() to 'linarize' an expression, i.e. approximate the function by linear interpolation.
Definition: expression.cpp:865
double * getVarAdress(const QString &VarName)
retrieve again the value pointer of a variable.
Definition: expression.cpp:676
static void setLinearizationEnabled(const bool enable)
global switch for linerization. If set to false, subsequent calls to linearize are ignored.
Definition: expression.h:48
void setExternalVarSpace(const QStringList &ExternSpaceNames, double *ExternSpace)
Definition: expression.cpp:735
double calculate(const double Val1=0., const double Val2=0., const bool forceExecution=false) const
calculate formula.
Definition: expression.cpp:487
bool calculateBool(const double Val1=0., const double Val2=0., const bool forceExecution=false) const
Definition: expression.h:58
bool executeBool(double *varlist=nullptr, ExpressionWrapper *object=nullptr) const
Definition: expression.h:51
bool isStrict()
strict property: if true, variables must be named before execution.
Definition: expression.h:80
Expression(const QString &expression, ExpressionWrapper *wrapper)
Definition: expression.h:34
void parse(ExpressionWrapper *wrapper=nullptr)
force a parsing of the expression
Definition: expression.cpp:240
void setAndParse(const QString &expr)
set expression and parse instantly
Definition: expression.cpp:191
void setModelObject(ExpressionWrapper *wrapper)
Definition: expression.h:38
void setStrict(bool str)
Definition: expression.h:81
static void addConstant(const QString const_name, const double const_value)
Definition: expression.cpp:89
void setExpression(const QString &aExpression)
set expression
Definition: expression.cpp:200
Expression(const QString &aExpression)
Definition: expression.h:33
void setCatchExceptions(bool docatch=true)
Definition: expression.h:82
void enableIncSum()
Definition: expression.cpp:750
const QString & expression() const
Definition: expression.h:39
bool isEmpty() const
returns true if expression is empty
Definition: expression.h:74
double * addVar(const QString &VarName)
adds variable "VarName" and returns a double pointer to the variable. Use *ptr to set the value (befo...
Definition: expression.cpp:665
double execute(double *varlist=nullptr, ExpressionWrapper *object=nullptr) const
calculate formula and return result. variable values need to be set using "setVar()"
Definition: expression.cpp:522
const QString & lastError() const
Definition: expression.h:75
bool isConstExpression() const
returns true if current expression is a constant.
Definition: expression.h:73
double calculateBool(ExpressionWrapper &object, const double variable_value1=0., const double variable_value2=0.) const
Definition: expression.h:62
Expression()
Definition: expression.cpp:95
ExpressionWrapper is the base class for exposing C++ elements to the built-in Expression engine.
Definition: expressionwrapper.h:27