|
| 1 | +#![allow(clippy::too_many_arguments)] |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved. |
| 6 | + */ |
| 7 | + |
| 8 | +use deep_causality::utils_test::test_utils; |
| 9 | +use deep_causality::*; |
| 10 | + |
| 11 | +#[test] |
| 12 | +fn test_evaluate_subgraph_from_cause_with_relay_to_simple() { |
| 13 | + // Graph: Root (0) -> A (1) -> B (2) -> C (3) |
| 14 | + // A will relay to C |
| 15 | + let mut g = CausaloidGraph::new(0); |
| 16 | + |
| 17 | + let root_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 18 | + let root_index = g.add_root_causaloid(root_causaloid).unwrap(); |
| 19 | + |
| 20 | + // Causaloid A: Relays to C (index 3) with a specific effect |
| 21 | + let causaloid_a_id = 10; |
| 22 | + let causaloid_a_description = "Causaloid A relays to node 3 with Numerical(100.0)"; |
| 23 | + let causaloid_a_fn = |
| 24 | + |_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> { |
| 25 | + Ok(PropagatingEffect::RelayTo( |
| 26 | + 3, |
| 27 | + Box::new(PropagatingEffect::Deterministic(false)), |
| 28 | + )) |
| 29 | + }; |
| 30 | + let causaloid_a = Causaloid::new(causaloid_a_id, causaloid_a_fn, causaloid_a_description); |
| 31 | + let idx_a = g.add_causaloid(causaloid_a).unwrap(); |
| 32 | + |
| 33 | + // Causaloid B: Standard causaloid, should be skipped |
| 34 | + let causaloid_b = test_utils::get_test_causaloid_deterministic_input_output(); |
| 35 | + let idx_b = g.add_causaloid(causaloid_b).unwrap(); |
| 36 | + |
| 37 | + // Causaloid C: Standard causaloid, should be the final evaluated node |
| 38 | + let causaloid_c = test_utils::get_test_causaloid_deterministic_input_output(); |
| 39 | + let idx_c = g.add_causaloid(causaloid_c).unwrap(); |
| 40 | + |
| 41 | + // Link the graph: Root -> A -> B -> C |
| 42 | + g.add_edge(root_index, idx_a).unwrap(); |
| 43 | + g.add_edge(idx_a, idx_b).unwrap(); |
| 44 | + g.add_edge(idx_b, idx_c).unwrap(); |
| 45 | + |
| 46 | + g.freeze(); |
| 47 | + |
| 48 | + let initial_effect = PropagatingEffect::Deterministic(true); |
| 49 | + let res = g.evaluate_subgraph_from_cause(root_index, &initial_effect); |
| 50 | + |
| 51 | + dbg!(&res); |
| 52 | + assert!(res.is_ok()); |
| 53 | + // Expected: Root (true) -> A (Relay to C with Deterministic(false)) |
| 54 | + // C (input Deterministic(false)) -> Deterministic(true) (C just inverts the input) |
| 55 | + assert_eq!(res.unwrap(), PropagatingEffect::Deterministic(true)); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn test_evaluate_subgraph_from_cause_with_relay_to_visited_node() { |
| 60 | + // Graph: Root (0) -> A (1) -> B (2) |
| 61 | + // B will relay back to Root (0) |
| 62 | + let mut g = CausaloidGraph::new(0); |
| 63 | + |
| 64 | + let root_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 65 | + let root_index = g.add_root_causaloid(root_causaloid).unwrap(); |
| 66 | + |
| 67 | + let causaloid_a = test_utils::get_test_causaloid_deterministic_input_output(); |
| 68 | + let idx_a = g.add_causaloid(causaloid_a).unwrap(); |
| 69 | + |
| 70 | + // Causaloid B: Relays back to Root (index 0) |
| 71 | + let causaloid_b_id = 11; |
| 72 | + let causaloid_b_description = "Causaloid B relays to node 0 with Numerical(50.0)"; |
| 73 | + let causaloid_b_fn = |
| 74 | + |_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> { |
| 75 | + Ok(PropagatingEffect::RelayTo( |
| 76 | + 0, |
| 77 | + Box::new(PropagatingEffect::Numerical(50.0)), |
| 78 | + )) |
| 79 | + }; |
| 80 | + let causaloid_b = Causaloid::new(causaloid_b_id, causaloid_b_fn, causaloid_b_description); |
| 81 | + let idx_b = g.add_causaloid(causaloid_b).unwrap(); |
| 82 | + |
| 83 | + // Link the graph: Root -> A -> B |
| 84 | + g.add_edge(root_index, idx_a).unwrap(); |
| 85 | + g.add_edge(idx_a, idx_b).unwrap(); |
| 86 | + |
| 87 | + g.freeze(); |
| 88 | + |
| 89 | + let initial_effect = PropagatingEffect::Deterministic(true); |
| 90 | + let res = g.evaluate_subgraph_from_cause(root_index, &initial_effect); |
| 91 | + |
| 92 | + assert!(res.is_ok()); |
| 93 | + // Expected: Root (true) -> A (false) -> B (Relay to Root with Numerical(50.0)) |
| 94 | + // The traversal should jump back to Root, but since Root is already visited, it won't be re-added. |
| 95 | + // The last propagated effect will be the RelayTo effect from B. |
| 96 | + assert_eq!( |
| 97 | + res.unwrap(), |
| 98 | + PropagatingEffect::RelayTo(0, Box::new(PropagatingEffect::Numerical(50.0))) |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn test_evaluate_shortest_path_between_causes_with_relay_interrupt() { |
| 104 | + // Graph: Root (0) -> A (1) -> B (2) -> C (3) |
| 105 | + // A will relay, interrupting the shortest path from Root to C |
| 106 | + let mut g = CausaloidGraph::new(0); |
| 107 | + |
| 108 | + let root_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 109 | + let root_index = g.add_root_causaloid(root_causaloid).unwrap(); |
| 110 | + |
| 111 | + // Causaloid A: Relays to C (index 3) with a specific effect |
| 112 | + let causaloid_a_id = 12; |
| 113 | + let causaloid_a_description = "Causaloid A relays to node 3 with Numerical(200.0)"; |
| 114 | + let causaloid_a_fn = |
| 115 | + |_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> { |
| 116 | + Ok(PropagatingEffect::RelayTo( |
| 117 | + 3, |
| 118 | + Box::new(PropagatingEffect::Numerical(200.0)), |
| 119 | + )) |
| 120 | + }; |
| 121 | + let causaloid_a = Causaloid::new(causaloid_a_id, causaloid_a_fn, causaloid_a_description); |
| 122 | + let idx_a = g.add_causaloid(causaloid_a).unwrap(); |
| 123 | + |
| 124 | + // Causaloid B: Standard causaloid, should not be reached |
| 125 | + let causaloid_b = test_utils::get_test_causaloid_deterministic_input_output(); |
| 126 | + let idx_b = g.add_causaloid(causaloid_b).unwrap(); |
| 127 | + |
| 128 | + // Causaloid C: Standard causaloid |
| 129 | + let causaloid_c = test_utils::get_test_causaloid_deterministic_input_output(); |
| 130 | + let idx_c = g.add_causaloid(causaloid_c).unwrap(); |
| 131 | + |
| 132 | + // Link the graph: Root -> A -> B -> C |
| 133 | + g.add_edge(root_index, idx_a).unwrap(); |
| 134 | + g.add_edge(idx_a, idx_b).unwrap(); |
| 135 | + g.add_edge(idx_b, idx_c).unwrap(); |
| 136 | + |
| 137 | + g.freeze(); |
| 138 | + |
| 139 | + let initial_effect = PropagatingEffect::Deterministic(true); |
| 140 | + // Attempt to find shortest path from Root (0) to C (3) |
| 141 | + let res = g.evaluate_shortest_path_between_causes(root_index, idx_c, &initial_effect); |
| 142 | + |
| 143 | + assert!(res.is_ok()); |
| 144 | + // Expected: The RelayTo effect from A should interrupt the path and be returned. |
| 145 | + assert_eq!( |
| 146 | + res.unwrap(), |
| 147 | + PropagatingEffect::RelayTo(3, Box::new(PropagatingEffect::Numerical(200.0))) |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +#[test] |
| 152 | +fn test_evaluate_shortest_path_between_causes_with_relay_at_end() { |
| 153 | + // Graph: Root (0) -> A (1) -> B (2) |
| 154 | + // B will relay, as the last node on the shortest path |
| 155 | + let mut g = CausaloidGraph::new(0); |
| 156 | + |
| 157 | + let root_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 158 | + let root_index = g.add_root_causaloid(root_causaloid).unwrap(); |
| 159 | + |
| 160 | + let causaloid_a = test_utils::get_test_causaloid_deterministic_input_output(); |
| 161 | + let idx_a = g.add_causaloid(causaloid_a).unwrap(); |
| 162 | + |
| 163 | + // Causaloid B: Relays to Root (index 0) |
| 164 | + let causaloid_b_id = 13; |
| 165 | + let causaloid_b_description = "Causaloid B relays to node 0 with Numerical(50.0)"; |
| 166 | + let causaloid_b_fn = |
| 167 | + |_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> { |
| 168 | + Ok(PropagatingEffect::RelayTo( |
| 169 | + 0, |
| 170 | + Box::new(PropagatingEffect::Numerical(50.0)), |
| 171 | + )) |
| 172 | + }; |
| 173 | + let causaloid_b = Causaloid::new(causaloid_b_id, causaloid_b_fn, causaloid_b_description); |
| 174 | + let idx_b = g.add_causaloid(causaloid_b).unwrap(); |
| 175 | + |
| 176 | + // Link the graph: Root -> A -> B |
| 177 | + g.add_edge(root_index, idx_a).unwrap(); |
| 178 | + g.add_edge(idx_a, idx_b).unwrap(); |
| 179 | + |
| 180 | + g.freeze(); |
| 181 | + |
| 182 | + let initial_effect = PropagatingEffect::Deterministic(true); |
| 183 | + // Attempt to find shortest path from Root (0) to B (2) |
| 184 | + let res = g.evaluate_shortest_path_between_causes(root_index, idx_b, &initial_effect); |
| 185 | + |
| 186 | + assert!(res.is_ok()); |
| 187 | + // Expected: The RelayTo effect from B should be returned. |
| 188 | + assert_eq!( |
| 189 | + res.unwrap(), |
| 190 | + PropagatingEffect::RelayTo(0, Box::new(PropagatingEffect::Numerical(50.0))) |
| 191 | + ); |
| 192 | +} |
| 193 | + |
| 194 | +#[test] |
| 195 | +fn test_evaluate_subgraph_from_cause_relay_to_non_existent_node() { |
| 196 | + // Graph: Root (0) -> A (1) |
| 197 | + // A will relay to a non-existent node (e.g., index 99) |
| 198 | + let mut g = CausaloidGraph::new(0); |
| 199 | + |
| 200 | + let root_causaloid = test_utils::get_test_causaloid_deterministic_true(); |
| 201 | + let root_index = g.add_root_causaloid(root_causaloid).unwrap(); |
| 202 | + |
| 203 | + // Causaloid A: Relays to a non-existent node (index 99) |
| 204 | + let non_existent_target_index = 124; |
| 205 | + let causaloid_a_id = 14; |
| 206 | + let causaloid_a_description = format!( |
| 207 | + "Causaloid A relays to non-existent node {}", |
| 208 | + non_existent_target_index |
| 209 | + ); |
| 210 | + let causaloid_a_fn = |
| 211 | + |_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> { |
| 212 | + Ok(PropagatingEffect::RelayTo( |
| 213 | + 124, // non_existent_target_index |
| 214 | + Box::new(PropagatingEffect::Numerical(1.0)), |
| 215 | + )) |
| 216 | + }; |
| 217 | + |
| 218 | + let causaloid_a = Causaloid::new(causaloid_a_id, causaloid_a_fn, &causaloid_a_description); |
| 219 | + let idx_a = g.add_causaloid(causaloid_a).unwrap(); |
| 220 | + |
| 221 | + // Link the graph: Root -> A |
| 222 | + g.add_edge(root_index, idx_a).unwrap(); |
| 223 | + |
| 224 | + g.freeze(); |
| 225 | + |
| 226 | + let initial_effect = PropagatingEffect::Deterministic(true); |
| 227 | + let res = g.evaluate_subgraph_from_cause(root_index, &initial_effect); |
| 228 | + |
| 229 | + assert!(res.is_err()); |
| 230 | + assert_eq!( |
| 231 | + res.unwrap_err().to_string(), |
| 232 | + format!( |
| 233 | + "CausalityError: RelayTo target causaloid with index {} not found in graph.", |
| 234 | + non_existent_target_index |
| 235 | + ) |
| 236 | + ); |
| 237 | +} |
0 commit comments