mig-assembly 0.41.4

MIG-guided EDIFACT tree assembly — parse RawSegments into typed MIG trees
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
//! One transaction of a message, as if the message held no other.
//!
//! A UTILMD message repeats SG4, one per transaction (`IDE+24`), and the AHB
//! states its conditions per transaction: "Wenn SG4 STS+7++ZG9 vorhanden" asks
//! about the transaction the annotated field sits in. Asked of the whole
//! message, another transaction answers. A list PID (55065, 55195, …) adds an
//! SG4 of another kind, the list itself (`IDE+Z01`), which the transactions'
//! conditions do refer to ("Wenn im SG4 IDE+Z01 … STS+E01 nicht vorhanden").
//!
//! [`TransactionView`] keeps the repetition of the transaction group asked
//! about, first, and every repetition of another kind; it leaves out the other
//! repetitions of the same kind.

use std::collections::HashMap;

use crate::assembler::{AssembledGroup, AssembledGroupInstance, AssembledSegment, AssembledTree};
use crate::tokenize::OwnedSegment;

/// [`AssembledTree::transaction_view`]'s result.
#[derive(Debug, Clone)]
pub struct TransactionView {
    /// The message with the other transactions of the same kind left out. The
    /// transaction itself is repetition 0 of its group.
    pub tree: AssembledTree,
    /// Segment numbers (`AssembledSegment::segment_number`) of everything left
    /// out, sorted.
    pub left_out: Vec<u32>,
}

/// The kind of a repetition: its entry segment's tag and qualifier
/// (`IDE`, `24`).
fn kind(instance: &AssembledGroupInstance) -> Option<(&str, &str)> {
    let entry = instance.segments.first()?;
    let qualifier = entry
        .elements
        .first()
        .and_then(|e| e.first())
        .map_or("", String::as_str);
    Some((entry.tag.as_str(), qualifier))
}

fn segment_numbers(instance: &AssembledGroupInstance, out: &mut Vec<u32>) {
    let own = instance.segments.iter().chain(&instance.skipped_segments);
    out.extend(own.filter_map(|s: &AssembledSegment| s.segment_number));
    for group in &instance.child_groups {
        for repetition in &group.repetitions {
            segment_numbers(repetition, out);
        }
    }
}

impl AssembledTree {
    /// The message as transaction `index` of the top-level group `group_id`
    /// sees it (see the [module](crate::transaction_view)). `None` when the
    /// group has no such repetition.
    pub fn transaction_view(&self, group_id: &str, index: usize) -> Option<TransactionView> {
        let position = self.groups.iter().position(|g| g.group_id == group_id)?;
        let group = &self.groups[position];
        let own = group.repetitions.get(index)?;
        let own_kind = kind(own);
        let mut repetitions = vec![own.clone()];
        let mut left_out = Vec::new();
        for (i, repetition) in group.repetitions.iter().enumerate() {
            if i == index {
                continue;
            }
            if kind(repetition) == own_kind {
                segment_numbers(repetition, &mut left_out);
            } else {
                repetitions.push(repetition.clone());
            }
        }
        left_out.sort_unstable();
        let mut groups = Vec::with_capacity(self.groups.len());
        for (i, g) in self.groups.iter().enumerate() {
            if i == position {
                let mut view = g.clone_shallow();
                view.repetitions = std::mem::take(&mut repetitions);
                groups.push(view);
            } else {
                groups.push(g.clone());
            }
        }
        Some(TransactionView {
            tree: AssembledTree {
                segments: self.segments.clone(),
                groups,
                post_group_start: self.post_group_start,
                inter_group_segments: self.inter_group_segments.clone(),
            },
            left_out,
        })
    }
}

/// One transaction's view together with the message segments it keeps:
/// [`TransactionViews::view`]'s result.
#[derive(Debug, Clone)]
pub struct TransactionSegmentsView {
    /// As [`TransactionView::tree`].
    pub tree: AssembledTree,
    /// The message's segments without those of the other transactions of
    /// the same kind, in message order.
    pub segments: Vec<OwnedSegment>,
}

/// Every transaction view of a message, prepared once.
///
/// [`AssembledTree::transaction_view`] finds what a view leaves out by
/// walking every other transaction, and the caller then filters the whole
/// message by it: one view costs as much as the message, all of them its
/// square — 1,600 UTILMD transactions took 1.6 s for the views alone. This
/// sorts each segment once into what every view of a kind keeps and what
/// only one transaction's view keeps, so a view costs its own transaction
/// plus what the transactions share (the header, SG2, ...).
#[derive(Debug)]
pub struct TransactionViews<'t> {
    tree: &'t AssembledTree,
    segments: &'t [OwnedSegment],
    groups: Vec<TransactionGroupIndex>,
}

#[derive(Debug)]
struct TransactionGroupIndex {
    /// The group's position in `AssembledTree::groups`.
    position: usize,
    /// Per repetition: its kind, an index into the `per_kind` vectors.
    kind_of: Vec<usize>,
    /// Per kind: the repetitions of the other kinds, in order.
    others: Vec<Vec<usize>>,
    /// Per kind: the indices into the message segments every view of that
    /// kind keeps — no repetition of the kind holds them.
    shared: Vec<Vec<usize>>,
    /// Per repetition: the indices of the message segments it holds alone
    /// among the repetitions of its kind.
    own: Vec<Vec<usize>>,
}

impl AssembledTree {
    /// Prepares the views [`transaction_view`](Self::transaction_view) gives,
    /// of every top-level group `is_transaction_group` accepts, over the
    /// message `segments` the tree was assembled from (matched by
    /// `segment_number`).
    pub fn transaction_views<'t>(
        &'t self,
        segments: &'t [OwnedSegment],
        is_transaction_group: impl Fn(&str) -> bool,
    ) -> TransactionViews<'t> {
        let groups = self
            .groups
            .iter()
            .enumerate()
            .filter(|(_, g)| is_transaction_group(&g.group_id))
            .map(|(position, g)| TransactionGroupIndex::new(position, g, segments))
            .collect();
        TransactionViews {
            tree: self,
            segments,
            groups,
        }
    }
}

impl TransactionGroupIndex {
    fn new(position: usize, group: &AssembledGroup, segments: &[OwnedSegment]) -> Self {
        let mut kinds: Vec<Option<(&str, &str)>> = Vec::new();
        let kind_of: Vec<usize> = group
            .repetitions
            .iter()
            .map(|repetition| {
                let k = kind(repetition);
                kinds
                    .iter()
                    .position(|known| *known == k)
                    .unwrap_or_else(|| {
                        kinds.push(k);
                        kinds.len() - 1
                    })
            })
            .collect();
        let others = (0..kinds.len())
            .map(|k| (0..kind_of.len()).filter(|&r| kind_of[r] != k).collect())
            .collect();

        // Which repetitions hold each segment number.
        let mut holders: HashMap<u32, Vec<usize>> = HashMap::new();
        let mut numbers = Vec::new();
        for (r, repetition) in group.repetitions.iter().enumerate() {
            numbers.clear();
            segment_numbers(repetition, &mut numbers);
            for &n in &numbers {
                let held = holders.entry(n).or_default();
                if held.last() != Some(&r) {
                    held.push(r);
                }
            }
        }

        // A view of repetition `r` keeps a segment unless another repetition
        // of `r`'s kind holds it.
        let mut shared = vec![Vec::new(); kinds.len()];
        let mut own = vec![Vec::new(); kind_of.len()];
        for (i, segment) in segments.iter().enumerate() {
            let held = holders
                .get(&segment.segment_number)
                .map_or(&[][..], Vec::as_slice);
            for (k, shared) in shared.iter_mut().enumerate() {
                let mut of_kind = held.iter().filter(|&&r| kind_of[r] == k);
                match of_kind.next() {
                    None => shared.push(i),
                    Some(&r) if of_kind.all(|&other| other == r) => own[r].push(i),
                    // Held by two repetitions of the kind: every view of
                    // the kind leaves it out.
                    Some(_) => {}
                }
            }
        }
        Self {
            position,
            kind_of,
            others,
            shared,
            own,
        }
    }
}

impl TransactionViews<'_> {
    /// Transaction `index` of the top-level group `group_id`, as
    /// [`AssembledTree::transaction_view`] gives it, with the message
    /// segments it keeps. `None` when the group is no transaction group or
    /// has no such repetition.
    pub fn view(&self, group_id: &str, index: usize) -> Option<TransactionSegmentsView> {
        let index_of = self
            .groups
            .iter()
            .find(|g| self.tree.groups[g.position].group_id == group_id)?;
        let group = &self.tree.groups[index_of.position];
        let own = group.repetitions.get(index)?;
        let k = index_of.kind_of[index];

        let mut repetitions = Vec::with_capacity(1 + index_of.others[k].len());
        repetitions.push(own.clone());
        repetitions.extend(
            index_of.others[k]
                .iter()
                .map(|&r| group.repetitions[r].clone()),
        );
        let groups = self
            .tree
            .groups
            .iter()
            .enumerate()
            .map(|(i, g)| {
                if i == index_of.position {
                    AssembledGroup {
                        group_id: g.group_id.clone(),
                        repetitions: std::mem::take(&mut repetitions),
                    }
                } else {
                    g.clone()
                }
            })
            .collect();

        // Both lists ascend; merged, they keep the message order.
        let (shared, own) = (&index_of.shared[k], &index_of.own[index]);
        let mut segments = Vec::with_capacity(shared.len() + own.len());
        let (mut s, mut o) = (0, 0);
        while s < shared.len() || o < own.len() {
            let next = if o == own.len() || (s < shared.len() && shared[s] < own[o]) {
                s += 1;
                shared[s - 1]
            } else {
                o += 1;
                own[o - 1]
            };
            segments.push(self.segments[next].clone());
        }

        Some(TransactionSegmentsView {
            tree: AssembledTree {
                segments: self.tree.segments.clone(),
                groups,
                post_group_start: self.tree.post_group_start,
                inter_group_segments: self.tree.inter_group_segments.clone(),
            },
            segments,
        })
    }
}

impl AssembledGroup {
    /// The group without its repetitions.
    fn clone_shallow(&self) -> Self {
        Self {
            group_id: self.group_id.clone(),
            repetitions: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assembler::AssembledGroup;

    fn seg(text: &str, number: u32) -> AssembledSegment {
        let mut parts = text.split('+');
        AssembledSegment {
            tag: parts.next().unwrap().to_string(),
            elements: parts.map(|e| vec![e.to_string()]).collect(),
            mig_number: None,
            segment_number: Some(number),
        }
    }

    fn inst(
        segments: Vec<AssembledSegment>,
        child_groups: Vec<AssembledGroup>,
    ) -> AssembledGroupInstance {
        AssembledGroupInstance {
            segments,
            child_groups,
            entry_mig_number: None,
            variant_mig_numbers: vec![],
            skipped_segments: vec![],
            skipped_positions: vec![],
        }
    }

    fn sg8(seq: &str, number: u32) -> AssembledGroup {
        AssembledGroup {
            group_id: "SG8".into(),
            repetitions: vec![inst(vec![seg(seq, number)], vec![])],
        }
    }

    /// BGM, SG2, then SG4: a list (`IDE+Z01`) and three transactions.
    fn message() -> AssembledTree {
        AssembledTree {
            segments: vec![seg("BGM+E01", 1)],
            groups: vec![
                AssembledGroup {
                    group_id: "SG2".into(),
                    repetitions: vec![inst(vec![seg("NAD+MS", 2)], vec![])],
                },
                AssembledGroup {
                    group_id: "SG4".into(),
                    repetitions: vec![
                        inst(vec![seg("IDE+Z01", 3)], vec![]),
                        inst(vec![seg("IDE+24", 4)], vec![sg8("SEQ+Z01", 5)]),
                        inst(vec![seg("IDE+24", 6)], vec![sg8("SEQ+Z02", 7)]),
                        inst(vec![seg("IDE+24", 8)], vec![sg8("SEQ+Z03", 9)]),
                    ],
                },
            ],
            post_group_start: 1,
            inter_group_segments: Default::default(),
        }
    }

    fn entries(tree: &AssembledTree) -> Vec<String> {
        tree.groups[1]
            .repetitions
            .iter()
            .map(|r| format!("{}+{}", r.segments[0].tag, r.segments[0].elements[0][0]))
            .collect()
    }

    #[test]
    fn a_transaction_sees_itself_first_and_the_list_but_no_other_transaction() {
        let view = message().transaction_view("SG4", 2).unwrap();
        assert_eq!(entries(&view.tree), ["IDE+24", "IDE+Z01"]);
        assert_eq!(
            view.tree.groups[1].repetitions[0].segments[0].segment_number,
            Some(6)
        );
        assert_eq!(view.left_out, [4, 5, 8, 9]);
        // What is not the transaction group is kept as it is.
        assert_eq!(view.tree.groups[0], message().groups[0]);
        assert_eq!(view.tree.segments, message().segments);
    }

    #[test]
    fn the_list_sees_every_transaction() {
        let view = message().transaction_view("SG4", 0).unwrap();
        assert_eq!(
            entries(&view.tree),
            ["IDE+Z01", "IDE+24", "IDE+24", "IDE+24"]
        );
        assert!(view.left_out.is_empty());
    }

    /// The message's segments in order, numbered as the tree numbers them,
    /// plus one no group holds (UNT).
    fn segments(tree: &AssembledTree) -> Vec<OwnedSegment> {
        fn collect(instance: &AssembledGroupInstance, out: &mut Vec<OwnedSegment>) {
            for s in &instance.segments {
                out.push(OwnedSegment {
                    id: s.tag.clone(),
                    elements: s.elements.clone(),
                    segment_number: s.segment_number.unwrap(),
                });
            }
            for g in &instance.child_groups {
                g.repetitions.iter().for_each(|r| collect(r, out));
            }
        }
        let mut out: Vec<OwnedSegment> = Vec::new();
        let root = inst(tree.segments.clone(), tree.groups.clone());
        collect(&root, &mut out);
        out.push(OwnedSegment {
            id: "UNT".into(),
            elements: vec![],
            segment_number: 10,
        });
        out.sort_by_key(|s| s.segment_number);
        out
    }

    #[test]
    fn prepared_views_are_the_views_with_the_segments_they_keep() {
        let tree = message();
        let all = segments(&tree);
        let views = tree.transaction_views(&all, |g| g == "SG4");
        for index in 0..4 {
            let expected = tree.transaction_view("SG4", index).unwrap();
            let view = views.view("SG4", index).unwrap();
            assert_eq!(view.tree.groups, expected.tree.groups, "view {index}");
            assert_eq!(view.tree.segments, expected.tree.segments);
            let kept: Vec<OwnedSegment> = all
                .iter()
                .filter(|s| expected.left_out.binary_search(&s.segment_number).is_err())
                .cloned()
                .collect();
            assert_eq!(view.segments, kept, "view {index}");
        }
        let numbers = |index| -> Vec<u32> {
            let view = views.view("SG4", index).unwrap();
            view.segments.iter().map(|s| s.segment_number).collect()
        };
        assert_eq!(numbers(2), [1, 2, 3, 6, 7, 10]);
        assert_eq!(numbers(0), (1..=10).collect::<Vec<_>>());
        assert!(views.view("SG4", 4).is_none());
        assert!(views.view("SG2", 0).is_none());
    }

    #[test]
    fn no_such_group_or_repetition_is_no_view() {
        assert!(message().transaction_view("SG5", 0).is_none());
        assert!(message().transaction_view("SG4", 4).is_none());
    }
}