FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTTheme.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019 Gouvernement du Québec
3
4SPDX-License-Identifier: LiLiQ-R-1.1
5License-Filename: LICENSES/EN/LiLiQ-R11unicode.txt
6*/
7
8#ifndef FMTtheme_Hm_included
9#define FMTtheme_Hm_included
10
11#include <boost/dynamic_bitset_fwd.hpp>
12#include <boost/serialization/serialization.hpp>
13#include <string>
14#include <vector>
15#include <map>
16#include <boost/serialization/nvp.hpp>
17#include <boost/serialization/map.hpp>
18#include <boost/serialization/vector.hpp>
19#include <boost/serialization/export.hpp>
20#include <boost/serialization/string.hpp>
21#include "FMTExceptionHandler.h"
22#include "FMTObject.h"
23#include <unordered_map>
24
25#if defined FMTWITHR
26 #include <Rcpp.h>
27#endif
28
29namespace Parser
30{
31 class FMTLandscapeParser;
32}
33
34
35
36
37namespace Core
38{
39class FMTMask;
40class FMTMaskFilter;
41// DocString: FMTTheme
47 {
48 friend class FMTMask;
50 public:
51 // DocString: FMTTheme()
56 // DocString: ~FMTTheme()
60 ~FMTTheme() = default;
61 // DocString: FMTTheme(const std::vector<std::string>&,const std::vector<std::string>&,const std::vector<std::vector<std::string>>,const std::vector<std::map<std::string, double>>&,const size_t&, const size_t&, const std::string&)
73 FMTTheme(const std::vector<std::string>& p_attributes,
74 const std::vector<std::string>& p_attributenames,
75 const std::vector<std::string>& p_aggregates,
76 const std::vector<std::vector<std::string>>& p_aggregatenames,
77 const std::vector<std::map<std::string, double>>& p_indexes,
78 const size_t& p_id, const size_t& p_start, const std::string& p_name);
79 // DocString: FMTTheme(const std::vector<std::string>&,const std::vector<std::string>&,const std::vector<std::vector<std::string>>,const size_t&, const size_t&, const std::string&)
90 FMTTheme(const std::vector<std::string>& p_attributes,
91 const std::vector<std::string>& p_attributenames,
92 const std::vector<std::string>& p_aggregates,
93 const std::vector<std::vector<std::string>>& p_aggregatenames,
94 const size_t& p_id, const size_t& p_start, const std::string& p_name);
95 // DocString: FMTTheme(const std::vector<std::string>& lattributes,const size_t&,const size_t&,const std::string&)
103 FMTTheme(const std::vector<std::string>& p_attributes,
104 const size_t& p_id, const size_t& p_start, const std::string& p_name);
105 // DocString: FMTTheme(const FMTTheme&)
110 FMTTheme(const FMTTheme& rhs);
111 // DocString: FMTTheme::operator=
117 FMTTheme& operator = (const FMTTheme& rhs);
118 // DocString: FMTTheme::isAttribute
124 inline bool isAttribute(const std::string& p_value) const
125 {
126 if (!p_value.empty() && p_value.at(0) == '!')
127 {
128 return (_getAttribute(p_value.substr(1, p_value.size())) != m_attributem_locations.end());
129 }else {
130 return (_getAttribute(p_value) != m_attributem_locations.end());
131 }
132
133 }
134 // DocString: FMTTheme::isAggregate
140 inline bool isAggregate(const std::string& p_value) const
141 {
142 if (!p_value.empty() && p_value.at(0) == '!')
143 {
144 return (std::find(m_aggregates.begin(), m_aggregates.end(), p_value.substr(1, p_value.size())) !=
145 m_aggregates.end());
146 }else {
147 return (std::find(m_aggregates.begin(), m_aggregates.end(), p_value) != m_aggregates.end());
148 }
149
150 }
151 // DocString: FMTTheme::isIndex
158 bool isIndex(const std::string& p_attribute, const std::string& p_value) const;
159 // DocString: FMTTheme::isIndex
165 bool isIndex(const std::string& p_value) const;
166 // DocString: FMTTheme::useIndex
171 bool useIndex() const;
172 // DocString: FMTTheme::getIndex
179 double getIndex(const std::string& p_attribute, const std::string& p_value) const;
180 // DocString: FMTTheme::inAggregate
187 bool inAggregate(const std::string& p_value, const std::string& p_aggregate);
188 // DocString: FMTTheme::isValid
194 bool isValid(const std::string& p_value) const;
195 // DocString: FMTTheme::operator==
201 bool operator == (const FMTTheme& p_rhs) const;
202 // DocString: FMTTheme::size
207 inline size_t size() const
208 {
209 return m_attributes.size();
210 }
211 // DocString: FMTTheme::getStart
216 inline const size_t& getStart() const
217 {
218 return m_start;
219 }
220 // DocString: FMTTheme::getId
225 inline const size_t& getId() const
226 {
227 return m_id;
228 }
229 // DocString: FMTTheme::getName
234 inline std::string getName() const
235 {
236 return m_name;
237 }
238 // DocString: FMTTheme::empty
243 inline bool empty() const
244 {
245 return m_attributes.empty();
246 }
247 // DocString: FMTTheme::getAttributes
254 std::vector<std::string>getAttributes(const std::string& p_value, bool p_aggregate_source = false) const;
255 // DocString: FMTTheme::getBaseAttributes
260 inline const std::vector<std::string>& getBaseAttributes() const
261 {
262 return m_attributes;
263 }
264 // DocString: FMTTheme::getAttributeNames
269 const std::vector<std::string>& getAttributeNames() const
270 {
271 return m_attributenames;
272 }
273 // DocString: FMTTheme::preSolve
281 FMTTheme preSolve(FMTMaskFilter& p_maskfilter, size_t& p_newid, size_t& p_newstart) const;
282 // DocString: FMTTheme::updateFromMask
288 std::string updateFromMask(const Core::FMTMask& p_globalmask);
289 // DocString: FMTTheme::operator std::string
294 operator std::string() const;
295#if defined FMTWITHR
296 // DocString: FMTTheme::getaggregatesdataframe
301 Rcpp::DataFrame getAggregatesAsDataFrame() const;
302 // DocString: FMTTheme::getAttributesAsDataFrame
307 Rcpp::DataFrame getAttributesAsDataFrame() const;
308#endif
309 // DocString: FMTTheme::validate
317 static bool validate(const std::vector<Core::FMTTheme>& p_themes,
318 std::string& p_mask, std::string p_otherinformation = std::string());
319 // DocString: FMTObject::theme
328 static bool checkMask(const std::vector<Core::FMTTheme>& p_themes,
329 const std::vector<std::string>& p_values, std::string& p_mask,
330 const std::string& p_otherinformation);
331 // DocString: FMTObject::getAggregates
336 std::vector<std::string>getAggregates() const;
337 protected:
338 // DocString: FMTTheme::id
340 size_t m_id;
341 // DocString: FMTTheme::start
343 size_t m_start;
344 private:
345 // DocString: FMTTheme::serialize
352 friend class boost::serialization::access;
353 template<class Archive>
354 void serialize(Archive& ar, const unsigned int version)
355 {
356 try{
357 ar & boost::serialization::make_nvp("FMTobject", boost::serialization::base_object<FMTObject>(*this));
358 ar & BOOST_SERIALIZATION_NVP(m_id);
359 ar & BOOST_SERIALIZATION_NVP(m_start);
360 ar & BOOST_SERIALIZATION_NVP(m_attributes);
361 ar & BOOST_SERIALIZATION_NVP(m_attributenames);
362 ar & BOOST_SERIALIZATION_NVP(m_aggregates);
363 ar & BOOST_SERIALIZATION_NVP(m_aggregatenames);
364 ar & BOOST_SERIALIZATION_NVP(m_indexes);
365 ar & BOOST_SERIALIZATION_NVP(m_name);
366 _buildAttributeLocations();
367 }catch (...)
368 {
369 _exhandler->printExceptions("", "FMTTheme::serialize", __LINE__, __FILE__);
370 }
371 }
372 // DocString: FMTTheme::attributes
374 std::vector<std::string>m_attributes;
375 // DocString: FMTTheme::attributenames
377 std::vector<std::string>m_attributenames;
378 // DocString: FMTTheme::aggregates
380 std::vector<std::string>m_aggregates;
381 // DocString: FMTTheme::aggregates
383 std::vector<std::vector<std::string>>m_aggregatenames;
384 // DocString: FMTTheme::indexes
386 std::vector<std::map<std::string, double>>m_indexes;
387 // DocString: FMTTheme::attributem_locations
389 std::unordered_map<std::string, std::vector<size_t>>m_attributem_locations;
390 // DocString: FMTTheme::lookiterator
392 typedef typename std::unordered_map<std::string, std::vector<size_t>>::const_iterator lookiterator;
393 // DocString: FMTTheme::name
395 std::string m_name;
396 // DocString: FMTTheme::_strToBits
402 boost::dynamic_bitset<uint8_t> _strToBits(const std::string& p_value) const;
403 // DocString: FMTTheme::_getCount
409 size_t _getCount(const Core::FMTMask& p_mask) const;
410 // DocString: FMTTheme::_getFlipCount
416 size_t _getFlipCount(const Core::FMTMask& p_mask) const;
417 // DocString: FMTTheme::_findFirstFlip
423 size_t _findFirstFlip(const Core::FMTMask& p_mask) const;
424 // DocString: FMTTheme::_findFirst
430 size_t _findFirst(const Core::FMTMask& p_mask) const;
431 // DocString: FMTTheme::_isEqual
438 bool _isEqual(const Core::FMTMask& p_mask,
439 const boost::dynamic_bitset<uint8_t>& p_bits) const;
440 // DocString: FMTTheme::_isFlipEqual
447 bool _isFlipEqual(const Core::FMTMask& p_mask,
448 const boost::dynamic_bitset<uint8_t>& p_bits) const;
449 // DocString: FMTTheme::_bitsToStr
455 std::string _bitsToStr(const Core::FMTMask& p_mask) const;
456 // DocString: FMTTheme::_getAttribute
462 const std::string& _getAttribute(size_t p_attributeId) const;
463 // DocString: FMTTheme::_getAttribute
470 lookiterator _getAttribute(const std::string& p_value, bool p_raiseifnotfound = false) const;
471 // DocString: FMTTheme::_buildAttributeLocations
475 void _buildAttributeLocations();
476 // DocString: FMTTheme::_fillupAggregates
483 void _fillupAggregates(std::vector<int>& p_themeids, std::vector<std::string>& p_locattributes, std::vector<std::string>& p_locaggregates) const;
484 // DocString: FMTTheme::_pushAggregate
489 void _pushAggregate(const std::string& p_aggregatename);
490 // DocString: FMTTheme::_pushAggregate
496 void _pushAggregateValue(const std::string& p_aggregatename, const std::string& p_value);
497
498
499 };
500
501 // DocString: FMTThemeComparator
506 {
507 public:
508 // DocString: FMTThemeComparator(const FMTTheme&,const bool&)
514 FMTThemeComparator(const FMTTheme& p_lbase_theme,const bool& p_lcomparedwithpresolved=false);
515 // DocString: FMTthemecomparatorr::operator()(const FMTTheme&)
521 bool operator()(const FMTTheme& p_theme) const;
522 private:
523 // DocString: FMTThemeComparator::base_theme
525 FMTTheme m_base_theme;
526 // DocString: FMTThemeComparator::comparedwithpresolved
528 bool m_comparedwithpresolved;
529
530 };
531
532
533
534}
535
536BOOST_CLASS_EXPORT_KEY(Core::FMTTheme)
537
538#endif // FMTtheme_Hm_included
#define FMTEXPORT
Definition: FMTutility.h:125
Filter built from masks used to preSolve and postsolve FMTMask objects.
Definition: FMTMaskFilter.h:31
Core class holding, as a boost dynamic bitset, which theme attributes are part of a mask.
Definition: FMTMask.h:96
Base class of multiple FMT classes holding a shared exception handler pointer and logger.
Definition: FMTObject.h:49
Comparator used to check if two themes are the same.
Definition: FMTTheme.h:506
FMTThemeComparator(const FMTTheme &p_lbase_theme, const bool &p_lcomparedwithpresolved=false)
Construct the comparator from the theme to match.
bool operator()(const FMTTheme &p_theme) const
Matching test operator for the comparator.
Class holding the attributes and aggregates of a single theme, describing a part of a FMTMask.
Definition: FMTTheme.h:47
FMTTheme(const std::vector< std::string > &p_attributes, const std::vector< std::string > &p_attributenames, const std::vector< std::string > &p_aggregates, const std::vector< std::vector< std::string > > &p_aggregatenames, const std::vector< std::map< std::string, double > > &p_indexes, const size_t &p_id, const size_t &p_start, const std::string &p_name)
Constructor used for theme indexing with indexes.
FMTTheme(const std::vector< std::string > &p_attributes, const size_t &p_id, const size_t &p_start, const std::string &p_name)
Simpler constructor for a theme without aggregates or indexing.
const std::vector< std::string > & getAttributeNames() const
Return the names of the attributes.
Definition: FMTTheme.h:269
const std::vector< std::string > & getBaseAttributes() const
Return a reference to the base attributes of the theme.
Definition: FMTTheme.h:260
const size_t & getStart() const
Return the bit location of the mask at which the theme starts.
Definition: FMTTheme.h:216
size_t m_start
Start is the bit location in the FMTMask at which the attributes of the theme starts.
Definition: FMTTheme.h:343
std::string updateFromMask(const Core::FMTMask &p_globalmask)
Update the theme with the aggregates in a global mask or with newly created aggregates and return the...
bool inAggregate(const std::string &p_value, const std::string &p_aggregate)
Check if a value is part of an aggregate.
static bool validate(const std::vector< Core::FMTTheme > &p_themes, std::string &p_mask, std::string p_otherinformation=std::string())
Validate the construction of a valid mask string based on the themes, trimming the mask if there are ...
FMTTheme preSolve(FMTMaskFilter &p_maskfilter, size_t &p_newid, size_t &p_newstart) const
Presolve the theme, removing unused attributes based on the filter and assigning a new id and start.
bool isIndex(const std::string &p_value) const
Return true if the value is an index of the theme (yields section only).
size_t m_id
Id is the id of the theme first theme in the landscape section is number 1.
Definition: FMTTheme.h:340
bool isAggregate(const std::string &p_value) const
Return true if the value is an aggregate of the theme.
Definition: FMTTheme.h:140
bool isValid(const std::string &p_value) const
Check if a value is a valid attribute, aggregate or question mark for the theme.
bool useIndex() const
Return true if the theme uses indexes.
std::vector< std::string > getAttributes(const std::string &p_value, bool p_aggregate_source=false) const
Get the attributes of an aggregate for the theme.
size_t size() const
Return the size of the theme, 0 if it has no attribute.
Definition: FMTTheme.h:207
bool empty() const
Return true if the theme has no attribute.
Definition: FMTTheme.h:243
static bool checkMask(const std::vector< Core::FMTTheme > &p_themes, const std::vector< std::string > &p_values, std::string &p_mask, const std::string &p_otherinformation)
Validate the mask string for a vector of themes and throw an exception if something is wrong,...
~FMTTheme()=default
Default destructor for FMTTheme.
FMTTheme(const std::vector< std::string > &p_attributes, const std::vector< std::string > &p_attributenames, const std::vector< std::string > &p_aggregates, const std::vector< std::vector< std::string > > &p_aggregatenames, const size_t &p_id, const size_t &p_start, const std::string &p_name)
Constructor used for theme indexing.
bool isAttribute(const std::string &p_value) const
Return true if the value is an attribute of the theme.
Definition: FMTTheme.h:124
double getIndex(const std::string &p_attribute, const std::string &p_value) const
Get the index value for a given attribute and index.
FMTTheme(const FMTTheme &rhs)
Copy constructor for FMTTheme.
FMTTheme()
Default constructor for FMTTheme.
std::vector< std::string > getAggregates() const
Return the aggregates of the theme.
std::string getName() const
Return the name of the theme.
Definition: FMTTheme.h:234
bool isIndex(const std::string &p_attribute, const std::string &p_value) const
Return true if the value is an index of the theme for a given attribute (yields section only).
const size_t & getId() const
Return the id of the theme.
Definition: FMTTheme.h:225
Parser reading and writing the landscape section, and generating themes from vector or raster files.
Definition: FMTLandscapeParser.h:34
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTAction.h:34
Namespace handling all FMT's parsers. Everything related to I/O should be located in this namespace.
Definition: FMTActionParser.h:27