Adding documentation for classical.rs, modifying compute_step with pointers

This commit is contained in:
elvis
2025-05-19 13:10:05 +02:00
parent 6d5be8c36c
commit 32c2fc2701
3 changed files with 35 additions and 6 deletions

View File

@ -1,13 +1,24 @@
//! Definitions for the 'classical' mechanism for computation.
//!
//! This initial part allows to define the 'classical' mechanism to compute in a
//! Reaction System (RS) Framework.
//! The data is held in RSset or RSreaction, in the latter the reagents,
//! inhibitors and products are held.
use super::structure::{RSset, RSreaction}; use super::structure::{RSset, RSreaction};
pub fn compute_step<'a>(current_state: &RSset<'a>, reaction: &RSreaction<'a>) -> RSset<'a> { /// Computes the result of a single reaction (if enabled returns the products)
/// otherwise returns None.
pub fn compute_step<'a>(current_state: &RSset<'a>, reaction: &'a RSreaction<'a>) -> Option<&'a RSset<'a>> {
if reaction.enabled(current_state) { if reaction.enabled(current_state) {
reaction.products_clone() Some(reaction.products())
} else { } else {
RSset::new() None
} }
} }
pub fn compute_all<'a>(current_state: &RSset<'a>, reactions: Vec<&RSreaction<'a>>) -> RSset<'a> { /// Computes the result of a series of reactions. Returns the union of all
reactions.iter().fold(RSset::new(), |acc, r| acc.union(&compute_step(current_state, r))) /// products.
pub fn compute_all<'a>(current_state: &RSset<'a>, reactions: Vec<&'a RSreaction<'a>>) -> RSset<'a> {
reactions.iter().fold(RSset::new(), |acc, r| acc.union_option(compute_step(current_state, r)))
} }

View File

@ -55,7 +55,7 @@ CTX_process: RSprocess<'input> = {
next_process: Box::new(k)}, next_process: Box::new(k)},
"nil" => RSprocess::Nill, "nil" => RSprocess::Nill,
<identifier: Literal> => <identifier: Literal> =>
RSprocess::ConstantIdentifier{ identifier: identifier } RSprocess::ConstantIdentifier{ identifier }
}; };
// ----- EnvironmentParser ----- // ----- EnvironmentParser -----

View File

@ -40,12 +40,24 @@ impl<'a> RSset<'a> {
self.identifiers.is_disjoint(&b.identifiers) self.identifiers.is_disjoint(&b.identifiers)
} }
#[allow(dead_code)]
pub fn union(&self, b: &RSset<'a>) -> RSset<'a> { pub fn union(&self, b: &RSset<'a>) -> RSset<'a> {
// TODO maybe find more efficient way // TODO maybe find more efficient way
let mut ret: RSset = b.clone(); let mut ret: RSset = b.clone();
ret.identifiers.extend(self.identifiers.iter()); ret.identifiers.extend(self.identifiers.iter());
ret ret
} }
pub fn union_option(&self, b: Option<&RSset<'a>>) -> RSset<'a> {
if let Some(b) = b {
// TODO maybe find more efficient way
let mut ret: RSset = b.clone();
ret.identifiers.extend(self.identifiers.iter());
ret
} else {
self.clone()
}
}
} }
@ -61,6 +73,7 @@ pub struct RSreaction<'a> {
} }
impl<'a> RSreaction<'a> { impl<'a> RSreaction<'a> {
#[allow(dead_code)]
pub fn new() -> Self { pub fn new() -> Self {
RSreaction{ reactants: RSset::new(), RSreaction{ reactants: RSset::new(),
inihibitors: RSset::new(), inihibitors: RSset::new(),
@ -78,9 +91,14 @@ impl<'a> RSreaction<'a> {
&& self.inihibitors.is_disjoint(current_state) && self.inihibitors.is_disjoint(current_state)
} }
#[allow(dead_code)]
pub fn products_clone(&self) -> RSset<'a> { pub fn products_clone(&self) -> RSset<'a> {
self.products.clone() self.products.clone()
} }
pub fn products(&self) -> &RSset<'a> {
&self.products
}
} }