Files
ReactionSystems/src/rsprocess/transitions.rs

89 lines
2.7 KiB
Rust
Raw Normal View History

//! Module for helper structure for simulation
2025-07-10 15:02:14 +02:00
use super::structure::{RSlabel, RSprocess, RSset, RSsystem};
2025-05-21 00:03:36 +02:00
use std::rc::Rc;
2025-06-12 16:23:39 +02:00
#[derive(Clone, Debug)]
pub struct TransitionsIterator<'a> {
choices_iterator: std::vec::IntoIter<(Rc<RSset>, Rc<RSprocess>)>,
system: &'a RSsystem,
2025-06-16 14:46:04 +02:00
}
2025-06-16 16:35:54 +02:00
impl<'a> TransitionsIterator<'a> {
pub fn from(
system: &'a RSsystem
) -> Result<TransitionsIterator<'a>, String> {
match system.delta.unfold(&system.context_process,
&system.available_entities) {
Ok(o) => Ok(TransitionsIterator {
choices_iterator: o.into_iter(),
system,
}),
Err(e) => Err(e),
}
2025-08-22 01:40:15 +02:00
}
2025-08-21 21:43:54 +02:00
}
impl<'a> Iterator for TransitionsIterator<'a> {
type Item = (RSlabel, RSsystem);
/// Creates the next arc from the current system.
fn next(&mut self) -> Option<(RSlabel, RSsystem)> {
let (c, k) = self.choices_iterator.next()?;
let t = self.system.available_entities.union(c.as_ref());
let (
reactants,
reactants_absent,
inhibitors,
inhibitors_present,
products
) =
self.system.reaction_rules.iter().fold(
(
RSset::new(), // reactants
RSset::new(), // reactants_absent
RSset::new(), // inhibitors
RSset::new(), // inhibitors_present
RSset::new(), // products
),
|acc, reaction| {
if reaction.enabled(&t) {
(
acc.0.union(&reaction.reactants),
acc.1,
acc.2.union(&reaction.inhibitors),
acc.3,
acc.4.union(&reaction.products),
)
} else {
(
acc.0,
acc.1.union(&reaction.inhibitors.intersection(&t)),
acc.2,
acc.3.union(&reaction.reactants.subtraction(&t)),
acc.4,
)
}
},
);
let label = RSlabel::from(
self.system.available_entities.clone(),
(*c).clone(),
t,
reactants,
reactants_absent,
inhibitors,
inhibitors_present,
products.clone(),
);
let new_system = RSsystem::from(
Rc::clone(&self.system.delta),
products,
(*k).clone(),
Rc::clone(&self.system.reaction_rules),
);
Some((label, new_system))
2025-08-22 01:40:15 +02:00
}
2025-06-17 13:45:35 +02:00
}