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
// SPDX-License-Identifier: MIT
// Copyright (c) "2023" . Marvin Hansen <marvin.hansen@gmail.com> All rights reserved.

use std::collections::HashMap;
use crate::errors::CausalityError;
use crate::prelude::{Identifiable, IdentificationValue, NumericalValue};

pub trait Causable: Identifiable
{
    fn explain(&self) -> Result<String, CausalityError>;
    fn is_active(&self) -> bool;
    fn is_singleton(&self) -> bool;

    fn verify_single_cause(
        &self,
        obs: &NumericalValue,
    )
        -> Result<bool, CausalityError>;

    fn verify_all_causes(
        &self,
        data: &[NumericalValue],
        data_index: Option<&HashMap<IdentificationValue, IdentificationValue>>,
    )
        -> Result<bool, CausalityError>;
}

pub trait CausableReasoning<T>
    where
        T: Causable,
{
    // These methods can be generated by compiler macros.
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn to_vec(&self) -> Vec<T>;
    fn get_all_items(&self) -> Vec<&T>;

    // Default implementations for all other methods are provided below.

    fn get_all_causes_true(&self)
        -> bool
    {
        for cause in self.get_all_items() {
            if !cause.is_active() {
                return false;
            }
        }

        true
    }

    fn get_all_active_causes(&self)
        -> Vec<&T>
    {
        self.get_all_items().into_iter().filter(|cause| cause.is_active()).collect()
    }

    fn get_all_inactive_causes(&self)
        -> Vec<&T>
    {
        self.get_all_items().into_iter().filter(|cause| !cause.is_active()).collect()
    }

    fn number_active(&self)
        -> NumericalValue
    {
        self.get_all_items().iter().filter(|c| c.is_active()).count() as NumericalValue
    }

    fn percent_active(&self)
        -> NumericalValue
    {
        let count = self.number_active();
        let total = self.len() as NumericalValue;
        (count / total) * (100 as NumericalValue)
    }

    fn reason_all_causes(
        &self,
        data: &[NumericalValue]
    )
        -> Result<bool, CausalityError>
    {
        if self.is_empty() {
            return Err(CausalityError("Causality collection is empty".into()));
        }

        // Emulate the data index using an enumerated iterator
        // assuming that values in the map have the same order as the data.
        for (i, cause) in self.get_all_items().iter().enumerate() {
            let valid = if cause.is_singleton()
            {
                match cause.verify_single_cause(data.get(i).expect("failed to get value")) {
                    Ok(res) => res,
                    Err(e) => return Err(e),
                }
            } else {
                match cause.verify_all_causes(data, None) {
                    Ok(res) => res,
                    Err(e) => return Err(e),
                }
            };

            if !valid {
                return Ok(false);
            }
        }

        Ok(true)
    }

    fn explain(&self)
               -> String
    {
        let mut explanation = String::new();
        for cause in self.get_all_items() {
            explanation.push('\n');
            explanation.push_str(format!(" * {}", cause.explain().unwrap()).as_str());
            explanation.push('\n');
        }
        explanation
    }
}