use crate::mechanics::{EventEmitter, Mechanic, Transactional};
use std::marker::PhantomData;
use super::policies::PropagationPolicy;
use super::types::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PropagationMechanic<P: PropagationPolicy> {
_marker: PhantomData<P>,
}
impl<P: PropagationPolicy> Mechanic for PropagationMechanic<P> {
type Config = PropagationGraph;
type State = PropagationState;
type Input = PropagationInput;
type Event = PropagationEvent;
type Execution = Transactional;
fn step(
config: &Self::Config,
state: &mut Self::State,
input: Self::Input,
emitter: &mut impl EventEmitter<Self::Event>,
) {
state.node_pressures.clear();
let target_nodes: std::collections::HashSet<NodeId> =
config.edges.iter().map(|edge| edge.to.clone()).collect();
for target in target_nodes {
let mut total_pressure = 0.0;
for edge in config.incoming_edges(&target) {
if let Some(&source_severity) = input.node_states.get(&edge.from) {
if source_severity > 0.0 {
let pressure = P::calculate_pressure(source_severity, edge.rate);
total_pressure += pressure;
}
}
}
if total_pressure > 0.0 {
state.node_pressures.insert(target.clone(), total_pressure);
emitter.emit(PropagationEvent::PressureCalculated {
node: target.clone(),
pressure: total_pressure,
});
let target_severity = input.node_states.get(&target).copied().unwrap_or(0.0);
if target_severity == 0.0 && P::should_trigger_infection(total_pressure) {
let initial_severity = P::calculate_initial_severity(total_pressure);
emitter.emit(PropagationEvent::InitialInfection {
node: target.clone(),
initial_severity,
});
}
}
}
}
}
pub type LinearPropagationMechanic = PropagationMechanic<super::strategies::LinearPropagation>;
#[cfg(test)]
mod tests {
use super::*;
struct VecEmitter<E> {
events: Vec<E>,
}
impl<E> EventEmitter<E> for VecEmitter<E> {
fn emit(&mut self, event: E) {
self.events.push(event);
}
}
#[test]
fn test_single_edge_propagation() {
let graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.5,
)]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 100.0);
node_states.insert("B".to_string(), 0.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
let pressure_b = state.get_pressure(&"B".to_string());
assert!((pressure_b - 0.5).abs() < 0.001);
assert_eq!(emitter.events.len(), 2);
assert!(matches!(
emitter.events[0],
PropagationEvent::PressureCalculated { ref node, pressure } if node == "B" && (pressure - 0.5).abs() < 0.001
));
assert!(matches!(
emitter.events[1],
PropagationEvent::InitialInfection { ref node, initial_severity: 20 } if node == "B"
));
}
#[test]
fn test_multiple_incoming_edges() {
let graph = PropagationGraph::new(vec![
PropagationEdge::new("A".to_string(), "C".to_string(), 0.3),
PropagationEdge::new("B".to_string(), "C".to_string(), 0.2),
]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 100.0);
node_states.insert("B".to_string(), 80.0);
node_states.insert("C".to_string(), 0.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
let pressure_c = state.get_pressure(&"C".to_string());
let expected = 0.3 * (100.0 / 100.0) + 0.2 * (80.0 / 100.0); assert!((pressure_c - expected).abs() < 0.001);
}
#[test]
fn test_no_propagation_from_clean_nodes() {
let graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.5,
)]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 0.0);
node_states.insert("B".to_string(), 0.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
assert_eq!(state.get_pressure(&"B".to_string()), 0.0);
assert_eq!(emitter.events.len(), 0);
}
#[test]
fn test_no_infection_below_threshold() {
let graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.1,
)]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 100.0);
node_states.insert("B".to_string(), 0.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
assert_eq!(emitter.events.len(), 1);
assert!(matches!(
emitter.events[0],
PropagationEvent::PressureCalculated { .. }
));
}
#[test]
fn test_already_infected_node() {
let graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.5,
)]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 100.0);
node_states.insert("B".to_string(), 50.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
assert_eq!(emitter.events.len(), 1);
assert!(matches!(
emitter.events[0],
PropagationEvent::PressureCalculated { .. }
));
}
#[test]
fn test_complex_topology() {
let graph = PropagationGraph::new(vec![
PropagationEdge::new("A".to_string(), "B".to_string(), 0.4),
PropagationEdge::new("A".to_string(), "C".to_string(), 0.3),
PropagationEdge::new("B".to_string(), "D".to_string(), 0.2),
PropagationEdge::new("C".to_string(), "D".to_string(), 0.2),
]);
let mut node_states = std::collections::HashMap::new();
node_states.insert("A".to_string(), 100.0);
node_states.insert("B".to_string(), 0.0);
node_states.insert("C".to_string(), 0.0);
node_states.insert("D".to_string(), 0.0);
let input = PropagationInput { node_states };
let mut state = PropagationState::default();
let mut emitter = VecEmitter { events: Vec::new() };
LinearPropagationMechanic::step(&graph, &mut state, input, &mut emitter);
assert!(state.get_pressure(&"B".to_string()) > 0.15);
assert!(state.get_pressure(&"C".to_string()) > 0.15);
assert_eq!(state.get_pressure(&"D".to_string()), 0.0);
}
}