From a25316fa202dbf4cc748297f45f2c27f4abf4ff1 Mon Sep 17 00:00:00 2001 From: elvis Date: Thu, 12 Jun 2025 19:16:32 +0200 Subject: [PATCH] Better AllTransitions and implemented iterator over transitions --- src/main.rs | 48 +++++++++++++-------- src/rsprocess/mod.rs | 1 + src/rsprocess/support_structures.rs | 65 +++++++++++++++++++++++++++++ src/rsprocess/transitions.rs | 51 ++++------------------ 4 files changed, 104 insertions(+), 61 deletions(-) create mode 100644 src/rsprocess/support_structures.rs diff --git a/src/main.rs b/src/main.rs index f338f99..c0cd556 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,8 +67,32 @@ fn main() -> Result<(), Box> { // println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys)); // ------------------------------------------------------------------------- // parse_ctx("[({a}.nil + {b}.nil)]",Ks) , allTransitions(sys([],[],Ks,[react([a],[c],[a]),react([b],[d],[b])]),Moves). + // let env = grammar::EnvironmentParser::new().parse("[]").unwrap(); + // let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil)]").unwrap(); + + // let sys = rsprocess::structure::RSsystem::from(*env, + // rsprocess::structure::RSset::from(vec![]), + // *process, + // vec![ + // rsprocess::structure::RSreaction::from( + // rsprocess::structure::RSset::from(vec!["a"]), + // rsprocess::structure::RSset::from(vec!["c"]), + // rsprocess::structure::RSset::from(vec!["a"]) + // ), + // rsprocess::structure::RSreaction::from( + // rsprocess::structure::RSset::from(vec!["b"]), + // rsprocess::structure::RSset::from(vec!["d"]), + // rsprocess::structure::RSset::from(vec!["b"]) + // ) + // ]); + + + // println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys)); + + // ------------------------------------------------------------------------- + // parse_ctx("[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]",Ks) , allTransitions(sys([],[],Ks,[react([a],[c],[a]),react([b],[d],[b])]),Moves). let env = grammar::EnvironmentParser::new().parse("[]").unwrap(); - let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil)]").unwrap(); + let process = grammar::ContextParser::new().parse("[({a}.nil + {b}.nil),({c}.nil + {d}.nil)]").unwrap(); let sys = rsprocess::structure::RSsystem::from(*env, rsprocess::structure::RSset::from(vec![]), @@ -86,24 +110,12 @@ fn main() -> Result<(), Box> { ) ]); + let it = rsprocess::transitions::iterator_transitions(&sys)?; - println!("all_transitions: {:?}", rsprocess::transitions::all_transitions(&sys)); + for (n, i) in it.into_iter().enumerate() { + println!("next i - {n}: {:?}", i); + println!("--------------------"); + } - // ------------------------------------------------------------------------- - // use std::rc::Rc; - // let mut a = rsprocess::structure::RSChoices::from(vec![ - // (Rc::new(rsprocess::structure::RSset::from(vec!["a"])), - // Rc::new(rsprocess::structure::RSprocess::Nill)), - // ]); - - // let b = rsprocess::structure::RSChoices::from(vec![ - // (Rc::new(rsprocess::structure::RSset::from(vec!["b"])), - // Rc::new(rsprocess::structure::RSprocess::Nill)), - // ]); - - // a.shuffle(b); - // println!("shuffle: {:?}", a); - - println!("--------------------"); Ok(()) } diff --git a/src/rsprocess/mod.rs b/src/rsprocess/mod.rs index 19a7a5a..8886a9a 100644 --- a/src/rsprocess/mod.rs +++ b/src/rsprocess/mod.rs @@ -1,3 +1,4 @@ pub mod structure; +pub mod support_structures; pub mod classical; pub mod transitions; diff --git a/src/rsprocess/support_structures.rs b/src/rsprocess/support_structures.rs new file mode 100644 index 0000000..63b475f --- /dev/null +++ b/src/rsprocess/support_structures.rs @@ -0,0 +1,65 @@ +#![allow(dead_code)] +use std::rc::Rc; +use super::structure::{RSsystem, RSlabel, RSset, RSprocess}; +use super::transitions::unfold; + +#[derive(Clone, Debug)] +pub struct TransitionsIterator<'a> { + choices_iterator: std::vec::IntoIter<(Rc>, Rc>)>, + system: &'a RSsystem<'a> +} + +impl<'a> TransitionsIterator<'a> { + pub fn from(system: &'a RSsystem<'a>) -> Result, String> { + match unfold(system.get_delta(), system.get_context_process()) { + Ok(o) => Ok( + TransitionsIterator { + choices_iterator: o.into_iter(), + system + } + ), + Err(e) => Err(e) + } + } +} + +impl<'a> Iterator for TransitionsIterator<'a> { + type Item = (RSlabel<'a>, RSsystem<'a>); + + fn next(&mut self) -> Option<(RSlabel<'a>, RSsystem<'a>)> { + let choice = self.choices_iterator.next()?; + let t = self.system.get_available_entities().union(choice.0.as_ref()); + let (reactants, reactantsi, inihibitors, ireactants, products) + = self.system.get_reaction_rules().iter() + .fold((RSset::new(), RSset::new(), RSset::new(), RSset::new(), RSset::new()), + |acc, reaction| if reaction.enabled(&t) { + (acc.0.union(reaction.reactants()), + acc.1, + acc.2.union(reaction.inihibitors()), + acc.3, + acc.4.union(reaction.products())) + } else { + (acc.0, + acc.1.union(&reaction.inihibitors().intersection(&t)), + acc.2, + acc.3.union(&reaction.reactants().subtraction(&t)), + acc.4) + } + + ); + + let label = RSlabel::from(self.system.get_available_entities().clone(), + (*choice.0).clone(), + t, + reactants, + reactantsi, + inihibitors, + ireactants, + products.clone()); + let new_system = RSsystem::from(self.system.get_delta().clone(), + products, + (*choice.1).clone(), + self.system.get_reaction_rules().clone()); + Some((label, new_system)) + } +} diff --git a/src/rsprocess/transitions.rs b/src/rsprocess/transitions.rs index df25d27..a1c9bba 100644 --- a/src/rsprocess/transitions.rs +++ b/src/rsprocess/transitions.rs @@ -2,6 +2,7 @@ use std::rc::Rc; use super::structure::{RSset, RSChoices, RSenvironment, RSprocess, RSsystem, RSlabel}; +use super::support_structures::{TransitionsIterator}; pub fn unfold<'a>(environment: &'a RSenvironment<'a>, context_process: &'a RSprocess<'a>) -> Result, String> { @@ -60,47 +61,11 @@ pub fn unfold<'a>(environment: &'a RSenvironment<'a>, context_process: &'a RSpro } } -pub fn all_transitions<'a>(system: &'a RSsystem<'a>) -> Result, RSsystem<'a>)>, String> { - let choices = unfold(system.get_delta(), system.get_context_process())?; - println!("choices: {:?}", choices); - println!("\n\n"); - let mut results = vec![]; - - for choice in choices { - let t = system.get_available_entities().union(choice.0.as_ref()); - let (reactants, reactantsi, inihibitors, ireactants, products) - = system.get_reaction_rules().iter() - .fold((RSset::new(), RSset::new(), RSset::new(), RSset::new(), RSset::new()), - |acc, reaction| if reaction.enabled(&t) { - (acc.0.union(reaction.reactants()), - acc.1, - acc.2.union(reaction.inihibitors()), - acc.3, - acc.4.union(reaction.products())) - } else { - (acc.0, - acc.1.union(&reaction.inihibitors().intersection(&t)), - acc.2, - acc.3.union(&reaction.reactants().subtraction(&t)), - acc.4) - } - - ); - - let label = RSlabel::from(system.get_available_entities().clone(), - (*choice.0).clone(), - t, - reactants, - reactantsi, - inihibitors, - ireactants, - products.clone()); - let new_system = RSsystem::from(system.get_delta().clone(), - products, - (*choice.1).clone(), - system.get_reaction_rules().clone()); - results.push((label, new_system)) - } - - Ok(results) +pub fn iterator_transitions<'a>(system: &'a RSsystem<'a>) -> Result, String> { + TransitionsIterator::from(system) +} + +pub fn all_transitions<'a>(system: &'a RSsystem<'a>) -> Result, RSsystem<'a>)>, String> { + let tr = TransitionsIterator::from(system)?; + Ok(tr.collect::>()) }