refactoring

This commit is contained in:
elvis
2025-07-26 16:46:48 +02:00
parent c8b437d9e8
commit 850d53acf8
2 changed files with 47 additions and 351 deletions

View File

@ -2,7 +2,7 @@
use petgraph::{Graph, Directed}; use petgraph::{Graph, Directed};
use std::collections::HashMap; use std::collections::HashMap;
use super::structure::{RSlabel, RSsystem, RSset, RSprocess}; use super::structure::{RSlabel, RSsystem, RSset};
use super::support_structures::TransitionsIterator; use super::support_structures::TransitionsIterator;
use super::translator::{self, IdType}; use super::translator::{self, IdType};
use std::rc::Rc; use std::rc::Rc;
@ -86,6 +86,8 @@ impl GraphMapNodesTy {
translator: Rc<translator::Translator> translator: Rc<translator::Translator>
) -> Self { ) -> Self {
use GraphMapNodes::*; use GraphMapNodes::*;
use super::format_helpers::graph_map_nodes_ty_from::*;
let function: Box<GraphMapNodesFnTy> = let function: Box<GraphMapNodesFnTy> =
// rust cant unify closures (they all have different types) so box needs // rust cant unify closures (they all have different types) so box needs
// to happen inside the match // to happen inside the match
@ -94,58 +96,19 @@ impl GraphMapNodesTy {
// is not enough // is not enough
match f { match f {
Hide => { Hide => {
Box::new( format_hide(translator)
|_, _|
String::new()
)
}, },
Entities => { Entities => {
Box::new( format_entities(translator)
move |_, node: &RSsystem|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&node.available_entities)
)
)
}, },
MaskEntities { mask } => { MaskEntities { mask } => {
Box::new( format_mask_entities(translator, mask)
move |_, node: &RSsystem| {
let masked_entities =
node.available_entities
.intersection(&mask);
format!("{}",
translator::RSsetDisplay::from(
&translator,
&masked_entities)
)
}
)
}, },
ExcludeEntities { mask } => { ExcludeEntities { mask } => {
Box::new( format_exclude_entities(translator, mask)
move |_, node: &RSsystem| {
let masked_entities =
node.available_entities
.subtraction(&mask);
format!("{}",
translator::RSsetDisplay::from(
&translator,
&masked_entities)
)
}
)
} }
Context => { Context => {
Box::new( format_context(translator)
move |_, node: &RSsystem|
format!("{}",
translator::RSprocessDisplay::from(
&translator,
&node.context_process)
)
)
}, },
}; };
GraphMapNodesTy { function } GraphMapNodesTy { function }
@ -197,6 +160,8 @@ impl GraphMapEdgesTy {
translator: Rc<translator::Translator> translator: Rc<translator::Translator>
) -> Self { ) -> Self {
use GraphMapEdges::*; use GraphMapEdges::*;
use super::format_helpers::graph_map_edges_ty_from::*;
let function: Box<GraphMapEdgesFnTy> = let function: Box<GraphMapEdgesFnTy> =
// rust cant unify closures (they all have different types) so box needs // rust cant unify closures (they all have different types) so box needs
// to happen inside the match // to happen inside the match
@ -205,182 +170,49 @@ impl GraphMapEdgesTy {
// is not enough // is not enough
match f { match f {
Hide => { Hide => {
Box::new( format_hide(translator)
|_, _|
String::new()
)
}, },
Products => { Products => {
Box::new( format_products(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.products
)
)
)
}, },
MaskProducts { mask } => { MaskProducts { mask } => {
Box::new( format_mask_products(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(&edge.products)
)
)
)
}, },
Entities => { Entities => {
Box::new( format_entities(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.available_entities
)
)
)
}, },
MaskEntities { mask } => { MaskEntities { mask } => {
Box::new( format_mask_entities(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(&edge.available_entities)
)
)
)
}, },
Context => { Context => {
Box::new( format_context(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.context
)
)
)
}, },
MaskContext { mask } => { MaskContext { mask } => {
Box::new( format_mask_context(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(&edge.context)
)
)
)
}, },
Union => { Union => {
Box::new( format_union(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.t
)
)
)
}, },
MaskUnion { mask } => { MaskUnion { mask } => {
Box::new( format_mask_union(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(&edge.t)
)
)
)
}, },
Difference => { Difference => {
Box::new( format_difference(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.context.subtraction(
&edge.available_entities
)
)
)
)
}, },
MaskDifference { mask } => { MaskDifference { mask } => {
Box::new( format_mask_difference(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(
&edge.context.subtraction(
&edge.available_entities
)
)
)
)
)
}, },
EntitiesDeleted => { EntitiesDeleted => {
Box::new( format_entities_deleted(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.available_entities.subtraction(
&edge.products
)
)
)
)
}, },
MaskEntitiesDeleted { mask } => { MaskEntitiesDeleted { mask } => {
Box::new( format_mask_entities_deleted(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(
&edge.available_entities.subtraction(
&edge.products
)
)
)
)
)
}, },
EntitiesAdded => { EntitiesAdded => {
Box::new( format_entities_added(translator)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&edge.products.subtraction(
&edge.available_entities
)
)
)
)
}, },
MaskEntitiesAdded { mask } => { MaskEntitiesAdded { mask } => {
Box::new( format_mask_entities_added(translator, mask)
move |_, edge: &RSlabel|
format!("{}",
translator::RSsetDisplay::from(
&translator,
&mask.intersection(
&edge.products.subtraction(
&edge.available_entities
)
)
)
)
)
}, },
}; };
GraphMapEdgesTy { function } GraphMapEdgesTy { function }
@ -394,9 +226,7 @@ impl GraphMapEdgesTy {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Formatting Nodes & Edges // Formatting Nodes & Edges
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
use petgraph::visit::{ IntoEdgeReferences, use petgraph::visit::{IntoEdgeReferences, IntoNodeReferences};
IntoNodeReferences,
EdgeRef };
type RSdotGraph = Graph<String, String, Directed, u32>; type RSdotGraph = Graph<String, String, Directed, u32>;
type RSformatNodeTy = type RSformatNodeTy =
@ -465,6 +295,7 @@ pub fn node_formatter_base_color(
", fillcolor=".to_string() + &base_color ", fillcolor=".to_string() + &base_color
} }
pub fn node_formatter( pub fn node_formatter(
original_graph: Rc<RSgraph>, original_graph: Rc<RSgraph>,
rule: NodeColorConditional, rule: NodeColorConditional,
@ -472,99 +303,32 @@ pub fn node_formatter(
star: Option<IdType>, star: Option<IdType>,
) -> Box<RSformatNodeTy> ) -> Box<RSformatNodeTy>
{ {
use super::format_helpers::node_formatter::*;
match rule { match rule {
NodeColorConditional::ContextConditional(ccc) => { NodeColorConditional::ContextConditional(ccc) => {
match ccc { match ccc {
ContextColorConditional::Nill => { ContextColorConditional::Nill => {
Box::new( format_nill(original_graph, color, star)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
if rssystem.context_process == RSprocess::Nill {
Some(", fillcolor=".to_string() + &color)
} else {
None
}
}
)
}, },
ContextColorConditional::RecursiveIdentifier(s) => { ContextColorConditional::RecursiveIdentifier(s) => {
Box::new( format_recursive_identifier(original_graph, color, star, s)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
match (Some(s) == star, &rssystem.context_process) {
(true, RSprocess::RecursiveIdentifier { identifier: _ }) => {
Some(", fillcolor=".to_string() + &color)
},
(false, RSprocess::RecursiveIdentifier { identifier: id }) if id == &s => {
Some(", fillcolor=".to_string() + &color)
},
_ => {None}
}
}
)
}, },
ContextColorConditional::EntitySet(ot, set) => { ContextColorConditional::EntitySet(ot, set) => {
Box::new( format_entity_set(original_graph, color, star, ot, set)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
match &rssystem.context_process {
RSprocess::EntitySet { entities, next_process: _ } if ot.evaluate(entities, &set) => {
Some(", fillcolor=".to_string() + &color)
},
_ => {None}
}
}
)
}, },
ContextColorConditional::NonDeterministicChoice => { ContextColorConditional::NonDeterministicChoice => {
Box::new( format_non_deterministic_choice(original_graph, color, star)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
if let RSprocess::NondeterministicChoice { children: _ } = rssystem.context_process {
Some(", fillcolor=".to_string() + &color)
} else {
None
}
}
)
}, },
ContextColorConditional::Summation => { ContextColorConditional::Summation => {
Box::new( format_summation(original_graph, color, star)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
if let RSprocess::Summation { children: _ } = rssystem.context_process {
Some(", fillcolor=".to_string() + &color)
} else {
None
}
}
)
}, },
ContextColorConditional::WaitEntity => { ContextColorConditional::WaitEntity => {
Box::new( format_wait_entity(original_graph, color, star)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
if let RSprocess::WaitEntity { repeat: _, repeated_process: _, next_process: _ } = &rssystem.context_process {
Some(", fillcolor=".to_string() + &color)
} else {
None
}
}
)
}, },
} }
}, },
NodeColorConditional::EntitiesConditional(ot, set) => { NodeColorConditional::EntitiesConditional(ot, set) => {
Box::new( format_entities_conditional(original_graph, color, star, ot, set)
move |_, n| {
let rssystem = original_graph.node_weight(n.0).unwrap();
if ot.evaluate(&rssystem.available_entities, &set) {
Some(", fillcolor=".to_string() + &color)
} else {
None
}
}
)
}, },
} }
} }
@ -601,108 +365,38 @@ pub fn edge_formatter_base_color(
", color=".to_string() + &base_color ", color=".to_string() + &base_color
} }
pub fn edge_formatter( pub fn edge_formatter(
original_graph: Rc<RSgraph>, original_graph: Rc<RSgraph>,
rule: EdgeColorConditional, rule: EdgeColorConditional,
color: String, color: String,
) -> Box<RSformatEdgeTy> ) -> Box<RSformatEdgeTy>
{ {
use super::format_helpers::edge_formatter::*;
match rule { match rule {
EdgeColorConditional::Entities(ot, set) => { EdgeColorConditional::Entities(ot, set) => {
Box::new( format_entities(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.available_entities, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::Context(ot, set) => { EdgeColorConditional::Context(ot, set) => {
Box::new( format_context(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.context, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::T(ot, set) => { EdgeColorConditional::T(ot, set) => {
Box::new( format_t(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.t, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::Reactants(ot, set) => { EdgeColorConditional::Reactants(ot, set) => {
Box::new( format_reactants(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.reactants, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::ReactantsAbsent(ot, set) => { EdgeColorConditional::ReactantsAbsent(ot, set) => {
Box::new( format_reactants_absent(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.reactants_absent, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::Inhibitors(ot, set) => { EdgeColorConditional::Inhibitors(ot, set) => {
Box::new( format_inhibitors(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.inhibitors, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::InhibitorsPresent(ot, set) => { EdgeColorConditional::InhibitorsPresent(ot, set) => {
Box::new( format_inhibitors_present(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.inhibitors_present, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
EdgeColorConditional::Products(ot, set) => { EdgeColorConditional::Products(ot, set) => {
Box::new( format_products(original_graph, color, ot, set)
move |_, e| {
let rssystem = original_graph.edge_weight(e.id()).unwrap();
if ot.evaluate(&rssystem.products, &set) {
Some(", color=".to_string() + &color)
} else {
None
}
}
)
}, },
} }
} }

View File

@ -14,3 +14,5 @@ pub mod rsdot;
pub mod serialize; pub mod serialize;
pub mod presets; pub mod presets;
pub mod bisimilarity; pub mod bisimilarity;
mod format_helpers;