yaml_export.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifdef YAML_SUPPORT
26 
27 #include <corbo-core/yaml_export.h>
28 
29 namespace corbo {
30 
32 
33 bool YamlExporter::exportSignalGroup(const std::string& filename, const CommonSignalTarget::SignalGroup& signal_group)
34 {
35  YAML::Emitter yaml_emitter;
36 
37  emitSignalGroup(signal_group, yaml_emitter);
38 
39  // write yaml emitter stream to given file
40  return write_file(yaml_emitter, filename);
41 }
42 
43 bool YamlExporter::exportTimeSeriesSignal(const std::string& filename, const TimeSeriesSignal& signal)
44 {
45  YAML::Emitter yaml_emitter;
46 
47  emitTimeSeries(signal, yaml_emitter);
48 
49  // write yaml emitter stream to given file
50  return write_file(yaml_emitter, filename);
51 }
52 
53 bool YamlExporter::exportTimeSeries(const std::string& filename, const TimeSeries& time_series)
54 {
55  YAML::Emitter yaml_emitter;
56 
57  emitTimeSeries(time_series, yaml_emitter);
58 
59  // write yaml emitter stream to given file
60  return write_file(yaml_emitter, filename);
61 }
62 
63 bool YamlExporter::exportTimeSeriesSequenceSignal(const std::string& filename, const TimeSeriesSequenceSignal& signal)
64 {
65  YAML::Emitter yaml_emitter;
66 
67  emitTimeSeriesSequence(signal, yaml_emitter);
68 
69  // write yaml emitter stream to given file
70  return write_file(yaml_emitter, filename);
71 }
72 
73 bool YamlExporter::exportIndexedValuesSignal(const std::string& filename, const IndexedValuesSignal& signal)
74 {
75  YAML::Emitter yaml_emitter;
76 
77  emitIndexedValues(signal, yaml_emitter);
78 
79  return write_file(yaml_emitter, filename);
80 }
81 
82 bool YamlExporter::exportIndexedValuesSetSignal(const std::string& filename, const IndexedValuesSetSignal& signal)
83 {
84  YAML::Emitter yaml_emitter;
85 
86  emitIndexedValuesSet(signal, yaml_emitter);
87 
88  return write_file(yaml_emitter, filename);
89 }
90 
91 bool YamlExporter::exportMatrixSignal(const std::string& filename, const MatrixSignal& signal)
92 {
93  YAML::Emitter yaml_emitter;
94 
95  emitMatrix(signal, yaml_emitter);
96 
97  return write_file(yaml_emitter, filename);
98 }
99 
100 bool YamlExporter::exportMatrixSetSignal(const std::string& filename, const MatrixSetSignal& signal)
101 {
102  YAML::Emitter yaml_emitter;
103 
104  emitMatrixSet(signal, yaml_emitter);
105 
106  return write_file(yaml_emitter, filename);
107 }
108 
109 void YamlExporter::emitSignalGroup(const CommonSignalTarget::SignalGroup& signal_group, YAML::Emitter& emitter, bool wrap_in_map)
110 {
111  if (wrap_in_map) emitter << YAML::BeginMap;
112 
113  emitter << YAML::Key << signal_group.group_name;
114  emitter << YAML::Value;
115 
116  emitter << YAML::BeginMap;
117 
118  if (!signal_group.group_signals.empty())
119  {
120  // emitter << YAML::Key << "signals";
121  // emitter << YAML::Value << YAML::BeginMap;
122 
123  for (const SignalInterface::Ptr& signal : signal_group.group_signals)
124  {
125  // check we have supported types
126  const TimeSeriesSignal* ts_signal = dynamic_cast<const TimeSeriesSignal*>(signal.get());
127  if (ts_signal)
128  {
129  emitTimeSeries(*ts_signal, emitter, false);
130  continue;
131  }
132  const TimeSeriesSequenceSignal* ts_seq_signal = dynamic_cast<const TimeSeriesSequenceSignal*>(signal.get());
133  if (ts_seq_signal)
134  {
135  emitTimeSeriesSequence(*ts_seq_signal, emitter, false);
136  continue;
137  }
138  const IndexedValuesSignal* indexed_values_signal = dynamic_cast<const IndexedValuesSignal*>(signal.get());
139  if (indexed_values_signal)
140  {
141  emitIndexedValues(*indexed_values_signal, emitter, false);
142  continue;
143  }
144  const IndexedValuesSetSignal* indexed_values_set_signal = dynamic_cast<const IndexedValuesSetSignal*>(signal.get());
145  if (indexed_values_set_signal)
146  {
147  emitIndexedValuesSet(*indexed_values_set_signal, emitter, false);
148  continue;
149  }
150  const MatrixSignal* matrix_signal = dynamic_cast<const MatrixSignal*>(signal.get());
151  if (matrix_signal)
152  {
153  emitMatrix(*matrix_signal, emitter, false);
154  continue;
155  }
156  const MatrixSetSignal* matrix_set_signal = dynamic_cast<const MatrixSetSignal*>(signal.get());
157  if (matrix_set_signal)
158  {
159  emitMatrixSet(*matrix_set_signal, emitter, false);
160  continue;
161  }
162  PRINT_WARNING_NAMED("Signal '" << signal->header.name << "' cannot be exported (not implemented).");
163  // emitter << "unsupported_signal";
164  }
165 
166  // emitter << YAML::EndMap;
167  }
168 
169  if (!signal_group.children.empty())
170  {
171  // iterate children
172  for (const CommonSignalTarget::SignalGroup& child : signal_group.children)
173  {
174  // start recursion
175  emitSignalGroup(child, emitter, false);
176  }
177  }
178 
179  emitter << YAML::EndMap;
180 
181  if (wrap_in_map) emitter << YAML::EndMap;
182 }
183 
184 void YamlExporter::emitTimeSeries(const TimeSeries& time_series, YAML::Emitter& emitter, bool wrap_in_map)
185 {
186  // go through time series signal and fill out yaml emitter
187  if (wrap_in_map) emitter << YAML::BeginMap;
188 
189  std::vector<std::string> labels = time_series.getValueLabels();
190  emitter << YAML::Key << "label";
191  emitter << YAML::Value << YAML::Flow << labels;
192 
193  emitter << YAML::Key << "time";
194  emitter << YAML::Value << YAML::Flow << time_series.getTime();
195 
196  emitter << YAML::Key << "rows";
197  emitter << YAML::Value << time_series.getValuesMatrixView().rows();
198 
199  emitter << YAML::Key << "cols";
200  emitter << YAML::Value << time_series.getValuesMatrixView().cols();
201 
202  emitter << YAML::Key << "is_column_major";
203  // emitter << YAML::Value << YAML::TrueFalseBool << !time_series.getValuesMatrixView().IsRowMajor;
204  emitter << YAML::Value << (int)time_series.isColumnMajor();
205 
206  emitter << YAML::Key << "values";
207  emitter << YAML::Value << YAML::Flow << time_series.getValues();
208 
209  emitter << YAML::Key << "time_from_start";
210  emitter << YAML::Value << time_series.getTimeFromStart();
211 
212  if (wrap_in_map) emitter << YAML::EndMap;
213 }
214 
215 void YamlExporter::emitTimeSeries(const TimeSeriesSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
216 {
217  if (wrap_in_map) emitter << YAML::BeginMap;
218 
219  emitter << YAML::Key << signal.header.getShortName();
220  emitter << YAML::Value << YAML::BeginMap;
221 
222  emitHeader(signal.header, emitter);
223 
224  if (signal.getTimeSeries())
225  {
226  const TimeSeries& time_series = *signal.getTimeSeries();
227  std::vector<std::string> labels = time_series.getValueLabels();
228  emitter << YAML::Key << "label";
229  emitter << YAML::Value << YAML::Flow << labels;
230 
231  emitter << YAML::Key << "time";
232  emitter << YAML::Value << YAML::Flow << time_series.getTime();
233 
234  emitter << YAML::Key << "rows";
235  emitter << YAML::Value << time_series.getValuesMatrixView().rows();
236 
237  emitter << YAML::Key << "cols";
238  emitter << YAML::Value << time_series.getValuesMatrixView().cols();
239 
240  emitter << YAML::Key << "is_column_major";
241  // emitter << YAML::Value << YAML::TrueFalseBool << !time_series.getValuesMatrixView().IsRowMajor;
242  emitter << YAML::Value << (int)!time_series.getValuesMatrixView().IsRowMajor;
243 
244  emitter << YAML::Key << "values";
245  emitter << YAML::Value << YAML::Flow << time_series.getValues();
246 
247  emitter << YAML::Key << "time_from_start";
248  emitter << YAML::Value << time_series.getTimeFromStart();
249  }
250  emitter << YAML::EndMap;
251  if (wrap_in_map) emitter << YAML::EndMap;
252 }
253 
254 void YamlExporter::emitTimeSeriesSequence(const TimeSeriesSequenceSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
255 {
256  if (wrap_in_map) emitter << YAML::BeginMap;
257 
258  emitter << YAML::Key << signal.header.getShortName();
259  emitter << YAML::Value << YAML::BeginMap;
260 
261  emitHeader(signal.header, emitter);
262 
263  if (!signal.isEmpty())
264  {
265  emitter << YAML::Key << "ts_seq";
266  emitter << YAML::Value << YAML::BeginSeq;
267  for (const TimeSeries::Ptr& ts : signal.getSequence()->getSequence())
268  {
269  emitTimeSeries(*ts, emitter, true);
270  }
271  emitter << YAML::EndSeq;
272  }
273 
274  emitter << YAML::EndMap;
275 
276  if (wrap_in_map) emitter << YAML::EndMap;
277 }
278 
279 void YamlExporter::emitIndexedValues(const IndexedValuesSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
280 {
281  if (wrap_in_map) emitter << YAML::BeginMap;
282 
283  emitter << YAML::Key << signal.header.getShortName();
284  emitter << YAML::Value << YAML::BeginMap;
285 
286  emitHeader(signal.header, emitter);
287 
288  emitter << YAML::Key << "values";
289  emitter << YAML::Value << YAML::Flow << YAML::BeginSeq;
290 
291  // start tuple
292  emitter << YAML::BeginSeq;
293 
294  // insert index
295  emitter << signal.getIndex();
296 
297  // insert data
298  emitter << signal.getValues();
299 
300  // end tupel
301  emitter << YAML::EndSeq;
302 
303  emitter << YAML::EndSeq;
304  emitter << YAML::EndMap;
305  if (wrap_in_map) emitter << YAML::EndMap;
306 }
307 
308 void YamlExporter::emitIndexedValuesSet(const IndexedValuesSetSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
309 {
310  // emitter << YAML::Comment("You are using the free version of corbo. The free version is limited to 1 export per hour.");
311  if (wrap_in_map) emitter << YAML::BeginMap;
312 
313  emitter << YAML::Key << signal.header.getShortName();
314  emitter << YAML::Value << YAML::BeginMap;
315 
316  emitHeader(signal.header, emitter);
317 
318  emitter << YAML::Key << "values";
319  emitter << YAML::Value << YAML::BeginSeq;
320 
321  const std::map<int, std::vector<double>>& map = signal.getData();
322  auto it = map.begin();
323 
324  while (it != map.end())
325  {
326  // start tupel
327  emitter << YAML::Flow << YAML::BeginSeq;
328 
329  // insert index
330  emitter << it->first;
331 
332  // insert data
333  emitter << it->second;
334  // emitter << it->second.size();
335 
336  // end tupel
337  emitter << YAML::EndSeq;
338  it++;
339  }
340 
341  emitter << YAML::EndSeq;
342  emitter << YAML::EndMap;
343  if (wrap_in_map) emitter << YAML::EndMap;
344 }
345 
346 void YamlExporter::emitMatrix(const MatrixSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
347 {
348  if (wrap_in_map) emitter << YAML::BeginMap;
349 
350  emitter << YAML::Key << signal.header.getShortName();
351  emitter << YAML::Value << YAML::BeginMap;
352 
353  emitHeader(signal.header, emitter);
354 
355  if (!signal.isEmpty())
356  {
357  emitter << YAML::Key << "label";
358  emitter << YAML::Value << signal.getLabel();
359 
360  emitter << YAML::Key << "rows";
361  emitter << YAML::Value << signal.getRowDimension();
362 
363  emitter << YAML::Key << "cols";
364  emitter << YAML::Value << signal.getColDimension();
365 
366  emitter << YAML::Key << "is_column_major";
367  // emitter << YAML::Value << YAML::TrueFalseBool << !signal.getMatrix().IsRowMajor;
368  emitter << YAML::Value << 1; // see for-loops below
369 
370  emitter << YAML::Key << "values";
371  emitter << YAML::Value << YAML::Flow << YAML::BeginSeq;
372  for (int col_idx = 0; col_idx < signal.getMatrix().cols(); ++col_idx)
373  for (int row_idx = 0; row_idx < signal.getMatrix().rows(); ++row_idx) emitter << signal.getMatrix()(row_idx, col_idx);
374 
375  emitter << YAML::EndSeq;
376  }
377 
378  emitter << YAML::EndMap;
379  if (wrap_in_map) emitter << YAML::EndMap;
380 }
381 
382 void YamlExporter::emitMatrixSet(const MatrixSetSignal& signal, YAML::Emitter& emitter, bool wrap_in_map)
383 {
384  if (wrap_in_map) emitter << YAML::BeginMap;
385 
386  emitter << YAML::Key << signal.header.getShortName();
387  emitter << YAML::Value << YAML::BeginMap;
388 
389  emitHeader(signal.header, emitter);
390 
391  if (!signal.isEmpty())
392  {
393  emitter << YAML::Key << "mat_seq";
394  emitter << YAML::Value << YAML::BeginSeq;
395  for (const MatrixSignal::Ptr& mat : signal.getData())
396  {
397  emitMatrix(*mat, emitter, true);
398  }
399  emitter << YAML::EndSeq;
400  }
401 
402  emitter << YAML::EndMap;
403 
404  if (wrap_in_map) emitter << YAML::EndMap;
405 }
406 
407 void YamlExporter::emitHeader(const SignalHeader& header, YAML::Emitter& emitter)
408 {
409  emitter << YAML::Key << "header";
410  emitter << YAML::Value << YAML::BeginMap;
411 
412  emitter << YAML::Key << "name";
413  emitter << YAML::Value << header.name;
414 
415  emitter << YAML::Key << "time_stamp";
416  emitter << YAML::Value << header.time.toSec();
417 
418  emitter << YAML::EndMap;
419 }
420 
421 bool YamlExporter::write_file(const YAML::Emitter& emitter, const std::string& filename)
422 {
423  std::ofstream file;
424  file.open(filename);
425  if (file.is_open())
426  {
427  file << emitter.c_str();
428  file.close();
429  return true;
430  }
431  else
432  {
433  return false;
434  }
435 }
436 
437 } // namespace corbo
438 
439 #endif // YAML_SUPPORT
#define PRINT_WARNING_NAMED(msg)
Definition: console.h:255
return int(ret)+1
bool exportTimeSeriesSignal(const std::string &filename, const TimeSeriesSignal &signal) override
bool exportIndexedValuesSetSignal(const std::string &filename, const IndexedValuesSetSignal &signal) override
bool exportTimeSeries(const std::string &filename, const TimeSeries &time_series) override
void emitIndexedValuesSet(const IndexedValuesSetSignal &signal, YAML::Emitter &emitter, bool wrap_in_map=true)
void emitTimeSeriesSequence(const TimeSeriesSequenceSignal &signal, YAML::Emitter &emitter, bool wrap_in_map=true)
void emitIndexedValues(const IndexedValuesSignal &signal, YAML::Emitter &emitter, bool wrap_in_map=true)
bool exportMatrixSetSignal(const std::string &filename, const MatrixSetSignal &signal) override
void emitHeader(const SignalHeader &header, YAML::Emitter &emitter)
bool write_file(const YAML::Emitter &emitter, const std::string &filename)
std::shared_ptr< SignalInterface > Ptr
Definition: signals.h:136
bool exportSignalGroup(const std::string &filename, const CommonSignalTarget::SignalGroup &signal_group) override
bool exportTimeSeriesSequenceSignal(const std::string &filename, const TimeSeriesSequenceSignal &signal) override
bool exportIndexedValuesSignal(const std::string &filename, const IndexedValuesSignal &signal) override
void emitMatrix(const MatrixSignal &signal, YAML::Emitter &emitter, bool wrap_in_map=true)
bool exportMatrixSignal(const std::string &filename, const MatrixSignal &signal) override
std::shared_ptr< MatrixSignal > Ptr
Definition: signals.h:542
void emitSignalGroup(const CommonSignalTarget::SignalGroup &signal_group, YAML::Emitter &emitter, bool wrap_in_map=true)
void emitMatrixSet(const MatrixSetSignal &signal, YAML::Emitter &emitter, bool wrap_in_map=true)
void emitTimeSeries(const TimeSeries &time_series, YAML::Emitter &emitter, bool wrap_in_map=true)
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:64
else mat
Definition: eigenvalues.cpp:43


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:59