Added guarded context

This commit is contained in:
elvis
2025-08-20 13:51:48 +02:00
parent 331635981a
commit d4ade0d921
4 changed files with 53 additions and 42 deletions

View File

@ -143,7 +143,7 @@ impl IntoIterator for RSset {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/// Basic structure for a reaction. /// Basic structure for a reaction.
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct RSreaction { pub struct RSreaction {
pub reactants: RSset, pub reactants: RSset,
pub inhibitors: RSset, pub inhibitors: RSset,
@ -195,6 +195,10 @@ pub enum RSprocess {
entities: RSset, entities: RSset,
next_process: Rc<RSprocess>, next_process: Rc<RSprocess>,
}, },
Guarded {
reaction: RSreaction,
next_process: Rc<RSprocess>,
},
WaitEntity { WaitEntity {
repeat: i64, repeat: i64,
repeated_process: Rc<RSprocess>, repeated_process: Rc<RSprocess>,
@ -248,6 +252,12 @@ impl RSprocess {
elements.push(entities); elements.push(entities);
queue.push_back(next_process); 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 { Self::WaitEntity {
repeat: _, repeat: _,
repeated_process, repeated_process,

View File

@ -14,7 +14,7 @@ impl<'a> TransitionsIterator<'a> {
pub fn from( pub fn from(
system: &'a RSsystem system: &'a RSsystem
) -> Result<TransitionsIterator<'a>, String> { ) -> Result<TransitionsIterator<'a>, String> {
match unfold(&system.delta, &system.context_process) { match unfold(&system.delta, &system.context_process, &system.available_entities) {
Ok(o) => Ok(TransitionsIterator { Ok(o) => Ok(TransitionsIterator {
choices_iterator: o.into_iter(), choices_iterator: o.into_iter(),
system, system,

View File

@ -16,44 +16,46 @@ use std::rc::Rc;
pub fn unfold( pub fn unfold(
environment: &RSenvironment, environment: &RSenvironment,
context_process: &RSprocess, context_process: &RSprocess,
current_entities: &RSset,
) -> Result<RSchoices, String> { ) -> Result<RSchoices, String> {
match context_process { match context_process {
RSprocess::Nill => Ok(RSchoices::new()), RSprocess::Nill => {
Ok(RSchoices::new())
},
RSprocess::RecursiveIdentifier { identifier } => { RSprocess::RecursiveIdentifier { identifier } => {
let newprocess = environment.get(*identifier); let newprocess = environment.get(*identifier);
if let Some(newprocess) = newprocess { if let Some(newprocess) = newprocess {
unfold(environment, newprocess) unfold(environment, newprocess, current_entities)
} else { } else {
Err(format!("Missing symbol in context: {identifier}")) Err(format!("Missing symbol in context: {identifier}"))
} }
} }
RSprocess::EntitySet { RSprocess::EntitySet { entities, next_process, } => {
entities, Ok(RSchoices::from([(
next_process,
} => Ok(RSchoices::from(vec![(
Rc::new(entities.clone()), Rc::new(entities.clone()),
Rc::clone(next_process), Rc::clone(next_process),
)])), )]))
RSprocess::WaitEntity { },
repeat, RSprocess::Guarded { reaction, next_process } => {
repeated_process: _, if reaction.enabled(current_entities) {
next_process, Ok(RSchoices::from([(Rc::new(reaction.products.clone()),
} if *repeat <= 0 => unfold(environment, next_process), Rc::clone(next_process))]))
RSprocess::WaitEntity { } else {
repeat, Ok(RSchoices::new())
repeated_process, }
next_process, }
} if *repeat == 1 => { RSprocess::WaitEntity { repeat, repeated_process: _, next_process, }
let mut choices1 = unfold(environment, repeated_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)); choices1.replace(Rc::clone(next_process));
Ok(choices1) Ok(choices1)
} }
RSprocess::WaitEntity { RSprocess::WaitEntity { repeat, repeated_process, next_process, } => {
repeat, let mut choices1 = unfold(environment, repeated_process, current_entities)?;
repeated_process,
next_process,
} => {
let mut choices1 = unfold(environment, repeated_process)?;
choices1.replace(Rc::new(RSprocess::WaitEntity { choices1.replace(Rc::new(RSprocess::WaitEntity {
repeat: (*repeat - 1), repeat: (*repeat - 1),
repeated_process: Rc::clone(repeated_process), repeated_process: Rc::clone(repeated_process),
@ -64,7 +66,7 @@ pub fn unfold(
RSprocess::Summation { children } => { RSprocess::Summation { children } => {
// short-circuits with try_fold. // short-circuits with try_fold.
children.iter().try_fold(RSchoices::new(), |mut acc, x| { children.iter().try_fold(RSchoices::new(), |mut acc, x| {
match unfold(environment, x) { match unfold(environment, x, current_entities) {
Ok(mut choices) => { Ok(mut choices) => {
acc.append(&mut choices); acc.append(&mut choices);
Ok(acc) Ok(acc)
@ -82,7 +84,7 @@ pub fn unfold(
)])) )]))
} else { } else {
children.iter().try_fold(RSchoices::new(), |mut acc, x| { children.iter().try_fold(RSchoices::new(), |mut acc, x| {
acc.shuffle(unfold(environment, x)?); acc.shuffle(unfold(environment, x, current_entities)?);
Ok(acc) Ok(acc)
}) })
} }

View File

@ -166,28 +166,27 @@ fn print_process(
match process { match process {
Nill => { Nill => {
write!(f, "Nill") write!(f, "Nill")
} },
RecursiveIdentifier { identifier } => { RecursiveIdentifier { identifier } => {
write!(f, write!(f,
"[{}]", "[{}]",
translator.decode(*identifier).unwrap_or("Missing".into())) translator.decode(*identifier).unwrap_or("Missing".into()))
} },
EntitySet { EntitySet { entities, next_process, } => {
entities,
next_process,
} => {
write!( write!(
f, f,
"{}.{}", "{}.{}",
RSsetDisplay::from(translator, entities), RSsetDisplay::from(translator, entities),
RSprocessDisplay::from(translator, next_process) RSprocessDisplay::from(translator, next_process)
) )
} },
WaitEntity { Guarded { reaction, next_process } => {
repeat, write!(f,
repeated_process, "?{}?.{}",
next_process, RSreactionDisplay::from(translator, reaction),
} => { RSprocessDisplay::from(translator, next_process))
},
WaitEntity { repeat, repeated_process, next_process, } => {
write!( write!(
f, f,
"({})^{repeat}.{}", "({})^{repeat}.{}",