2025-05-21 00:03:36 +02:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
2025-06-16 14:46:04 +02:00
|
|
|
use super::structure::{RSChoices, RSenvironment, RSlabel, RSprocess, RSset, RSsystem};
|
|
|
|
|
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-06-16 14:46:04 +02:00
|
|
|
pub fn unfold(
|
|
|
|
|
environment: &RSenvironment,
|
|
|
|
|
context_process: &RSprocess,
|
|
|
|
|
) -> Result<RSChoices, String> {
|
2025-05-21 00:03:36 +02:00
|
|
|
match context_process {
|
|
|
|
|
RSprocess::Nill => Ok(RSChoices::new()),
|
2025-06-16 14:46:04 +02:00
|
|
|
RSprocess::RecursiveIdentifier { identifier } => {
|
|
|
|
|
let newprocess = environment.get(*identifier);
|
2025-05-21 00:03:36 +02:00
|
|
|
if let Some(newprocess) = newprocess {
|
|
|
|
|
unfold(environment, newprocess)
|
|
|
|
|
} else {
|
|
|
|
|
Err(format!("Recursive call to missing symbol: {identifier}"))
|
|
|
|
|
}
|
2025-06-16 14:46:04 +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 => {
|
2025-05-21 00:03:36 +02:00
|
|
|
let mut choices1 = unfold(environment, repeated_process)?;
|
|
|
|
|
choices1.replace(Rc::clone(next_process));
|
|
|
|
|
Ok(choices1)
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
|
|
|
|
RSprocess::WaitEntity {
|
|
|
|
|
repeat,
|
|
|
|
|
repeated_process,
|
|
|
|
|
next_process,
|
|
|
|
|
} => {
|
2025-05-21 00:03:36 +02:00
|
|
|
let mut choices1 = unfold(environment, repeated_process)?;
|
2025-06-16 14:46:04 +02:00
|
|
|
choices1.replace(Rc::new(RSprocess::WaitEntity {
|
|
|
|
|
repeat: (*repeat - 1),
|
|
|
|
|
repeated_process: Rc::clone(repeated_process),
|
|
|
|
|
next_process: Rc::clone(next_process),
|
|
|
|
|
}));
|
2025-05-21 00:03:36 +02:00
|
|
|
Ok(choices1)
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
|
|
|
|
RSprocess::Summation { children } => {
|
2025-05-21 00:03:36 +02:00
|
|
|
// short-circuits with try_fold.
|
|
|
|
|
children.iter().try_fold(RSChoices::new(), |mut acc, x| {
|
|
|
|
|
match unfold(environment, x) {
|
2025-06-16 14:46:04 +02:00
|
|
|
Ok(mut choices) => {
|
|
|
|
|
acc.append(&mut choices);
|
|
|
|
|
Ok(acc)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e),
|
2025-05-21 00:03:36 +02:00
|
|
|
}
|
|
|
|
|
})
|
2025-06-16 14:46:04 +02:00
|
|
|
}
|
|
|
|
|
RSprocess::NondeterministicChoice { children } => {
|
2025-05-21 00:03:36 +02:00
|
|
|
// short-circuits with try_fold.
|
2025-06-12 16:23:39 +02:00
|
|
|
if children.is_empty() {
|
2025-06-16 14:46:04 +02:00
|
|
|
Ok(RSChoices::from(vec![(
|
|
|
|
|
Rc::new(RSset::new()),
|
|
|
|
|
Rc::new(RSprocess::Nill),
|
|
|
|
|
)]))
|
2025-06-12 16:23:39 +02:00
|
|
|
} 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>(
|
|
|
|
|
system: &'a RSsystem,
|
|
|
|
|
) -> Result<TransitionsIterator<'a>, String> {
|
2025-06-12 19:16:32 +02:00
|
|
|
TransitionsIterator::from(system)
|
|
|
|
|
}
|
2025-06-12 16:23:39 +02:00
|
|
|
|
2025-06-16 14:46:04 +02:00
|
|
|
pub fn one_transition(
|
|
|
|
|
system: &RSsystem,
|
|
|
|
|
) -> Result<Option<(RSlabel, RSsystem)>, String> {
|
|
|
|
|
let mut tr = TransitionsIterator::from(system)?;
|
|
|
|
|
Ok(tr.next())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn all_transitions(
|
|
|
|
|
system: &RSsystem,
|
|
|
|
|
) -> 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
|
|
|
|
|
|
|
|
pub fn one_target(system: &RSsystem) -> Result<(i64, RSset), String> {
|
|
|
|
|
let current = one_transition(system)?;
|
|
|
|
|
if current.is_none() {
|
|
|
|
|
return Ok((0, system.get_available_entities().clone()));
|
|
|
|
|
}
|
|
|
|
|
let mut n = 1;
|
|
|
|
|
let mut current = current.unwrap().1;
|
|
|
|
|
while let Some((_, next)) = one_transition(¤t)? {
|
|
|
|
|
current = next;
|
|
|
|
|
n += 1;
|
|
|
|
|
}
|
|
|
|
|
Ok((n, current.get_available_entities().clone()))
|
|
|
|
|
}
|