2025-08-23 23:40:19 +02:00
|
|
|
//! Definitions for the 'classical' mechanism for computation.
|
|
|
|
|
//!
|
|
|
|
|
//! Allows to define the 'classical' mechanism to compute in a Reaction System
|
|
|
|
|
//! (RS) Framework.
|
|
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
2025-09-11 02:49:14 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-08-29 17:59:49 +02:00
|
|
|
use super::element::{IdState, IdType};
|
2025-09-07 17:55:53 +02:00
|
|
|
use super::set::{BasicSet, ExtensionsSet, PositiveSet, Set};
|
|
|
|
|
use super::translator::{Formatter, PrintableWithTranslator, Translator};
|
2025-08-23 23:40:19 +02:00
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
pub trait BasicReaction:
|
|
|
|
|
Clone + Default + Eq + Hash + Serialize + PrintableWithTranslator
|
2025-09-07 17:55:53 +02:00
|
|
|
where
|
|
|
|
|
for<'de> Self: Deserialize<'de>,
|
2025-08-26 16:56:08 +02:00
|
|
|
{
|
2025-09-05 13:13:35 +02:00
|
|
|
type Set: BasicSet;
|
|
|
|
|
fn enabled(&self, state: &Self::Set) -> bool;
|
|
|
|
|
fn compute_step(&self, state: &Self::Set) -> Option<&Self::Set>;
|
2025-08-23 23:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-27 23:58:43 +02:00
|
|
|
pub trait ExtensionReaction: Sized {
|
2025-09-05 13:13:35 +02:00
|
|
|
type Set: BasicSet;
|
|
|
|
|
|
|
|
|
|
fn compute_all(reactions: &[Self], state: &Self::Set) -> Self::Set;
|
|
|
|
|
|
|
|
|
|
fn find_loop(
|
2025-09-07 17:55:53 +02:00
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: Self::Set,
|
|
|
|
|
q: &Self::Set,
|
2025-09-05 13:13:35 +02:00
|
|
|
) -> (Vec<Self::Set>, Vec<Self::Set>);
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
fn find_only_loop(
|
|
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: &Self::Set,
|
|
|
|
|
q: &Self::Set,
|
|
|
|
|
) -> Vec<Self::Set>;
|
2025-09-05 13:13:35 +02:00
|
|
|
|
|
|
|
|
fn find_prefix_len_loop(
|
2025-09-07 17:55:53 +02:00
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: Self::Set,
|
|
|
|
|
q: &Self::Set,
|
2025-09-05 13:13:35 +02:00
|
|
|
) -> (usize, Vec<Self::Set>);
|
|
|
|
|
|
|
|
|
|
fn lollipops_only_loop_decomposed_q(
|
2025-09-07 17:55:53 +02:00
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: &Self::Set,
|
|
|
|
|
q: &Self::Set,
|
2025-09-05 13:13:35 +02:00
|
|
|
) -> Vec<Self::Set>;
|
2025-08-26 16:56:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-27 23:58:43 +02:00
|
|
|
/// Implementations for all reactions.
|
|
|
|
|
impl<T: BasicReaction<Set = Set>, Set: BasicSet> ExtensionReaction for T {
|
2025-09-05 13:13:35 +02:00
|
|
|
type Set = Set;
|
|
|
|
|
|
|
|
|
|
/// Computes the result of a series of reactions. Returns the union of all
|
|
|
|
|
/// products.
|
|
|
|
|
/// see result
|
2025-09-07 17:55:53 +02:00
|
|
|
fn compute_all(reactions: &[Self], state: &Set) -> Set
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
reactions.iter().fold(Set::default(), |mut acc: Set, r| {
|
|
|
|
|
acc.extend(r.compute_step(state));
|
|
|
|
|
acc
|
|
|
|
|
})
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finds the loops by simulating the system.
|
2025-09-10 22:41:40 +02:00
|
|
|
fn find_loop(
|
|
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: Set,
|
|
|
|
|
q: &Set,
|
|
|
|
|
) -> (Vec<Set>, Vec<Set>) {
|
2025-09-07 17:55:53 +02:00
|
|
|
let mut entities = entities;
|
|
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
|
|
|
|
if let Some((prefix, hoop)) = entities.split(&trace) {
|
|
|
|
|
return (prefix.to_vec(), hoop.to_vec());
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
|
|
|
|
let products = Self::compute_all(reactions, &t);
|
|
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finds the loops by simulating the system.
|
2025-09-07 17:55:53 +02:00
|
|
|
fn find_only_loop(reactions: &[Self], entities: &Set, q: &Set) -> Vec<Set> {
|
|
|
|
|
let mut entities = entities.clone();
|
|
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
|
|
|
|
if let Some((_prefix, hoop)) = entities.split(&trace) {
|
|
|
|
|
return hoop.to_vec();
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
|
|
|
|
let products = Self::compute_all(reactions, &t);
|
|
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finds the loops and the length of the prefix by simulating the system.
|
2025-09-10 22:41:40 +02:00
|
|
|
fn find_prefix_len_loop(
|
|
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: Set,
|
|
|
|
|
q: &Set,
|
|
|
|
|
) -> (usize, Vec<Set>) {
|
2025-09-07 17:55:53 +02:00
|
|
|
let mut entities = entities;
|
|
|
|
|
let mut trace = vec![];
|
|
|
|
|
loop {
|
|
|
|
|
if let Some((prefix, hoop)) = entities.split(&trace) {
|
|
|
|
|
return (prefix.len(), hoop.to_vec());
|
|
|
|
|
} else {
|
|
|
|
|
let t = entities.union(q);
|
|
|
|
|
let products = Self::compute_all(reactions, &t);
|
|
|
|
|
trace.push(entities.clone());
|
|
|
|
|
entities = products;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// see loop/5
|
2025-09-10 22:41:40 +02:00
|
|
|
fn lollipops_only_loop_decomposed_q(
|
|
|
|
|
reactions: &[Self],
|
|
|
|
|
entities: &Set,
|
|
|
|
|
q: &Set,
|
|
|
|
|
) -> Vec<Set> {
|
2025-09-07 17:55:53 +02:00
|
|
|
let find_loop_fn = |q| Self::find_only_loop(reactions, entities, q);
|
|
|
|
|
find_loop_fn(q)
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-26 16:56:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/// Basic structure for a reaction.
|
2025-09-10 22:41:40 +02:00
|
|
|
#[derive(
|
|
|
|
|
Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash,
|
|
|
|
|
)]
|
2025-08-26 16:56:08 +02:00
|
|
|
pub struct Reaction {
|
2025-09-11 02:49:14 +02:00
|
|
|
pub reactants: Set,
|
2025-09-05 13:13:35 +02:00
|
|
|
pub inhibitors: Set,
|
2025-09-11 02:49:14 +02:00
|
|
|
pub products: Set,
|
2025-08-26 16:56:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-27 23:58:43 +02:00
|
|
|
impl BasicReaction for Reaction {
|
2025-09-05 13:13:35 +02:00
|
|
|
type Set = Set;
|
|
|
|
|
|
|
|
|
|
/// returns true if ```current_state``` enables the reaction
|
|
|
|
|
/// see enable
|
|
|
|
|
fn enabled(&self, current_state: &Self::Set) -> bool {
|
2025-09-10 22:41:40 +02:00
|
|
|
self.reactants.is_subset(current_state)
|
|
|
|
|
&& self.inhibitors.is_disjoint(current_state)
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Computes the result of a single reaction (if enabled returns the
|
|
|
|
|
/// products) otherwise returns None.
|
|
|
|
|
/// see result
|
|
|
|
|
fn compute_step(&self, state: &Self::Set) -> Option<&Self::Set> {
|
2025-09-07 17:55:53 +02:00
|
|
|
if self.enabled(state) {
|
|
|
|
|
Some(&self.products)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-23 23:40:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
impl PrintableWithTranslator for Reaction {
|
2025-09-10 22:41:40 +02:00
|
|
|
fn print(
|
|
|
|
|
&self,
|
|
|
|
|
f: &mut std::fmt::Formatter,
|
|
|
|
|
translator: &Translator,
|
|
|
|
|
) -> std::fmt::Result {
|
2025-09-07 17:55:53 +02:00
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"(r: {}, i: {}, p: {})",
|
|
|
|
|
Formatter::from(translator, &self.reactants),
|
|
|
|
|
Formatter::from(translator, &self.inhibitors),
|
|
|
|
|
Formatter::from(translator, &self.products)
|
|
|
|
|
)
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-24 02:01:24 +02:00
|
|
|
}
|
2025-08-26 23:36:29 +02:00
|
|
|
|
|
|
|
|
impl Reaction {
|
2025-09-05 13:13:35 +02:00
|
|
|
pub fn from(reactants: Set, inhibitors: Set, products: Set) -> Self {
|
2025-09-07 17:55:53 +02:00
|
|
|
Reaction {
|
|
|
|
|
reactants,
|
|
|
|
|
inhibitors,
|
|
|
|
|
products,
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn all_products(reactions: &[Self]) -> Set {
|
2025-09-07 17:55:53 +02:00
|
|
|
reactions
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(Set::default(), |acc, r| acc.union(&r.products))
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
pub fn all_reactions_with_product<'a>(
|
|
|
|
|
reactions: &'a [Self],
|
|
|
|
|
el: &IdType,
|
|
|
|
|
) -> Vec<&'a Self> {
|
2025-09-07 17:55:53 +02:00
|
|
|
reactions.iter().fold(vec![], |mut acc, r| {
|
|
|
|
|
if r.products.contains(el) {
|
|
|
|
|
acc.push(r);
|
|
|
|
|
}
|
|
|
|
|
acc
|
|
|
|
|
})
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-27 23:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2025-09-10 22:41:40 +02:00
|
|
|
#[derive(
|
|
|
|
|
Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash,
|
|
|
|
|
)]
|
2025-08-27 23:58:43 +02:00
|
|
|
pub struct PositiveReaction {
|
2025-09-05 13:13:35 +02:00
|
|
|
pub reactants: PositiveSet,
|
2025-09-11 02:49:14 +02:00
|
|
|
pub products: PositiveSet,
|
2025-08-27 23:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BasicReaction for PositiveReaction {
|
2025-09-05 13:13:35 +02:00
|
|
|
type Set = PositiveSet;
|
2025-08-27 23:58:43 +02:00
|
|
|
|
2025-09-05 13:13:35 +02:00
|
|
|
fn enabled(&self, state: &Self::Set) -> bool {
|
2025-09-07 17:55:53 +02:00
|
|
|
self.reactants.is_subset(state)
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-27 23:58:43 +02:00
|
|
|
|
2025-09-05 13:13:35 +02:00
|
|
|
fn compute_step(&self, state: &Self::Set) -> Option<&Self::Set> {
|
2025-09-07 17:55:53 +02:00
|
|
|
if self.enabled(state) {
|
|
|
|
|
Some(&self.products)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-26 23:36:29 +02:00
|
|
|
}
|
2025-08-27 23:58:43 +02:00
|
|
|
|
|
|
|
|
impl PrintableWithTranslator for PositiveReaction {
|
2025-09-10 22:41:40 +02:00
|
|
|
fn print(
|
|
|
|
|
&self,
|
|
|
|
|
f: &mut std::fmt::Formatter,
|
|
|
|
|
translator: &Translator,
|
|
|
|
|
) -> std::fmt::Result {
|
2025-09-07 17:55:53 +02:00
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"(r: {}, p: {})",
|
|
|
|
|
Formatter::from(translator, &self.reactants),
|
|
|
|
|
Formatter::from(translator, &self.products),
|
|
|
|
|
)
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-08-27 23:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PositiveReaction {
|
2025-09-05 13:13:35 +02:00
|
|
|
pub fn from(reactants: PositiveSet, products: PositiveSet) -> Self {
|
2025-09-07 17:55:53 +02:00
|
|
|
Self {
|
|
|
|
|
reactants,
|
|
|
|
|
products,
|
|
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create(reactants: Set, inhibitors: Set, products: Set) -> Self {
|
2025-09-07 17:55:53 +02:00
|
|
|
Self {
|
|
|
|
|
reactants: reactants
|
|
|
|
|
.to_positive_set(IdState::Positive)
|
|
|
|
|
.union(&inhibitors.to_positive_set(IdState::Negative)),
|
2025-09-11 02:49:14 +02:00
|
|
|
products: products.to_positive_set(IdState::Positive),
|
2025-09-07 17:55:53 +02:00
|
|
|
}
|
2025-09-05 13:13:35 +02:00
|
|
|
}
|
2025-09-08 19:04:26 +02:00
|
|
|
|
|
|
|
|
/// returns the reactants that are equal
|
|
|
|
|
pub fn differ_only_one_element(&self, other: &Self) -> Option<PositiveSet> {
|
|
|
|
|
if self.products != other.products {
|
2025-09-10 22:41:40 +02:00
|
|
|
return None;
|
2025-09-08 19:04:26 +02:00
|
|
|
}
|
|
|
|
|
let mut found = false;
|
|
|
|
|
for el in self.reactants.iter() {
|
|
|
|
|
match other.reactants.identifiers.get(el.0) {
|
2025-09-11 02:49:14 +02:00
|
|
|
| None => return None,
|
|
|
|
|
| Some(s) =>
|
2025-09-08 19:04:26 +02:00
|
|
|
if s != el.1 {
|
2025-09-10 22:41:40 +02:00
|
|
|
if found { return None } else { found = true }
|
2025-09-11 02:49:14 +02:00
|
|
|
},
|
2025-09-08 19:04:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(self.reactants.intersection(&other.reactants))
|
|
|
|
|
}
|
2025-08-27 23:58:43 +02:00
|
|
|
}
|