2025-06-16 14:46:04 +02:00
|
|
|
use super::structure::{RSlabel, RSprocess, RSset, RSsystem};
|
2025-06-12 19:16:32 +02:00
|
|
|
use super::transitions::unfold;
|
2025-06-16 14:46:04 +02:00
|
|
|
use std::rc::Rc;
|
2025-06-12 19:16:32 +02:00
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct TransitionsIterator<'a> {
|
2025-06-16 14:46:04 +02:00
|
|
|
choices_iterator: std::vec::IntoIter<(Rc<RSset>, Rc<RSprocess>)>,
|
|
|
|
|
system: &'a RSsystem,
|
2025-06-12 19:16:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> TransitionsIterator<'a> {
|
2025-07-01 19:22:50 +02:00
|
|
|
pub fn from(
|
|
|
|
|
system: &'a RSsystem
|
|
|
|
|
) -> Result<TransitionsIterator<'a>, String> {
|
2025-07-09 16:12:22 +02:00
|
|
|
match unfold(&system.delta, &system.context_process) {
|
2025-06-16 14:46:04 +02:00
|
|
|
Ok(o) => Ok(TransitionsIterator {
|
|
|
|
|
choices_iterator: o.into_iter(),
|
|
|
|
|
system,
|
|
|
|
|
}),
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
2025-06-12 19:16:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for TransitionsIterator<'a> {
|
2025-06-16 14:46:04 +02:00
|
|
|
type Item = (RSlabel, RSsystem);
|
2025-06-12 19:16:32 +02:00
|
|
|
|
2025-06-16 14:46:04 +02:00
|
|
|
fn next(&mut self) -> Option<(RSlabel, RSsystem)> {
|
|
|
|
|
let (c, k) = self.choices_iterator.next()?;
|
2025-07-09 16:12:22 +02:00
|
|
|
let t = self.system.available_entities.union(c.as_ref());
|
2025-07-09 19:34:15 +02:00
|
|
|
let (
|
|
|
|
|
reactants,
|
|
|
|
|
reactants_absent,
|
|
|
|
|
inihibitors,
|
|
|
|
|
inihibitors_present,
|
|
|
|
|
products
|
|
|
|
|
) =
|
2025-07-09 16:12:22 +02:00
|
|
|
self.system.reaction_rules.iter().fold(
|
2025-06-16 14:46:04 +02:00
|
|
|
(
|
|
|
|
|
RSset::new(), // reactants
|
2025-07-09 19:34:15 +02:00
|
|
|
RSset::new(), // reactants_absent
|
2025-06-16 14:46:04 +02:00
|
|
|
RSset::new(), // inihibitors
|
2025-07-09 19:34:15 +02:00
|
|
|
RSset::new(), // inihibitors_present
|
2025-06-16 14:46:04 +02:00
|
|
|
RSset::new(), // products
|
|
|
|
|
),
|
|
|
|
|
|acc, reaction| {
|
|
|
|
|
if reaction.enabled(&t) {
|
2025-07-01 19:22:50 +02:00
|
|
|
(
|
2025-07-09 16:12:22 +02:00
|
|
|
acc.0.union(&reaction.reactants),
|
2025-06-16 14:46:04 +02:00
|
|
|
acc.1,
|
2025-07-09 16:12:22 +02:00
|
|
|
acc.2.union(&reaction.inihibitors),
|
2025-06-16 14:46:04 +02:00
|
|
|
acc.3,
|
2025-07-09 16:12:22 +02:00
|
|
|
acc.4.union(&reaction.products),
|
2025-07-01 19:22:50 +02:00
|
|
|
)
|
2025-06-16 14:46:04 +02:00
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
acc.0,
|
2025-07-09 16:12:22 +02:00
|
|
|
acc.1.union(&reaction.inihibitors.intersection(&t)),
|
2025-06-16 14:46:04 +02:00
|
|
|
acc.2,
|
2025-07-09 16:12:22 +02:00
|
|
|
acc.3.union(&reaction.reactants.subtraction(&t)),
|
2025-06-16 14:46:04 +02:00
|
|
|
acc.4,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-06-12 19:16:32 +02:00
|
|
|
|
2025-06-16 14:46:04 +02:00
|
|
|
let label = RSlabel::from(
|
2025-07-09 16:12:22 +02:00
|
|
|
self.system.available_entities.clone(),
|
2025-06-16 14:46:04 +02:00
|
|
|
(*c).clone(),
|
|
|
|
|
t,
|
|
|
|
|
reactants,
|
2025-07-09 19:34:15 +02:00
|
|
|
reactants_absent,
|
2025-06-16 14:46:04 +02:00
|
|
|
inihibitors,
|
2025-07-09 19:34:15 +02:00
|
|
|
inihibitors_present,
|
2025-06-16 14:46:04 +02:00
|
|
|
products.clone(),
|
|
|
|
|
);
|
|
|
|
|
let new_system = RSsystem::from(
|
2025-07-09 16:12:22 +02:00
|
|
|
Rc::clone(&self.system.delta),
|
2025-06-16 14:46:04 +02:00
|
|
|
products,
|
|
|
|
|
(*k).clone(),
|
2025-07-09 16:12:22 +02:00
|
|
|
Rc::clone(&self.system.reaction_rules),
|
2025-06-16 14:46:04 +02:00
|
|
|
);
|
|
|
|
|
Some((label, new_system))
|
2025-06-12 19:16:32 +02:00
|
|
|
}
|
|
|
|
|
}
|