std-mel 0.11.1

Mélodium standard library
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
//! Scatter and gather: one track per value of a stream, as workflow languages scatter a
//! sub-workflow over the elements of a collection and gather how each ended.

use futures::{pin_mut, select, FutureExt};
use melodium_core::{
    common::descriptor::DataType,
    common::executive::{Input, Output, ResultStatus, TrackId},
    *,
};
use melodium_macro::{mel_context, mel_model, mel_treatment};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, Weak};

/// Context of a scattered track.
///
/// - `scatter`: identifier of the `scatter` that made the track;
/// - `index`: position of the value of the track in the stream scattered, from 0.
#[mel_context]
pub struct Scattered {
    pub scatter: string,
    pub index: u64,
}

/// How a scattered track ended, as `gather` tells.
#[derive(Debug)]
enum Outcome {
    Completed,
    Failed(String),
    /// Ended without telling.
    Silent,
}

/// End of a scattered track, with the value it gathered, if any, and its type.
#[derive(Debug)]
struct End {
    index: u64,
    outcome: Outcome,
    value: Option<(DataType, Value)>,
}

/// Scatters values of streams over tracks of their own, and gathers how they end.
///
/// `scatter` makes a track for each value it receives, where `scattered` gives the value and the
/// value shared by all of them, and `gather` tells how the track ended. Scattered tracks get the
/// contexts of the track of `scatter`, with `@Scattered`.
///
/// Each use of `Scatter` is meant for one `scatter` (or `scatterGathering`), and the `scattered`
/// and `gather` (or `gatherValue`) of its tracks, in the same treatment, as
/// `http/server::connection` uses `incoming` and `outgoing`.
#[mel_model(
    source scattered (Scattered) (generic T () generic S () inherit) (value Block<T> shared Block<S>)
    invoke_generic_source invoke_source
)]
#[derive(Debug)]
pub struct Scatter {
    /// Types of each use of `scattered`.
    entries: Mutex<Vec<HashMap<String, DataType>>>,
    /// `scatter` waiting for their tracks to end, by identifier.
    scatters: Mutex<HashMap<String, async_channel::Sender<End>>>,
    next: AtomicU64,
}

impl Scatter {
    fn new(_model: Weak<ScatterModel>) -> Self {
        Self {
            entries: Mutex::new(Vec::new()),
            scatters: Mutex::new(HashMap::new()),
            next: AtomicU64::new(0),
        }
    }

    fn invoke_source(
        &self,
        _source: &str,
        _params: HashMap<String, Value>,
        generics: HashMap<String, DataType>,
    ) {
        self.entries.lock().unwrap().push(generics);
    }

    /// Opens a scatter, giving its identifier and where the ends of its tracks come.
    fn open(&self) -> (String, async_channel::Receiver<End>) {
        let id = self.next.fetch_add(1, Ordering::Relaxed).to_string();
        let (sender, receiver) = async_channel::unbounded();
        self.scatters.lock().unwrap().insert(id.clone(), sender);
        (id, receiver)
    }

    fn close(&self, id: &str) {
        self.scatters.lock().unwrap().remove(id);
    }

    /// Tells the scatter `id` how its track ended.
    fn report(&self, id: &str, end: End) {
        if let Some(sender) = self.scatters.lock().unwrap().get(id) {
            let _ = sender.try_send(end);
        }
    }
}

/// Scatter values over tracks of their own.
///
/// For each value received through `values`, a track is made where `scattered` gives it, with
/// the value received through `shared` (a trigger when nothing is shared), at most `parallel`
/// at once: `values` is not read further until one ends, a track ending when it tells
/// `gather` how, even if some of its treatments still finish. Tracks start in the order of `values`,
/// and end in any order, each telling how through `gather`. They get the contexts of the track
/// of `scatter`, with `@Scattered`.
///
/// Once `values` ends and every track ended, `finished` is emitted, and `completed` if they all
/// completed. If one failed, or ended without gathering, `failed` is emitted and `errors` gives
/// why. `scattered<T, S>` must be used with the same types and the same `Scatter`.
///
/// ```mermaid
/// graph LR
///     T("scatter()")
///     V["… 🟦 🟦 🟦"] -->|values| T
///     S["〈🟨〉"] -->|shared| T
///
///     T -.->|"track per 🟦"| X["scattered() … gather()"]
///     T -->|completed| C["〈🟩〉"]
///
///     style V fill:#ffffff,stroke:#ffffff
///     style S fill:#ffffff,stroke:#ffffff
///     style X fill:#ffffff,stroke:#ffffff
///     style C fill:#ffffff,stroke:#ffffff
/// ```
#[mel_treatment(
    generic T ()
    generic S ()
    model scatter Scatter
    default parallel 8
    input values Stream<T>
    input shared Block<S>
    output completed Block<void>
    output failed Block<void>
    output finished Block<void>
    output errors Stream<string>
)]
pub async fn scatter(parallel: u32) {
    let generics = HashMap::from([("T".to_string(), T), ("S".to_string(), S)]);
    scatter_values(
        ScatterModel::into(scatter),
        track_id,
        generics,
        parallel,
        &**values,
        &**shared,
        None,
        [&**completed, &**failed, &**finished],
        &**errors,
    )
    .await
}

/// Scatter values over tracks of their own, and gather their values.
///
/// As `scatter` does, with the values the tracks give to `gatherValue` coming through
/// `gathered`, in the order the tracks end. A track gathering no value gives none, and values
/// of another type than `R` are refused, the scatter failing.
///
/// ```mermaid
/// graph LR
///     T("scatterGathering()")
///     V["… 🟦 🟦 🟦"] -->|values| T
///     S["〈🟨〉"] -->|shared| T
///
///     T -.->|"track per 🟦"| X["scattered() … gatherValue()"]
///     T -->|gathered| G["… 🟪 🟪 🟪"]
///     T -->|completed| C["〈🟩〉"]
///
///     style V fill:#ffffff,stroke:#ffffff
///     style S fill:#ffffff,stroke:#ffffff
///     style X fill:#ffffff,stroke:#ffffff
///     style G fill:#ffffff,stroke:#ffffff
///     style C fill:#ffffff,stroke:#ffffff
/// ```
#[mel_treatment(
    generic T ()
    generic S ()
    generic R ()
    model scatter Scatter
    default parallel 8
    input values Stream<T>
    input shared Block<S>
    output gathered Stream<R>
    output completed Block<void>
    output failed Block<void>
    output finished Block<void>
    output errors Stream<string>
)]
pub async fn scatter_gathering(parallel: u32) {
    let generics = HashMap::from([("T".to_string(), T), ("S".to_string(), S)]);
    scatter_values(
        ScatterModel::into(scatter),
        track_id,
        generics,
        parallel,
        &**values,
        &**shared,
        Some((R, &**gathered)),
        [&**completed, &**failed, &**finished],
        &**errors,
    )
    .await
}

/// Makes the tracks of `scatter` and `scatterGathering`, giving their gathered values through
/// `gathered` when there is one.
async fn scatter_values(
    model: Arc<ScatterModel>,
    track_id: TrackId,
    generics: HashMap<String, DataType>,
    parallel: u32,
    values: &dyn Input,
    shared: &dyn Input,
    gathered: Option<(DataType, &dyn Output)>,
    [completed, failed, finished]: [&dyn Output; 3],
    errors: &dyn Output,
) {
    let scatter = model.inner();

    let mut problems = Vec::new();
    let mut gathered_all = true;
    if parallel == 0 {
        problems.push("parallel must be at least 1".to_string());
    } else if !scatter.entries.lock().unwrap().contains(&generics) {
        problems.push(format!(
            "no `scattered<{}, {}>` of the same `Scatter` takes the values",
            generics["T"], generics["S"]
        ));
    } else if let Ok(shared_value) = shared.recv_one().await {
        let (id, ends) = scatter.open();
        let mut running = 0;
        let mut index = 0;
        let mut values_ended = false;
        loop {
            // Ends of tracks already known are taken first.
            while let Ok(end) = ends.try_recv() {
                running -= 1;
                gathered_all &= ended(end, gathered.as_ref(), &mut problems).await;
            }
            if values_ended || running >= parallel {
                if running == 0 {
                    break;
                }
                if let Ok(end) = ends.recv().await {
                    running -= 1;
                    gathered_all &= ended(end, gathered.as_ref(), &mut problems).await;
                }
                continue;
            }
            match values.recv_one().await {
                Ok(value) => {
                    let shared_value = shared_value.clone();
                    model
                        .new_scattered(
                            Some(track_id),
                            Scattered {
                                scatter: id.clone(),
                                index,
                            },
                            &HashMap::new(),
                            &generics,
                            Some(Box::new(move |mut outputs| {
                                let value_output = outputs.get("value");
                                let shared_output = outputs.get("shared");
                                vec![Box::new(Box::pin(give(
                                    value_output,
                                    value,
                                    shared_output,
                                    shared_value,
                                )))]
                            })),
                        )
                        .await;
                    running += 1;
                    index += 1;
                }
                Err(_) => values_ended = true,
            }
        }
        scatter.close(&id);
    } else {
        problems.push("no shared value was given".to_string());
    }

    values.close();
    if let Some((_, gathered)) = &gathered {
        gathered.close().await;
    }
    if problems.is_empty() && gathered_all {
        let _ = completed.send_one(().into()).await;
    } else {
        let _ = failed.send_one(().into()).await;
        for problem in problems {
            let _ = errors.send_one(problem.into()).await;
        }
    }
    let _ = finished.send_one(().into()).await;
}

/// Takes the end of a scattered track, giving its value through `gathered` when there is one,
/// and telling whether it completed.
async fn ended(
    End {
        index,
        outcome,
        value,
    }: End,
    gathered: Option<&(DataType, &dyn Output)>,
    problems: &mut Vec<String>,
) -> bool {
    let mut gathered_well = true;
    if let (Some((expected, gathered)), Some((datatype, value))) = (gathered, value) {
        if datatype == *expected {
            let _ = gathered.send_one(value).await;
        } else {
            problems.push(format!(
                "scattered track {index} gathered a value of type {datatype}, not {expected}"
            ));
            gathered_well = false;
        }
    }
    gathered_well
        && match outcome {
            Outcome::Completed => true,
            Outcome::Failed(error) => {
                problems.push(format!("scattered track {index} failed: {error}"));
                false
            }
            Outcome::Silent => {
                problems.push(format!("scattered track {index} ended without telling how"));
                false
            }
        }
}

/// Gives the value and the shared value to a scattered track.
async fn give(
    value_output: Box<dyn Output>,
    value: Value,
    shared_output: Box<dyn Output>,
    shared: Value,
) -> ResultStatus {
    let _ = value_output.send_one(value).await;
    value_output.close().await;
    let _ = shared_output.send_one(shared).await;
    shared_output.close().await;
    ResultStatus::Ok
}

/// Tell the scatter `scatter_id` how its track `index` ended.
///
/// `gather` gives the identifier and the index from `@Scattered`. When `completed` and `failed`
/// end without any of them being emitted, the track ended without telling, and is counted as
/// failed.
#[mel_treatment(
    model scatter Scatter
    input completed Block<void>
    input failed Block<void>
    input error Block<string>
)]
pub async fn gathered(scatter_id: string, index: u64) {
    let outcome = outcome(&**completed, &**failed, &**error).await;
    ScatterModel::into(scatter).inner().report(
        &scatter_id,
        End {
            index,
            outcome,
            value: None,
        },
    );
}

/// Tell the scatter `scatter_id` how its track `index` ended, with a value.
///
/// As `gathered`, giving the value received through `value` to `scatterGathering`, whatever
/// the track's end.
#[mel_treatment(
    generic R ()
    model scatter Scatter
    input value Block<R>
    input completed Block<void>
    input failed Block<void>
    input error Block<string>
)]
pub async fn gathered_value(scatter_id: string, index: u64) {
    let (value, outcome) =
        futures::join!(value.recv_one(), outcome(&**completed, &**failed, &**error));
    ScatterModel::into(scatter).inner().report(
        &scatter_id,
        End {
            index,
            outcome,
            value: value.ok().map(|value| (R, value)),
        },
    );
}

/// How a track ended, from its `completed`, `failed` and `error`.
async fn outcome(completed: &dyn Input, failed: &dyn Input, error: &dyn Input) -> Outcome {
    let completion = completed.recv_one().fuse();
    let failure = failed.recv_one().fuse();
    pin_mut!(completion, failure);
    let has_failed = select! {
        result = completion => match result {
            Ok(_) => Some(false),
            Err(_) => failure.await.ok().map(|_| true),
        },
        result = failure => match result {
            Ok(_) => Some(true),
            Err(_) => completion.await.ok().map(|_| false),
        },
    };
    match has_failed {
        Some(false) => Outcome::Completed,
        Some(true) => Outcome::Failed(
            error
                .recv_one_as::<string>()
                .await
                .unwrap_or_else(|_| "no reason given".to_string()),
        ),
        None => Outcome::Silent,
    }
}