GCBM
transitionruletransform.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_CBM_TRANSITIONRULETRANSFORM_H_
2 #define MOJA_MODULES_CBM_TRANSITIONRULETRANSFORM_H_
3 
4 #include "moja/datarepository/iproviderrelationalinterface.h"
5 #include "moja/flint/ilandunitcontroller.h"
6 #include "moja/flint/itransform.h"
7 
8 #include <Poco/LRUCache.h>
9 #include <Poco/ThreadLocal.h>
10 
11 namespace moja {
12 namespace modules {
13 namespace cbm {
14 
15 class TransitionRuleTransform : public flint::ITransform {
16 public:
17  void configure(DynamicObject config,
18  const flint::ILandUnitController& landUnitController,
19  datarepository::DataRepository& dataRepository) override;
20 
21  void controllerChanged(const flint::ILandUnitController& controller) override;
22  const DynamicVar& value() const override;
23 
24 private:
25  const flint::ILandUnitController* _landUnitController;
26  datarepository::DataRepository* _dataRepository;
27  std::shared_ptr<datarepository::IProviderRelationalInterface> _provider;
28  mutable const flint::IVariable* _csetVar;
29  mutable DynamicVar _value;
30  mutable Poco::ThreadLocal<Poco::LRUCache<std::string, DynamicVar>> _cache;
31 
32  const std::string buildSql(const DynamicObject& classifierSet) const;
33 
34  const std::string _baseSql = R"(
35  SELECT
36  disturbance_type,
37  transition_id
38  FROM (
39  SELECT
40  ROW_NUMBER() OVER (PARTITION BY disturbance_type ORDER BY score DESC) AS tr_rank,
41  disturbance_type,
42  transition_id
43  FROM (
44  SELECT
45  dt.name AS disturbance_type,
46  tr.transition_id,
47  SUM(CASE
48  WHEN c.name IN (%1%) THEN
49  CASE
50  WHEN cv.value LIKE '?' THEN 1
51  %2%
52  ELSE -1000
53  END
54  ELSE -1000
55  END
56  ) AS score
57  FROM transition_rule tr
58  INNER JOIN transition_rule_classifier_value trv
59  ON trv.transition_rule_id = tr.id
60  INNER JOIN classifier_value cv
61  ON trv.classifier_value_id = cv.id
62  INNER JOIN classifier c
63  ON cv.classifier_id = c.id
64  INNER JOIN disturbance_type dt
65  ON tr.disturbance_type_id = dt.id
66  GROUP BY
67  tr.transition_id,
68  tr.disturbance_type_id
69  HAVING SUM(
70  CASE
71  WHEN c.name IN (%1%) THEN
72  CASE
73  WHEN cv.value LIKE '?' THEN 1
74  %2%
75  ELSE -1000
76  END
77  ELSE -1000
78  END
79  ) > 0
80  ) AS transition_ranking
81  ) AS best_matches
82  WHERE tr_rank = 1
83  )";
84 
85  const std::string _matchSql = "WHEN c.name LIKE '%1%' AND cv.value LIKE '%2%' THEN 4 ";
86 };
87 
88 }}}
89 
90 #endif // MOJA_MODULES_CBM_TRANSITIONRULETRANSFORM_H_
moja::modules::cbm::TransitionRuleTransform::_landUnitController
const flint::ILandUnitController * _landUnitController
Definition: transitionruletransform.h:25
moja::modules::cbm
Definition: ageclasshelper.cpp:12
moja::modules::cbm::TransitionRuleTransform::_cache
Poco::ThreadLocal< Poco::LRUCache< std::string, DynamicVar > > _cache
Definition: transitionruletransform.h:30
moja::modules::cbm::TransitionRuleTransform::_dataRepository
datarepository::DataRepository * _dataRepository
Definition: transitionruletransform.h:26
moja::modules::cbm::TransitionRuleTransform::_provider
std::shared_ptr< datarepository::IProviderRelationalInterface > _provider
Definition: transitionruletransform.h:27
moja::modules::cbm::TransitionRuleTransform
Definition: transitionruletransform.h:15
moja::modules::cbm::TransitionRuleTransform::buildSql
const std::string buildSql(const DynamicObject &classifierSet) const
Definition: transitionruletransform.cpp:69
moja::modules::cbm::TransitionRuleTransform::configure
void configure(DynamicObject config, const flint::ILandUnitController &landUnitController, datarepository::DataRepository &dataRepository) override
Definition: transitionruletransform.cpp:33
moja::modules::cbm::TransitionRuleTransform::_matchSql
const std::string _matchSql
Definition: transitionruletransform.h:85
moja::modules::cbm::TransitionRuleTransform::_baseSql
const std::string _baseSql
Definition: transitionruletransform.h:34
moja::modules::cbm::TransitionRuleTransform::_value
DynamicVar _value
Definition: transitionruletransform.h:29
moja::modules::cbm::TransitionRuleTransform::_csetVar
const flint::IVariable * _csetVar
Definition: transitionruletransform.h:28
moja::modules::cbm::TransitionRuleTransform::value
const DynamicVar & value() const override
Definition: transitionruletransform.cpp:99
moja::modules::cbm::TransitionRuleTransform::controllerChanged
void controllerChanged(const flint::ILandUnitController &controller) override
Definition: transitionruletransform.cpp:55
moja
Definition: ageclasshelper.cpp:10