ebi 0.3.14

A stochastic process mining utility and 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
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
use super::ebi_command_sample::{self, SAMPLED_OBJECT_INPUTS};
use crate::{
    EbiInputTypeEnum,
    ebi_framework::{
        ebi_command::EbiCommand,
        ebi_input::{EbiInput, EbiInputType},
        ebi_output::{EbiOutput, EbiOutputType},
        ebi_trait::EbiTrait,
        ebi_trait_object::EbiTraitObject,
    },
    ebi_traits::{
        ebi_trait_finite_stochastic_language::EbiTraitFiniteStochasticLanguage,
        ebi_trait_queriable_stochastic_language::EbiTraitQueriableStochasticLanguage,
    },
    techniques::{
        chi_square_stochastic_conformance::ChiSquareStochasticConformance,
        earth_movers_stochastic_conformance::EarthMoversStochasticConformance,
        entropic_relevance::EntropicRelvance,
        gain_precision_recall::PotentialGainRecallPrecision,
        hellinger_stochastic_conformance::HellingerStochasticConformance,
        jensen_shannon_stochastic_conformance::JensenShannonStochasticConformance,
        partially_ordered_earth_movers_stochastic_conformance::PartiallyOrderedEarthMoversStochasticConformance,
        stochastic_markovian_abstraction::AbstractMarkovian,
        stochastic_markovian_abstraction_conformance::{
            DistanceMeasure, StochasticMarkovianConformance,
        },
        unit_earth_movers_stochastic_conformance::UnitEarthMoversStochasticConformance,
    },
};
use ebi_objects::{
    EbiObject, EbiObjectType,
    anyhow::{Context, anyhow},
    ebi_arithmetic::{ConstFraction, Fraction},
    ebi_objects::stochastic_deterministic_finite_automaton::StochasticDeterministicFiniteAutomaton,
};
use strum::VariantNames;

pub const EBI_CONFORMANCE: EbiCommand = EbiCommand::Group {
    name_short: "conf",
    name_long: Some("conformance"),
    explanation_short: "Check the conformance of two stochastic languages.",
    explanation_long: None,
    children: &[
        &EBI_CONFORMANCE_CHI_SQUARED,
        &EBI_CONFORMANCE_CHI_SQUARED_SAMPLE,
        &EBI_CONFORMANCE_EARTH_MOVERS,
        &EBI_CONFORMANCE_EARTH_MOVERS_SAMPLE,
        &EBI_CONFORMANCE_ENTROPIC_RELEVANCE,
        &EBI_CONFORMANCE_GAIN_PRECISION,
        &EBI_CONFORMANCE_GAIN_RECALL,
        &EBI_CONFORMANCE_HELLINGER,
        &EBI_CONFORMANCE_HELLINGER_SAMPLE,
        &EBI_CONFORMANCE_JSSC,
        &EBI_CONFORMANCE_JSSC_SAMPLE,
        &EBI_CONFORMANCE_MARKOVIAN,
        &EBI_CONFORMANCE_UEMSC,
        &EBI_CONFORMANCE_UEMSC_SAMPLE,
    ],
};

pub const EBI_CONFORMANCE_UEMSC: EbiCommand = EbiCommand::Command {
    name_short: "uemsc",
    name_long: Some("unit-earth-movers"),
    explanation_short: "Compute unit-earth movers' stochastic conformance.",
    explanation_long: Some(
        "Compute unit-earth movers' stochastic conformance, which is 1 - the total variation distance.",
    ),
    latex_link: Some(
        r"Unit earth movers' stochastic conformance is computed as follows:
        
        \begin{definition}[Unit earth movers' stochastic conformance~\cite{DBLP:conf/bpm/LeemansSA19}]
            \label{def:uemsc}
                Let $L$ be a finite stochastic language and let $M$ be a queriable stochastic langauge.
                Then, the \emph{unit earth movers' stochastic conformance ($\text{uemsc}$) of $L$ and $M$} is defined as follows: 
                \begin{align*}
                    \text{uemsc}(L, M) ={}& 1 - \sum_{t \in L} \max(L(t) - M(t),0)
                \end{align*}       
            \end{definition}",
    ),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Trait(EbiTrait::QueriableStochasticLanguage)],
    ],
    input_names: &["SLANG", "MODEL"],
    input_helps: &[
        "A finite stochastic language (log) to compare.",
        "A queriable stochastic language (model) to compare.",
    ],
    execute: |mut inputs, _| {
        let log: Box<dyn EbiTraitFiniteStochasticLanguage> = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>(
        )?;
        let model = inputs
            .remove(0)
            .to_type::<dyn EbiTraitQueriableStochasticLanguage>()?;
        let uemsc = log
            .unit_earth_movers_stochastic_conformance(model)
            .context("cannot compute uEMSC")?;
        Ok(EbiOutput::Fraction(uemsc))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_UEMSC_SAMPLE: EbiCommand = EbiCommand::Command {
    name_short: "uemsc-sample",
    name_long: Some("unit-earth-movers-sample"),
    explanation_short: "Compute unit-earth movers' stochastic conformance, which is 1 - the total variation distance, if both inputs need to be sampled.",
    explanation_long: Some(
        "Compute unit-earth movers' stochastic conformance, which is 1 - the total variation distance, if both inputs need to be sampled. 
        If one input is a log or a finite stochastic language, then use `uemsc`.
        Livelocks are silently ignored.",
    ),
    latex_link: Some("see \\cref{def:uemsc}"),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        SAMPLED_OBJECT_INPUTS,
        SAMPLED_OBJECT_INPUTS,
        &[&EbiInputType::Usize(Some(1), None, None)],
    ],
    input_names: &["FILE_1", "FILE_2", "NUMBER_OF_TRACES"],
    input_helps: &[
        "A sampleable stochastic language to compare.",
        "A sampleable stochastic language to compare.",
        "Number of traces to sample.",
    ],
    execute: |mut inputs, _| {
        let object1 = inputs.remove(0);
        let object2 = inputs.remove(0);
        let number_of_traces = inputs.remove(0).to_type::<usize>()?;

        let lang1 = Box::new(ebi_command_sample::get_sampled_object(
            object1,
            *number_of_traces,
        )?);
        let lang2 = Box::new(ebi_command_sample::get_sampled_object(
            object2,
            *number_of_traces,
        )?);

        let lang1: Box<dyn EbiTraitFiniteStochasticLanguage> = lang1;
        Ok(EbiOutput::Fraction(
            lang1
                .unit_earth_movers_stochastic_conformance(lang2)
                .context("Compute JSSC by sampling.")?,
        ))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_ENTROPIC_RELEVANCE: EbiCommand = EbiCommand::Command {
    name_short: "er",
    name_long: Some("entropic-relevance"),
    explanation_short: "Compute entropic relevance (uniform).",
    explanation_long: None,
    latex_link: Some(
        r"Entropic relevance is computed as follows:
        
        \begin{definition}[Entropic Relevance~\cite{DBLP:journals/is/AlkhammashPMG22}]
            \label{def:ER}
                Let $L$ be a finite stochastic language and let $M$ be a queriable stochastic language.
                Let $\Lambda$ be the set of all activities appearing in the traces of $L$.
                Then, the \emph{entropic relevance ($\entrel$) of $M$ to $L$} is defined as follows: 
                \begin{align*}
                    \entrel(L, M) ={}& H_0\left(\sum_{\sigma \in \bar{L},\, M(\sigma)>0}{L(\sigma)}\right) + 
                    \sum_{\sigma \in \bar{L}}L(\sigma) J(\sigma, M)\\
                    J(\sigma, M) ={}& \begin{cases}
                    -\log_2 M(\sigma) & M(\sigma) > 0\\
                    (1+|\sigma|) \log_2 (1 + |\Lambda|)) & \text{otherwise}
                    \end{cases}\\
                    H_0(x) ={}& -x \log_2{x} - (1-x) \log_2{(1-x)} \text{ with } H_0(0) = H_0(1) = 0 &\\
                \end{align*}       
            \end{definition}",
    ),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Trait(EbiTrait::QueriableStochasticLanguage)],
    ],
    input_names: &["SLANG", "MODEL"],
    input_helps: &[
        "A finite stochastic language (log) to compare.",
        "A queriable stochastic language (model) to compare.",
    ],
    execute: |mut inputs, _| {
        let log = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;
        let model = inputs
            .remove(0)
            .to_type::<dyn EbiTraitQueriableStochasticLanguage>()?;
        Ok(EbiOutput::LogPolynomial(
            log.entropic_relevance(model).context("cannot compute ER")?,
        ))
    },
    output_type: &EbiOutputType::LogPolynomial,
};

pub const EBI_CONFORMANCE_JSSC: EbiCommand = EbiCommand::Command {
    name_short: "jssc",
    name_long: Some("jensen-shannon"),
    explanation_short: "Compute Jensen-Shannon stochastic conformance, which is 1 - the Jensen-Shannon distance.",
    explanation_long: None,
    latex_link: Some(
        r"Compute Jensen-Shannon stochastic conformance is computed as follows:
        
        \begin{definition}[Jensen-Shannon stochastic conformance~\cite{DBLP:conf/icpm/LiLP24}]
            \label{def:jssc}
                Let $L$ be a finite stochastic language, let $M$ be a queriable stochastic langauge, and let $\Sigma$ be their combined alphabet.
                Then, the \emph{Jensen-Shannon stochastic conformance ($\text{jssc}$) of $L$ and $M$} is defined as follows: 
                \begin{align*}
                    \text{jssc}(L, M) ={}& 1 - \sqrt{\frac{\sum_{\sigma \in \Sigma^*} L(\sigma) \log_2 \frac{2 L(\sigma)}{L(\sigma) + M(\sigma)} + M(\sigma) \log_2 \frac{2M(\sigma)}{L(\sigma)+ M(\sigma)}}{2}}
                \end{align*}       
            \end{definition}",
    ),
    cli_command: None,
    exact_arithmetic: false,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[
            &EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage),
            &EbiInputType::Trait(EbiTrait::QueriableStochasticLanguage),
        ],
    ],
    input_names: &["SLANG", "MODEL"],
    input_helps: &[
        "A finite stochastic language to compare.",
        "A queriable stochastic language to compare.",
    ],
    execute: |mut inputs, _| {
        let event_log = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;

        match inputs.remove(0) {
            EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(slang), _) => Ok(
                EbiOutput::RootLogDiv(event_log.jssc_log2log(slang).context("Compute JSSC.")?),
            ),
            EbiInput::Trait(EbiTraitObject::QueriableStochasticLanguage(slang), _) => Ok(
                EbiOutput::RootLogDiv(event_log.jssc_log2model(slang).context("Compute JSSC.")?),
            ),
            _ => Err(anyhow!("wrong input given")),
        }
    },
    output_type: &EbiOutputType::RootLogDiv,
};

pub const EBI_CONFORMANCE_JSSC_SAMPLE: EbiCommand = EbiCommand::Command {
    name_short: "jssc-sample",
    name_long: Some("jensen-shannon-sample"),
    explanation_short: "Compute Jensen-Shannon stochastic conformance, which is 1 - the Jensen-Shannon distance, if both inputs need to be sampled.",
    explanation_long: Some(
        "Compute Jensen-Shannon stochastic conformance, which is 1 - the Jensen-Shannon distance, if both inputs need to be sampled. 
        If one input is a log or a finite stochastic language, then use `jssc`.
        Livelocks are silently ignored.",
    ),
    latex_link: Some("see \\cref{def:jssc}"),
    cli_command: None,
    exact_arithmetic: false,
    input_types: &[
        SAMPLED_OBJECT_INPUTS,
        SAMPLED_OBJECT_INPUTS,
        &[&EbiInputType::Usize(Some(1), None, None)],
    ],
    input_names: &["FILE_1", "FILE_2", "NUMBER_OF_TRACES"],
    input_helps: &[
        "A sampleable stochastic language to compare.",
        "A sampleable stochastic language to compare.",
        "Number of traces to sample.",
    ],
    execute: |mut inputs, _| {
        let object1 = inputs.remove(0);
        let object2 = inputs.remove(0);
        let number_of_traces = inputs.remove(0).to_type::<usize>()?;

        let lang1 = Box::new(ebi_command_sample::get_sampled_object(
            object1,
            *number_of_traces,
        )?);
        let lang2 = Box::new(ebi_command_sample::get_sampled_object(
            object2,
            *number_of_traces,
        )?);

        let lang1: Box<dyn EbiTraitFiniteStochasticLanguage> = lang1;
        Ok(EbiOutput::RootLogDiv(
            lang1
                .jssc_log2log(lang2)
                .context("Compute JSSC by sampling.")?,
        ))
    },
    output_type: &EbiOutputType::RootLogDiv,
};

pub const EBI_CONFORMANCE_EARTH_MOVERS: EbiCommand = EbiCommand::Command {
    name_short: "emsc",
    name_long: Some("earth-movers"),
    explanation_short: "Compute Earth mover's stochastic conformance.",
    explanation_long: Some(
        "Compute Earth mover's stochastic conformance, which is 1 - the Wasserstein distance.",
    ),
    latex_link: Some(
        r"Earth movers' stochastic conformance is computed as follows:
        
        \begin{definition}[Earth movers' stochastic conformance~\cite{DBLP:journals/is/LeemansABP21}]
            \label{def:emsc}
                Let $L$ and $L'$ be a finite stochastic languages.
                Then, the \emph{Earth movers' stochastic conformance ($\text{emsc}$) of $L$ and $L'$} is defined as follows, with reallocation matrix $R$ and normalised Levenshtein distance $\delta$:
                \begin{align*}
                    \text{emsc}(L, L') ={}& 1 - \min_{R} \sum_{t \in L}\sum_{t' \in L'} R(t, t') \delta(t,t')\\
                    \text{ such that } & \forall_{t\in L} L(t) \leq \sum_{t' \in L'} R(t, t') \land{}\\
                    & \forall_{t' \in L'} L'(t') \leq \sum_{t \in L} R(t, t') \land{}\\
                    & \sum_{t \in L} \sum_{t' \in L'} R(t,t') = 1
                \end{align*}       
            \end{definition}
            
        In case one or both of the inputs are partially ordered, EMSC is computed with a corresponding distance function.",
    ),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[
            &EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage),
            &EbiInputType::Object(EbiObjectType::FiniteStochasticPartiallyOrderedLanguage),
        ],
        &[
            &EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage),
            &EbiInputType::Object(EbiObjectType::FiniteStochasticPartiallyOrderedLanguage),
        ],
    ],
    input_names: &["SLANG", "SLANG"],
    input_helps: &[
        "A finite stochastic language to compare (may be partially ordered).",
        "A finite stochastic language to compare (may be partially ordered).",
    ],
    execute: |mut inputs, _| {
        let lang_a = inputs.remove(0);
        let lang_b = inputs.remove(0);

        // .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;

        match (lang_a, lang_b) {
            (
                EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(mut lang_a), _),
                EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(mut lang_b), _),
            ) => Ok(EbiOutput::Fraction(
                lang_a
                    .earth_movers_stochastic_conformance(lang_b.as_mut())
                    .with_context(|| anyhow!("Error while computing EMSC."))?,
            )),
            (
                EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(mut lang_a), _),
                EbiInput::Object(
                    EbiObject::FiniteStochasticPartiallyOrderedLanguage(mut lang_b),
                    _,
                ),
            ) => Ok(EbiOutput::Fraction(
                lang_a
                    .partially_ordered_earth_movers_stochastic_conformance(&mut lang_b)
                    .with_context(|| anyhow!("Error while computing partially ordered EMSC."))?,
            )),
            (
                EbiInput::Object(
                    EbiObject::FiniteStochasticPartiallyOrderedLanguage(mut lang_a),
                    _,
                ),
                EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(mut lang_b), _),
            ) => Ok(EbiOutput::Fraction(
                lang_b
                    .partially_ordered_earth_movers_stochastic_conformance(&mut lang_a)
                    .with_context(|| anyhow!("Error while computing partially ordered EMSC."))?,
            )),
            (
                EbiInput::Object(
                    EbiObject::FiniteStochasticPartiallyOrderedLanguage(mut lang_a),
                    _,
                ),
                EbiInput::Object(
                    EbiObject::FiniteStochasticPartiallyOrderedLanguage(mut lang_b),
                    _,
                ),
            ) => Ok(EbiOutput::Fraction(
                lang_a
                    .partially_ordered_earth_movers_stochastic_conformance(&mut lang_b)
                    .with_context(|| anyhow!("Error while computing partially ordered EMSC."))?,
            )),
            _ => Err(anyhow!("Inputs not recognised.")),
        }
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_EARTH_MOVERS_SAMPLE: EbiCommand = EbiCommand::Command {
    name_short: "emsc-sample",
    name_long: Some("earth-movers-sample"),
    explanation_short: "Compute Earth mover's stochastic conformance, which is 1 - the Wasserstein distance, where one or both of the inputs needs to be sampled.",
    explanation_long: Some(
        "Compute Earth mover's stochastic conformance, which is 1 - the Wasserstein distance, where one or both of the inputs needs to be sampled. 
        If both inputs are logs or finite stochastic languages, then use `emsc`.
        Livelocks are silently ignored.",
    ),
    latex_link: Some("see \\cref{def:emsc}"),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        SAMPLED_OBJECT_INPUTS,
        SAMPLED_OBJECT_INPUTS,
        &[&EbiInputType::Usize(Some(1), None, None)],
    ],
    input_names: &["FILE_1", "FILE_2", "NUMBER_OF_TRACES"],
    input_helps: &[
        "A stochastic language (log or model) to compare.",
        "A stochastic language (log or model) to compare.",
        "Number of traces to sample.",
    ],
    execute: |mut inputs, _| {
        let object_a = inputs.remove(0);
        let object_b = inputs.remove(0);
        let number_of_traces = inputs.remove(0).to_type::<usize>()?;

        let mut lang_a =
            ebi_command_sample::get_sampled_object_if_necessary(object_a, *number_of_traces)?;
        let mut lang_b =
            ebi_command_sample::get_sampled_object_if_necessary(object_b, *number_of_traces)?;

        // Compute EMSC
        Ok(EbiOutput::Fraction(
            lang_a
                .earth_movers_stochastic_conformance(lang_b.as_mut())
                .context("Compute EMSC.")?,
        ))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_HELLINGER: EbiCommand = EbiCommand::Command {
    name_short: "hsc",
    name_long: Some("hellinger"),
    explanation_short: "Compute Hellinger stochastic conformance.",
    explanation_long: Some(
        "Compute Hellinger stochastic conformance, which is 1 - the Hellinger distance.",
    ),
    latex_link: Some(
        r"Hellinger stochastic conformance is computed as follows:
        
        \begin{definition}[Hellinger stochastic conformance~\cite{hellinger1909neue}]
            \label{def:hsc}
                Let $L$ be a finite stochastic language and let $M$ be a queriable stochastic langauge.
                Then, the \emph{Hellinger stochastic conformance ($\text{hsc}$) of $L$ and $M$} is defined as follows: 
                \begin{align*}
                    \text{hsc}(L, M) ={}& \frac{1}{\sqrt{2}}\sqrt{\sum_{\sigma \in L \cup M}(\sqrt{L(x)}-\sqrt{M(x)})^2}
                \end{align*}       
            \end{definition}",
    ),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Trait(EbiTrait::QueriableStochasticLanguage)],
    ],
    input_names: &["SLANG", "MODEL"],
    input_helps: &[
        "A finite stochastic language (log) to compare.",
        "A queriable stochastic language (model) to compare.",
    ],
    execute: |mut inputs, _| {
        let log = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;
        let model = inputs
            .remove(0)
            .to_type::<dyn EbiTraitQueriableStochasticLanguage>()?;
        let hsc = log
            .hellinger_stochastic_conformance(model)
            .context("cannot compute HSC")?;
        Ok(EbiOutput::Fraction(hsc))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_HELLINGER_SAMPLE: EbiCommand = EbiCommand::Command {
    name_short: "hsc-sample",
    name_long: Some("hellinger-sample"),
    explanation_short: "Compute Hellinger stochastic conformance, which is 1 - the Hellinger distance, if both inputs need to be sampled.",
    explanation_long: Some(
        "Compute Hellinger stochastic conformance, which is 1 - the Hellinger distance, if both inputs need to be sampled. 
        If one input is a log or a finite stochastic language, then use `hsc`.
        Livelocks are silently ignored.",
    ),
    latex_link: Some("see \\cref{def:hsc}"),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        SAMPLED_OBJECT_INPUTS,
        SAMPLED_OBJECT_INPUTS,
        &[&EbiInputType::Usize(Some(1), None, None)],
    ],
    input_names: &["FILE_1", "FILE_2", "NUMBER_OF_TRACES"],
    input_helps: &[
        "A sampleable stochastic language to compare.",
        "A sampleable stochastic language to compare.",
        "Number of traces to sample.",
    ],
    execute: |mut inputs, _| {
        let object1 = inputs.remove(0);
        let object2 = inputs.remove(0);
        let number_of_traces = inputs.remove(0).to_type::<usize>()?;

        let lang1 = Box::new(ebi_command_sample::get_sampled_object(
            object1,
            *number_of_traces,
        )?);
        let lang2 = Box::new(ebi_command_sample::get_sampled_object(
            object2,
            *number_of_traces,
        )?);

        let lang1: Box<dyn EbiTraitFiniteStochasticLanguage> = lang1;
        Ok(EbiOutput::Fraction(
            lang1
                .hellinger_stochastic_conformance(lang2)
                .context("Compute HSC by sampling.")?,
        ))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_CHI_SQUARED: EbiCommand = EbiCommand::Command {
    name_short: "cssc",
    name_long: Some("chi-squared"),
    explanation_short: "Compute Chi-Square stochastic conformance.",
    explanation_long: Some("Compute chi-square stochastic conformance."),
    latex_link: Some(
        r"Chi-squared stochastic conformance is computed as follows:
        
        \begin{definition}[Chi-squared stochastic conformance~\cite{DBLP:conf/eccv/PeleW10}]
            \label{def:cssc}
                Let $L$ be a finite stochastic language and let $M$ be a queriable stochastic langauge.
                Then, the \emph{chi-squared stochastic conformance ($\text{cssc}$) of $L$ and $M$} is defined as follows: 
                \begin{align*}
                    \text{cssc}(L, M) ={}& 1 - \frac{1}{2} \sum_{\sigma \in L \cup M} \frac{(L(\sigma)-M(\sigma))^2}{L(\sigma)+M(\sigma)}
                \end{align*}       
            \end{definition}",
    ),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Trait(EbiTrait::QueriableStochasticLanguage)],
    ],
    input_names: &["SLANG", "MODEL"],
    input_helps: &[
        "A finite stochastic language (log) to compare.",
        "A queriable stochastic language (model) to compare.",
    ],
    execute: |mut inputs, _| {
        let log = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;
        let model = inputs
            .remove(0)
            .to_type::<dyn EbiTraitQueriableStochasticLanguage>()?;
        let cssc = log
            .chi_square_stochastic_conformance(model)
            .context("cannot compute CSSC")?;
        Ok(EbiOutput::Fraction(cssc))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_CHI_SQUARED_SAMPLE: EbiCommand = EbiCommand::Command {
    name_short: "cssc-sample",
    name_long: Some("chi-squared-sample"),
    explanation_short: "Compute chi-squared stochastic conformance, if both inputs need to be sampled.",
    explanation_long: Some(
        "Compute chi-square stochastic conformance, if both inputs need to be sampled. 
        If one input is a log or a finite stochastic language, then use `cssc`.
        Livelocks are silently ignored.",
    ),
    latex_link: Some("see \\cref{def:cssc}"),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        SAMPLED_OBJECT_INPUTS,
        SAMPLED_OBJECT_INPUTS,
        &[&EbiInputType::Usize(Some(1), None, None)],
    ],
    input_names: &["FILE_1", "FILE_2", "NUMBER_OF_TRACES"],
    input_helps: &[
        "A sampleable stochastic language to compare.",
        "A sampleable stochastic language to compare.",
        "Number of traces to sample.",
    ],
    execute: |mut inputs, _| {
        let object1 = inputs.remove(0);
        let object2 = inputs.remove(0);
        let number_of_traces = inputs.remove(0).to_type::<usize>()?;

        let lang1 = Box::new(ebi_command_sample::get_sampled_object(
            object1,
            *number_of_traces,
        )?);
        let lang2 = Box::new(ebi_command_sample::get_sampled_object(
            object2,
            *number_of_traces,
        )?);

        let lang1: Box<dyn EbiTraitFiniteStochasticLanguage> = lang1;
        Ok(EbiOutput::Fraction(
            lang1
                .chi_square_stochastic_conformance(lang2)
                .context("Compute CSSC by sampling.")?,
        ))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_MARKOVIAN: EbiCommand = EbiCommand::Command {
    name_short: "ma",
    name_long: Some("markovian"),
    explanation_short: "Compute the conformance between two stochastic languages using a stochastic Markovian abstraction.",
    explanation_long: Some(
        "Compute the conformance between two stochastic languages using a stochastic Markovian abstraction, which represents languages based on the expected frequency of subtraces to handle partially matching traces.",
    ),
    latex_link: Some("\\cite{DBLP:journals/procsci/RochaLA25}"),
    cli_command: None,
    exact_arithmetic: true,
    input_types: &[
        &[
            &EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage),
            &EbiInputType::Object(EbiObjectType::StochasticNondeterministicFiniteAutomaton),
            &EbiInputType::Object(EbiObjectType::StochasticLabelledPetriNet),
        ],
        &[
            &EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage),
            &EbiInputType::Object(EbiObjectType::StochasticNondeterministicFiniteAutomaton),
            &EbiInputType::Object(EbiObjectType::StochasticLabelledPetriNet),
        ],
        &[&EbiInputType::Usize(Some(1), None, None)],
        &[&EbiInputTypeEnum!(DistanceMeasure)],
    ],
    input_names: &["FILE_1", "FILE_2", "K_ORDER", "MEASURE"],
    input_helps: &[
        "A finite stochastic language or a stochastic labelled Petri net (log) to compare. A model must be livelock-free and bounded.",
        "A finite stochastic language or a stochastic labelled Petri net (model) to compare. A model must be livelock-free and bounded.",
        "The order of the Markovian abstraction (length of subtraces).",
        "The stochastic conformance measure to be applied to the abstractions.",
    ],
    execute: |mut inputs, _| {
        let mut lang1 = inputs.remove(0);
        let mut lang2 = inputs.remove(0);
        let order = inputs.remove(0).to_type::<usize>()?;
        let measure = inputs.remove(0).to_type::<DistanceMeasure>()?;

        //read abstractions
        let abstraction1 = match &mut lang1 {
            EbiInput::Object(EbiObject::StochasticNondeterministicFiniteAutomaton(snfa), _) => {
                snfa.abstract_markovian(*order)
            }
            EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(slang), _) => {
                slang.abstract_markovian(*order)
            }
            EbiInput::Object(EbiObject::StochasticLabelledPetriNet(slpn), _) => {
                slpn.abstract_markovian(*order)
            }
            _ => {
                return Err(anyhow!(
                    "Unsupported object {:?} provided.",
                    lang1.get_type()
                ));
            }
        }?;
        let abstraction2 = match &mut lang2 {
            EbiInput::Object(EbiObject::StochasticNondeterministicFiniteAutomaton(snfa), _) => {
                snfa.abstract_markovian(*order)
            }
            EbiInput::Trait(EbiTraitObject::FiniteStochasticLanguage(slang), _) => {
                slang.abstract_markovian(*order)
            }
            EbiInput::Object(EbiObject::StochasticLabelledPetriNet(slpn), _) => {
                slpn.abstract_markovian(*order)
            }
            _ => {
                return Err(anyhow!(
                    "Unsupported object {:?} provided.",
                    lang2.get_type()
                ));
            }
        }?;

        let result = abstraction1.markovian_conformance(abstraction2, *measure)?;

        Ok(EbiOutput::Fraction(result))
    },
    output_type: &EbiOutputType::Fraction,
};

pub const EBI_CONFORMANCE_GAIN_PRECISION: EbiCommand = EbiCommand::Command {
    name_short: "gp",
    name_long: Some("gain-precision"),
    explanation_short: "Compute gain-based precision.",
    explanation_long: Some(
        "Compute gain-based precision for a finite stochastic language and an SDFA. If the SDFA supports less than two traces, set lambda to a small non-zero value.",
    ),
    latex_link: Some("\\cite{DBLP:journals/is/LeemansP23}"),
    cli_command: None,
    exact_arithmetic: false,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Object(
            EbiObjectType::StochasticDeterministicFiniteAutomaton,
        )],
        &[&EbiInputType::Fraction(
            Some(ConstFraction::zero()),
            Some(ConstFraction::one()),
            Some(ConstFraction::zero()),
        )],
    ],
    input_names: &["SLANG", "MODEL", "LAMBDA"],
    input_helps: &[
        "A finite stochastic language to compare.",
        "A stochastic deterministic finite automaton (SDFA) to compare.",
        "The smoothing parameter. If the SDFA supports less than two traces, set this to a small value.",
    ],
    execute: |mut inputs, _| {
        let slang = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;
        let sdfa = *inputs
            .remove(0)
            .to_type::<StochasticDeterministicFiniteAutomaton>()?;
        let lambda = inputs.remove(0).to_type::<Fraction>()?;

        let precision = sdfa
            .potential_gain_precision(&slang, &lambda)
            .context("Cannot compute gain precision.")?;

        Ok(EbiOutput::LogPolynomial(precision))
    },
    output_type: &EbiOutputType::LogPolynomial,
};

pub const EBI_CONFORMANCE_GAIN_RECALL: EbiCommand = EbiCommand::Command {
    name_short: "gr",
    name_long: Some("gain-recall"),
    explanation_short: "Compute gain-based recall.",
    explanation_long: Some(
        "Compute gain-based recall for a finite stochastic language and an SDFA. If the SDFA supports less than two traces, set lambda to a small non-zero value.",
    ),
    latex_link: Some("\\cite{DBLP:journals/is/LeemansP23}"),
    cli_command: None,
    exact_arithmetic: false,
    input_types: &[
        &[&EbiInputType::Trait(EbiTrait::FiniteStochasticLanguage)],
        &[&EbiInputType::Object(
            EbiObjectType::StochasticDeterministicFiniteAutomaton,
        )],
        &[&EbiInputType::Fraction(
            Some(ConstFraction::zero()),
            Some(ConstFraction::one()),
            Some(ConstFraction::zero()),
        )],
    ],
    input_names: &["SLANG", "MODEL", "LAMBDA"],
    input_helps: &[
        "A finite stochastic language to compare.",
        "A stochastic deterministic finite automaton (SDFA) to compare.",
        "The smoothing parameter. If the SDFA supports less than two traces, set this to a small value.",
    ],
    execute: |mut inputs, _| {
        let slang = inputs
            .remove(0)
            .to_type::<dyn EbiTraitFiniteStochasticLanguage>()?;
        let sdfa = *inputs
            .remove(0)
            .to_type::<StochasticDeterministicFiniteAutomaton>()?;
        let lambda = inputs.remove(0).to_type::<Fraction>()?;

        let recall = sdfa
            .potential_gain_recall(&slang, &lambda)
            .context("Cannot compute gain recall.")?;

        Ok(EbiOutput::LogPolynomial(recall))
    },
    output_type: &EbiOutputType::LogPolynomial,
};

#[cfg(test)]
pub mod tests {
    use crate::{
        ebi_traits::ebi_trait_finite_stochastic_language::EbiTraitFiniteStochasticLanguage,
        techniques::earth_movers_stochastic_conformance::EarthMoversStochasticConformance,
    };
    use ebi_objects::FiniteStochasticLanguage;
    use std::fs;

    #[test]
    fn emsc_empty_traces() {
        let fin1 = fs::read_to_string("testfiles/empty_trace.slang").unwrap();
        let mut object_a: Box<dyn EbiTraitFiniteStochasticLanguage> =
            Box::new(fin1.parse::<FiniteStochasticLanguage>().unwrap());

        let fin2 = fs::read_to_string("testfiles/empty_trace.slang").unwrap();
        let mut object_b: Box<dyn EbiTraitFiniteStochasticLanguage> =
            Box::new(fin2.parse::<FiniteStochasticLanguage>().unwrap());

        // Compute EMSC
        object_a
            .earth_movers_stochastic_conformance(object_b.as_mut())
            .unwrap();
    }
}