Antares Simulator
Power System Simulator
decoders.hxx
1 
2 /*
3  * Copyright 2007-2025, RTE (https://www.rte-france.com)
4  * See AUTHORS.txt
5  * SPDX-License-Identifier: MPL-2.0
6  * This file is part of Antares-Simulator,
7  * Adequacy and Performance assessment for interconnected energy networks.
8  *
9  * Antares_Simulator is free software: you can redistribute it and/or modify
10  * it under the terms of the Mozilla Public Licence 2.0 as published by
11  * the Mozilla Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Antares_Simulator is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * Mozilla Public Licence 2.0 for more details.
18  *
19  * You should have received a copy of the Mozilla Public Licence 2.0
20  * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
21  */
22 
23 #pragma once
24 
25 #include "antares/io/inputs/yml-model/Library.h"
26 
27 #include "yaml-cpp/yaml.h"
28 
29 // Implement convert specializations
30 namespace YAML
31 {
32 
43 template<typename T>
44 inline T as_fallback_default(const Node& n)
45 {
46  return n.as<T>(T());
47 }
48 
49 template<>
50 struct convert<Antares::IO::Inputs::YmlModel::Parameter>
51 {
52  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Parameter& rhs)
53  {
54  if (!node.IsMap())
55  {
56  return false;
57  }
58  rhs.id = node["id"].as<std::string>();
59  rhs.time_dependent = node["time-dependent"].as<bool>(true);
60  rhs.scenario_dependent = node["scenario-dependent"].as<bool>(true);
61  return true;
62  }
63 };
64 
65 template<>
66 struct convert<Antares::IO::Inputs::YmlModel::ValueType>
67 {
68  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::ValueType& rhs)
69  {
70  if (!node.IsScalar())
71  {
72  return false;
73  }
74  const auto value = node.as<std::string>();
75  if (value == "continuous")
76  {
77  rhs = Antares::IO::Inputs::YmlModel::ValueType::CONTINUOUS;
78  }
79  else if (value == "integer")
80  {
81  rhs = Antares::IO::Inputs::YmlModel::ValueType::INTEGER;
82  }
83  else if (value == "boolean")
84  {
85  rhs = Antares::IO::Inputs::YmlModel::ValueType::BOOL;
86  }
87  else
88  {
89  return false;
90  }
91  return true;
92  }
93 };
94 
95 template<>
96 struct convert<Antares::IO::Inputs::YmlModel::Variable>
97 {
98  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Variable& rhs)
99  {
100  if (!node.IsMap())
101  {
102  return false;
103  }
104  rhs.id = node["id"].as<std::string>();
105  rhs.lower_bound = node["lower-bound"].as<std::string>("");
106  rhs.upper_bound = node["upper-bound"].as<std::string>("");
107  rhs.variable_type = node["variable-type"].as<Antares::IO::Inputs::YmlModel::ValueType>(
108  Antares::IO::Inputs::YmlModel::ValueType::CONTINUOUS);
109  rhs.time_dependent = node["time-dependent"].as<bool>(true);
110  rhs.scenario_dependent = node["scenario-dependent"].as<bool>(true);
111  rhs.location = node["location"].as<std::string>("subproblems");
112  return true;
113  }
114 };
115 
116 template<>
117 struct convert<Antares::IO::Inputs::YmlModel::Port>
118 {
119  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Port& rhs)
120  {
121  if (!node.IsMap())
122  {
123  return false;
124  }
125  rhs.id = node["id"].as<std::string>();
126  rhs.type = node["type"].as<std::string>();
127  return true;
128  }
129 };
130 
131 template<>
132 struct convert<Antares::IO::Inputs::YmlModel::PortFieldDefinition>
133 {
134  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::PortFieldDefinition& rhs)
135  {
136  if (!node.IsMap())
137  {
138  // return true to avoid error ? port field definition not mandatory
139  return false;
140  }
141  rhs.port = node["port"].as<std::string>();
142  rhs.field = node["field"].as<std::string>();
143  rhs.definition = node["definition"].as<std::string>();
144  return true;
145  }
146 };
147 
148 template<>
149 struct convert<Antares::IO::Inputs::YmlModel::Constraint>
150 {
151  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Constraint& rhs)
152  {
153  if (!node.IsMap())
154  {
155  return false;
156  }
157  rhs.id = node["id"].as<std::string>();
158  rhs.expression = node["expression"].as<std::string>();
159  rhs.location = node["location"].as<std::string>("subproblems");
160  return true;
161  }
162 };
163 
164 template<>
165 struct convert<Antares::IO::Inputs::YmlModel::ExtraOutput>
166 {
167  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::ExtraOutput& rhs)
168  {
169  if (!node.IsMap())
170  {
171  return false;
172  }
173  rhs.id = node["id"].as<std::string>();
174  rhs.expression = node["expression"].as<std::string>();
175  return true;
176  }
177 };
178 
179 template<>
180 struct convert<Antares::IO::Inputs::YmlModel::Objective>
181 {
182  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Objective& rhs)
183  {
184  if (!node.IsMap())
185  {
186  return false;
187  }
188  rhs.id = node["id"].as<std::string>();
189  rhs.expression = node["expression"].as<std::string>();
190  rhs.location = node["location"].as<std::string>("subproblems");
191  return true;
192  }
193 };
194 
195 template<>
196 struct convert<Antares::IO::Inputs::YmlModel::Model>
197 {
198  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Model& rhs)
199  {
200  if (!node.IsMap())
201  {
202  return false;
203  }
204  rhs.id = node["id"].as<std::string>();
205  rhs.description = node["description"].as<std::string>("");
206  rhs.parameters = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Parameter>>(
207  node["parameters"]);
208  rhs.variables = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Variable>>(
209  node["variables"]);
210  rhs.ports = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Port>>(
211  node["ports"]);
212  rhs.port_field_definitions = as_fallback_default<
213  std::vector<Antares::IO::Inputs::YmlModel::PortFieldDefinition>>(
214  node["port-field-definitions"]);
215  rhs.constraints = as_fallback_default<
216  std::vector<Antares::IO::Inputs::YmlModel::Constraint>>(node["constraints"]);
217  rhs.binding_constraints = as_fallback_default<
218  std::vector<Antares::IO::Inputs::YmlModel::Constraint>>(node["binding-constraints"]);
219  rhs.objectives = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Objective>>(
220  node["objective-contributions"]);
221  rhs.extra_outputs = as_fallback_default<
222  std::vector<Antares::IO::Inputs::YmlModel::ExtraOutput>>(node["extra-outputs"]);
223  return true;
224  }
225 };
226 
227 template<>
228 struct convert<Antares::IO::Inputs::YmlModel::PortType>
229 {
230  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::PortType& rhs)
231  {
232  if (!node.IsMap())
233  {
234  return false;
235  }
236  rhs.id = node["id"].as<std::string>();
237  rhs.description = node["description"].as<std::string>("");
238  for (const auto& field: node["fields"])
239  {
240  rhs.fields.push_back(field["id"].as<std::string>());
241  }
242  if (node["area-connection"].IsDefined())
243  {
244  if (node["area-connection"].size() != 1)
245  {
246  // Must have exactly one area connection field definition
247  return false;
248  }
249  for (const auto& field: node["area-connection"])
250  {
251  rhs.area_connection_injection_field = field["injection-field"].as<std::string>("");
252  }
253  }
254  return true;
255  }
256 };
257 
258 template<>
259 struct convert<Antares::IO::Inputs::YmlModel::Library>
260 {
261  static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Library& rhs)
262  {
263  rhs.id = node["id"].as<std::string>();
264  rhs.description = node["description"].as<std::string>("");
265  rhs.port_types = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::PortType>>(
266  node["port-types"]);
267  rhs.models = node["models"].as<std::vector<Antares::IO::Inputs::YmlModel::Model>>();
268  return true;
269  }
270 };
271 } // namespace YAML
Definition: Library.h:106
Definition: Library.h:73