2025-07-10 15:02:14 +02:00
|
|
|
//! Definitions for simple simulation steps.
|
|
|
|
|
|
2025-07-01 19:22:50 +02:00
|
|
|
use super::structure::{RSchoices,
|
|
|
|
|
RSenvironment,
|
|
|
|
|
RSlabel,
|
|
|
|
|
RSprocess,
|
|
|
|
|
RSset,
|
|
|
|
|
RSsystem};
|
2025-06-16 14:46:04 +02:00
|
|
|
use super::support_structures::TransitionsIterator;
|
2025-05-21 00:03:36 +02:00
|
|
|
use std::rc::Rc;
|
2025-06-12 16:23:39 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// unfold returns the list of choices for the context given the process
|
|
|
|
|
/// definitions environment. RSchoices is a list of context moves mapping a set
|
2025-07-10 15:02:14 +02:00
|
|
|
/// of entities and the continuation.
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see unfold
|
2025-06-16 14:46:04 +02:00
|
|
|
pub fn unfold(
|
|
|
|
|
environment: &RSenvironment,
|
|
|
|
|
context_process: &RSprocess,
|
2025-08-20 13:51:48 +02:00
|
|
|
current_entities: &RSset,
|
2025-06-18 11:28:04 +02:00
|
|
|
) -> Result<RSchoices, String> {
|
2025-05-21 00:03:36 +02:00
|
|
|
match context_process {
|
2025-08-20 13:51:48 +02:00
|
|
|
RSprocess::Nill => {
|
|
|
|
|
Ok(RSchoices::new())
|
|
|
|
|
},
|
2025-07-01 19:22:50 +02:00
|
|
|
RSprocess::RecursiveIdentifier { identifier } => {
|
|
|
|
|
let newprocess = environment.get(*identifier);
|
|
|
|
|
if let Some(newprocess) = newprocess {
|
2025-08-20 13:51:48 +02:00
|
|
|
unfold(environment, newprocess, current_entities)
|
2025-07-01 19:22:50 +02:00
|
|
|
} else {
|
2025-07-03 23:44:10 +02:00
|
|
|
Err(format!("Missing symbol in context: {identifier}"))
|
2025-07-01 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-20 13:51:48 +02:00
|
|
|
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)?;
|
2025-07-01 19:22:50 +02:00
|
|
|
choices1.replace(Rc::clone(next_process));
|
|
|
|
|
Ok(choices1)
|
|
|
|
|
}
|
2025-08-20 13:51:48 +02:00
|
|
|
RSprocess::WaitEntity { repeat, repeated_process, next_process, } => {
|
|
|
|
|
let mut choices1 = unfold(environment, repeated_process, current_entities)?;
|
2025-07-01 19:22:50 +02:00
|
|
|
choices1.replace(Rc::new(RSprocess::WaitEntity {
|
|
|
|
|
repeat: (*repeat - 1),
|
|
|
|
|
repeated_process: Rc::clone(repeated_process),
|
|
|
|
|
next_process: Rc::clone(next_process),
|
|
|
|
|
}));
|
|
|
|
|
Ok(choices1)
|
|
|
|
|
}
|
|
|
|
|
RSprocess::Summation { children } => {
|
|
|
|
|
// short-circuits with try_fold.
|
|
|
|
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
2025-08-20 13:51:48 +02:00
|
|
|
match unfold(environment, x, current_entities) {
|
2025-07-01 19:22:50 +02:00
|
|
|
Ok(mut choices) => {
|
|
|
|
|
acc.append(&mut choices);
|
|
|
|
|
Ok(acc)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
RSprocess::NondeterministicChoice { children } => {
|
|
|
|
|
// short-circuits with try_fold.
|
|
|
|
|
if children.is_empty() {
|
|
|
|
|
Ok(RSchoices::from(vec![(
|
|
|
|
|
Rc::new(RSset::new()),
|
|
|
|
|
Rc::new(RSprocess::Nill),
|
|
|
|
|
)]))
|
|
|
|
|
} else {
|
|
|
|
|
children.iter().try_fold(RSchoices::new(), |mut acc, x| {
|
2025-08-20 13:51:48 +02:00
|
|
|
acc.shuffle(unfold(environment, x, current_entities)?);
|
2025-07-01 19:22:50 +02:00
|
|
|
Ok(acc)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-21 00:03:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-06-12 16:23:39 +02:00
|
|
|
|
2025-06-16 14:46:04 +02:00
|
|
|
pub fn iterator_transitions<'a>(
|
2025-07-01 19:22:50 +02:00
|
|
|
system: &'a RSsystem
|
2025-06-16 14:46:04 +02:00
|
|
|
) -> Result<TransitionsIterator<'a>, String> {
|
2025-06-12 19:16:32 +02:00
|
|
|
TransitionsIterator::from(system)
|
|
|
|
|
}
|
2025-06-12 16:23:39 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see oneTransition, transition, smartTransition, smartOneTransition
|
2025-06-16 14:46:04 +02:00
|
|
|
pub fn one_transition(
|
2025-07-01 19:22:50 +02:00
|
|
|
system: &RSsystem
|
2025-06-16 14:46:04 +02:00
|
|
|
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
|
|
|
|
let mut tr = TransitionsIterator::from(system)?;
|
|
|
|
|
Ok(tr.next())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see allTransitions, smartAllTransitions
|
2025-06-16 14:46:04 +02:00
|
|
|
pub fn all_transitions(
|
2025-07-01 19:22:50 +02:00
|
|
|
system: &RSsystem
|
2025-06-16 14:46:04 +02:00
|
|
|
) -> Result<Vec<(RSlabel, RSsystem)>, String> {
|
2025-06-12 19:16:32 +02:00
|
|
|
let tr = TransitionsIterator::from(system)?;
|
|
|
|
|
Ok(tr.collect::<Vec<_>>())
|
2025-06-12 16:23:39 +02:00
|
|
|
}
|
2025-06-16 14:46:04 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see oneTarget, smartOneTarget, target, smartTarget
|
2025-07-01 19:22:50 +02:00
|
|
|
pub fn target(
|
|
|
|
|
system: &RSsystem
|
|
|
|
|
) -> Result<(i64, RSset), String> {
|
2025-06-16 14:46:04 +02:00
|
|
|
let current = one_transition(system)?;
|
|
|
|
|
if current.is_none() {
|
2025-07-09 16:12:22 +02:00
|
|
|
return Ok((0, system.available_entities.clone()));
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
|
|
|
|
let mut n = 1;
|
|
|
|
|
let mut current = current.unwrap().1;
|
|
|
|
|
while let Some((_, next)) = one_transition(¤t)? {
|
2025-07-01 19:22:50 +02:00
|
|
|
current = next;
|
|
|
|
|
n += 1;
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
2025-07-09 16:12:22 +02:00
|
|
|
Ok((n, current.available_entities.clone()))
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
2025-06-16 16:35:54 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see oneRun, run, smartOneRunEK, smartRunEK
|
2025-06-17 13:45:35 +02:00
|
|
|
pub fn run(system: RSsystem) -> Result<Vec<Rc<RSsystem>>, String> {
|
2025-06-16 16:35:54 +02:00
|
|
|
let mut res = vec![Rc::new(system)];
|
|
|
|
|
while let Some((_, next_sys)) = one_transition(res.last().unwrap())? {
|
2025-07-01 19:22:50 +02:00
|
|
|
res.push(Rc::new(next_sys));
|
2025-06-16 16:35:54 +02:00
|
|
|
}
|
|
|
|
|
Ok(res)
|
|
|
|
|
}
|
2025-06-17 13:45:35 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// see smartOneRunECT, smartRunECT
|
2025-06-19 23:48:16 +02:00
|
|
|
pub fn run_separated(
|
|
|
|
|
system: &RSsystem
|
|
|
|
|
) -> Result<Vec<(RSset, RSset, RSset)>, String> {
|
2025-06-17 13:45:35 +02:00
|
|
|
let mut res = vec![];
|
|
|
|
|
let current = one_transition(system)?;
|
|
|
|
|
if current.is_none() {
|
2025-07-01 19:22:50 +02:00
|
|
|
return Ok(res);
|
2025-06-17 13:45:35 +02:00
|
|
|
}
|
|
|
|
|
let current = current.unwrap();
|
2025-07-09 16:12:22 +02:00
|
|
|
let (available_entities, context, t) = current.0.get_context();
|
|
|
|
|
res.push((available_entities.clone(), context.clone(), t.clone()));
|
2025-06-17 13:45:35 +02:00
|
|
|
let mut current = current.1;
|
|
|
|
|
while let Some((label, next)) = one_transition(¤t)? {
|
2025-07-01 19:22:50 +02:00
|
|
|
current = next;
|
2025-07-09 16:12:22 +02:00
|
|
|
let (available_entities, context, t) = label.get_context();
|
|
|
|
|
res.push((available_entities.clone(), context.clone(), t.clone()));
|
2025-06-17 13:45:35 +02:00
|
|
|
}
|
|
|
|
|
Ok(res)
|
|
|
|
|
}
|