use crate::mechanics::propagation::{PropagationEdge, PropagationGraph};
use super::types::{NodeType, SpatialEdge, SpatialGraph, SpatialNode};
pub fn propagation_to_spatial(propagation: &PropagationGraph) -> SpatialGraph {
let mut spatial = SpatialGraph::new();
let node_ids = propagation.all_nodes();
for node_id in node_ids {
spatial.add_node(SpatialNode::new(node_id, NodeType::Custom));
}
for prop_edge in &propagation.edges {
let cost = if prop_edge.rate > 0.0 {
1.0 / prop_edge.rate
} else {
f32::MAX };
spatial.add_edge(SpatialEdge::new(
prop_edge.from.clone(),
prop_edge.to.clone(),
cost,
));
}
spatial
}
pub fn spatial_to_propagation(spatial: &SpatialGraph) -> PropagationGraph {
let mut edges = Vec::new();
for spatial_edge in &spatial.edges {
let rate = if spatial_edge.cost > 0.0 {
1.0 / spatial_edge.cost
} else {
1.0 };
edges.push(PropagationEdge::new(
spatial_edge.from.clone(),
spatial_edge.to.clone(),
rate,
));
if spatial_edge.bidirectional {
edges.push(PropagationEdge::new(
spatial_edge.to.clone(),
spatial_edge.from.clone(),
rate,
));
}
}
PropagationGraph::new(edges)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_propagation_to_spatial_basic() {
let prop_graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.5,
)]);
let spatial = propagation_to_spatial(&prop_graph);
assert_eq!(spatial.nodes.len(), 2);
assert!(spatial.has_node(&"A".to_string()));
assert!(spatial.has_node(&"B".to_string()));
assert_eq!(spatial.edges.len(), 1);
assert_eq!(spatial.edges[0].cost, 2.0); }
#[test]
fn test_spatial_to_propagation_basic() {
let mut spatial = SpatialGraph::new();
spatial.add_node(SpatialNode::new("A", NodeType::City));
spatial.add_node(SpatialNode::new("B", NodeType::City));
spatial.add_edge(SpatialEdge::new("A", "B", 2.0));
let prop_graph = spatial_to_propagation(&spatial);
assert_eq!(prop_graph.edges.len(), 1);
assert_eq!(prop_graph.edges[0].rate, 0.5); }
#[test]
fn test_bidirectional_conversion() {
let mut spatial = SpatialGraph::new();
spatial.add_node(SpatialNode::new("A", NodeType::City));
spatial.add_node(SpatialNode::new("B", NodeType::City));
spatial.add_edge(SpatialEdge::new_bidirectional("A", "B", 1.0));
let prop_graph = spatial_to_propagation(&spatial);
assert_eq!(prop_graph.edges.len(), 2);
let has_ab = prop_graph
.edges
.iter()
.any(|e| e.from == "A" && e.to == "B");
let has_ba = prop_graph
.edges
.iter()
.any(|e| e.from == "B" && e.to == "A");
assert!(has_ab);
assert!(has_ba);
}
#[test]
fn test_roundtrip_preserves_topology() {
let prop_graph = PropagationGraph::new(vec![
PropagationEdge::new("A".to_string(), "B".to_string(), 0.5),
PropagationEdge::new("B".to_string(), "C".to_string(), 0.25),
]);
let spatial = propagation_to_spatial(&prop_graph);
let prop_graph2 = spatial_to_propagation(&spatial);
assert_eq!(prop_graph.edges.len(), prop_graph2.edges.len());
for original_edge in &prop_graph.edges {
let found = prop_graph2.edges.iter().any(|e| {
e.from == original_edge.from
&& e.to == original_edge.to
&& (e.rate - original_edge.rate).abs() < 0.001
});
assert!(found, "Edge {:?} not found after roundtrip", original_edge);
}
}
#[test]
fn test_high_rate_low_cost() {
let prop_graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
1.0,
)]);
let spatial = propagation_to_spatial(&prop_graph);
assert_eq!(spatial.edges[0].cost, 1.0); }
#[test]
fn test_zero_rate_max_cost() {
let prop_graph = PropagationGraph::new(vec![PropagationEdge::new(
"A".to_string(),
"B".to_string(),
0.0,
)]);
let spatial = propagation_to_spatial(&prop_graph);
assert_eq!(spatial.edges[0].cost, f32::MAX); }
}