Added guarded context
This commit is contained in:
@ -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<RSprocess>,
|
||||
},
|
||||
Guarded {
|
||||
reaction: RSreaction,
|
||||
next_process: Rc<RSprocess>,
|
||||
},
|
||||
WaitEntity {
|
||||
repeat: i64,
|
||||
repeated_process: Rc<RSprocess>,
|
||||
@ -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,
|
||||
|
||||
@ -14,7 +14,7 @@ impl<'a> TransitionsIterator<'a> {
|
||||
pub fn from(
|
||||
system: &'a RSsystem
|
||||
) -> 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 {
|
||||
choices_iterator: o.into_iter(),
|
||||
system,
|
||||
|
||||
@ -16,44 +16,46 @@ use std::rc::Rc;
|
||||
pub fn unfold(
|
||||
environment: &RSenvironment,
|
||||
context_process: &RSprocess,
|
||||
current_entities: &RSset,
|
||||
) -> Result<RSchoices, String> {
|
||||
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![(
|
||||
RSprocess::EntitySet { entities, next_process, } => {
|
||||
Ok(RSchoices::from([(
|
||||
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::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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -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}.{}",
|
||||
|
||||
Reference in New Issue
Block a user