Files
ReactionSystems/src/rsprocess/transitions.rs

156 lines
4.7 KiB
Rust
Raw Normal View History

2025-05-21 00:03:36 +02:00
#![allow(dead_code)]
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
// see unfold
2025-06-16 14:46:04 +02:00
pub fn unfold(
environment: &RSenvironment,
context_process: &RSprocess,
2025-06-18 11:28:04 +02:00
) -> Result<RSchoices, String> {
2025-05-21 00:03:36 +02:00
match context_process {
2025-07-01 19:22:50 +02:00
RSprocess::Nill => Ok(RSchoices::new()),
RSprocess::RecursiveIdentifier { identifier } => {
let newprocess = environment.get(*identifier);
if let Some(newprocess) = newprocess {
unfold(environment, newprocess)
} else {
2025-07-03 23:44:10 +02:00
Err(format!("Missing symbol in context: {identifier}"))
2025-07-01 19:22:50 +02:00
}
}
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)?;
choices1.replace(Rc::clone(next_process));
Ok(choices1)
}
RSprocess::WaitEntity {
repeat,
repeated_process,
next_process,
} => {
let mut choices1 = unfold(environment, repeated_process)?;
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| {
match unfold(environment, x) {
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| {
acc.shuffle(unfold(environment, x)?);
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> {
TransitionsIterator::from(system)
}
2025-06-12 16:23:39 +02:00
2025-06-16 16:35:54 +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-06-16 16:35:54 +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> {
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-06-16 16:35:54 +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-01 19:22:50 +02:00
return Ok((0, system.get_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(&current)? {
2025-07-01 19:22:50 +02:00
current = next;
n += 1;
2025-06-16 14:46:04 +02:00
}
Ok((n, current.get_available_entities().clone()))
}
2025-06-16 16:35:54 +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
// see smartOneRunECT, smartRunECT
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();
res.push(current.0.get_context());
let mut current = current.1;
while let Some((label, next)) = one_transition(&current)? {
2025-07-01 19:22:50 +02:00
current = next;
res.push(label.get_context());
2025-06-17 13:45:35 +02:00
}
Ok(res)
}