From d4ade0d9212baef92979e16735e33e615e799a13 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 20 Aug 2025 13:51:48 +0200 Subject: [PATCH] Added guarded context --- src/rsprocess/structure.rs | 12 +++++- src/rsprocess/support_structures.rs | 2 +- src/rsprocess/transitions.rs | 58 +++++++++++++++-------------- src/rsprocess/translator.rs | 23 ++++++------ 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/rsprocess/structure.rs b/src/rsprocess/structure.rs index 090b081..18d3902 100644 --- a/src/rsprocess/structure.rs +++ b/src/rsprocess/structure.rs @@ -143,7 +143,7 @@ impl IntoIterator for RSset { // ----------------------------------------------------------------------------- /// Basic structure for a reaction. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] pub struct RSreaction { pub reactants: RSset, pub inhibitors: RSset, @@ -195,6 +195,10 @@ pub enum RSprocess { entities: RSset, next_process: Rc, }, + Guarded { + reaction: RSreaction, + next_process: Rc, + }, WaitEntity { repeat: i64, repeated_process: Rc, @@ -248,6 +252,12 @@ impl RSprocess { elements.push(entities); queue.push_back(next_process); } + Self::Guarded { reaction, next_process } => { + elements.push(&reaction.reactants); + elements.push(&reaction.inhibitors); + elements.push(&reaction.products); + queue.push_back(next_process); + } Self::WaitEntity { repeat: _, repeated_process, diff --git a/src/rsprocess/support_structures.rs b/src/rsprocess/support_structures.rs index 289688b..f70ec99 100644 --- a/src/rsprocess/support_structures.rs +++ b/src/rsprocess/support_structures.rs @@ -14,7 +14,7 @@ impl<'a> TransitionsIterator<'a> { pub fn from( system: &'a RSsystem ) -> Result, String> { - match unfold(&system.delta, &system.context_process) { + match unfold(&system.delta, &system.context_process, &system.available_entities) { Ok(o) => Ok(TransitionsIterator { choices_iterator: o.into_iter(), system, diff --git a/src/rsprocess/transitions.rs b/src/rsprocess/transitions.rs index 615a0a2..a2569e4 100644 --- a/src/rsprocess/transitions.rs +++ b/src/rsprocess/transitions.rs @@ -16,44 +16,46 @@ use std::rc::Rc; pub fn unfold( environment: &RSenvironment, context_process: &RSprocess, + current_entities: &RSset, ) -> Result { match context_process { - RSprocess::Nill => Ok(RSchoices::new()), + RSprocess::Nill => { + Ok(RSchoices::new()) + }, RSprocess::RecursiveIdentifier { identifier } => { let newprocess = environment.get(*identifier); if let Some(newprocess) = newprocess { - unfold(environment, newprocess) + unfold(environment, newprocess, current_entities) } else { Err(format!("Missing symbol in context: {identifier}")) } } - RSprocess::EntitySet { - entities, - next_process, - } => Ok(RSchoices::from(vec![( - Rc::new(entities.clone()), - Rc::clone(next_process), - )])), - RSprocess::WaitEntity { - repeat, - repeated_process: _, - next_process, - } if *repeat <= 0 => unfold(environment, next_process), - RSprocess::WaitEntity { - repeat, - repeated_process, - next_process, - } if *repeat == 1 => { - let mut choices1 = unfold(environment, repeated_process)?; + RSprocess::EntitySet { entities, next_process, } => { + Ok(RSchoices::from([( + Rc::new(entities.clone()), + Rc::clone(next_process), + )])) + }, + RSprocess::Guarded { reaction, next_process } => { + if reaction.enabled(current_entities) { + Ok(RSchoices::from([(Rc::new(reaction.products.clone()), + Rc::clone(next_process))])) + } else { + Ok(RSchoices::new()) + } + } + RSprocess::WaitEntity { repeat, repeated_process: _, next_process, } + if *repeat <= 0 => { + unfold(environment, next_process, current_entities) + }, + RSprocess::WaitEntity { repeat, repeated_process, next_process, } + if *repeat == 1 => { + let mut choices1 = unfold(environment, repeated_process, current_entities)?; choices1.replace(Rc::clone(next_process)); Ok(choices1) } - RSprocess::WaitEntity { - repeat, - repeated_process, - next_process, - } => { - let mut choices1 = unfold(environment, repeated_process)?; + RSprocess::WaitEntity { repeat, repeated_process, next_process, } => { + let mut choices1 = unfold(environment, repeated_process, current_entities)?; choices1.replace(Rc::new(RSprocess::WaitEntity { repeat: (*repeat - 1), repeated_process: Rc::clone(repeated_process), @@ -64,7 +66,7 @@ pub fn unfold( RSprocess::Summation { children } => { // short-circuits with try_fold. children.iter().try_fold(RSchoices::new(), |mut acc, x| { - match unfold(environment, x) { + match unfold(environment, x, current_entities) { Ok(mut choices) => { acc.append(&mut choices); Ok(acc) @@ -82,7 +84,7 @@ pub fn unfold( )])) } else { children.iter().try_fold(RSchoices::new(), |mut acc, x| { - acc.shuffle(unfold(environment, x)?); + acc.shuffle(unfold(environment, x, current_entities)?); Ok(acc) }) } diff --git a/src/rsprocess/translator.rs b/src/rsprocess/translator.rs index 7f2e80e..4a8d386 100644 --- a/src/rsprocess/translator.rs +++ b/src/rsprocess/translator.rs @@ -166,28 +166,27 @@ fn print_process( match process { Nill => { write!(f, "Nill") - } + }, RecursiveIdentifier { identifier } => { write!(f, "[{}]", translator.decode(*identifier).unwrap_or("Missing".into())) - } - EntitySet { - entities, - next_process, - } => { + }, + EntitySet { entities, next_process, } => { write!( f, "{}.{}", RSsetDisplay::from(translator, entities), RSprocessDisplay::from(translator, next_process) ) - } - WaitEntity { - repeat, - repeated_process, - next_process, - } => { + }, + Guarded { reaction, next_process } => { + write!(f, + "?{}?.{}", + RSreactionDisplay::from(translator, reaction), + RSprocessDisplay::from(translator, next_process)) + }, + WaitEntity { repeat, repeated_process, next_process, } => { write!( f, "({})^{repeat}.{}",