From 1b9c0ce44b4d07644c1b8e94fa047f46215b74a4 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 9 Jul 2025 16:12:22 +0200 Subject: [PATCH] Refactoring names of structures, removing useless functions --- src/examples.rs | 8 +-- src/rsprocess/classical.rs | 2 +- src/rsprocess/graph.rs | 8 +-- src/rsprocess/perpetual.rs | 24 ++++----- src/rsprocess/statistics.rs | 30 +++++------ src/rsprocess/structure.rs | 84 +++++++++-------------------- src/rsprocess/support_structures.rs | 22 ++++---- src/rsprocess/transitions.rs | 10 ++-- src/rsprocess/translator.rs | 18 +++---- testing/first.system | 4 +- 10 files changed, 90 insertions(+), 120 deletions(-) diff --git a/src/examples.rs b/src/examples.rs index 5066385..a0dcedf 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -188,8 +188,8 @@ pub fn limit_freq() -> std::io::Result<()> { let (_, sets) = read_file(&mut translator, path, parser_experiment)?; let res = match frequency::limit_frequency(&sets, - system.get_reaction_rules(), - system.get_available_entities()) { + &system.reaction_rules, + &system.available_entities) { Some(e) => e, None => {return Ok(());} }; @@ -216,8 +216,8 @@ pub fn fast_freq() -> std::io::Result<()> { let (weights, sets) = read_file(&mut translator, path, parser_experiment)?; let res = match frequency::fast_frequency(&sets, - system.get_reaction_rules(), - system.get_available_entities(), + &system.reaction_rules, + &system.available_entities, &weights) { Some(e) => e, None => {return Ok(());} diff --git a/src/rsprocess/classical.rs b/src/rsprocess/classical.rs index f8ac38d..5729e02 100644 --- a/src/rsprocess/classical.rs +++ b/src/rsprocess/classical.rs @@ -16,7 +16,7 @@ pub fn compute_step<'a>( reaction: &'a RSreaction ) -> Option<&'a RSset> { if reaction.enabled(current_state) { - Some(reaction.products()) + Some(&reaction.products) } else { None } diff --git a/src/rsprocess/graph.rs b/src/rsprocess/graph.rs index d49269d..83eed44 100644 --- a/src/rsprocess/graph.rs +++ b/src/rsprocess/graph.rs @@ -86,7 +86,7 @@ impl GraphMapNodesTy { format!("{}", translator::RSsetDisplay::from( &translator, - node.get_available_entities()) + &node.available_entities) ) ) }, @@ -94,7 +94,7 @@ impl GraphMapNodesTy { Box::new( move |_, node: &RSsystem| { let masked_entities = - node.get_available_entities() + node.available_entities .intersection(&mask); format!("{}", translator::RSsetDisplay::from( @@ -110,7 +110,7 @@ impl GraphMapNodesTy { format!("{}", translator::RSprocessDisplay::from( &translator, - node.get_context_process()) + &node.context_process) ) ) }, @@ -373,7 +373,7 @@ pub fn default_node_formatter( Box::new( move |_g, n| String::from( - match original_graph.node_weight(n.0).unwrap().get_context_process() + match original_graph.node_weight(n.0).unwrap().context_process { RSprocess::Nill => ", fillcolor=white", diff --git a/src/rsprocess/perpetual.rs b/src/rsprocess/perpetual.rs index f09d3d9..72f1cef 100644 --- a/src/rsprocess/perpetual.rs +++ b/src/rsprocess/perpetual.rs @@ -115,9 +115,9 @@ pub fn lollipops_decomposed( // see lollipop pub fn lollipops(system: RSsystem) -> Vec<(Vec, Vec)> { lollipops_decomposed( - system.get_delta(), - system.get_reaction_rules(), - system.get_available_entities(), + &system.delta, + &system.reaction_rules, + &system.available_entities, ) } @@ -125,12 +125,12 @@ pub fn lollipops(system: RSsystem) -> Vec<(Vec, Vec)> { pub fn lollipops_only_loop(system: RSsystem) -> Vec> { // FIXME: i think we are only interested in "x", not all symbols that // satisfy X = pre(Q, rec(X)) - let filtered = system.get_delta().iter().filter_map(filter_delta); + let filtered = system.delta.iter().filter_map(filter_delta); let find_loop_fn = |q| { find_only_loop( - system.get_reaction_rules(), - system.get_available_entities().clone(), + &system.reaction_rules, + system.available_entities.clone(), q, ) }; @@ -226,9 +226,9 @@ pub fn lollipops_named( symb: IdType ) -> Option<(Vec, Vec)> { lollipops_decomposed_named( - system.get_delta(), - system.get_reaction_rules(), - system.get_available_entities(), + &system.delta, + &system.reaction_rules, + &system.available_entities, symb, ) } @@ -239,15 +239,15 @@ pub fn lollipops_only_loop_named( symb: IdType ) -> Option> { let filtered = system - .get_delta() + .delta .iter() .filter_map(|x| filter_delta_named(x, &symb)) .next(); let find_loop_fn = |q| { find_only_loop( - system.get_reaction_rules(), - system.get_available_entities().clone(), + &system.reaction_rules, + system.available_entities.clone(), q, ) }; diff --git a/src/rsprocess/statistics.rs b/src/rsprocess/statistics.rs index 26877cc..b5a4cfe 100644 --- a/src/rsprocess/statistics.rs +++ b/src/rsprocess/statistics.rs @@ -13,17 +13,17 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri ); result.push_str(&format!( "the initial state has {} entities:\n", - system.get_available_entities().len() + system.available_entities.len() )); result.push_str(&format!( "{}\n", - translator::RSsetDisplay::from(translator, system.get_available_entities()) + translator::RSsetDisplay::from(translator, &system.available_entities) )); let reactants = system - .get_reaction_rules() + .reaction_rules .iter() - .fold(RSset::new(), |acc, new| acc.union(new.reactants())); + .fold(RSset::new(), |acc, new| acc.union(&new.reactants)); result.push_str(&format!( "The reactants are {}:\n{}\n", reactants.len(), @@ -31,9 +31,9 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri )); let inhibitors = system - .get_reaction_rules() + .reaction_rules .iter() - .fold(RSset::new(), |acc, new| acc.union(new.inihibitors())); + .fold(RSset::new(), |acc, new| acc.union(&new.inihibitors)); result.push_str(&format!( "The inhibitors are {}:\n{}\n", inhibitors.len(), @@ -41,9 +41,9 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri )); let products = system - .get_reaction_rules() + .reaction_rules .iter() - .fold(RSset::new(), |acc, new| acc.union(new.products())); + .fold(RSset::new(), |acc, new| acc.union(&new.products)); result.push_str(&format!( "The products are {}:\n{}\n", products.len(), @@ -57,14 +57,14 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri translator::RSsetDisplay::from(translator, &total) )); - let entities_env = system.get_delta().all_elements(); + let entities_env = system.delta.all_elements(); result.push_str(&format!( "The environment involves {} entities:\n{}\n", entities_env.len(), translator::RSsetDisplay::from(translator, &entities_env) )); - let entities_context = system.get_context_process().all_elements(); + let entities_context = system.context_process.all_elements(); result.push_str(&format!( "The context involves {} entities:\n{}\n", entities_context.len(), @@ -74,7 +74,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri let entities_all = total .union(&entities_env) .union(&entities_context) - .union(system.get_available_entities()); + .union(&system.available_entities); result.push_str(&format!( "The whole RS involves {} entities:\n{}\n", @@ -83,7 +83,7 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri )); let possible_e = products - .union(system.get_available_entities()) + .union(&system.available_entities) .union(&entities_context); let missing_e = reactants.subtraction(&possible_e); result.push_str(&format!( @@ -101,14 +101,14 @@ pub fn of_RSsystem<'a>(translator: &'a Translator, system: &'a RSsystem) -> Stri result.push_str(&format!( "There are {} reactions in total.\n", - system.get_reaction_rules().len() + system.reaction_rules.len() )); let mut admissible_reactions = vec![]; let mut nonadmissible_reactions = vec![]; - for reaction in system.get_reaction_rules().iter() { - if reaction.reactants().is_disjoint(&missing_e) { + for reaction in system.reaction_rules.iter() { + if reaction.reactants.is_disjoint(&missing_e) { admissible_reactions.push(reaction); } else { nonadmissible_reactions.push(reaction); diff --git a/src/rsprocess/structure.rs b/src/rsprocess/structure.rs index 19566c4..8014c25 100644 --- a/src/rsprocess/structure.rs +++ b/src/rsprocess/structure.rs @@ -10,7 +10,7 @@ use std::rc::Rc; // ----------------------------------------------------------------------------- #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct RSset { - identifiers: BTreeSet, + pub identifiers: BTreeSet, } impl From<[IdType; N]> for RSset { @@ -89,10 +89,6 @@ impl RSset { RSset { identifiers: res } } - pub fn set(&self) -> &BTreeSet { - &self.identifiers - } - pub fn iter(&self) -> std::collections::btree_set::Iter<'_, IdType> { self.identifiers.iter() } @@ -134,9 +130,9 @@ impl IntoIterator for RSset { // ----------------------------------------------------------------------------- #[derive(Clone, Debug)] pub struct RSreaction { - reactants: RSset, - inihibitors: RSset, - products: RSset, + pub reactants: RSset, + pub inihibitors: RSset, + pub products: RSset, } impl RSreaction { @@ -161,22 +157,6 @@ impl RSreaction { self.reactants.is_subset(current_state) && self.inihibitors.is_disjoint(current_state) } - - pub fn products_clone(&self) -> RSset { - self.products.clone() - } - - pub fn reactants(&self) -> &RSset { - &self.reactants - } - - pub fn inihibitors(&self) -> &RSset { - &self.inihibitors - } - - pub fn products(&self) -> &RSset { - &self.products - } } impl Default for RSreaction { @@ -434,10 +414,10 @@ impl From> for RSenvironment { // ----------------------------------------------------------------------------- #[derive(Clone, Debug)] pub struct RSsystem { - delta: Rc, - available_entities: RSset, - context_process: RSprocess, - reaction_rules: Rc>, + pub delta: Rc, + pub available_entities: RSset, + pub context_process: RSprocess, + pub reaction_rules: Rc>, } impl RSsystem { @@ -463,24 +443,10 @@ impl RSsystem { reaction_rules: Rc::clone(&reaction_rules), } } - - pub fn get_delta(&self) -> &Rc { - &self.delta - } - - pub fn get_available_entities(&self) -> &RSset { - &self.available_entities - } - - pub fn get_context_process(&self) -> &RSprocess { - &self.context_process - } - - pub fn get_reaction_rules(&self) -> &Rc> { - &self.reaction_rules - } } +/// Equality does not care about delta or reaction rules, only entities and +/// context is compared impl PartialEq for RSsystem { // we ignore delta and reaction rules fn eq(&self, other: &RSsystem) -> bool { @@ -489,8 +455,12 @@ impl PartialEq for RSsystem { } } +/// Equality does not care about delta or reaction rules, only entities and +/// context is compared impl Eq for RSsystem {} +/// Hash does not care about delta or reaction rules, only entities and +/// context is hashed impl Hash for RSsystem { // ignores delta and reaction rules fn hash(&self, state: &mut H) { @@ -513,11 +483,10 @@ pub struct RSlabel { pub available_entities: RSset, pub context: RSset, pub t: RSset, - /// union of available_entities and context pub reactants: RSset, - pub reactantsi: RSset, // reactants absent + pub reactants_absent: RSset, pub inihibitors: RSset, - pub ireactants: RSset, // inhibitors present + pub inihibitors_present: RSset, pub products: RSset, } @@ -528,9 +497,9 @@ impl RSlabel { context: RSset::new(), t: RSset::new(), reactants: RSset::new(), - reactantsi: RSset::new(), + reactants_absent: RSset::new(), inihibitors: RSset::new(), - ireactants: RSset::new(), + inihibitors_present: RSset::new(), products: RSset::new(), } } @@ -541,9 +510,9 @@ impl RSlabel { context: RSset, t: RSset, reactants: RSset, - reactantsi: RSset, + reactants_absent: RSset, inihibitors: RSset, - ireactants: RSset, + inihibitors_present: RSset, products: RSset, ) -> Self { RSlabel { @@ -551,19 +520,18 @@ impl RSlabel { context, t, reactants, - reactantsi, + reactants_absent, inihibitors, - ireactants, + inihibitors_present, products, } } - pub fn get_context(&self) -> (RSset, RSset, RSset) { - // TODO remove clone? + pub fn get_context(&self) -> (&RSset, &RSset, &RSset) { ( - self.available_entities.clone(), - self.context.clone(), - self.t.clone(), + &self.available_entities, + &self.context, + &self.t, ) } } diff --git a/src/rsprocess/support_structures.rs b/src/rsprocess/support_structures.rs index ed0dd75..6b2319c 100644 --- a/src/rsprocess/support_structures.rs +++ b/src/rsprocess/support_structures.rs @@ -13,7 +13,7 @@ impl<'a> TransitionsIterator<'a> { pub fn from( system: &'a RSsystem ) -> Result, String> { - match unfold(system.get_delta(), system.get_context_process()) { + match unfold(&system.delta, &system.context_process) { Ok(o) => Ok(TransitionsIterator { choices_iterator: o.into_iter(), system, @@ -28,9 +28,9 @@ impl<'a> Iterator for TransitionsIterator<'a> { fn next(&mut self) -> Option<(RSlabel, RSsystem)> { let (c, k) = self.choices_iterator.next()?; - let t = self.system.get_available_entities().union(c.as_ref()); + let t = self.system.available_entities.union(c.as_ref()); let (reactants, reactantsi, inihibitors, ireactants, products) = - self.system.get_reaction_rules().iter().fold( + self.system.reaction_rules.iter().fold( ( RSset::new(), // reactants RSset::new(), // reactantsi @@ -41,18 +41,18 @@ impl<'a> Iterator for TransitionsIterator<'a> { |acc, reaction| { if reaction.enabled(&t) { ( - acc.0.union(reaction.reactants()), + acc.0.union(&reaction.reactants), acc.1, - acc.2.union(reaction.inihibitors()), + acc.2.union(&reaction.inihibitors), acc.3, - acc.4.union(reaction.products()), + acc.4.union(&reaction.products), ) } else { ( acc.0, - acc.1.union(&reaction.inihibitors().intersection(&t)), + acc.1.union(&reaction.inihibitors.intersection(&t)), acc.2, - acc.3.union(&reaction.reactants().subtraction(&t)), + acc.3.union(&reaction.reactants.subtraction(&t)), acc.4, ) } @@ -60,7 +60,7 @@ impl<'a> Iterator for TransitionsIterator<'a> { ); let label = RSlabel::from( - self.system.get_available_entities().clone(), + self.system.available_entities.clone(), (*c).clone(), t, reactants, @@ -70,10 +70,10 @@ impl<'a> Iterator for TransitionsIterator<'a> { products.clone(), ); let new_system = RSsystem::from( - Rc::clone(self.system.get_delta()), + Rc::clone(&self.system.delta), products, (*k).clone(), - Rc::clone(self.system.get_reaction_rules()), + Rc::clone(&self.system.reaction_rules), ); Some((label, new_system)) } diff --git a/src/rsprocess/transitions.rs b/src/rsprocess/transitions.rs index ec3d9d2..8d911eb 100644 --- a/src/rsprocess/transitions.rs +++ b/src/rsprocess/transitions.rs @@ -115,7 +115,7 @@ pub fn target( ) -> Result<(i64, RSset), String> { let current = one_transition(system)?; if current.is_none() { - return Ok((0, system.get_available_entities().clone())); + return Ok((0, system.available_entities.clone())); } let mut n = 1; let mut current = current.unwrap().1; @@ -123,7 +123,7 @@ pub fn target( current = next; n += 1; } - Ok((n, current.get_available_entities().clone())) + Ok((n, current.available_entities.clone())) } // see oneRun, run, smartOneRunEK, smartRunEK @@ -145,11 +145,13 @@ pub fn run_separated( return Ok(res); } let current = current.unwrap(); - res.push(current.0.get_context()); + let (available_entities, context, t) = current.0.get_context(); + res.push((available_entities.clone(), context.clone(), t.clone())); let mut current = current.1; while let Some((label, next)) = one_transition(¤t)? { current = next; - res.push(label.get_context()); + let (available_entities, context, t) = label.get_context(); + res.push((available_entities.clone(), context.clone(), t.clone())); } Ok(res) } diff --git a/src/rsprocess/translator.rs b/src/rsprocess/translator.rs index 16da0d2..de2c1cd 100644 --- a/src/rsprocess/translator.rs +++ b/src/rsprocess/translator.rs @@ -121,9 +121,9 @@ fn print_reaction( write!( f, "(r: {}, i: {}, p: {})", - RSsetDisplay::from(translator, reaction.reactants()), - RSsetDisplay::from(translator, reaction.inihibitors()), - RSsetDisplay::from(translator, reaction.products()) + RSsetDisplay::from(translator, &reaction.reactants), + RSsetDisplay::from(translator, &reaction.inihibitors), + RSsetDisplay::from(translator, &reaction.products) ) } @@ -294,11 +294,11 @@ fn print_system( write!( f, "[delta: {}, available_entities: {}, context_process: {}, reaction_rules: [", - RSenvironmentDisplay::from(translator, system.get_delta()), - RSsetDisplay::from(translator, system.get_available_entities()), - RSprocessDisplay::from(translator, system.get_context_process()) + RSenvironmentDisplay::from(translator, &system.delta), + RSsetDisplay::from(translator, &system.available_entities), + RSprocessDisplay::from(translator, &system.context_process) )?; - let mut it = system.get_reaction_rules().iter().peekable(); + let mut it = system.reaction_rules.iter().peekable(); while let Some(el) = it.next() { if it.peek().is_none() { write!(f, "{}", RSreactionDisplay::from(translator, el))?; @@ -327,9 +327,9 @@ fn print_label( RSsetDisplay::from(translator, &label.context), RSsetDisplay::from(translator, &label.t), RSsetDisplay::from(translator, &label.reactants), - RSsetDisplay::from(translator, &label.reactantsi), + RSsetDisplay::from(translator, &label.reactants_absent), RSsetDisplay::from(translator, &label.inihibitors), - RSsetDisplay::from(translator, &label.ireactants), + RSsetDisplay::from(translator, &label.inihibitors_present), RSsetDisplay::from(translator, &label.products), ) } diff --git a/testing/first.system b/testing/first.system index aadc18c..c9a79b1 100644 --- a/testing/first.system +++ b/testing/first.system @@ -1,4 +1,4 @@ -Environment: [x = {a}.y, y =({a}.x + {b}.y)] +Environment: [x = {a}.😵, 😵 =({a}.x + {b}.😵)] Initial Entities: {a, b} Context: [({a,b}.{a}.{a,c}.x + {a,b}.{a}.{a}.nill)] -Reactions: ([r: {a,b}, i: {c}, p: {b}]) +Reactions: ([{a,b}, {c}, {b}])