io.hpp 28.7 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
6
7
8
9
10
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef ORG_VLEPROJECT_IRRITATOR_IO_2020
#define ORG_VLEPROJECT_IRRITATOR_IO_2020

#include <irritator/core.hpp>

#include <algorithm>
11
12
#include <istream>
#include <ostream>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
13
#include <vector>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
14
15
16
17
18
19

namespace irt {

class reader
{
private:
20
    std::istream& is;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
21

22
    std::vector<model_id> map;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
23
24
25
    int model_error = 0;
    int connection_error = 0;

26
27
28
    char temp_1[32];
    char temp_2[32];

Gauthier Quesnel's avatar
Gauthier Quesnel committed
29
public:
30
31
    reader(std::istream& is_) noexcept
      : is(is_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
32
33
    {}

34
    ~reader() noexcept = default;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
35
36
37
38
39

    status operator()(simulation& sim) noexcept
    {
        int model_number = 0;

40
        irt_return_if_fail((is >> model_number), status::io_file_format_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
41
42
43
        irt_return_if_fail(model_number > 0,
                           status::io_file_format_model_number_error);

44
45
46
47
48
        try {
            map.resize(model_number, model_id{ 0 });
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
49
50
51
52

        int id;

        for (int i = 0; i != model_number; ++i, ++model_error) {
53
54
            irt_return_if_fail((is >> id >> temp_1 >> temp_2),
                               status::io_file_format_model_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
55

Gauthier Quesnel's avatar
Gauthier Quesnel committed
56
57
58
            irt_return_if_fail(0 <= id && id < model_number,
                               status::io_file_format_model_error);

59
            irt_return_if_bad(read(sim, id, temp_1, temp_2));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
60
61
        }

62
        while (is) {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
63
64
            int mdl_src_index, port_src_index, mdl_dst_index, port_dst_index;

65
66
67
68
            if (!(is >> mdl_src_index >> port_src_index >> mdl_dst_index >>
                  port_dst_index)) {
                if (is.eof())
                    break;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
69

70
71
                irt_bad_return(status::io_file_format_error);
            }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
72

73
74
            auto* mdl_src = sim.models.try_to_get(mdl_src_index);
            irt_return_if_fail(mdl_src, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
75

76
77
            auto* mdl_dst = sim.models.try_to_get(mdl_dst_index);
            irt_return_if_fail(mdl_dst, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
78

79
80
            output_port_id output_port;
            input_port_id input_port;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
81

82
83
84
85
            irt_return_if_bad(
              sim.get_output_port_id(*mdl_src, port_src_index, &output_port));
            irt_return_if_bad(
              sim.get_input_port_id(*mdl_dst, port_dst_index, &input_port));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
86

87
88
            irt_return_if_bad(sim.connect(output_port, input_port));
            ++connection_error;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
89
90
91
92
93
94
        }

        return status::success;
    }

private:
95
96
    bool convert(const std::string_view dynamics_name,
                 dynamics_type* type) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
97
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        struct string_to_type
        {
            constexpr string_to_type(const std::string_view n,
                                     const dynamics_type t)
              : name(n)
              , type(t)
            {}

            const std::string_view name;
            dynamics_type type;
        };

        static constexpr string_to_type table[] = {
            { "accumulator_2", dynamics_type::accumulator_2 },
            { "adder_2", dynamics_type::adder_2 },
            { "adder_3", dynamics_type::adder_3 },
            { "adder_4", dynamics_type::adder_4 },
            { "constant", dynamics_type::constant },
            { "counter", dynamics_type::counter },
            { "cross", dynamics_type::cross },
            { "generator", dynamics_type::generator },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
119
            { "flow", dynamics_type::flow },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
120
121
122
123
124
125
126
            { "integrator", dynamics_type::integrator },
            { "mult_2", dynamics_type::mult_2 },
            { "mult_3", dynamics_type::mult_3 },
            { "mult_4", dynamics_type::mult_4 },
            { "none", dynamics_type::none },
            { "quantifier", dynamics_type::quantifier },
            { "qss1_integrator", dynamics_type::qss1_integrator },
127
128
            { "qss1_multiplier", dynamics_type::qss1_multiplier },
            { "qss1_cross", dynamics_type::qss1_cross },
129
130
            { "qss1_power", dynamics_type::qss1_power },
            { "qss1_square", dynamics_type::qss1_square },
131
132
133
134
135
136
            { "qss1_sum_2", dynamics_type::qss1_sum_2 },
            { "qss1_sum_3", dynamics_type::qss1_sum_3 },
            { "qss1_sum_4", dynamics_type::qss1_sum_4 },
            { "qss1_wsum_2", dynamics_type::qss1_wsum_2 },
            { "qss1_wsum_3", dynamics_type::qss1_wsum_3 },
            { "qss1_wsum_4", dynamics_type::qss1_wsum_4 },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
137
138
            { "qss2_integrator", dynamics_type::qss2_integrator },
            { "qss2_multiplier", dynamics_type::qss2_multiplier },
139
            { "qss2_cross", dynamics_type::qss2_cross },
140
141
            { "qss2_power", dynamics_type::qss2_power },
            { "qss2_square", dynamics_type::qss2_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
142
143
144
145
146
147
            { "qss2_sum_2", dynamics_type::qss2_sum_2 },
            { "qss2_sum_3", dynamics_type::qss2_sum_3 },
            { "qss2_sum_4", dynamics_type::qss2_sum_4 },
            { "qss2_wsum_2", dynamics_type::qss2_wsum_2 },
            { "qss2_wsum_3", dynamics_type::qss2_wsum_3 },
            { "qss2_wsum_4", dynamics_type::qss2_wsum_4 },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
148
149
150
            { "qss3_integrator", dynamics_type::qss3_integrator },
            { "qss3_multiplier", dynamics_type::qss3_multiplier },
            { "qss3_cross", dynamics_type::qss3_cross },
151
152
            { "qss3_power", dynamics_type::qss3_power },
            { "qss3_square", dynamics_type::qss3_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
153
154
155
156
157
158
            { "qss3_sum_2", dynamics_type::qss3_sum_2 },
            { "qss3_sum_3", dynamics_type::qss3_sum_3 },
            { "qss3_sum_4", dynamics_type::qss3_sum_4 },
            { "qss3_wsum_2", dynamics_type::qss3_wsum_2 },
            { "qss3_wsum_3", dynamics_type::qss3_wsum_3 },
            { "qss3_wsum_4", dynamics_type::qss3_wsum_4 },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
159
160
161
            { "time_func", dynamics_type::time_func }
        };

162
163
        static_assert(std::size(table) ==
                      static_cast<sz>(dynamics_type_size()));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
164
165
166
167
168
169
170
171
172
173

        const auto it =
          std::lower_bound(std::begin(table),
                           std::end(table),
                           dynamics_name,
                           [](const string_to_type& l,
                              const std::string_view r) { return l.name < r; });

        if (it != std::end(table) && it->name == dynamics_name) {
            *type = it->type;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
174
175
176
            return true;
        }

177
178
179
180
181
        if (dynamics_name == "flow") {
            *type = dynamics_type::flow;
            return true;
        }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
182
183
184
        return false;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
185
186
187
188
    status read(simulation& sim,
                int id,
                const char* name,
                const char* dynamics_name) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
189
190
191
192
193
194
195
    {
        dynamics_type type;

        irt_return_if_fail(convert(dynamics_name, &type),
                           status::io_file_format_dynamics_unknown);

        model_id mdl = static_cast<model_id>(0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
196
        auto ret = sim.dispatch(type, [this, &sim, name](auto& dyn_models) {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
197
198
199
200
201
202
203
204
205
206
207
208
209
            irt_return_if_fail(dyn_models.can_alloc(1),
                               status::io_file_format_dynamics_limit_reach);
            auto& dyn = dyn_models.alloc();
            auto dyn_id = dyn_models.get_id(dyn);

            sim.alloc(dyn, dyn_id, name);

            irt_return_if_fail(read(dyn),
                               status::io_file_format_dynamics_init_error);

            return status::success;
        });

Gauthier Quesnel's avatar
Gauthier Quesnel committed
210
211
        irt_return_if_bad(ret);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
212
        map[id] = mdl;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
213
214

        return status::success;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
215
216
217
218
219
220
221
    }

    bool read(none& /*dyn*/) noexcept
    {
        return true;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    bool read(qss1_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

    bool read(qss2_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
238
239
240
241
242
243
244
245
    bool read(qss3_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    bool read(qss1_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss1_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss1_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
292

Gauthier Quesnel's avatar
Gauthier Quesnel committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    bool read(qss2_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss2_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss2_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    bool read(qss3_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss3_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss3_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
387
388
    bool read(integrator& dyn) noexcept
    {
389
        return !!(is >> dyn.default_current_value >> dyn.default_reset_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
390
391
392
393
    }

    bool read(quantifier& dyn) noexcept
    {
394
395
        if (!(is >> dyn.default_step_size >> dyn.default_past_length >>
              temp_1 >> temp_2))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
396
397
            return false;

398
        if (std::strcmp(temp_1, "possible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
399
            dyn.default_adapt_state = quantifier::adapt_state::possible;
400
        else if (std::strcmp(temp_1, "impossible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
401
            dyn.default_adapt_state = quantifier::adapt_state::impossible;
402
        else if (std::strcmp(temp_1, "done") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
403
404
405
406
            dyn.default_adapt_state = quantifier::adapt_state::done;
        else
            return false;

407
        if (std::strcmp(temp_2, "true") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
408
            dyn.default_zero_init_offset = true;
409
        else if (std::strcmp(temp_2, "false") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
410
411
412
413
414
415
416
417
418
            dyn.default_zero_init_offset = false;
        else
            return false;

        return true;
    }

    bool read(adder_2& dyn) noexcept
    {
419
420
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
421
422
423
424
    }

    bool read(adder_3& dyn) noexcept
    {
425
426
427
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
428
429
430
431
    }

    bool read(adder_4& dyn) noexcept
    {
432
433
434
435
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
436
437
438
439
    }

    bool read(mult_2& dyn) noexcept
    {
440
441
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
442
443
444
445
    }

    bool read(mult_3& dyn) noexcept
    {
446
447
448
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
449
450
451
452
    }

    bool read(mult_4& dyn) noexcept
    {
453
454
455
456
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
457
458
459
460
461
462
463
    }

    bool read(counter& /*dyn*/) noexcept
    {
        return true;
    }

464
    bool read(generator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
465
    {
466
        return !!(is >> dyn.default_value >> dyn.default_period >>
467
                  dyn.default_offset);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
468
469
470
471
    }

    bool read(constant& dyn) noexcept
    {
472
        return !!(is >> dyn.default_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
473
474
    }

475
476
477
478
479
480
481
482
483
484
    bool read(qss1_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

    bool read(qss2_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
485
486
487
488
489
    bool read(qss3_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

490
    bool read(qss1_power& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
491
    {
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
        return !!(is >> dyn.default_n);
    }

    bool read(qss2_power& dyn) noexcept
    {
        return !!(is >> dyn.default_n);
    }

    bool read(qss3_power& dyn) noexcept
    {
        return !!(is >> dyn.default_n);
    }

    bool read(qss1_square& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_square& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_square& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(cross& /*dyn*/) noexcept
    {
        return true;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
523
524
    }

525
526
527
528
529
    bool read(accumulator_2& /*dyn*/) noexcept
    {
        return true;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
530
531
    bool read(time_func& dyn) noexcept
    {
532
        if (!(is >> temp_1))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
533
534
            return false;

535
        if (std::strcmp(temp_1, "square") == 0)
536
            dyn.default_f = &square_time_function;
537
        else
538
            dyn.default_f = &time_function;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
539
540
541

        return true;
    }
542
543
544

    bool read(flow& dyn) noexcept
    {
545
        return !!(is >> dyn.default_samplerate);
546
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
547
548
549
550
};

struct writer
{
551
    std::ostream& os;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
552

553
    std::vector<model_id> map;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
554

555
556
    writer(std::ostream& os_) noexcept
      : os(os_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
557
558
559
560
    {}

    status operator()(const simulation& sim) noexcept
    {
561
        os << sim.models.size() << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
562

563
564
565
566
567
        try {
            map.resize(sim.models.size(), model_id{ 0 });
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
568

Gauthier Quesnel's avatar
Gauthier Quesnel committed
569
        model* mdl = nullptr;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
570
        int id = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
571
572
573
        while (sim.models.next(mdl)) {
            const auto mdl_id = sim.models.get_id(mdl);

574
            os << id << ' ' << mdl->name.c_str() << ' ';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
575
            map[id] = mdl_id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
576
577
578
579
580

            sim.dispatch(mdl->type, [this, mdl](auto& dyn_models) {
                write(dyn_models.get(mdl->id));
                return status::success;
            });
Gauthier Quesnel's avatar
Gauthier Quesnel committed
581
582

            ++id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
583
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
584

Gauthier Quesnel's avatar
Gauthier Quesnel committed
585
586
587
588
589
590
        irt::output_port* out = nullptr;
        while (sim.output_ports.next(out)) {
            for (auto dst : out->connections) {
                if (auto* in = sim.input_ports.try_to_get(dst); in) {
                    auto* mdl_src = sim.models.try_to_get(out->model);
                    auto* mdl_dst = sim.models.try_to_get(in->model);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
591

Gauthier Quesnel's avatar
Gauthier Quesnel committed
592
593
                    if (!(mdl_src && mdl_dst))
                        continue;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
594

Gauthier Quesnel's avatar
Gauthier Quesnel committed
595
596
                    int src_index = -1;
                    int dst_index = -1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
597

Gauthier Quesnel's avatar
Gauthier Quesnel committed
598
                    irt_return_if_bad(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
599
                      sim.get_input_port_index(*mdl_dst, dst, &dst_index));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
600

Gauthier Quesnel's avatar
Gauthier Quesnel committed
601
                    irt_return_if_bad(sim.get_output_port_index(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
602
603
604
605
606
607
608
                      *mdl_src, sim.output_ports.get_id(out), &src_index));

                    auto it_out = std::find(map.begin(), map.end(), out->model);
                    auto it_in = std::find(map.begin(), map.end(), in->model);

                    assert(it_out != map.end());
                    assert(it_in != map.end());
Gauthier Quesnel's avatar
Gauthier Quesnel committed
609

610
                    os << std::distance(map.begin(), it_out) << ' ' << src_index
611
612
                       << ' ' << std::distance(map.begin(), it_in) << ' '
                       << dst_index << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
613
614
615
616
617
618
619
620
                }
            }
        }

        return status::success;
    }

private:
Gauthier Quesnel's avatar
Gauthier Quesnel committed
621
    void write(const none& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
622
    {
623
        os << "none\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
624
625
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
626
627
628
629
630
631
632
633
634
635
636
637
    void write(const qss1_integrator& dyn) noexcept
    {
        os << "qss1_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

    void write(const qss2_integrator& dyn) noexcept
    {
        os << "qss2_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
638
639
640
641
642
643
    void write(const qss3_integrator& dyn) noexcept
    {
        os << "qss3_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
    void write(const qss1_multiplier& /*dyn*/) noexcept
    {
        os << "qss1_multiplier\n";
    }

    void write(const qss1_sum_2& /*dyn*/) noexcept
    {
        os << "qss1_sum_2\n";
    }

    void write(const qss1_sum_3& /*dyn*/) noexcept
    {
        os << "qss1_sum_3\n";
    }

    void write(const qss1_sum_4& /*dyn*/) noexcept
    {
        os << "qss1_sum_4\n";
    }

    void write(const qss1_wsum_2& dyn) noexcept
    {
        os << "qss1_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss1_wsum_3& dyn) noexcept
    {
        os << "qss1_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss1_wsum_4& dyn) noexcept
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
679
        os << "qss1_wsum_4 " << dyn.default_input_coeffs[0] << ' '
680
681
682
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
683

Gauthier Quesnel's avatar
Gauthier Quesnel committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
    void write(const qss2_multiplier& /*dyn*/) noexcept
    {
        os << "qss2_multiplier\n";
    }

    void write(const qss2_sum_2& /*dyn*/) noexcept
    {
        os << "qss2_sum_2\n";
    }

    void write(const qss2_sum_3& /*dyn*/) noexcept
    {
        os << "qss2_sum_3\n";
    }

    void write(const qss2_sum_4& /*dyn*/) noexcept
    {
        os << "qss2_sum_4\n";
    }

    void write(const qss2_wsum_2& dyn) noexcept
    {
        os << "qss2_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss2_wsum_3& dyn) noexcept
    {
        os << "qss2_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss2_wsum_4& dyn) noexcept
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
        os << "qss2_wsum_4 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }

    void write(const qss3_multiplier& /*dyn*/) noexcept
    {
        os << "qss3_multiplier\n";
    }

    void write(const qss3_sum_2& /*dyn*/) noexcept
    {
        os << "qss3_sum_2\n";
    }

    void write(const qss3_sum_3& /*dyn*/) noexcept
    {
        os << "qss3_sum_3\n";
    }

    void write(const qss3_sum_4& /*dyn*/) noexcept
    {
        os << "qss3_sum_4\n";
    }

    void write(const qss3_wsum_2& dyn) noexcept
    {
        os << "qss3_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss3_wsum_3& dyn) noexcept
    {
        os << "qss3_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss3_wsum_4& dyn) noexcept
    {
        os << "qss3_wsum_4 " << dyn.default_input_coeffs[0] << ' '
Gauthier Quesnel's avatar
Gauthier Quesnel committed
760
761
762
763
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
764
765
    void write(const integrator& dyn) noexcept
    {
766
767
        os << "integrator " << dyn.default_current_value << ' '
           << dyn.default_reset_value << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
768
769
770
771
    }

    void write(const quantifier& dyn) noexcept
    {
772
773
774
775
776
777
778
779
780
        os << "quantifier " << dyn.default_step_size << ' '
           << dyn.default_past_length << ' '
           << ((dyn.default_adapt_state == quantifier::adapt_state::possible)
                 ? "possible "
                 : dyn.default_adapt_state ==
                       quantifier::adapt_state::impossible
                     ? "impossibe "
                     : "done ")
           << (dyn.default_zero_init_offset == true ? "true\n" : "false\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
781
782
783
784
    }

    void write(const adder_2& dyn) noexcept
    {
785
786
787
        os << "adder_2 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
788
789
790
791
    }

    void write(const adder_3& dyn) noexcept
    {
792
793
794
795
        os << "adder_3 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_values[2] << ' '
           << dyn.default_input_coeffs[0] << ' ' << dyn.default_input_coeffs[1]
           << ' ' << dyn.default_input_coeffs[2] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
796
797
798
799
    }

    void write(const adder_4& dyn) noexcept
    {
800
801
802
803
804
        os << "adder_4 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_values[2] << ' '
           << dyn.default_values[3] << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
805
806
807
808
    }

    void write(const mult_2& dyn) noexcept
    {
809
810
811
        os << "mult_2 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
812
813
814
815
    }

    void write(const mult_3& dyn) noexcept
    {
816
817
818
819
        os << "mult_3 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_values[2] << ' ' << dyn.default_input_coeffs[0]
           << ' ' << dyn.default_input_coeffs[1] << ' '
           << dyn.default_input_coeffs[2] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
820
821
822
823
    }

    void write(const mult_4& dyn) noexcept
    {
824
825
826
827
828
        os << "mult_4 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_values[2] << ' ' << dyn.default_values[3]
           << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
829
830
831
832
    }

    void write(const counter& /*dyn*/) noexcept
    {
833
        os << "counter\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
834
835
    }

836
    void write(const generator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
837
    {
838
839
840
        os << "generator " << dyn.default_value << ' ' << dyn.default_period
           << ' ' << dyn.default_offset << '\n';
        ;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
841
842
843
844
    }

    void write(const constant& dyn) noexcept
    {
845
        os << "constant " << dyn.default_value << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
846
847
    }

848
849
850
851
852
853
854
855
856
857
    void write(const qss1_cross& dyn) noexcept
    {
        os << "qss1_cross " << dyn.default_threshold << '\n';
    }

    void write(const qss2_cross& dyn) noexcept
    {
        os << "qss2_cross " << dyn.default_threshold << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
858
859
860
861
862
    void write(const qss3_cross& dyn) noexcept
    {
        os << "qss3_cross " << dyn.default_threshold << '\n';
    }

863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
    void write(const qss1_power& dyn) noexcept
    {
        os << "qss1_power " << dyn.default_n << '\n';
    }

    void write(const qss2_power& dyn) noexcept
    {
        os << "qss2_power " << dyn.default_n << '\n';
    }

    void write(const qss3_power& dyn) noexcept
    {
        os << "qss3_power " << dyn.default_n << '\n';
    }

    void write(const qss1_square& /*dyn*/) noexcept
    {
        os << "qss1_square\n";
    }

    void write(const qss2_square& /*dyn*/) noexcept
    {
        os << "qss2_square\n";
    }

    void write(const qss3_square& /*dyn*/) noexcept
    {
        os << "qss3_square\n";
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
893
894
    void write(const cross& dyn) noexcept
    {
895
        os << "cross " << dyn.default_threshold << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
896
897
    }

898
899
900
901
902
    void write(const accumulator_2& /*dyn*/) noexcept
    {
        os << "accumulator_2\n";
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
903
904
    void write(const time_func& dyn) noexcept
    {
905
906
        os << "time_func "
           << (dyn.default_f == &time_function ? "time\n" : "square\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
907
    }
908
909
910

    void write(const flow& dyn) noexcept
    {
911
        os << "flow " << dyn.default_samplerate << '\n';
912
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
913
914
915
916
917
};

class dot_writer
{
private:
918
    std::ostream& os;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
919
920

public:
921
922
    dot_writer(std::ostream& os_)
      : os(os_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
    {}

    /* With input and output port.

    digraph graphname{
        graph[rankdir = "LR"];
        node[shape = "record"];
        edge[];

        "sum_a"[label = "sum-a | <f0> | <f1>"];

        "sum_a":f0->int_a[id = 1];
        sum_b->int_b[label = "2-10"];
        prod->sum_b[label = "3-4"];
        prod -> "sum_a":f0[label = "3-2"];
        int_a->qua_a[label = "4-11"];
        int_a->prod[label = "4-5"];
        int_a -> "sum_a":f1[label = "4-1"];
        int_b->qua_b[label = "5-12"];
        int_b->prod[label = "5-6"];
        int_b->sum_b[label = "5-3"];
        qua_a->int_a[label = "6-7"];
        qua_b->int_b[label = "7-9"];
    }
    */

    void operator()(const simulation& sim) noexcept
    {
951
        os << "digraph graphname {\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
952
953
954
955
956
957
958
959
960
961
962
963
964

        irt::output_port* output_port = nullptr;
        while (sim.output_ports.next(output_port)) {
            for (const irt::input_port_id dst : output_port->connections) {
                if (auto* input_port = sim.input_ports.try_to_get(dst);
                    input_port) {
                    auto* mdl_src = sim.models.try_to_get(output_port->model);
                    auto* mdl_dst = sim.models.try_to_get(input_port->model);

                    if (!(mdl_src && mdl_dst))
                        continue;

                    if (mdl_src->name.empty())
965
                        os << irt::get_key(output_port->model);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
966
                    else
967
968
969
                        os << mdl_src->name.c_str();

                    os << " -> ";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
970
971

                    if (mdl_dst->name.empty())
972
                        os << irt::get_key(input_port->model);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
973
                    else
974
                        os << mdl_dst->name.c_str();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
975

976
                    os << " [label=\"";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
977
978

                    if (output_port->name.empty())
979
980
                        os << irt::get_key(
                          sim.output_ports.get_id(*output_port));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
981
                    else
982
                        os << output_port->name.c_str();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
983

984
                    os << " - ";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
985
986

                    if (input_port->name.empty())
987
                        os << irt::get_key(sim.input_ports.get_id(*input_port));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
988
                    else
989
                        os << input_port->name.c_str();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
990

991
                    os << "\"];\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
992
993
994
995
996
997
998
999
1000
                }
            }
        }
    }
};

} // namespace irt

#endif