2025-08-23 23:40:19 +02:00
|
|
|
//! Module for helper structure for simulation
|
2025-07-10 15:02:14 +02:00
|
|
|
|
2025-05-21 00:03:36 +02:00
|
|
|
use std::rc::Rc;
|
2025-06-12 16:23:39 +02:00
|
|
|
|
2025-08-29 17:59:49 +02:00
|
|
|
use super::label::{Label, PositiveLabel};
|
|
|
|
|
use super::process::{BasicProcess, PositiveProcess, Process};
|
2025-09-07 17:55:53 +02:00
|
|
|
use super::reaction::BasicReaction;
|
2025-08-29 17:59:49 +02:00
|
|
|
use super::set::{BasicSet, PositiveSet, Set};
|
2025-09-10 22:41:40 +02:00
|
|
|
use super::system::{BasicSystem, ExtensionsSystem, PositiveSystem, System};
|
2025-08-24 02:01:24 +02:00
|
|
|
|
2025-08-23 23:40:19 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2025-09-07 17:55:53 +02:00
|
|
|
pub struct TransitionsIterator<
|
|
|
|
|
'a,
|
|
|
|
|
S: BasicSet,
|
|
|
|
|
Sys: BasicSystem<Set = S>,
|
|
|
|
|
Proc: BasicProcess<Set = S>,
|
|
|
|
|
> {
|
2025-08-29 17:59:49 +02:00
|
|
|
choices_iterator: std::vec::IntoIter<(Rc<S>, Rc<Proc>)>,
|
|
|
|
|
system: &'a Sys,
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
2025-06-16 16:35:54 +02:00
|
|
|
|
2025-08-29 17:59:49 +02:00
|
|
|
impl<'a> TransitionsIterator<'a, Set, System, Process> {
|
2025-09-07 17:55:53 +02:00
|
|
|
pub fn from(system: &'a System) -> Result<Self, String> {
|
2025-09-08 20:35:37 +02:00
|
|
|
match system.unfold() {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(o) => Ok(TransitionsIterator {
|
2025-08-23 23:40:19 +02:00
|
|
|
choices_iterator: o.into_iter(),
|
|
|
|
|
system,
|
|
|
|
|
}),
|
2025-09-11 02:49:14 +02:00
|
|
|
| Err(e) => Err(e),
|
2025-08-23 23:40:19 +02:00
|
|
|
}
|
2025-08-22 01:40:15 +02:00
|
|
|
}
|
2025-08-21 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-29 17:59:49 +02:00
|
|
|
impl<'a> Iterator for TransitionsIterator<'a, Set, System, Process> {
|
2025-08-24 02:01:24 +02:00
|
|
|
type Item = (Label, System);
|
2025-08-23 23:40:19 +02:00
|
|
|
|
|
|
|
|
/// Creates the next arc from the current system.
|
2025-08-24 02:01:24 +02:00
|
|
|
fn next(&mut self) -> Option<(Label, System)> {
|
2025-08-23 23:40:19 +02:00
|
|
|
let (c, k) = self.choices_iterator.next()?;
|
|
|
|
|
let t = self.system.available_entities.union(c.as_ref());
|
2025-09-10 22:41:40 +02:00
|
|
|
let (
|
|
|
|
|
reactants,
|
|
|
|
|
reactants_absent,
|
|
|
|
|
inhibitors,
|
|
|
|
|
inhibitors_present,
|
|
|
|
|
products,
|
|
|
|
|
) = self.system.reaction_rules.iter().fold(
|
|
|
|
|
(
|
|
|
|
|
Set::default(), // reactants
|
|
|
|
|
Set::default(), // reactants_absent
|
|
|
|
|
Set::default(), // inhibitors
|
|
|
|
|
Set::default(), // inhibitors_present
|
|
|
|
|
Set::default(), // 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,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-08-23 23:40:19 +02:00
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
let label = Label::from(
|
2025-08-23 23:40:19 +02:00
|
|
|
self.system.available_entities.clone(),
|
|
|
|
|
(*c).clone(),
|
|
|
|
|
t,
|
|
|
|
|
reactants,
|
|
|
|
|
reactants_absent,
|
|
|
|
|
inhibitors,
|
|
|
|
|
inhibitors_present,
|
|
|
|
|
products.clone(),
|
|
|
|
|
);
|
2025-08-24 02:01:24 +02:00
|
|
|
let new_system = System::from(
|
2025-08-23 23:40:19 +02:00
|
|
|
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
|
|
|
}
|
2025-08-29 17:59:49 +02:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
impl<'a> TransitionsIterator<'a, PositiveSet, PositiveSystem, PositiveProcess> {
|
2025-09-07 17:55:53 +02:00
|
|
|
pub fn from(system: &'a PositiveSystem) -> Result<Self, String> {
|
2025-09-08 20:35:37 +02:00
|
|
|
match system.unfold() {
|
2025-09-11 02:49:14 +02:00
|
|
|
| Ok(o) => Ok(TransitionsIterator {
|
2025-08-29 17:59:49 +02:00
|
|
|
choices_iterator: o.into_iter(),
|
|
|
|
|
system,
|
2025-09-07 17:55:53 +02:00
|
|
|
}),
|
2025-09-11 02:49:14 +02:00
|
|
|
| Err(e) => Err(e),
|
2025-08-29 17:59:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
impl<'a> Iterator
|
|
|
|
|
for TransitionsIterator<'a, PositiveSet, PositiveSystem, PositiveProcess>
|
|
|
|
|
{
|
2025-08-29 17:59:49 +02:00
|
|
|
type Item = (PositiveLabel, PositiveSystem);
|
|
|
|
|
|
|
|
|
|
/// Creates the next arc from the current system.
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let (c, k) = self.choices_iterator.next()?;
|
2025-08-29 17:59:49 +02:00
|
|
|
let t = self.system.available_entities.union(c.as_ref());
|
2025-09-10 22:41:40 +02:00
|
|
|
let (
|
|
|
|
|
reactants,
|
|
|
|
|
reactants_absent,
|
|
|
|
|
inhibitors,
|
|
|
|
|
inhibitors_present,
|
|
|
|
|
products,
|
|
|
|
|
) = self.system.reaction_rules.iter().fold(
|
|
|
|
|
(
|
|
|
|
|
PositiveSet::default(), // reactants
|
|
|
|
|
PositiveSet::default(), // reactants_absent
|
|
|
|
|
PositiveSet::default(), // inhibitors
|
|
|
|
|
PositiveSet::default(), // inhibitors_present
|
|
|
|
|
PositiveSet::default(), // products
|
|
|
|
|
),
|
|
|
|
|
|acc, reaction| {
|
|
|
|
|
if reaction.enabled(&t) {
|
|
|
|
|
(
|
|
|
|
|
acc.0.union(&reaction.reactants.positives()),
|
|
|
|
|
acc.1,
|
|
|
|
|
acc.2.union(&reaction.reactants.negatives()),
|
|
|
|
|
acc.3,
|
|
|
|
|
acc.4.union(&reaction.products),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
acc.0,
|
|
|
|
|
acc.1.union(
|
|
|
|
|
&reaction.reactants.negatives().intersection(&t),
|
|
|
|
|
),
|
|
|
|
|
acc.2,
|
|
|
|
|
acc.3.union(
|
|
|
|
|
&reaction.reactants.positives().subtraction(&t),
|
|
|
|
|
),
|
|
|
|
|
acc.4,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2025-08-29 17:59:49 +02:00
|
|
|
|
|
|
|
|
let label = PositiveLabel::from(
|
|
|
|
|
self.system.available_entities.clone(),
|
|
|
|
|
(*c).clone(),
|
|
|
|
|
t,
|
|
|
|
|
reactants,
|
|
|
|
|
reactants_absent,
|
2025-09-08 20:35:37 +02:00
|
|
|
inhibitors,
|
|
|
|
|
inhibitors_present,
|
2025-08-29 17:59:49 +02:00
|
|
|
products.clone(),
|
|
|
|
|
);
|
|
|
|
|
let new_system = PositiveSystem::from(
|
|
|
|
|
Rc::clone(&self.system.delta),
|
|
|
|
|
products,
|
|
|
|
|
(*k).clone(),
|
|
|
|
|
Rc::clone(&self.system.reaction_rules),
|
|
|
|
|
);
|
|
|
|
|
Some((label, new_system))
|
|
|
|
|
}
|
|
|
|
|
}
|