Prohibiting set

This commit is contained in:
elvis
2025-08-27 23:58:43 +02:00
parent 8b0fbcee00
commit a0f4297774
12 changed files with 291 additions and 77 deletions

View File

@ -6,53 +6,61 @@
use serde::{Deserialize, Serialize};
use std::hash::Hash;
use super::set::{BasicSet, ExtensionsSet, Set};
use super::set::{BasicSet, ExtensionsSet, Set, PositiveSet};
use super::translator::{Translator, PrintableWithTranslator, Formatter};
pub trait BasicReaction<S: BasicSet>:
pub trait BasicReaction:
Clone + Default + Eq + Hash + Serialize + PrintableWithTranslator
where for<'de> Self: Deserialize<'de>,
{
fn enabled(&self, state: &S) -> bool;
fn compute_step(&self, state: &S) -> Option<&S>;
type Set: BasicSet;
fn enabled(&self, state: &Self::Set) -> bool;
fn compute_step(&self, state: &Self::Set) -> Option<&Self::Set>;
}
pub trait ExtensionReaction<S: BasicSet> {
fn compute_all(reactions: &[Self], state: &S) -> S
where Self: Sized;
pub trait ExtensionReaction: Sized {
type Set: BasicSet;
fn find_loop(reactions: &[Self], entities: S, q: &S) -> (Vec<S>, Vec<S>)
where Self: Sized;
fn compute_all(reactions: &[Self], state: &Self::Set) -> Self::Set;
fn find_only_loop(reactions: &[Self], entities: S, q: &S) -> Vec<S>
where Self: Sized;
fn find_loop(
reactions: &[Self],
entities: Self::Set,
q: &Self::Set
) -> (Vec<Self::Set>, Vec<Self::Set>);
fn find_only_loop(
reactions: &[Self],
entities: Self::Set,
q: &Self::Set
) -> Vec<Self::Set>;
fn find_prefix_len_loop(
reactions: &[Self],
entities: S,
q: &S
) -> (usize, Vec<S>)
where Self: Sized;
entities: Self::Set,
q: &Self::Set
) -> (usize, Vec<Self::Set>);
fn lollipops_only_loop_decomposed_q(
reactions: &[Self],
entities: &S,
q: &S
) -> Vec<S>
where Self: Sized;
entities: &Self::Set,
q: &Self::Set
) -> Vec<Self::Set>;
}
/// Implementations for all reactions.
impl<T: BasicReaction<Set = Set>, Set: BasicSet> ExtensionReaction for T {
type Set = Set;
impl<T: BasicReaction<S>, S: BasicSet> ExtensionReaction<S> for T {
/// Computes the result of a series of reactions. Returns the union of all
/// products.
/// see result
fn compute_all(
reactions: &[Self],
state: &S
) -> S
state: &Set
) -> Set
where Self: Sized {
reactions.iter().fold(S::default(), |mut acc: S, r| {
reactions.iter().fold(Set::default(), |mut acc: Set, r| {
acc.extend(r.compute_step(state));
acc
})
@ -61,9 +69,9 @@ impl<T: BasicReaction<S>, S: BasicSet> ExtensionReaction<S> for T {
/// Finds the loops by simulating the system.
fn find_loop(
reactions: &[Self],
entities: S,
q: &S
) -> (Vec<S>, Vec<S>) {
entities: Set,
q: &Set
) -> (Vec<Set>, Vec<Set>) {
let mut entities = entities;
let mut trace = vec![];
loop {
@ -82,9 +90,9 @@ impl<T: BasicReaction<S>, S: BasicSet> ExtensionReaction<S> for T {
/// Finds the loops by simulating the system.
fn find_only_loop(
reactions: &[Self],
entities: S,
q: &S
) -> Vec<S> {
entities: Set,
q: &Set
) -> Vec<Set> {
let mut entities = entities;
let mut trace = vec![];
loop {
@ -103,9 +111,9 @@ impl<T: BasicReaction<S>, S: BasicSet> ExtensionReaction<S> for T {
/// Finds the loops and the length of the prefix by simulating the system.
fn find_prefix_len_loop(
reactions: &[Self],
entities: S,
q: &S
) -> (usize, Vec<S>) {
entities: Set,
q: &Set
) -> (usize, Vec<Set>) {
let mut entities = entities;
let mut trace = vec![];
loop {
@ -124,9 +132,9 @@ impl<T: BasicReaction<S>, S: BasicSet> ExtensionReaction<S> for T {
/// see loop/5
fn lollipops_only_loop_decomposed_q(
reactions: &[Self],
entities: &S,
q: &S,
) -> Vec<S> {
entities: &Set,
q: &Set,
) -> Vec<Set> {
let find_loop_fn =
|q| Self::find_only_loop(reactions,
entities.clone(),
@ -148,7 +156,9 @@ pub struct Reaction {
pub products: Set,
}
impl BasicReaction<Set> for Reaction {
impl BasicReaction for Reaction {
type Set = Set;
/// returns true if ```current_state``` enables the reaction
/// see enable
fn enabled(&self, current_state: &Set) -> bool {
@ -159,10 +169,7 @@ impl BasicReaction<Set> for Reaction {
/// Computes the result of a single reaction (if enabled returns the
/// products) otherwise returns None.
/// see result
fn compute_step(
&self,
state: &Set,
) -> Option<&Set> {
fn compute_step(&self, state: &Set) -> Option<&Set> {
if self.enabled(state) {
Some(&self.products)
} else {
@ -186,10 +193,48 @@ impl PrintableWithTranslator for Reaction {
impl Reaction {
pub fn from(reactants: Set, inhibitors: Set, products: Set) -> Self {
Reaction {
reactants,
inhibitors,
products,
Reaction { reactants, inhibitors, products }
}
}
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct PositiveReaction {
pub reactants: PositiveSet,
pub products: PositiveSet
}
impl BasicReaction for PositiveReaction {
type Set = PositiveSet;
fn enabled(&self, state: &PositiveSet) -> bool {
self.reactants.is_subset(state)
}
fn compute_step(&self, state: &PositiveSet) -> Option<&PositiveSet> {
if self.enabled(state) {
Some(&self.products)
} else {
None
}
}
}
impl PrintableWithTranslator for PositiveReaction {
fn print(&self, f: &mut std::fmt::Formatter, translator: &Translator)
-> std::fmt::Result {
write!(
f,
"(r: {}, p: {})",
Formatter::from(translator, &self.reactants),
Formatter::from(translator, &self.products),
)
}
}
impl PositiveReaction {
pub fn from(reactants: PositiveSet, products: PositiveSet) -> Self {
PositiveReaction { reactants, products }
}
}