pub struct Graph { /* private fields */ }Expand description
The static topology of a flow-based network.
A Graph defines the structure of a network: which nodes exist, how they’re connected, and what conditions govern packet flow. The graph is immutable after creation.
§Example
use netrun_sim::graph::{Graph, Node, Edge, EdgeRef, PortRef, PortType, Port, PortSlotSpec};
use std::collections::HashMap;
// Create a simple A -> B graph
let node_a = Node {
name: "A".to_string(),
in_ports: HashMap::new(),
out_ports: [("out".to_string(), Port { slots_spec: PortSlotSpec::Infinite })].into(),
in_salvo_conditions: HashMap::new(),
out_salvo_conditions: HashMap::new(),
};
let node_b = Node {
name: "B".to_string(),
in_ports: [("in".to_string(), Port { slots_spec: PortSlotSpec::Infinite })].into(),
out_ports: HashMap::new(),
in_salvo_conditions: HashMap::new(),
out_salvo_conditions: HashMap::new(),
};
let edge = (
EdgeRef {
source: PortRef { node_name: "A".to_string(), port_type: PortType::Output, port_name: "out".to_string() },
target: PortRef { node_name: "B".to_string(), port_type: PortType::Input, port_name: "in".to_string() },
},
Edge {},
);
let graph = Graph::new(vec![node_a, node_b], vec![edge]);
assert!(graph.validate().is_empty());Implementations§
Source§impl Graph
impl Graph
Sourcepub fn new(nodes: Vec<Node>, edges: Vec<(EdgeRef, Edge)>) -> Self
pub fn new(nodes: Vec<Node>, edges: Vec<(EdgeRef, Edge)>) -> Self
Creates a new Graph from a list of nodes and edges.
Builds internal indexes for efficient edge lookups by source (tail) and target (head) ports.
Sourcepub fn nodes(&self) -> &HashMap<NodeName, Node>
pub fn nodes(&self) -> &HashMap<NodeName, Node>
Returns a reference to all nodes in the graph, keyed by name.
Sourcepub fn edges(&self) -> &HashMap<EdgeRef, Edge>
pub fn edges(&self) -> &HashMap<EdgeRef, Edge>
Returns a reference to all edges in the graph, keyed by their endpoints.
Sourcepub fn get_edge_by_tail(&self, output_port_ref: &PortRef) -> Option<&EdgeRef>
pub fn get_edge_by_tail(&self, output_port_ref: &PortRef) -> Option<&EdgeRef>
Returns the edge that has the given output port as its source (tail).
Sourcepub fn get_edge_by_head(&self, input_port_ref: &PortRef) -> Option<&EdgeRef>
pub fn get_edge_by_head(&self, input_port_ref: &PortRef) -> Option<&EdgeRef>
Returns the edge that has the given input port as its target (head).
Sourcepub fn validate(&self) -> Vec<GraphValidationError>
pub fn validate(&self) -> Vec<GraphValidationError>
Validates the graph structure.
Returns a list of all validation errors found. An empty list means the graph is valid.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Graph
impl<'de> Deserialize<'de> for Graph
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Graph
impl RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl UnwindSafe for Graph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more