Antares Simulator
Power System Simulator
sets.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #pragma once
22 
23 #include <cassert>
24 #include <map>
25 #include <memory>
26 #include <set>
27 
28 #include <yuni/yuni.h>
29 #include <yuni/core/string.h>
30 
31 #include <antares/inifile/inifile.h>
32 #include <antares/logs/logs.h>
33 #include "antares/study/area/area.h"
34 
35 namespace Antares::Data
36 {
37 class SetHandlerAreas;
38 
39 class Sets final
40 {
41 public:
42  //
43  using IDType = Yuni::ShortString128;
44 
46  // CompareAreaName : to control the order of areas in a set of areas. This order can have an
47  // effect, even if tiny, on the results of aggregations.
48  using SetAreasType = std::set<Area*, CompareAreaName>;
49 
51  using TypePtr = std::shared_ptr<SetAreasType>;
52 
54  using MapType = std::map<IDType, TypePtr>;
56  using iterator = MapType::iterator;
58  using const_iterator = MapType::const_iterator;
59 
60  enum RuleType
61  {
62  ruleNone = 0,
63  ruleAdd,
64  ruleRemove,
65  ruleFilter,
66  ruleMax,
67  };
68 
70  using Rule = std::pair<RuleType, std::string>;
72  using RuleSet = std::vector<Rule>;
73 
74  class Options final
75  {
76  public:
77  Options():
78  output(true),
79  resultSize(0)
80  {
81  }
82 
83  Options(const Options& rhs):
84  caption(rhs.caption),
85  comments(rhs.comments),
86  rules(rhs.rules),
87  output(rhs.output),
89  {
90  }
91 
92  void reset(const IDType& id)
93  {
94  caption = id;
95  comments.clear();
96  rules.clear();
97  output = false;
98  resultSize = 0;
99  }
100 
101  Options& operator=(const Options& rhs)
102  {
103  caption = rhs.caption;
104  comments = rhs.comments;
105  rules = rhs.rules;
106  output = rhs.output;
107  resultSize = rhs.resultSize;
108  return *this;
109  }
110 
111  public:
113  IDType caption;
115  Yuni::String comments;
119  bool output;
122 
123  }; // class Options
124 
125  using MapOptions = std::map<IDType, Options>;
126 
127 public:
129 
130 
133  Sets() = default;
137  Sets(const Sets& rhs);
139  ~Sets() = default;
141 
143 
144  iterator begin();
145  const_iterator begin() const;
146  iterator end();
147  const_iterator end() const;
149 
153  void clear();
154 
155  bool forceReload(bool /*reload*/) const
156  {
157  pModified = true;
158  return true;
159  }
160 
161  void markAsModified() const
162  {
163  pModified = true;
164  }
165 
166  uint size() const;
167 
171  bool hasOutput(const Yuni::ShortString128& s) const;
172 
176  bool hasOutput(const uint index) const;
177 
181  uint resultSize(const Yuni::ShortString128& s) const;
182 
186  uint resultSize(const uint index) const;
187 
188  void dumpToLogs() const;
189 
193  bool loadFromFile(const std::filesystem::path& filename);
194 
195  bool saveToFile(const Yuni::String& filename) const;
199  YString toString();
200 
204  void defaultForAreas();
205 
209  void rebuildAllFromRules(SetHandlerAreas& handler);
210 
211  const IDType& nameByIndex(const uint i) const
212  {
213  assert(i < pMap.size() && "Sets: operator[] index out of bounds");
214  return pNameByIndex[i];
215  }
216 
217  IDType caption(const Yuni::ShortString128& s) const;
218  IDType caption(const uint i) const;
219 
220  SetAreasType& operator[](uint i);
221  const SetAreasType& operator[](uint i) const;
222 
223 private:
224  TypePtr add(const IDType& name)
225  {
226  TypePtr p = std::make_shared<SetAreasType>();
227  pMap[name] = p;
228  pOptions[name].reset(name);
229  return p;
230  }
231 
232  TypePtr add(const IDType& name, const TypePtr& data)
233  {
234  pMap[name] = data;
235  pOptions[name].reset(name);
236  return data;
237  }
238 
239  TypePtr add(const IDType& name, const TypePtr& data, Options& opts)
240  {
241  pMap[name] = data;
242  pOptions[name] = opts;
243  return data;
244  }
245 
249  void rebuildFromRules(const IDType& id, SetHandlerAreas& handler);
250  void rebuildIndexes();
251 
253  MapType pMap;
254  MapOptions pOptions;
256  std::vector<TypePtr> pByIndex;
257  std::vector<IDType> pNameByIndex;
258  mutable bool pModified = false;
259 }; // class Sets
260 
261 class SetHandlerAreas final
262 {
263 public:
264  explicit SetHandlerAreas(AreaList& areas);
265  void clear(Sets::SetAreasType& set);
266  uint size(Sets::SetAreasType& set);
267 
268  bool add(Sets::SetAreasType& set, const std::string& value);
269  void add(Sets::SetAreasType& set, const Sets::SetAreasType& otherSet);
270  bool remove(Sets::SetAreasType& set, const std::string& value);
271  void remove(Sets::SetAreasType& set, const Sets::SetAreasType& otherSet);
272  bool applyFilter(Sets::SetAreasType& set, const std::string& value);
273 
274 private:
275  AreaList& areas_;
276 }; // class SetHandlerAreas
277 
278 } // namespace Antares::Data
A list of areas.
Definition: area.h:365
Definition: sets.h:262
Definition: sets.h:75
uint resultSize
The number of items in the result set.
Definition: sets.h:121
bool output
Enable/Disable the results in the output.
Definition: sets.h:119
Yuni::String comments
Comments.
Definition: sets.h:115
RuleSet rules
Rules to build the group.
Definition: sets.h:117
IDType caption
Caption.
Definition: sets.h:113
Definition: sets.h:40
MapType::iterator iterator
Standard iterators from the STL.
Definition: sets.h:56
std::set< Area *, CompareAreaName > SetAreasType
A single set of areas.
Definition: sets.h:48
bool hasOutput(const Yuni::ShortString128 &s) const
Get if the results for a given group should be written to the output.
Definition: sets.cpp:360
MapType::const_iterator const_iterator
Standard iterators from the STL (const)
Definition: sets.h:58
uint resultSize(const Yuni::ShortString128 &s) const
Get the size of a result set.
Definition: sets.cpp:371
bool loadFromFile(const std::filesystem::path &filename)
Load a rule set from an INI File.
Definition: sets.cpp:164
void rebuildAllFromRules(SetHandlerAreas &handler)
Rebuild the lists of all group from the rules.
Definition: sets.cpp:261
void defaultForAreas()
Create default groups for set of areas.
Definition: sets.cpp:86
~Sets()=default
Destructor.
void clear()
Clear all groups.
Definition: sets.cpp:55
std::shared_ptr< SetAreasType > TypePtr
Value.
Definition: sets.h:51
std::map< IDType, TypePtr > MapType
Map of Item.
Definition: sets.h:54
std::vector< Rule > RuleSet
Rule Set.
Definition: sets.h:72
Sets()=default
Default constructor.
std::pair< RuleType, std::string > Rule
Definition of a single rule.
Definition: sets.h:70
YString toString()
format the string to match the options
Definition: sets.cpp:99