Go to the documentation of this file. 1 #ifndef MOJA_MODULES_CBM_CBMDISTURBANCELISTENER_H_
2 #define MOJA_MODULES_CBM_CBMDISTURBANCELISTENER_H_
6 #include "moja/flint/ivariable.h"
7 #include "moja/flint/ipool.h"
8 #include "moja/flint/itiming.h"
10 #include <unordered_map>
11 #include <unordered_set>
41 virtual bool check()
const = 0;
63 const std::vector<std::string> disturbanceTypes,
64 const std::vector<std::shared_ptr<IDisturbanceSubCondition>> matchConditions,
65 const std::vector<std::shared_ptr<IDisturbanceSubCondition>> runConditions,
66 const std::vector<std::shared_ptr<IDisturbanceSubCondition>> overrideConditions,
67 const std::string& overrideDisturbanceType =
"") :
_disturbanceTypes(disturbanceTypes),
72 bool isMatchingDisturbance =
false;
74 if (disturbanceType == matchDisturbanceType) {
75 isMatchingDisturbance =
true;
80 if (!isMatchingDisturbance) {
85 if (!matchCondition->check()) {
101 if (runCondition->check()) {
112 if (overrideCondition->check()) {
136 if (!condition->check()) {
145 std::vector<std::shared_ptr<IDisturbanceSubCondition>>
_conditions;
152 const std::string& propertyName =
"") :
_var(var),
_property(propertyName),
173 for (
const auto& targetValue :
_target) {
174 if (value == targetValue) {
191 for (
auto pool :
_pools) {
192 sum += pool->value();
203 const std::vector<const flint::IPool*>
_pools;
211 flint::ITiming* timing,
212 std::shared_ptr<std::deque<DisturbanceHistoryRecord>> disturbanceHistory,
213 const std::vector<DisturbanceHistoryCondition>& sequence)
217 auto referenceYear =
_timing->curStartDate().year();
218 int historyIndex = 0;
220 for (
auto i = 0; i <
_sequence.size(); i++) {
222 bool matchNone = expected.disturbanceType ==
"none";
223 bool matchAny = expected.disturbanceType ==
"any";
225 if (historyIndex > lastHistoryIndex) {
226 return (matchNone && i ==
_sequence.size() - 1) ? true :
false;
231 if (!(matchNone || matchAny) && expected.disturbanceType != actual.disturbanceType) {
235 bool withinMaxYears = expected.maxYearsAgo == -1 || referenceYear - actual.year <= expected.maxYearsAgo;
236 if (matchNone && withinMaxYears) {
239 else if (!matchNone && !withinMaxYears) {
246 : expected.ageComparison ==
DisturbanceConditionType::Between ? actual.ageAtDisturbance >= expected.ageAtDisturbance[0] && actual.ageAtDisturbance <= expected.ageAtDisturbance[1]
253 referenceYear = actual.year;
260 if (expected.maxYearsAgo != -1) {
261 referenceYear -= expected.maxYearsAgo;
281 std::vector<std::shared_ptr<IDisturbanceSubCondition>> conditions,
294 if (!condition->check()) {
316 _sourcePool(landUnitData.getPool(data[
"source_pool_name"].convert<std::string>())),
317 _destPool(landUnitData.getPool(data[
"dest_pool_name"].convert<std::string>())),
348 void configure(
const DynamicObject& config)
override;
349 void subscribe(NotificationCenter& notificationCenter)
override;
351 flint::ModuleTypes
moduleType() {
return flint::ModuleTypes::DisturbanceEvent; };
361 typedef std::unordered_map<int, EventVector>
EventMap;
393 std::shared_ptr<IDisturbanceSubCondition>
createSubCondition(
const DynamicObject& config);
403 #endif // MOJA_MODULES_CBM_CBMDISTURBANCELISTENER_H_
void configure(const DynamicObject &config) override
Definition: cbmdisturbancelistener.cpp:45
DynamicVar _conditionConfig
Definition: cbmdisturbancelistener.h:383
virtual ~IDisturbanceSubCondition()=default
std::shared_ptr< std::deque< DisturbanceHistoryRecord > > _disturbanceHistory
Definition: cbmdisturbancelistener.h:271
CBMDistEventRef()=default
DynamicVar ageAtDisturbance
Definition: cbmdisturbancelistener.h:57
const DynamicObject & metadata()
Definition: cbmdisturbancelistener.h:290
Definition: cbmdisturbancelistener.h:23
int year
Definition: cbmdisturbancelistener.h:19
const std::string _overrideDisturbanceType
Definition: cbmdisturbancelistener.h:126
Definition: cbmdisturbancelistener.h:129
int ageAtDisturbance
Definition: cbmdisturbancelistener.h:20
void merge(const DisturbanceConditionResult &other)
Definition: cbmdisturbancelistener.h:28
bool isApplicable(const std::string &disturbanceType)
Definition: cbmdisturbancelistener.h:71
const std::vector< std::shared_ptr< IDisturbanceSubCondition > > _matchConditions
Definition: cbmdisturbancelistener.h:123
Definition: cbmdisturbancelistener.h:60
Definition: cbmdisturbancelistener.h:148
const std::string _property
Definition: cbmdisturbancelistener.h:167
Definition: cbmdisturbancelistener.h:340
bool _disturbanceConditionsInitialized
Definition: cbmdisturbancelistener.h:382
std::vector< std::shared_ptr< IDisturbanceSubCondition > > _disturbanceConditions
Definition: cbmdisturbancelistener.h:307
Definition: ageclasshelper.cpp:12
virtual void doSystemShutdown() override
Definition: cbmdisturbancelistener.cpp:107
std::unordered_set< std::string > _classifierNames
Definition: cbmdisturbancelistener.h:379
void fetchMatrices()
Definition: cbmdisturbancelistener.cpp:564
DisturbanceConditionType ageComparison
Definition: cbmdisturbancelistener.h:56
bool search(DynamicVar value) const
Definition: cbmdisturbancelistener.h:172
EventMap _matrices
Definition: cbmdisturbancelistener.h:370
Definition: cbmdisturbancelistener.h:310
int maxYearsAgo
Definition: cbmdisturbancelistener.h:55
Definition: cbmdisturbancelistener.h:37
const std::vector< const flint::IPool * > _pools
Definition: cbmdisturbancelistener.h:203
DisturbanceSequenceSubCondition(flint::ITiming *timing, std::shared_ptr< std::deque< DisturbanceHistoryRecord >> disturbanceHistory, const std::vector< DisturbanceHistoryCondition > &sequence)
Definition: cbmdisturbancelistener.h:210
std::string newDisturbanceType
Definition: cbmdisturbancelistener.h:26
DisturbanceCondition(const std::vector< std::string > disturbanceTypes, const std::vector< std::shared_ptr< IDisturbanceSubCondition >> matchConditions, const std::vector< std::shared_ptr< IDisturbanceSubCondition >> runConditions, const std::vector< std::shared_ptr< IDisturbanceSubCondition >> overrideConditions, const std::string &overrideDisturbanceType="")
Definition: cbmdisturbancelistener.h:62
Definition: cbmmodulebase.h:22
std::string disturbanceType() const
Definition: cbmdisturbancelistener.h:286
int _year
Definition: cbmdisturbancelistener.h:305
const std::vector< std::string > _disturbanceTypes
Definition: cbmdisturbancelistener.h:122
std::unordered_map< std::pair< std::string, int >, int > _dmAssociations
Definition: cbmdisturbancelistener.h:373
DynamicObject _metadata
Definition: cbmdisturbancelistener.h:306
std::unordered_map< int, EventVector > EventMap
Definition: cbmdisturbancelistener.h:361
int transitionRuleId() const
Definition: cbmdisturbancelistener.h:288
int _disturbanceMatrixId
Definition: cbmdisturbancelistener.h:334
const flint::IPool * _destPool
Definition: cbmdisturbancelistener.h:336
NotificationCenter * _notificationCenter
Definition: cbmdisturbancelistener.h:363
void fetchDistTypeCodes()
Definition: cbmdisturbancelistener.cpp:637
std::string getDisturbanceTypeName(const DynamicObject &eventData)
Definition: cbmdisturbancelistener.cpp:264
bool check() const override
Definition: cbmdisturbancelistener.h:189
CompositeDisturbanceSubCondition(std::vector< std::shared_ptr< IDisturbanceSubCondition >> conditions)
Definition: cbmdisturbancelistener.h:131
flint::IVariable * _age
Definition: cbmdisturbancelistener.h:369
Definition: cbmdisturbancelistener.h:53
const flint::IPool * destPool() const
Definition: cbmdisturbancelistener.h:329
bool checkConditions()
Definition: cbmdisturbancelistener.h:292
const flint::IPool * _sourcePool
Definition: cbmdisturbancelistener.h:335
virtual void doTimingInit() override
Definition: cbmdisturbancelistener.cpp:151
virtual void doTimingStep() override
Definition: cbmdisturbancelistener.cpp:365
std::unordered_map< std::pair< int, std::string >, std::pair< int, int > > _peatlandDmAssociations
Definition: cbmdisturbancelistener.h:372
void setProportion(double proportion)
Definition: cbmdisturbancelistener.h:331
flint::ModuleTypes moduleType()
Definition: cbmdisturbancelistener.h:351
void firePeatlandDisturbanceEvent(CBMDistEventRef &e)
Definition: cbmdisturbancelistener.cpp:446
virtual ~CBMDisturbanceListener()=default
std::unordered_set< std::string > _errorLayers
Definition: cbmdisturbancelistener.h:378
CBMDistEventRef(std::string &disturbanceType, int year, int transitionId, std::vector< std::shared_ptr< IDisturbanceSubCondition >> conditions, const DynamicObject &metadata)
Definition: cbmdisturbancelistener.h:279
int disturbanceMatrixId() const
Definition: cbmdisturbancelistener.h:327
std::vector< DisturbanceCondition > _disturbanceConditions
Definition: cbmdisturbancelistener.h:384
double proportion() const
Definition: cbmdisturbancelistener.h:330
std::shared_ptr< IDisturbanceSubCondition > createSubCondition(const DynamicObject &config)
Definition: cbmdisturbancelistener.cpp:737
DisturbanceConditionType
Definition: cbmdisturbancelistener.h:44
CBMDistEventTransfer(flint::ILandUnitDataWrapper &landUnitData, const DynamicObject &data)
Definition: cbmdisturbancelistener.h:314
flint::ITiming * _timing
Definition: cbmdisturbancelistener.h:270
CBMDisturbanceListener()
Definition: cbmdisturbancelistener.h:342
std::unordered_map< std::string, int > _disturbanceOrder
Definition: cbmdisturbancelistener.h:380
const DynamicVar _target
Definition: cbmdisturbancelistener.h:170
std::string disturbanceType
Definition: cbmdisturbancelistener.h:18
Definition: cbmdisturbancelistener.h:183
flint::IVariable * _classifierSet
Definition: cbmdisturbancelistener.h:368
const flint::IPool * sourcePool() const
Definition: cbmdisturbancelistener.h:328
int year() const
Definition: cbmdisturbancelistener.h:289
CBMDistEventTransfer()=default
int _transitionRuleId
Definition: cbmdisturbancelistener.h:304
std::vector< DisturbanceHistoryCondition > _sequence
Definition: cbmdisturbancelistener.h:272
VariableDisturbanceSubCondition(const flint::IVariable *var, DisturbanceConditionType type, const DynamicVar &target, const std::string &propertyName="")
Definition: cbmdisturbancelistener.h:150
bool check() const override
Definition: cbmdisturbancelistener.h:134
std::unordered_map< int, std::string > _distTypeNames
Definition: cbmdisturbancelistener.h:377
bool check() const override
Definition: cbmdisturbancelistener.h:155
bool check() const override
Definition: cbmdisturbancelistener.h:216
void fireCBMDisturbanceEvent(CBMDistEventRef &e)
Definition: cbmdisturbancelistener.cpp:497
DisturbanceConditionResult check()
Definition: cbmdisturbancelistener.h:93
const flint::IVariable * _var
Definition: cbmdisturbancelistener.h:168
const std::vector< std::shared_ptr< IDisturbanceSubCondition > > _runConditions
Definition: cbmdisturbancelistener.h:124
std::unordered_map< std::string, std::string > _landClassTransitions
Definition: cbmdisturbancelistener.h:374
bool hadRunConditions
Definition: cbmdisturbancelistener.h:24
CBMDistEventTransfer(flint::ILandUnitDataWrapper &landUnitData, const std::string &sourcePool, const std::string &destPool, double proportion)
Definition: cbmdisturbancelistener.h:320
void fetchPeatlandDMAssociations()
Definition: cbmdisturbancelistener.cpp:666
virtual bool check() const =0
const std::vector< std::shared_ptr< IDisturbanceSubCondition > > _overrideConditions
Definition: cbmdisturbancelistener.h:125
bool addLandUnitEvent(const DynamicVar &eventData)
Definition: cbmdisturbancelistener.cpp:314
std::vector< CBMDistEventTransfer > EventVector
Definition: cbmdisturbancelistener.h:360
void fetchDMAssociations()
Definition: cbmdisturbancelistener.cpp:591
Definition: cbmdisturbancelistener.h:17
flint::IVariable * _landClass
Definition: cbmdisturbancelistener.h:366
const DisturbanceConditionType _type
Definition: cbmdisturbancelistener.h:204
std::string disturbanceType
Definition: cbmdisturbancelistener.h:54
void subscribe(NotificationCenter ¬ificationCenter) override
Definition: cbmdisturbancelistener.cpp:65
const DynamicVar _target
Definition: cbmdisturbancelistener.h:205
flint::IVariable * _spu
Definition: cbmdisturbancelistener.h:367
PoolDisturbanceSubCondition(std::vector< const flint::IPool * > pools, DisturbanceConditionType type, const DynamicVar &target)
Definition: cbmdisturbancelistener.h:185
std::string _disturbanceType
Definition: cbmdisturbancelistener.h:303
std::vector< std::string > _layerNames
Definition: cbmdisturbancelistener.h:364
Definition: cbmdisturbancelistener.h:208
std::vector< std::shared_ptr< IDisturbanceSubCondition > > _conditions
Definition: cbmdisturbancelistener.h:145
std::shared_ptr< std::deque< DisturbanceHistoryRecord > > _disturbanceHistory
Definition: cbmdisturbancelistener.h:385
Definition: ageclasshelper.cpp:10
void fetchLandClassTransitions()
Definition: cbmdisturbancelistener.cpp:612
virtual void doDisturbanceEvent(DynamicVar) override
Definition: cbmdisturbancelistener.cpp:124
Definition: cbmdisturbancelistener.h:275
std::map< int, std::vector< CBMDistEventRef > > _landUnitEvents
Definition: cbmdisturbancelistener.h:375
double _proportion
Definition: cbmdisturbancelistener.h:337
void fetchDisturbanceOrder()
Definition: cbmdisturbancelistener.cpp:692
std::vector< const flint::IVariable * > _layers
Definition: cbmdisturbancelistener.h:365
virtual void doLocalDomainInit() override
Definition: cbmdisturbancelistener.cpp:84
std::unordered_map< std::string, int > _distTypeCodes
Definition: cbmdisturbancelistener.h:376
const DisturbanceConditionType _type
Definition: cbmdisturbancelistener.h:169
bool shouldRun
Definition: cbmdisturbancelistener.h:25
void setDisturbanceType(const std::string &disturbanceType)
Definition: cbmdisturbancelistener.h:287