io.hpp 20.3 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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>

#include <cstdio>

namespace irt {

class reader
{
private:
    ::std::FILE* file = stdin;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
21
    array<model_id> map;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    int model_error = 0;
    int connection_error = 0;

public:
    reader(::std::FILE* file_ = stdin) noexcept
      : file(file_ == nullptr ? stdin : file_)
    {}

    ~reader() noexcept
    {
        if (file && file != stdin)
            ::std::fclose(file);
    }

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

        irt_return_if_fail(std::fscanf(file, "%d", &model_number) == 1,
                           status::io_file_format_error);
        irt_return_if_fail(model_number > 0,
                           status::io_file_format_model_number_error);

        irt_return_if_bad(map.init(model_number));

Gauthier Quesnel's avatar
Gauthier Quesnel committed
47
        std::fill_n(std::begin(map), std::size(map), static_cast<model_id>(0));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
48
49
50
51
52
53
54
55
56
57

        int id;
        char name[8];
        char dynamics[11];

        for (int i = 0; i != model_number; ++i, ++model_error) {
            irt_return_if_fail(
              3 == std::fscanf(file, "%d %7s %10s", &id, name, dynamics),
              status::io_file_format_model_error);

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
61
62
63
64
65
66
67
68
69
70
71
72
73
            irt_return_if_bad(read(sim, id, name, dynamics));
        }

        while (!std::feof(file)) {
            int mdl_src_index, port_src_index, mdl_dst_index, port_dst_index;

            int read = std::fscanf(file,
                                   "%d %d %d %d",
                                   &mdl_src_index,
                                   &port_src_index,
                                   &mdl_dst_index,
                                   &port_dst_index);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
74
            irt_return_if_fail(read == -1 || read == 0 || read == 4,
Gauthier Quesnel's avatar
Gauthier Quesnel committed
75
76
                               status::io_file_format_error);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
77
78
79
            if (read == -1)
                break;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
80
            if (read == 4) {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
81
                auto* mdl_src = sim.models.try_to_get(mdl_src_index);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
82
83
84
                irt_return_if_fail(mdl_src,
                                   status::io_file_format_model_unknown);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
85
                auto* mdl_dst = sim.models.try_to_get(mdl_dst_index);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                irt_return_if_fail(mdl_dst,
                                   status::io_file_format_model_unknown);

                output_port_id output_port;
                input_port_id input_port;

                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));

                irt_return_if_bad(sim.connect(output_port, input_port));
                ++connection_error;
            }
        }

        return status::success;
    }

private:
    bool convert(const char* dynamics_name, dynamics_type* type) noexcept
    {
        if (std::strcmp(dynamics_name, "none") == 0) {
            *type = dynamics_type::none;
            return true;
        }

        if (std::strcmp(dynamics_name, "integrator") == 0) {
            *type = dynamics_type::integrator;
            return true;
        }

        if (std::strcmp(dynamics_name, "quantifier") == 0) {
            *type = dynamics_type::quantifier;
            return true;
        }

        if (std::strcmp(dynamics_name, "adder_2") == 0) {
            *type = dynamics_type::adder_2;
            return true;
        }

        if (std::strcmp(dynamics_name, "adder_3") == 0) {
            *type = dynamics_type::adder_3;
            return true;
        }

        if (std::strcmp(dynamics_name, "adder_4") == 0) {
            *type = dynamics_type::adder_4;
            return true;
        }

        if (std::strcmp(dynamics_name, "mult_2") == 0) {
            *type = dynamics_type::mult_2;
            return true;
        }

        if (std::strcmp(dynamics_name, "mult_3") == 0) {
            *type = dynamics_type::mult_3;
            return true;
        }

        if (std::strcmp(dynamics_name, "mult_4") == 0) {
            *type = dynamics_type::mult_4;
            return true;
        }

        if (std::strcmp(dynamics_name, "counter") == 0) {
            *type = dynamics_type::counter;
            return true;
        }

        if (std::strcmp(dynamics_name, "generator") == 0) {
            *type = dynamics_type::generator;
            return true;
        }

        if (std::strcmp(dynamics_name, "constant") == 0) {
            *type = dynamics_type::constant;
            return true;
        }

        if (std::strcmp(dynamics_name, "cross") == 0) {
            *type = dynamics_type::cross;
            return true;
        }

        if (std::strcmp(dynamics_name, "time_func") == 0) {
            *type = dynamics_type::time_func;
            return true;
        }

        return false;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
181
182
183
184
    status read(simulation& sim,
                int id,
                const char* name,
                const char* dynamics_name) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
185
186
187
188
189
190
191
    {
        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
192
        auto ret = sim.dispatch(type, [this, &sim, name](auto& dyn_models) {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
193
194
195
196
197
198
199
200
201
202
203
204
205
            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
206
207
        irt_return_if_bad(ret);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
208
        map[id] = mdl;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
209
210

        return status::success;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
292
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
    }

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

    bool read(integrator& dyn) noexcept
    {
        return 2 == std::fscanf(file,
                                "%lf %lf",
                                &dyn.default_current_value,
                                &dyn.default_reset_value);
    }

    bool read(quantifier& dyn) noexcept
    {
        char state[16];
        char init[8];

        int ret = std::fscanf(file,
                              "%lf %d %15s %7s",
                              &dyn.default_step_size,
                              &dyn.default_past_length,
                              state,
                              init);

        if (ret != 4)
            return false;

        if (std::strcmp(state, "possible") == 0)
            dyn.default_adapt_state = quantifier::adapt_state::possible;
        else if (std::strcmp(state, "impossible") == 0)
            dyn.default_adapt_state = quantifier::adapt_state::impossible;
        else if (std::strcmp(state, "done") == 0)
            dyn.default_adapt_state = quantifier::adapt_state::done;
        else
            return false;

        if (std::strcmp(init, "true") == 0)
            dyn.default_zero_init_offset = true;
        else if (std::strcmp(init, "false") == 0)
            dyn.default_zero_init_offset = false;
        else
            return false;

        return true;
    }

    bool read(adder_2& dyn) noexcept
    {
        return 4 == std::fscanf(file,
                                "%lf %lf %lf %lf",
                                &dyn.default_values[0],
                                &dyn.default_values[1],
                                &dyn.default_input_coeffs[0],
                                &dyn.default_input_coeffs[1]);
    }

    bool read(adder_3& dyn) noexcept
    {
        return 6 == std::fscanf(file,
                                "%lf %lf %lf %lf %lf %lf",
                                &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]);
    }

    bool read(adder_4& dyn) noexcept
    {
        return 8 == std::fscanf(file,
                                "%lf %lf %lf %lf %lf %lf %lf %lf",
                                &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]);
    }

    bool read(mult_2& dyn) noexcept
    {
        return 4 == std::fscanf(file,
                                "%lf %lf %lf %lf",
                                &dyn.default_values[0],
                                &dyn.default_values[1],
                                &dyn.default_input_coeffs[0],
                                &dyn.default_input_coeffs[1]);
    }

    bool read(mult_3& dyn) noexcept
    {
        return 6 == std::fscanf(file,
                                "%lf %lf %lf %lf %lf %lf",
                                &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]);
    }

    bool read(mult_4& dyn) noexcept
    {
        return 8 == std::fscanf(file,
                                "%lf %lf %lf %lf %lf %lf %lf %lf",
                                &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]);
    }

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

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

    bool read(constant& dyn) noexcept
    {
        return 1 == std::fscanf(file, "%lf", &dyn.default_value);
    }

    bool read(cross& dyn) noexcept
    {
        return 1 == std::fscanf(file, "%lf", &dyn.default_threshold);
    }

    bool read(time_func& dyn) noexcept
    {
        char fn[10];

        if (1 != std::fscanf(file, "%9s", fn))
            return false;

        if (std::strcmp(fn, "square") == 0)
360
361
362
            dyn.default_f = &square_time_function;
        else 
            dyn.default_f = &time_function;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
363
364
365
366
367
368
369
370
371

        return true;
    }
};

struct writer
{
    std::FILE* file = stdout;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
372
373
    array<model_id> map;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
374
375
376
377
378
379
380
381
382
383
384
385
    writer(std::FILE* file_ = stdout) noexcept
      : file(file_)
    {}

    ~writer() noexcept
    {
        if (file && file != stderr && file != stdout)
            std::fclose(file);
    }

    status operator()(const simulation& sim) noexcept
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
386
387
        std::fprintf(
          file, "%lu\n", static_cast<long unsigned>(sim.models.size()));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
388

Gauthier Quesnel's avatar
Gauthier Quesnel committed
389
390
391
392
        irt_return_if_bad(map.init(sim.models.size()));

        std::fill_n(std::begin(map), std::size(map), static_cast<model_id>(0));

Gauthier Quesnel's avatar
Gauthier Quesnel committed
393
        model* mdl = nullptr;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
394
        int id = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
395
396
397
        while (sim.models.next(mdl)) {
            const auto mdl_id = sim.models.get_id(mdl);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
398
399
            std::fprintf(file, "%d %s ", id, mdl->name.c_str());
            map[id] = mdl_id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
400
401
402
403
404

            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
405
406

            ++id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
407
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
408

Gauthier Quesnel's avatar
Gauthier Quesnel committed
409
410
411
412
413
414
        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
415

Gauthier Quesnel's avatar
Gauthier Quesnel committed
416
417
                    if (!(mdl_src && mdl_dst))
                        continue;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
418

Gauthier Quesnel's avatar
Gauthier Quesnel committed
419
420
                    int src_index = -1;
                    int dst_index = -1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
421

Gauthier Quesnel's avatar
Gauthier Quesnel committed
422
                    irt_return_if_bad(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
423
                      sim.get_input_port_index(*mdl_dst, dst, &dst_index));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
424

Gauthier Quesnel's avatar
Gauthier Quesnel committed
425
                    irt_return_if_bad(sim.get_output_port_index(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
426
427
428
429
430
431
432
                      *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
433

Gauthier Quesnel's avatar
Gauthier Quesnel committed
434
                    std::fprintf(file,
435
436
                                 "%d %d %d %d\n",
                                 static_cast<int>(std::distance(map.begin(), it_out)),
Gauthier Quesnel's avatar
Gauthier Quesnel committed
437
                                 src_index,
438
                                 static_cast<int>(std::distance(map.begin(), it_in)),
Gauthier Quesnel's avatar
Gauthier Quesnel committed
439
                                 dst_index);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
440
441
442
443
444
445
446
447
                }
            }
        }

        return status::success;
    }

private:
Gauthier Quesnel's avatar
Gauthier Quesnel committed
448
    void write(const none& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
    {
        std::fprintf(file, "none\n");
    }

    void write(const integrator& dyn) noexcept
    {
        std::fprintf(file,
                     "integrator %f %f\n",
                     dyn.default_current_value,
                     dyn.default_reset_value);
    }

    void write(const quantifier& dyn) noexcept
    {
        std::fprintf(
          file,
          "quantifier %f %d %s %s\n",
          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" : "false");
    }

    void write(const adder_2& dyn) noexcept
    {
        std::fprintf(file,
                     "adder_2 %f %f %f %f\n",
                     dyn.default_values[0],
                     dyn.default_values[1],
                     dyn.default_input_coeffs[0],
                     dyn.default_input_coeffs[1]);
    }

    void write(const adder_3& dyn) noexcept
    {
        std::fprintf(file,
                     "adder_3 %f %f %f %f %f %f\n",
                     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]);
    }

    void write(const adder_4& dyn) noexcept
    {
        std::fprintf(file,
                     "adder_4 %f %f %f %f %f %f %f %f\n",
                     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]);
    }

    void write(const mult_2& dyn) noexcept
    {
        std::fprintf(file,
                     "mult_2 %f %f %f %f\n",
                     dyn.default_values[0],
                     dyn.default_values[1],
                     dyn.default_input_coeffs[0],
                     dyn.default_input_coeffs[1]);
    }

    void write(const mult_3& dyn) noexcept
    {
        std::fprintf(file,
                     "mult_3 %f %f %f %f %f %f\n",
                     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]);
    }

    void write(const mult_4& dyn) noexcept
    {
        std::fprintf(file,
                     "mult_4 %f %f %f %f %f %f %f %f\n",
                     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]);
    }

    void write(const counter& /*dyn*/) noexcept
    {
        std::fprintf(file, "counter\n");
    }

    void write(const generator& /*dyn*/) noexcept
    {
        std::fprintf(file, "generator\n");
    }

    void write(const constant& dyn) noexcept
    {
        std::fprintf(file, "constant %f\n", dyn.default_value);
    }

    void write(const cross& dyn) noexcept
    {
        std::fprintf(file, "cross %f\n", dyn.default_threshold);
    }

    void write(const time_func& dyn) noexcept
    {
        std::fprintf(file,
                     "time_func %s\n",
572
                     dyn.default_f == &time_function ? "time" : "square");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
    }
};

class dot_writer
{
private:
    std::FILE* file = stdout;

public:
    dot_writer(std::FILE* file_ = stdout)
      : file(file_ == nullptr ? stdout : file_)
    {}

    ~dot_writer() noexcept
    {
        if (file && file != stdout)
            std::fclose(file);
    }

    /* 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
    {
        std::fputs("digraph graphname {\n", file);

        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())
                        std::fprintf(
                          file, "%u -> ", irt::get_key(output_port->model));
                    else
                        std::fprintf(file, "%s -> ", mdl_src->name.c_str());

                    if (mdl_dst->name.empty())
                        std::fprintf(
                          file, "%u", irt::get_key(input_port->model));
                    else
                        std::fprintf(file, "%s", mdl_dst->name.c_str());

                    std::fputs(" [label=\"", file);

                    if (output_port->name.empty())
                        std::fprintf(
                          file,
                          "%u",
                          irt::get_key(sim.output_ports.get_id(*output_port)));
                    else
                        std::fprintf(file, "%s", output_port->name.c_str());

                    std::fputs("-", file);

                    if (input_port->name.empty())
                        std::fprintf(
                          file,
                          "%u",
                          irt::get_key(sim.input_ports.get_id(*input_port)));
                    else
                        std::fprintf(file, "%s", input_port->name.c_str());

                    std::fputs("\"];\n", file);
                }
            }
        }
    }
};

} // namespace irt

#endif