Antares Simulator
Power System Simulator
Loading...
Searching...
No Matches
decoders.hxx
1
2/*
3 * Copyright 2007-2024, 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
30namespace YAML
31{
32
43template<typename T>
44inline T as_fallback_default(const Node& n)
45{
46 return n.as<T>(T());
47}
48
49template<>
50struct 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
65template<>
66struct 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
95template<>
96struct 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 return true;
112 }
113};
114
115template<>
116struct convert<Antares::IO::Inputs::YmlModel::Port>
117{
118 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Port& rhs)
119 {
120 if (!node.IsMap())
121 {
122 return false;
123 }
124 rhs.id = node["id"].as<std::string>();
125 rhs.type = node["type"].as<std::string>();
126 return true;
127 }
128};
129
130template<>
131struct convert<Antares::IO::Inputs::YmlModel::PortFieldDefinition>
132{
133 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::PortFieldDefinition& rhs)
134 {
135 if (!node.IsMap())
136 {
137 // return true to avoid error ? port field definition not mandatory
138 return false;
139 }
140 rhs.port = node["port"].as<std::string>();
141 rhs.field = node["field"].as<std::string>();
142 rhs.definition = node["definition"].as<std::string>();
143 return true;
144 }
145};
146
147template<>
148struct convert<Antares::IO::Inputs::YmlModel::Constraint>
149{
150 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Constraint& rhs)
151 {
152 if (!node.IsMap())
153 {
154 return false;
155 }
156 rhs.id = node["id"].as<std::string>();
157 rhs.expression = node["expression"].as<std::string>();
158 return true;
159 }
160};
161
162template<>
163struct convert<Antares::IO::Inputs::YmlModel::Model>
164{
165 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Model& rhs)
166 {
167 if (!node.IsMap())
168 {
169 return false;
170 }
171 rhs.id = node["id"].as<std::string>();
172 rhs.description = node["description"].as<std::string>("");
173 rhs.parameters = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Parameter>>(
174 node["parameters"]);
175 rhs.variables = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Variable>>(
176 node["variables"]);
177 rhs.ports = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::Port>>(
178 node["ports"]);
179 rhs.port_field_definitions = as_fallback_default<
180 std::vector<Antares::IO::Inputs::YmlModel::PortFieldDefinition>>(
181 node["port-field-definitions"]);
182 rhs.constraints = as_fallback_default<
183 std::vector<Antares::IO::Inputs::YmlModel::Constraint>>(node["constraints"]);
184 rhs.binding_constraints = as_fallback_default<
185 std::vector<Antares::IO::Inputs::YmlModel::Constraint>>(node["binding-constraints"]);
186 rhs.objective = node["objective"].as<std::string>("");
187 return true;
188 }
189};
190
191template<>
192struct convert<Antares::IO::Inputs::YmlModel::PortType>
193{
194 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::PortType& rhs)
195 {
196 if (!node.IsMap())
197 {
198 return false;
199 }
200 rhs.id = node["id"].as<std::string>();
201 rhs.description = node["description"].as<std::string>("");
202 for (const auto& field: node["fields"])
203 {
204 rhs.fields.push_back(field["id"].as<std::string>());
205 }
206 return true;
207 }
208};
209
210template<>
211struct convert<Antares::IO::Inputs::YmlModel::Library>
212{
213 static bool decode(const Node& node, Antares::IO::Inputs::YmlModel::Library& rhs)
214 {
215 rhs.id = node["id"].as<std::string>();
216 rhs.description = node["description"].as<std::string>("");
217 rhs.port_types = as_fallback_default<std::vector<Antares::IO::Inputs::YmlModel::PortType>>(
218 node["port-types"]);
219 rhs.models = node["models"].as<std::vector<Antares::IO::Inputs::YmlModel::Model>>();
220 return true;
221 }
222};
223} // namespace YAML