From c0a75e3f2e32b980ea0aa91ad20a64f43ae90728 Mon Sep 17 00:00:00 2001 From: elvis Date: Sat, 26 Jul 2025 21:22:30 +0200 Subject: [PATCH] refactoring --- src/rsprocess/graph.rs | 137 ++++++++++++++++++++++++++------------- src/rsprocess/presets.rs | 52 +++++---------- 2 files changed, 106 insertions(+), 83 deletions(-) diff --git a/src/rsprocess/graph.rs b/src/rsprocess/graph.rs index cd81bec..cd7d568 100644 --- a/src/rsprocess/graph.rs +++ b/src/rsprocess/graph.rs @@ -42,9 +42,7 @@ pub fn digraph( } -pub fn common_entities( - graph: &RSgraph -) -> RSset { +pub fn common_entities(graph: &RSgraph) -> RSset { graph.node_references().fold( None, |acc, node| @@ -73,28 +71,28 @@ pub enum GraphMapNodes { Context, } -type GraphMapNodesFnTy = - dyn Fn(petgraph::prelude::NodeIndex, &RSsystem) -> String; +type GraphMapNodesFnTy<'a> = + dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a; /// Helper structure that holds a formatting function from node as RSsystem to /// string -pub struct GraphMapNodesTy { - functions: Vec>, +pub struct GraphMapNodesTy<'a> { + functions: Vec>>, translator: Rc } -impl From<([GraphMapNodes; N], Rc)> for GraphMapNodesTy { +impl<'a, const N: usize> From<([GraphMapNodes; N], Rc)> for GraphMapNodesTy<'a> { fn from(value: ([GraphMapNodes; N], Rc)) -> Self { Self::from((value.0.to_vec(), value.1)) } } -impl From<(&[GraphMapNodes], Rc)> for GraphMapNodesTy { +impl<'a> From<(&[GraphMapNodes], Rc)> for GraphMapNodesTy<'a> { fn from(value: (&[GraphMapNodes], Rc)) -> Self { Self::from((value.0.to_vec(), value.1)) } } -impl From<(Vec, Rc)> for GraphMapNodesTy { +impl<'a> From<(Vec, Rc)> for GraphMapNodesTy<'a> { fn from(value: (Vec, Rc)) -> Self { use GraphMapNodes::*; use super::format_helpers::graph_map_nodes_ty_from::*; @@ -140,12 +138,9 @@ impl From<(Vec, Rc)> for GraphMapNodesTy } } -impl GraphMapNodesTy { - pub fn generate<'a>( - self - ) -> Box - { - let mut accumulator: Box = +impl<'a> GraphMapNodesTy<'a> { + pub fn generate(self) -> Box> { + let mut accumulator: Box> = super::format_helpers::graph_map_nodes_ty_from::format_hide( Rc::clone(&self.translator) ); @@ -184,27 +179,28 @@ pub enum GraphMapEdges { MaskEntitiesAdded { mask: RSset }, } -type GraphMapEdgesFnTy = dyn Fn(petgraph::prelude::EdgeIndex, &RSlabel) -> String; +type GraphMapEdgesFnTy<'a> = + dyn Fn(petgraph::prelude::EdgeIndex, &'a RSlabel) -> String + 'a; /// Helper structure that holds a formatting function from node as RSsystem to /// string -pub struct GraphMapEdgesTy { - functions: Vec>, +pub struct GraphMapEdgesTy<'a> { + functions: Vec>>, translator: Rc } -impl From<([GraphMapEdges; N], Rc)> for GraphMapEdgesTy { +impl<'a, const N: usize> From<([GraphMapEdges; N], Rc)> for GraphMapEdgesTy<'a> { fn from(value: ([GraphMapEdges; N], Rc)) -> Self { Self::from((value.0.to_vec(), value.1)) } } -impl From<(&[GraphMapEdges], Rc)> for GraphMapEdgesTy { +impl<'a> From<(&[GraphMapEdges], Rc)> for GraphMapEdgesTy<'a> { fn from(value: (&[GraphMapEdges], Rc)) -> Self { Self::from((value.0.to_vec(), value.1)) } } -impl From<(Vec, Rc)> for GraphMapEdgesTy { +impl<'a> From<(Vec, Rc)> for GraphMapEdgesTy<'a> { fn from(value: (Vec, Rc)) -> Self { use GraphMapEdges::*; use super::format_helpers::graph_map_edges_ty_from::*; @@ -292,11 +288,9 @@ impl From<(Vec, Rc)> for GraphMapEdgesTy } } -impl GraphMapEdgesTy { - pub fn generate( - self - ) -> Box { - let mut accumulator: Box = +impl<'a> GraphMapEdgesTy<'a> { + pub fn generate(self) -> Box> { + let mut accumulator: Box> = super::format_helpers::graph_map_edges_ty_from::format_hide( Rc::clone(&self.translator) ); @@ -317,11 +311,16 @@ impl GraphMapEdgesTy { use petgraph::visit::{IntoEdgeReferences, IntoNodeReferences}; type RSdotGraph = Graph; -type RSformatNodeTy = +type RSformatNodeTy<'a> = dyn Fn( - &RSdotGraph, - <&RSdotGraph as IntoNodeReferences>::NodeRef - ) -> Option; + &'a RSdotGraph, + <&'a RSdotGraph as IntoNodeReferences>::NodeRef + ) -> String + 'a; +type RSformatNodeTyOpt<'a> = + dyn Fn( + &'a RSdotGraph, + <&'a RSdotGraph as IntoNodeReferences>::NodeRef + ) -> Option + 'a; #[derive(Clone, Copy)] pub enum OperationType { @@ -373,9 +372,10 @@ pub enum NodeColorConditional { #[derive(Clone)] pub struct NodeColor { pub conditionals: Vec<(NodeColorConditional, String)>, - pub base_color: String + pub base_color: String, } +#[inline(always)] pub fn node_formatter_base_color( base_color: String ) -> String @@ -383,44 +383,89 @@ pub fn node_formatter_base_color( ", fillcolor=".to_string() + &base_color } - -pub fn node_formatter( +#[inline(always)] +fn match_node_color_conditional<'a>( + rule: &NodeColorConditional, + color: &String, original_graph: Rc, - rule: NodeColorConditional, - color: String, - star: Option, -) -> Box -{ + star: Option +) -> Box> { use super::format_helpers::node_formatter::*; match rule { NodeColorConditional::ContextConditional(ccc) => { match ccc { ContextColorConditional::Nill => { - format_nill(original_graph, color, star) + format_nill(Rc::clone(&original_graph), + color.to_string(), + star) }, ContextColorConditional::RecursiveIdentifier(s) => { - format_recursive_identifier(original_graph, color, star, s) + format_recursive_identifier(Rc::clone(&original_graph), + color.to_string(), + star, + *s) }, ContextColorConditional::EntitySet(ot, set) => { - format_entity_set(original_graph, color, star, ot, set) + format_entity_set(Rc::clone(&original_graph), + color.to_string(), + star, + *ot, + set.clone()) }, ContextColorConditional::NonDeterministicChoice => { - format_non_deterministic_choice(original_graph, color, star) + format_non_deterministic_choice(Rc::clone(&original_graph), + color.to_string(), + star) }, ContextColorConditional::Summation => { - format_summation(original_graph, color, star) + format_summation(Rc::clone(&original_graph), + color.to_string(), + star) }, ContextColorConditional::WaitEntity => { - format_wait_entity(original_graph, color, star) + format_wait_entity(Rc::clone(&original_graph), + color.to_string(), + star) }, } }, NodeColorConditional::EntitiesConditional(ot, set) => { - format_entities_conditional(original_graph, color, star, ot, set) + format_entities_conditional(Rc::clone(&original_graph), + color.to_string(), + star, + *ot, + set.clone()) }, } } +impl NodeColor { + pub fn generate<'a>( + self, + original_graph: Rc, + star: Option + ) -> Box> { + Box::new( + move |i, n| { + for (rule, color) in &self.conditionals { + let f = match_node_color_conditional( + rule, + color, + Rc::clone(&original_graph), + star + ); + + if let Some(s) = (f)(i, n) { + return s + } + } + node_formatter_base_color(self.base_color.clone()) + } + ) + } +} + + type RSformatEdgeTy = dyn Fn( &RSdotGraph, diff --git a/src/rsprocess/presets.rs b/src/rsprocess/presets.rs index 80b2887..fb09ccb 100644 --- a/src/rsprocess/presets.rs +++ b/src/rsprocess/presets.rs @@ -556,16 +556,21 @@ pub fn bisimilar( } } + + + + // ----------------------------------------------------------------------------- // Output Functions // ----------------------------------------------------------------------------- -#[allow(clippy::type_complexity)] +type GraphMapNodesFnTy<'a> = + dyn Fn(petgraph::prelude::NodeIndex, &'a RSsystem) -> String + 'a; fn generate_node_printing_fn<'a>( node_display: &[NodeDisplay], graph: &graph::RSgraph, translator: Rc, -) -> Box String + 'a> { +) -> Box> { // The type cannot be aliased since rust doesnt like generics. // We are iterating over the node_display and constructing a function // (accumulator) that prints out our formatted nodes. So at each step we @@ -601,11 +606,13 @@ fn generate_node_printing_fn<'a>( gmnt.generate() } -#[allow(clippy::type_complexity)] + +type GraphMapEdgesFnTy<'a> = + dyn Fn(petgraph::prelude::EdgeIndex, &'a RSlabel) -> String + 'a; fn generate_edge_printing_fn<'a>( edge_display: &[EdgeDisplay], translator: Rc, -) -> Box String + 'a> { +) -> Box> { // The type cannot be aliased since rust doesnt like generics. // We are iterating over the edge_display and constructing a function // (accumulator) that prints out our formatted nodes. So at each step we @@ -630,34 +637,6 @@ fn generate_edge_printing_fn<'a>( gmet.generate() } -use petgraph::visit::IntoNodeReferences; -#[allow(clippy::type_complexity)] -fn generate_node_color_fn<'a>( - node_color: &'a graph::NodeColor, - original_graph: Rc, - translator: Rc, -) -> Box, - <&Graph as IntoNodeReferences>::NodeRef -) -> String + 'a> -{ - Box::new( - move |i, n| { - let cloned_node_color = node_color.clone(); - for (rule, color) in cloned_node_color.conditionals { - if let Some(s) = graph::node_formatter( - original_graph.clone(), - rule, - color, - translator.encode_not_mut("*") - )(i, n) { - return s - } - } - graph::node_formatter_base_color(node_color.base_color.to_string()) - } - ) -} #[allow(clippy::type_complexity)] fn generate_edge_color_fn<'a>( @@ -691,7 +670,7 @@ pub fn dot( system: &EvaluatedSystem, node_display: Vec, edge_display: Vec, - node_color: &graph::NodeColor, + node_color: graph::NodeColor, edge_color: &graph::EdgeColor ) -> Result { match system { @@ -715,9 +694,8 @@ pub fn dot( let graph = Rc::new(graph.to_owned()); let node_formatter = - generate_node_color_fn(node_color, - Rc::clone(&graph), - rc_translator); + node_color.generate(Rc::clone(&graph), + translator.encode_not_mut("*")); let edge_formatter = generate_edge_color_fn(edge_color, graph); @@ -878,7 +856,7 @@ fn execute( edge_color: ec, so, } => { - save_options!(dot(system, nd, ed, &nc, &ec)?, so); + save_options!(dot(system, nd, ed, nc, &ec)?, so); } GraphSaveOptions::GraphML { node_display: nd,