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
use crate::namemap::*;
use crate::specification::*;
use std::collections::HashMap;

// check the cross references between various elements
pub fn check(a2l_file: &A2lFile, log_msgs: &mut Vec<String>) {
    for module in &a2l_file.project.module {
        let name_map = ModuleNameMap::build(&a2l_file.project.module[0], log_msgs);

        for axis_pts in &module.axis_pts {
            check_axis_pts(axis_pts, &name_map, log_msgs);
        }

        for characteristic in &module.characteristic {
            check_characteristic(characteristic, &name_map, log_msgs);
        }

        for compu_method in &module.compu_method {
            check_compu_method(compu_method, &name_map, log_msgs);
        }

        for function in &module.function {
            check_function(function, &name_map, log_msgs);
        }

        for group in &module.group {
            check_group(group, &name_map, log_msgs);
        }
        check_group_structure(&module.group, log_msgs);

        for measurement in &module.measurement {
            check_measurement(measurement, &name_map, log_msgs);
        }

        for transformer in &module.transformer {
            check_transformer(transformer, &name_map, log_msgs);
        }
    }
}

fn check_axis_descr(
    parent_name: &str,
    axis_descr: &AxisDescr,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    let line = axis_descr.get_line();
    if axis_descr.input_quantity != "NO_INPUT_QUANTITY"
        && name_map.object.get(&axis_descr.input_quantity).is_none()
    {
        log_msgs.push(format!("In AXIS_DESCR of CHARACTERISTIC {} on line {}: Referenced input MEASUREMENT {} does not exist.",
        parent_name, line, axis_descr.input_quantity));
    }

    if axis_descr.conversion != "NO_COMPU_METHOD"
        && name_map.compu_method.get(&axis_descr.conversion).is_none()
    {
        log_msgs.push(format!("In AXIS_DESCR of CHARACTERISTIC {} on line {}: Referenced COMPU_METHOD {} does not exist.",
        parent_name, line, axis_descr.conversion));
    }

    if let Some(axis_pts_ref) = &axis_descr.axis_pts_ref {
        let apr_line = axis_pts_ref.get_line();
        if name_map.object.get(&axis_pts_ref.axis_points).is_none() {
            log_msgs.push(format!("In AXIS_PTS_REF of CHARACTERISTIC {} on line {}: Referenced AXIS_PTS {} does not exist",
            parent_name, apr_line, axis_pts_ref.axis_points));
        }
    }

    if let Some(curve_axis_ref) = &axis_descr.curve_axis_ref {
        let car_line = curve_axis_ref.get_line();
        if name_map.object.get(&curve_axis_ref.curve_axis).is_none() {
            log_msgs.push(format!(
                "In CURVE_AXIS_REF on line {}: Referenced CHARACTERISTIC {} does not exist",
                car_line, curve_axis_ref.curve_axis
            ));
        }
    }
}

fn check_axis_pts(axis_pts: &AxisPts, name_map: &ModuleNameMap, log_msgs: &mut Vec<String>) {
    let name = &axis_pts.name;
    let line = axis_pts.get_line();

    if axis_pts.conversion != "NO_COMPU_METHOD"
        && name_map.compu_method.get(&axis_pts.conversion).is_none()
    {
        log_msgs.push(format!(
            "In AXIS_PTS {} on line {}: Referenced COMPU_METHOD {} does not exist.",
            name, line, axis_pts.conversion
        ));
    }

    check_function_list(&axis_pts.function_list, name_map, log_msgs);
    check_ref_memory_segment(&axis_pts.ref_memory_segment, name_map, log_msgs);
}

fn check_characteristic(
    characteristic: &Characteristic,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    let name = &characteristic.name;
    let line = characteristic.get_line();

    if characteristic.conversion != "NO_COMPU_METHOD"
        && name_map
            .compu_method
            .get(&characteristic.conversion)
            .is_none()
    {
        log_msgs.push(format!(
            "In CHARACTERISTIC {} on line {}: Referenced COMPU_METHOD {} does not exist.",
            name, line, characteristic.conversion
        ));
    }

    if name_map
        .record_layout
        .get(&characteristic.deposit)
        .is_none()
    {
        log_msgs.push(format!(
            "In CHARACTERISTIC {} on line {}: Referenced RECORD_LAYOUT {} does not exist.",
            name, line, characteristic.deposit
        ));
    }

    for axis_descr in &characteristic.axis_descr {
        check_axis_descr(name, axis_descr, name_map, log_msgs);
    }

    if let Some(comparison_quantity) = &characteristic.comparison_quantity {
        let cqline = comparison_quantity.get_line();
        if name_map.object.get(&comparison_quantity.name).is_none() {
            log_msgs.push(format!(
                "In COMPARISON_QUANTITY on line {}: Referenced MEASUREMENT {} does not exist",
                cqline, comparison_quantity.name
            ));
        }
    }

    if let Some(dependent_characteristic) = &characteristic.dependent_characteristic {
        let depline = dependent_characteristic.get_line();
        check_reference_list(
            "DEPENDENT_CHARACTERISTIC",
            "CHARACTERISTIC",
            depline,
            &dependent_characteristic.characteristic_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(map_list) = &characteristic.map_list {
        let ml_line = map_list.get_line();
        check_reference_list(
            "MAP_LIST",
            "CHARACTERISTIC",
            ml_line,
            &map_list.name_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(virtual_characteristic) = &characteristic.virtual_characteristic {
        let vc_line = virtual_characteristic.get_line();
        check_reference_list(
            "VIRTUAL_CHARACTERISTIC",
            "CHARACTERISTIC",
            vc_line,
            &virtual_characteristic.characteristic_list,
            &name_map.object,
            log_msgs,
        );
    }

    check_function_list(&characteristic.function_list, name_map, log_msgs);
    check_ref_memory_segment(&characteristic.ref_memory_segment, name_map, log_msgs);
}

fn check_compu_method(
    compu_method: &CompuMethod,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    let line = compu_method.get_line();

    if let Some(compu_tab_ref) = &compu_method.compu_tab_ref {
        if name_map
            .compu_tab
            .get(&compu_tab_ref.conversion_table)
            .is_none()
        {
            log_msgs.push(format!(
                "In COMPU_METHOD on line {}: The COMPU_TAB_REF references nonexistent COMPU_TAB {}",
                line, compu_tab_ref.conversion_table
            ));
        }
    }

    if let Some(ref_unit) = &compu_method.ref_unit {
        if name_map.compu_tab.get(&ref_unit.unit).is_none() {
            log_msgs.push(format!(
                "In COMPU_METHOD on line {}: The REF_UNIT references nonexistent UNIT {}",
                line, ref_unit.unit
            ));
        }
    }
}

fn check_function(function: &Function, name_map: &ModuleNameMap, log_msgs: &mut Vec<String>) {
    if let Some(in_measurement) = &function.in_measurement {
        let line = in_measurement.get_line();
        check_reference_list(
            "IN_MEASUREMENT",
            "MEASUREMENT",
            line,
            &in_measurement.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(loc_measurement) = &function.loc_measurement {
        let line = loc_measurement.get_line();
        check_reference_list(
            "LOC_MEASUREMENT",
            "MEASUREMENT",
            line,
            &loc_measurement.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(out_measurement) = &function.out_measurement {
        let line = out_measurement.get_line();
        check_reference_list(
            "OUT_MEASUREMENT",
            "MEASUREMENT",
            line,
            &out_measurement.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(def_characteristic) = &function.def_characteristic {
        let line = def_characteristic.get_line();
        check_reference_list(
            "DEF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &def_characteristic.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(ref_characteristic) = &function.ref_characteristic {
        let line = ref_characteristic.get_line();
        check_reference_list(
            "REF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &ref_characteristic.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(sub_function) = &function.sub_function {
        let line = sub_function.get_line();
        check_reference_list(
            "SUB_FUNCTION",
            "FUNCTION",
            line,
            &sub_function.identifier_list,
            &name_map.function,
            log_msgs,
        );
    }
}

fn check_function_list(
    opt_function_list: &Option<FunctionList>,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    if let Some(function_list) = opt_function_list {
        let line = function_list.get_line();
        check_reference_list(
            "FUNCTION_LIST",
            "FUNCTION",
            line,
            &function_list.name_list,
            &name_map.function,
            log_msgs,
        );
    }
}

fn check_group(group: &Group, name_map: &ModuleNameMap, log_msgs: &mut Vec<String>) {
    if let Some(ref_characteristic) = &group.ref_characteristic {
        let line = ref_characteristic.get_line();
        check_reference_list(
            "REF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &ref_characteristic.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(ref_measurement) = &group.ref_measurement {
        let line = ref_measurement.get_line();
        check_reference_list(
            "REF_MEASUREMENT",
            "MEASUREMENT",
            line,
            &ref_measurement.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(sub_group) = &group.sub_group {
        let line = sub_group.get_line();
        check_reference_list(
            "SUB_GROUP",
            "GROUP",
            line,
            &sub_group.identifier_list,
            &name_map.group,
            log_msgs,
        );
    }
}

fn check_measurement(
    measurement: &Measurement,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    let name = &measurement.name;
    let line = measurement.get_line();

    if measurement.conversion != "NO_COMPU_METHOD"
        && name_map.compu_method.get(&measurement.conversion).is_none()
    {
        log_msgs.push(format!(
            "In MEASUREMENT {} on line {}: Referenced COMPU_METHOD {} does not exist.",
            name, line, measurement.conversion
        ));
    }

    check_ref_memory_segment(&measurement.ref_memory_segment, name_map, log_msgs);
}

fn check_transformer(
    transformer: &Transformer,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    let name = &transformer.name;
    let line = transformer.get_line();

    if transformer.inverse_transformer != "NO_INVERSE_TRANSFORMER"
        && name_map
            .transformer
            .get(&transformer.inverse_transformer)
            .is_none()
    {
        log_msgs.push(format!(
            "In TRANSFORMTER {} on line {}: Referenced inverse TRANSFORMER {} does not exist.",
            name, line, transformer.inverse_transformer
        ));
    }

    if let Some(transformer_in_objects) = &transformer.transformer_in_objects {
        let line = transformer_in_objects.get_line();
        check_reference_list(
            "TRANSFORMER_IN_OBJECTS",
            "CHARACTERISTIC",
            line,
            &transformer_in_objects.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }

    if let Some(transformer_out_objects) = &transformer.transformer_out_objects {
        let line = transformer_out_objects.get_line();
        check_reference_list(
            "TRANSFORMER_OUT_OBJECTS",
            "CHARACTERISTIC",
            line,
            &transformer_out_objects.identifier_list,
            &name_map.object,
            log_msgs,
        );
    }
}

fn check_ref_memory_segment(
    opt_ref_memory_segment: &Option<RefMemorySegment>,
    name_map: &ModuleNameMap,
    log_msgs: &mut Vec<String>,
) {
    if let Some(ref_memory_segment) = opt_ref_memory_segment {
        let line = ref_memory_segment.get_line();

        if name_map
            .memory_segment
            .get(&ref_memory_segment.name)
            .is_none()
        {
            log_msgs.push(format!(
                "In REF_MEMORY_SEGMENT on line {}: reference to unknown memory segment {}",
                line, ref_memory_segment.name
            ));
        }
    }
}

fn check_reference_list<T>(
    container_type: &str,
    ref_type: &str,
    line: u32,
    identifier_list: &[String],
    map: &HashMap<String, T>,
    log_msgs: &mut Vec<String>,
) {
    for ident in identifier_list {
        if map.get(ident).is_none() {
            log_msgs.push(format!(
                "In {} on line {}: Reference to nonexistent {} \"{}\"",
                container_type, line, ref_type, ident
            ));
        }
    }
}

struct GroupInfo<'a> {
    is_root: bool,
    parents: Vec<&'a str>,
}

fn check_group_structure(grouplist: &[Group], log_msgs: &mut Vec<String>) {
    let mut groupinfo: HashMap<String, GroupInfo> = HashMap::new();

    for group in grouplist {
        groupinfo.insert(
            group.name.to_owned(),
            GroupInfo {
                is_root: group.root.is_some(),
                parents: Vec::new(),
            },
        );
    }

    for group in grouplist {
        if let Some(sub_group) = &group.sub_group {
            for sg in &sub_group.identifier_list {
                if let Some(gi) = groupinfo.get_mut(sg) {
                    gi.parents.push(&group.name);
                } else {
                    log_msgs.push(format!(
                        "GROUP {} references non-existent sub-group {}",
                        group.name, sg
                    ));
                }
            }
        }
    }

    for group in grouplist {
        if let Some(gi) = groupinfo.get(&group.name) {
            if gi.is_root && gi.parents.len() > 1 {
                log_msgs.push(format!("GROUP {} has the ROOT attribute, but is also referenced as a sub-group by GROUPs {}", group.name, gi.parents.join(", ")));
            } else if gi.is_root && gi.parents.len() == 1 {
                log_msgs.push(format!("GROUP {} has the ROOT attribute, but is also referenced as a sub-group by GROUP {}", group.name, gi.parents[0]));
            } else if !gi.is_root && gi.parents.len() > 1 {
                log_msgs.push(format!(
                    "GROUP {} is referenced as a sub-group by multiple groups: {}",
                    group.name,
                    gi.parents.join(", ")
                ));
            } else if !gi.is_root && gi.parents.is_empty() {
                log_msgs.push(format!("GROUP {} does not have the ROOT attribute, and is not referenced as a sub-group by any other group", group.name));
            }
        }
    }
}