dvcompute_gpss_cons 1.3.4

Discrete event simulation library (support of GPSS-like DSL language for conservative distributed simulation)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
572
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
// Copyright (c) 2020-2022  David Sorokin <david.sorokin@gmail.com>, based in Yoshkar-Ola, Russia
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::rc::Rc;
use std::marker::PhantomData;
use std::ops::Deref;

use dvcompute::simulation;
use dvcompute::simulation::Point;
use dvcompute::simulation::error::*;
use dvcompute::simulation::process::*;

/// The `GeneratorBlock` computation that allows creating generators.
pub mod generator;

/// Defines basic blocks.
pub mod basic;

/// Additional operations.
pub mod ops;

/// Return a new `Block` computation by the specified pure function.
#[inline]
pub fn arr_block<I, O, F>(f: F) -> Arr<I, O, F>
    where F: FnOnce(I) -> O + 'static
{
    Arr { f: f, _phantom1: PhantomData, _phantom2: PhantomData }
}

/// Delay the `Block` computation.
#[inline]
pub fn delay_block<F, A>(f: F) -> Delay<F, A>
    where F: FnOnce() -> A + 'static,
          A: Block + 'static
{
    Delay { f: f, _phantom: PhantomData }
}

/// Construct a `Block` computation.
#[inline]
pub fn cons_block<I, F, M>(f: F) -> Cons<I, F, M>
    where F: FnOnce(I) -> M + 'static,
          M: Process + 'static
{
    Cons { f: f, _phantom1: PhantomData, _phantom2: PhantomData }
}

/// The termination block.
#[inline]
pub fn terminate_block<T>() -> Terminate<T> {
    Terminate { _phantom: PhantomData }
}

/// Transfer the control flow to another terminating block.
#[inline]
pub fn transfer_block<I, O, A>(comp: A) -> Transfer<I, O, A>
    where A: Block<Input = I, Output = ()>
{
    Transfer { comp: comp, _phantom1: PhantomData, _phantom2: PhantomData }
}

/// The simulation block.
pub trait Block {

    /// The input type of the computation.
    type Input;

    /// The output type of the computation.
    type Output;

    /// Call the `Block` computation.
    #[doc(hidden)]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static;

    /// Call the `Block` computation using the boxed continuation.
    #[doc(hidden)]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>;

    /// Run the computation by the specified input.
    #[inline]
    fn run(self, a: Self::Input) -> Run<Self::Input, Self::Output, Self>
        where Self: Sized
    {
        Run { comp: self, input: a, _phantom: PhantomData }
    }

    /// Bind the current computation with its continuation within the resulting computation.
    #[inline]
    fn and_then<A>(self, other: A) -> AndThen<Self, A>
        where Self: Sized + 'static,
              A: Block<Input = Self::Output> + 'static,
    {
        AndThen { comp: self, other: other }
    }

    /// Convert into a boxed value.
    #[inline]
    fn into_boxed(self) -> BlockBox<Self::Input, Self::Output>
        where Self: Sized + 'static
    {
        BlockBox::new(move |a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point| {
            self.call_block_boxed(a, cont, pid, p)
        })
    }

    /// Trace the computation.
    #[inline]
    fn trace(self, msg: String) -> Trace<Self>
        where Self: Sized
    {
        Trace { comp: self, msg: msg }
    }
}

/// Allows converting to `Block` computations.
pub trait IntoBlock {

    /// The target computation.
    type Block: Block<Input = Self::Input, Output = Self::Output>;

    /// The input type of the computation.
    type Input;

    /// The output type of the computation.
    type Output;

    /// Convert to the `Block` computation.
    fn into_block(self) -> Self::Block;
}

impl<A: Block> IntoBlock for A {

    type Block = A;

    type Input = A::Input;

    type Output = A::Output;

    #[inline]
    fn into_block(self) -> Self::Block {
        self
    }
}

/// The `Block` computation box.
#[must_use = "computations are lazy and do nothing unless to be run"]
pub struct BlockBox<I, O> {
    f: Box<dyn BlockFnBox<I, O>>
}

#[doc(hidden)]
impl<I, O> BlockBox<I, O> {

    #[inline]
    pub fn new<F>(f: F) -> Self
        where F: FnOnce(I, ProcessBoxCont<O>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        BlockBox { f: Box::new(f) }
    }

    #[inline]
    pub fn call_box(self, args: (I, ProcessBoxCont<O>, Rc<ProcessId>, &Point)) -> simulation::Result<()> {
        self.f.call_box(args)
    }
}

impl<I, O> Block for BlockBox<I, O> {

    type Input  = I;
    type Output = O;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        let cont = ProcessBoxCont::new(cont);
        self.call_box((a, cont, pid, p))
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        self.call_box((a, cont, pid, p))
    }

    #[inline]
    fn into_boxed(self) -> BlockBox<Self::Input, Self::Output>
        where Self: Sized + 'static
    {
        self
    }
}

/// A trait to support the stable version of Rust, where there is no `FnBox`.
trait BlockFnBox<I, O> {

    /// Call the corresponding function.
    fn call_box(self: Box<Self>, args: (I, ProcessBoxCont<O>, Rc<ProcessId>, &Point)) -> simulation::Result<()>;
}

impl<I, O, F> BlockFnBox<I, O> for F
    where F: for<'a> FnOnce(I, ProcessBoxCont<O>, Rc<ProcessId>, &'a Point) -> simulation::Result<()>
{
    fn call_box(self: Box<Self>, args: (I, ProcessBoxCont<O>, Rc<ProcessId>, &Point)) -> simulation::Result<()> {
        let this: Self = *self;
        this(args.0, args.1, args.2, args.3)
    }
}

/// Allows creating the `Block` computation by a pure function.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Arr<I, O, F> {

    /// Return a pure function, which is then transformed to the computation.
    f: F,

    /// To keep the type parameter.
    _phantom1: PhantomData<I>,

    /// To keep the type parameter.
    _phantom2: PhantomData<O>
}

impl<I, O, F> Block for Arr<I, O, F>
    where F: FnOnce(I) -> O + 'static
{
    type Input  = I;
    type Output = O;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let Arr { f, _phantom1, _phantom2 } = self;
            cont(Result::Ok(f(a)), pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let Arr { f, _phantom1, _phantom2 } = self;
            cont.call_box((Result::Ok(f(a)), pid, p))
        }
    }
}

/// Allows delaying the `Block` computation by the specified function.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Delay<F, A> {

    /// Return the computation.
    f: F,

    /// To keep the type parameter.
    _phantom: PhantomData<A>
}

impl<F, A> Block for Delay<F, A>
    where F: FnOnce() -> A + 'static,
          A: Block + 'static
{
    type Input  = A::Input;
    type Output = A::Output;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        let Delay { f, _phantom } = self;
        let comp = f();
        comp.call_block(a, cont, pid, p)
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        let Delay { f, _phantom } = self;
        let comp = f();
        comp.call_block_boxed(a, cont, pid, p)
    }
}

/// A composition of the `Block` computations.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct AndThen<A, U>
    where A: Block,
          U: Block<Input = A::Output>
{
    /// The current computation.
    comp: A,

    /// The next computation.
    other: U
}

impl<A, U> Block for AndThen<A, U>
    where A: Block + 'static,
          U: Block<Input = A::Output> + 'static
{
    type Input  = A::Input;
    type Output = U::Output;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let AndThen { comp, other } = self;
            let cont = move |b: simulation::Result<A::Output>, pid: Rc<ProcessId>, p: &Point| {
                match b {
                    Result::Ok(b) => {
                        other.call_block(b, cont, pid, p)
                    },
                    Result::Err(Error::Cancel) => {
                        revoke_process(cont, pid, p)
                    },
                    Result::Err(Error::Other(e)) => {
                        match e.deref() {
                            &OtherError::Retry(_) => {
                                cut_error_process(cont, pid, e, p)
                            },
                            &OtherError::Panic(_) => {
                                cut_error_process(cont, pid, e, p)
                            },
                            &OtherError::IO(_) => {
                                propagate_error_process(cont, pid, e, p)
                            }
                        }
                    }
                }
            };
            comp.call_block(a, cont, pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let AndThen { comp, other } = self;
            let cont = move |b: simulation::Result<A::Output>, pid: Rc<ProcessId>, p: &Point| {
                match b {
                    Result::Ok(b) => {
                        other.call_block_boxed(b, cont, pid, p)
                    },
                    Result::Err(Error::Cancel) => {
                        revoke_process_boxed(cont, pid, p)
                    },
                    Result::Err(Error::Other(e)) => {
                        match e.deref() {
                            &OtherError::Retry(_) => {
                                cut_error_process_boxed(cont, pid, e, p)
                            },
                            &OtherError::Panic(_) => {
                                cut_error_process_boxed(cont, pid, e, p)
                            },
                            &OtherError::IO(_) => {
                                propagate_error_process_boxed(cont, pid, e, p)
                            }
                        }
                    }
                }
            };
            comp.call_block(a, cont, pid, p)
        }
    }
}

/// Allows constructing a `Block` computation by the specified function.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Cons<I, F, M> {

    /// Return the `Process` computation.
    f: F,

    /// To keep the type parameter.
    _phantom1: PhantomData<I>,

    /// To keep the type parameter.
    _phantom2: PhantomData<M>
}

impl<I, F, M> Block for Cons<I, F, M>
    where F: FnOnce(I) -> M + 'static,
          M: Process + 'static
{
    type Input  = I;
    type Output = M::Item;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        let Cons { f, _phantom1, _phantom2 } = self;
        let comp = f(a);
        comp.call_process(cont, pid, p)
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        let Cons { f, _phantom1, _phantom2 } = self;
        let comp = f(a);
        comp.call_process_boxed(cont, pid, p)
    }
}

/// Defines a termination block.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Terminate<T> {

    /// To keep the type parameter.
    _phantom: PhantomData<T>
}

impl<T> Block for Terminate<T> {

    type Input  = T;
    type Output = ();

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, _a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let Terminate { _phantom } = self;
            cont(Result::Ok(()), pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, _a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let Terminate { _phantom } = self;
            cont.call_box((Result::Ok(()), pid, p))
        }
    }
}

/// Defines a block that transfers the control flow to another terminating block.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Transfer<I, O, A> {

    /// Another terminating block computation.
    comp: A,

    /// To keep the type parameter.
    _phantom1: PhantomData<I>,

    /// To keep the type parameter.
    _phantom2: PhantomData<O>
}

impl<I, O, A> Block for Transfer<I, O, A>
    where A: Block<Input = I, Output = ()>
{
    type Input  = I;
    type Output = O;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let Transfer { comp, _phantom1, _phantom2 } = self;
            fn cont(b: simulation::Result<()>, _pid: Rc<ProcessId>, _p: &Point) -> simulation::Result<()> {
                b
            }
            comp.call_block(a, cont, pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let Transfer { comp, _phantom1, _phantom2 } = self;
            fn cont(b: simulation::Result<()>, _pid: Rc<ProcessId>, _p: &Point) -> simulation::Result<()> {
                b
            }
            comp.call_block(a, cont, pid, p)
        }
    }
}

/// Run the `Block` computation by the specified input.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Run<I, O, A> {

    /// Return a pure function, which is then transformed to the computation.
    comp: A,

    /// The input argument.
    input: I,

    /// To keep the type parameter.
    _phantom: PhantomData<O>
}

impl<I, O, A> Process for Run<I, O, A>
    where A: Block<Input = I, Output = O> + 'static
{
    type Item = O;

    #[doc(hidden)]
    #[inline]
    fn call_process<C>(self, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Item>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let Run { comp, input, _phantom } = self;
            comp.call_block(input, cont, pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_process_boxed(self, cont: ProcessBoxCont<Self::Item>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let Run { comp, input, _phantom } = self;
            comp.call_block_boxed(input, cont, pid, p)
        }
    }
}

impl<I, T, A> Block for I
    where I: Iterator<Item = A> + Sized + 'static,
          A: Block<Input = T, Output = T> + 'static
{
    type Input  = T;
    type Output = ();

    #[doc(hidden)]
    #[inline(never)]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        let mut x = self;
        match x.next() {
            None => Result::Ok(()),
            Some(comp) => {
                comp.and_then(x)
                    .call_block(a, cont, pid, p)
            }
        }
    }

    #[doc(hidden)]
    #[inline(never)]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        let mut x = self;
        match x.next() {
            None => Result::Ok(()),
            Some(comp) => {
                comp.and_then(x)
                    .call_block_boxed(a, cont, pid, p)
            }
        }
    }
}

/// Trace the computation.
#[must_use = "computations are lazy and do nothing unless to be run"]
#[derive(Clone)]
pub struct Trace<A> {

    /// The computation.
    comp: A,

    /// The message to print.
    msg: String
}

impl<A> Block for Trace<A>
    where A: Block
{
    type Input = A::Input;
    type Output = A::Output;

    #[doc(hidden)]
    #[inline]
    fn call_block<C>(self, a: Self::Input, cont: C, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()>
        where C: FnOnce(simulation::Result<Self::Output>, Rc<ProcessId>, &Point) -> simulation::Result<()> + 'static
    {
        if is_process_cancelled(&pid, p) {
            revoke_process(cont, pid, p)
        } else {
            let Trace { comp, msg } = self;
            p.trace(&msg);
            comp.call_block(a, cont, pid, p)
        }
    }

    #[doc(hidden)]
    #[inline]
    fn call_block_boxed(self, a: Self::Input, cont: ProcessBoxCont<Self::Output>, pid: Rc<ProcessId>, p: &Point) -> simulation::Result<()> {
        if is_process_cancelled(&pid, p) {
            revoke_process_boxed(cont, pid, p)
        } else {
            let Trace { comp, msg } = self;
            p.trace(&msg);
            comp.call_block_boxed(a, cont, pid, p)
        }
    }
}