Skip to content

Commit 1105f62

Browse files
committed
Added tests for adaptive reasoning.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent ea31ded commit 1105f62

File tree

2 files changed

+193
-0
lines changed

2 files changed

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

deep_causality/tests/types/causal_types/causaloid_graph/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ mod causality_graph_freeze_tests;
1212
#[cfg(test)]
1313
mod causality_graph_nodes_tests;
1414
#[cfg(test)]
15+
mod causality_graph_reasoning_adaptive_tests;
16+
#[cfg(test)]
1517
mod causality_graph_reasoning_all_tests;
1618
#[cfg(test)]
1719
mod causality_graph_reasoning_imbalanced_tests;

0 commit comments

Comments
 (0)