peace_flow_rt 0.0.15

Flow runtime types for the peace automation framework.
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
use peace_data::fn_graph::GraphInfo;
use peace_flow_model::{FlowId, FlowSpecInfo, ItemSpecInfo};

use crate::ItemGraph;

cfg_if::cfg_if! {
    if #[cfg(all(feature = "item_interactions", feature = "item_state_example"))] {
        use std::collections::{BTreeMap, BTreeSet};

        use indexmap::IndexMap;
        use peace_item_interaction_model::{
            ItemInteraction,
            ItemInteractionsCurrentOrExample,
            ItemLocation,
            ItemLocationsAndInteractions,
            ItemLocationTree,
        };
        use peace_item_model::ItemId;
        use peace_params::ParamsSpecs;
        use peace_resource_rt::{resources::ts::SetUp, Resources};
    }
}

#[cfg(all(
    feature = "item_interactions",
    feature = "item_state_example",
    feature = "output_progress",
))]
use std::collections::{HashMap, HashSet};

/// A flow to manage items.
///
/// A Flow ID is strictly associated with an [`ItemGraph`], as the graph
/// contains the definitions to read and write the items' [`State`]s.
///
/// [`State`]: peace_cfg::Item::State
#[derive(Debug)]
pub struct Flow<E> {
    /// ID of this flow.
    flow_id: FlowId,
    /// Graph of [`Item`]s in this flow.
    ///
    /// [`Item`]: peace_cfg::Item
    graph: ItemGraph<E>,
}

impl<E> PartialEq for Flow<E>
where
    E: 'static,
{
    fn eq(&self, other: &Flow<E>) -> bool {
        self.flow_id == other.flow_id && self.graph == other.graph
    }
}

impl<E> Clone for Flow<E> {
    fn clone(&self) -> Self {
        Self {
            flow_id: self.flow_id.clone(),
            graph: self.graph.clone(),
        }
    }
}

impl<E> Eq for Flow<E> where E: 'static {}

impl<E> Flow<E> {
    /// Returns a new `Flow`.
    pub fn new(flow_id: FlowId, graph: ItemGraph<E>) -> Self {
        Self { flow_id, graph }
    }

    /// Returns the flow ID.
    pub fn flow_id(&self) -> &FlowId {
        &self.flow_id
    }

    /// Returns the item graph.
    pub fn graph(&self) -> &ItemGraph<E> {
        &self.graph
    }

    /// Returns a mutable reference to the item graph.
    pub fn graph_mut(&self) -> &ItemGraph<E> {
        &self.graph
    }

    /// Generates a `FlowSpecInfo` from this `Flow`'s information.
    pub fn flow_spec_info(&self) -> FlowSpecInfo
    where
        E: 'static,
    {
        let flow_id = self.flow_id.clone();
        let graph_info = GraphInfo::from_graph(&self.graph, |item_boxed| {
            let item_id = item_boxed.id().clone();
            ItemSpecInfo { item_id }
        });

        FlowSpecInfo::new(flow_id, graph_info)
    }

    // TODO: Refactor -- there is a lot of duplication between this method and
    // `item_locations_and_interactions_current`
    #[cfg(all(feature = "item_interactions", feature = "item_state_example"))]
    pub fn item_locations_and_interactions_example(
        &self,
        params_specs: &ParamsSpecs,
        resources: &Resources<SetUp>,
    ) -> ItemLocationsAndInteractions
    where
        E: 'static,
    {
        // Build the flattened hierarchy.
        //
        // Regardless of how nested each `ItemLocation` is, the map will have an entry
        // for it.
        //
        // The entry key is the `ItemLocation`, and the values are a list of its direct
        // descendent `ItemLocation`s.
        //
        // This means a lot of cloning of `ItemLocation`s.

        let item_interactions_ctx = ItemInteractionsCtx {
            item_location_direct_descendents: BTreeMap::new(),
            item_to_item_interactions: IndexMap::with_capacity(self.graph().node_count()),
            // Rough estimate that each item has about 4 item locations
            //
            // * 2 `ItemLocationAncestors`s for from, 2 for to.
            // * Some may have more, but we also combine `ItemLocation`s.
            //
            // After the `ItemLocationTree`s are constructed, we'll have an accurate
            // number, but they are being constructed at the same time as this map.
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets: HashMap::with_capacity(self.graph().node_count() * 4),
        };
        let item_interactions_ctx = self
            .graph()
            .iter()
            // Note: This will silently drop the item locations if `interactions_example` fails to
            // return.
            .filter_map(|item| {
                item.interactions_example(params_specs, resources)
                    .ok()
                    .map(|item_interactions_example| (item.id(), item_interactions_example))
            })
            .fold(
                item_interactions_ctx,
                |item_interactions_ctx, (item_id, item_interactions_example)| {
                    let ItemInteractionsCtx {
                        mut item_location_direct_descendents,
                        mut item_to_item_interactions,
                        #[cfg(feature = "output_progress")]
                        mut item_location_to_item_id_sets,
                    } = item_interactions_ctx;

                    item_location_descendents_populate(
                        &item_interactions_example,
                        &mut item_location_direct_descendents,
                    );

                    #[cfg(feature = "output_progress")]
                    item_interactions_example
                        .iter()
                        .for_each(|item_interaction| match &item_interaction {
                            ItemInteraction::Push(item_interaction_push) => item_interaction_push
                                .location_from()
                                .iter()
                                .last()
                                .into_iter()
                                .chain(item_interaction_push.location_to().iter().last())
                                .for_each(|item_location| {
                                    item_location_to_item_id_sets_insert(
                                        &mut item_location_to_item_id_sets,
                                        item_location,
                                        item_id,
                                    )
                                }),
                            ItemInteraction::Pull(item_interaction_pull) => item_interaction_pull
                                .location_client()
                                .iter()
                                .last()
                                .into_iter()
                                .chain(item_interaction_pull.location_server().iter().last())
                                .for_each(|item_location| {
                                    item_location_to_item_id_sets_insert(
                                        &mut item_location_to_item_id_sets,
                                        item_location,
                                        item_id,
                                    )
                                }),
                            ItemInteraction::Within(item_interaction_within) => {
                                item_interaction_within
                                    .location()
                                    .iter()
                                    .last()
                                    .into_iter()
                                    .for_each(|item_location| {
                                        item_location_to_item_id_sets_insert(
                                            &mut item_location_to_item_id_sets,
                                            item_location,
                                            item_id,
                                        )
                                    })
                            }
                        });

                    item_to_item_interactions
                        .insert(item_id.clone(), item_interactions_example.into_inner());

                    ItemInteractionsCtx {
                        item_location_direct_descendents,
                        item_to_item_interactions,
                        #[cfg(feature = "output_progress")]
                        item_location_to_item_id_sets,
                    }
                },
            );

        let ItemInteractionsCtx {
            item_location_direct_descendents,
            item_to_item_interactions,
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets,
        } = item_interactions_ctx;

        let item_locations_top_level = item_location_direct_descendents
            .keys()
            .filter(|item_location| {
                // this item_location is not in any descendents
                !item_location_direct_descendents
                    .values()
                    .any(|item_location_descendents| {
                        item_location_descendents.contains(item_location)
                    })
            })
            .cloned()
            .collect::<Vec<ItemLocation>>();

        let item_locations_top_level_len = item_locations_top_level.len();
        let (_item_location_direct_descendents, item_location_trees) =
            item_locations_top_level.into_iter().fold(
                (
                    item_location_direct_descendents,
                    Vec::with_capacity(item_locations_top_level_len),
                ),
                |(mut item_location_direct_descendents, mut item_location_trees), item_location| {
                    let item_location_tree = item_location_tree_collect(
                        &mut item_location_direct_descendents,
                        item_location,
                    );
                    item_location_trees.push(item_location_tree);

                    (item_location_direct_descendents, item_location_trees)
                },
            );

        let item_location_count = item_location_trees.iter().fold(
            item_location_trees.len(),
            |item_location_count_acc, item_location_tree| {
                item_location_count_acc + item_location_tree.item_location_count()
            },
        );

        ItemLocationsAndInteractions::new(
            item_location_trees,
            item_to_item_interactions,
            item_location_count,
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets,
        )
    }

    // TODO: Refactor -- there is a lot of duplication between this method and
    // `item_locations_and_interactions_example`
    #[cfg(all(feature = "item_interactions", feature = "item_state_example"))]
    pub fn item_locations_and_interactions_current(
        &self,
        params_specs: &ParamsSpecs,
        resources: &Resources<SetUp>,
    ) -> ItemLocationsAndInteractions
    where
        E: 'static,
    {
        // Build the flattened hierarchy.
        //
        // Regardless of how nested each `ItemLocation` is, the map will have an entry
        // for it.
        //
        // The entry key is the `ItemLocation`, and the values are a list of its direct
        // descendent `ItemLocation`s.
        //
        // This means a lot of cloning of `ItemLocation`s.

        let item_interactions_ctx = ItemInteractionsCtx {
            item_location_direct_descendents: BTreeMap::new(),
            item_to_item_interactions: IndexMap::with_capacity(self.graph().node_count()),
            // Rough estimate that each item has about 4 item locations
            //
            // * 2 `ItemLocationAncestors`s for from, 2 for to.
            // * Some may have more, but we also combine `ItemLocation`s.
            //
            // After the `ItemLocationTree`s are constructed, we'll have an accurate
            // number, but they are being constructed at the same time as this map.
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets: HashMap::with_capacity(self.graph().node_count() * 4),
        };
        let item_interactions_ctx = self
            .graph()
            .iter()
            // Note: This will silently drop the item locations if `interactions_try_current` fails
            // to return.
            .filter_map(|item| {
                item.interactions_try_current(params_specs, resources)
                    .ok()
                    .map(|item_interactions_current_or_example| {
                        (item.id(), item_interactions_current_or_example)
                    })
            })
            .fold(
                item_interactions_ctx,
                |item_interactions_ctx, (item_id, item_interactions_current_or_example)| {
                    let ItemInteractionsCtx {
                        mut item_location_direct_descendents,
                        mut item_to_item_interactions,
                        #[cfg(feature = "output_progress")]
                        mut item_location_to_item_id_sets,
                    } = item_interactions_ctx;

                    // TODO: we need to hide the nodes if they came from `Example`.
                    let item_interactions_current_or_example =
                        match item_interactions_current_or_example {
                            ItemInteractionsCurrentOrExample::Current(
                                item_interactions_current,
                            ) => item_interactions_current.into_inner(),
                            ItemInteractionsCurrentOrExample::Example(
                                item_interactions_example,
                            ) => item_interactions_example.into_inner(),
                        };

                    item_location_descendents_populate(
                        &item_interactions_current_or_example,
                        &mut item_location_direct_descendents,
                    );

                    #[cfg(feature = "output_progress")]
                    item_interactions_current_or_example
                        .iter()
                        .for_each(|item_interaction| match &item_interaction {
                            ItemInteraction::Push(item_interaction_push) => item_interaction_push
                                .location_from()
                                .iter()
                                .last()
                                .into_iter()
                                .chain(item_interaction_push.location_to().iter().last())
                                .for_each(|item_location| {
                                    item_location_to_item_id_sets_insert(
                                        &mut item_location_to_item_id_sets,
                                        item_location,
                                        item_id,
                                    )
                                }),
                            ItemInteraction::Pull(item_interaction_pull) => item_interaction_pull
                                .location_client()
                                .iter()
                                .last()
                                .into_iter()
                                .chain(item_interaction_pull.location_server().iter().last())
                                .for_each(|item_location| {
                                    item_location_to_item_id_sets_insert(
                                        &mut item_location_to_item_id_sets,
                                        item_location,
                                        item_id,
                                    )
                                }),
                            ItemInteraction::Within(item_interaction_within) => {
                                item_interaction_within
                                    .location()
                                    .iter()
                                    .last()
                                    .into_iter()
                                    .for_each(|item_location| {
                                        item_location_to_item_id_sets_insert(
                                            &mut item_location_to_item_id_sets,
                                            item_location,
                                            item_id,
                                        )
                                    })
                            }
                        });

                    item_to_item_interactions
                        .insert(item_id.clone(), item_interactions_current_or_example);

                    ItemInteractionsCtx {
                        item_location_direct_descendents,
                        item_to_item_interactions,
                        #[cfg(feature = "output_progress")]
                        item_location_to_item_id_sets,
                    }
                },
            );

        let ItemInteractionsCtx {
            item_location_direct_descendents,
            item_to_item_interactions,
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets,
        } = item_interactions_ctx;

        let item_locations_top_level = item_location_direct_descendents
            .keys()
            .filter(|item_location| {
                // this item_location is not in any descendents
                !item_location_direct_descendents
                    .values()
                    .any(|item_location_descendents| {
                        item_location_descendents.contains(item_location)
                    })
            })
            .cloned()
            .collect::<Vec<ItemLocation>>();

        let item_locations_top_level_len = item_locations_top_level.len();
        let (_item_location_direct_descendents, item_location_trees) =
            item_locations_top_level.into_iter().fold(
                (
                    item_location_direct_descendents,
                    Vec::with_capacity(item_locations_top_level_len),
                ),
                |(mut item_location_direct_descendents, mut item_location_trees), item_location| {
                    let item_location_tree = item_location_tree_collect(
                        &mut item_location_direct_descendents,
                        item_location,
                    );
                    item_location_trees.push(item_location_tree);

                    (item_location_direct_descendents, item_location_trees)
                },
            );

        let item_location_count = item_location_trees.iter().fold(
            item_location_trees.len(),
            |item_location_count_acc, item_location_tree| {
                item_location_count_acc + item_location_tree.item_location_count()
            },
        );

        ItemLocationsAndInteractions::new(
            item_location_trees,
            item_to_item_interactions,
            item_location_count,
            #[cfg(feature = "output_progress")]
            item_location_to_item_id_sets,
        )
    }
}

#[cfg(all(
    feature = "item_interactions",
    feature = "item_state_example",
    feature = "output_progress",
))]
fn item_location_to_item_id_sets_insert(
    item_location_to_item_id_sets: &mut HashMap<ItemLocation, HashSet<ItemId>>,
    item_location: &ItemLocation,
    item_id: &ItemId,
) {
    if let Some(item_id_set) = item_location_to_item_id_sets.get_mut(item_location) {
        item_id_set.insert(item_id.clone());
    } else {
        let mut item_id_set = HashSet::new();
        item_id_set.insert(item_id.clone());
        item_location_to_item_id_sets.insert(item_location.clone(), item_id_set);
    }
}

#[cfg(all(feature = "item_interactions", feature = "item_state_example",))]
fn item_location_descendents_populate(
    item_interactions_current_or_example: &[ItemInteraction],
    item_location_direct_descendents: &mut BTreeMap<ItemLocation, BTreeSet<ItemLocation>>,
) {
    item_interactions_current_or_example.iter().for_each(
        |item_interaction| match &item_interaction {
            ItemInteraction::Push(item_interaction_push) => {
                item_location_descendents_insert(
                    item_location_direct_descendents,
                    item_interaction_push.location_from(),
                );
                item_location_descendents_insert(
                    item_location_direct_descendents,
                    item_interaction_push.location_to(),
                );
            }
            ItemInteraction::Pull(item_interaction_pull) => {
                item_location_descendents_insert(
                    item_location_direct_descendents,
                    item_interaction_pull.location_client(),
                );
                item_location_descendents_insert(
                    item_location_direct_descendents,
                    item_interaction_pull.location_server(),
                );
            }
            ItemInteraction::Within(item_interaction_within) => {
                item_location_descendents_insert(
                    item_location_direct_descendents,
                    item_interaction_within.location(),
                );
            }
        },
    );
}

/// Recursively constructs an `ItemLocationTree`.
#[cfg(all(feature = "item_interactions", feature = "item_state_example"))]
fn item_location_tree_collect(
    item_location_direct_descendents: &mut BTreeMap<ItemLocation, BTreeSet<ItemLocation>>,
    item_location_parent: ItemLocation,
) -> ItemLocationTree {
    match item_location_direct_descendents.remove_entry(&item_location_parent) {
        Some((item_location, item_location_children)) => {
            let children = item_location_children
                .into_iter()
                .map(|item_location_child| {
                    item_location_tree_collect(
                        item_location_direct_descendents,
                        item_location_child,
                    )
                })
                .collect::<Vec<ItemLocationTree>>();
            ItemLocationTree::new(item_location, children)
        }

        // Should never be reached.
        None => ItemLocationTree::new(item_location_parent, Vec::new()),
    }
}

/// Inserts / extends the `item_location_direct_descendents` with an entry for
/// each `ItemLocation` and its direct `ItemLocation` descendents.
#[cfg(all(feature = "item_interactions", feature = "item_state_example"))]
fn item_location_descendents_insert(
    item_location_direct_descendents: &mut BTreeMap<ItemLocation, BTreeSet<ItemLocation>>,
    item_location_ancestors: &[ItemLocation],
) {
    // Each subsequent `ItemLocation` in `location_from` is a child of the previous
    // `ItemLocation`.
    let item_location_iter = item_location_ancestors.iter();
    let item_location_child_iter = item_location_ancestors.iter().skip(1);

    item_location_iter.zip(item_location_child_iter).for_each(
        |(item_location, item_location_child)| {
            // Save one clone by not using `BTreeSet::entry`
            if let Some(item_location_children) =
                item_location_direct_descendents.get_mut(item_location)
            {
                if !item_location_children.contains(item_location_child) {
                    item_location_children.insert(item_location_child.clone());
                }
            } else {
                let mut item_location_children = BTreeSet::new();
                item_location_children.insert(item_location_child.clone());
                item_location_direct_descendents
                    .insert(item_location.clone(), item_location_children);
            }

            // Add an empty set for the child `ItemLocation`.
            if !item_location_direct_descendents.contains_key(item_location_child) {
                item_location_direct_descendents
                    .insert(item_location_child.clone(), BTreeSet::new());
            }
        },
    );
}

/// Accumulates the links between
#[cfg(all(feature = "item_interactions", feature = "item_state_example"))]
struct ItemInteractionsCtx {
    /// Map from each `ItemLocation` to all of its direct descendents collected
    /// from all items.
    item_location_direct_descendents: BTreeMap<ItemLocation, BTreeSet<ItemLocation>>,
    /// Map from each item to each of its `ItemInteractions`.
    item_to_item_interactions: IndexMap<ItemId, Vec<ItemInteraction>>,
    /// Tracks the items that referred to this item location.
    #[cfg(feature = "output_progress")]
    item_location_to_item_id_sets: HashMap<ItemLocation, HashSet<ItemId>>,
}