use super::entering::{EntrySet, compute_entry_set, compute_entry_set_of};
use super::selection::compute_exit_set;
use super::{History, Snapshot};
use crate::document::HistoryType;
use crate::machine::Transition;
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Exit {
pub(crate) states: Vec<usize>,
pub(crate) history: History,
pub(crate) configuration: BTreeSet<usize>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Completion {
pub(crate) name: String,
pub(crate) final_state: usize,
pub(crate) donedata: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Arrival {
pub(crate) index: usize,
pub(crate) initial_content: Option<usize>,
pub(crate) history_content: Option<usize>,
pub(crate) completions: Vec<Completion>,
pub(crate) stops: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Entry {
pub(crate) states: Vec<Arrival>,
pub(crate) configuration: BTreeSet<usize>,
}
pub(crate) fn exit_states(snapshot: &Snapshot<'_>, transitions: &[usize]) -> Exit {
let exit_set = compute_exit_set(snapshot, transitions);
let states: Vec<usize> = exit_set.iter().rev().copied().collect();
Exit {
history: record_history_values(snapshot, &states),
configuration: snapshot
.configuration
.difference(&exit_set)
.copied()
.collect(),
states,
}
}
pub(crate) fn record_history_values(snapshot: &Snapshot<'_>, states_to_exit: &[usize]) -> History {
let chart = snapshot.chart;
let mut history = snapshot.history.clone();
for &state in states_to_exit {
for &child in &chart.states[state].history_children {
let deep = chart.states[child].history_type == Some(HistoryType::Deep);
let value = snapshot
.configuration
.iter()
.copied()
.filter(|&active| {
if deep {
chart.is_atomic(active) && chart.is_descendant(active, state)
} else {
chart.states[active].parent == Some(state)
}
})
.collect();
history.insert(child, value);
}
}
history
}
pub(crate) fn enter_states(snapshot: &Snapshot<'_>, transitions: &[usize]) -> Entry {
arrivals(snapshot, &compute_entry_set(snapshot, transitions))
}
pub(crate) fn enter_states_of<'t>(
snapshot: &Snapshot<'_>,
transitions: impl IntoIterator<Item = &'t Transition>,
) -> Entry {
arrivals(snapshot, &compute_entry_set_of(snapshot, transitions))
}
fn arrivals(snapshot: &Snapshot<'_>, entry_set: &EntrySet) -> Entry {
let chart = snapshot.chart;
let mut configuration = snapshot.configuration.clone();
let mut states = Vec::with_capacity(entry_set.states_to_enter.len());
for &index in &entry_set.states_to_enter {
configuration.insert(index);
let entered = Snapshot {
configuration: &configuration,
..*snapshot
};
let stops = chart.is_final(index) && chart.states[index].parent == Some(0);
states.push(Arrival {
index,
initial_content: entry_set
.states_for_default_entry
.contains(&index)
.then_some(chart.states[index].initial_transition)
.flatten(),
history_content: entry_set.default_history_content.get(&index).copied(),
completions: if stops {
Vec::new()
} else {
completion_events(&entered, index)
},
stops,
});
}
Entry {
states,
configuration,
}
}
fn completion_events(snapshot: &Snapshot<'_>, index: usize) -> Vec<Completion> {
let chart = snapshot.chart;
if !chart.is_final(index) {
return Vec::new();
}
let Some(parent) = chart.states[index].parent else {
return Vec::new();
};
let Some(parent_id) = id(snapshot, parent) else {
return Vec::new();
};
let mut completions = vec![Completion {
name: format!("done.state.{parent_id}"),
final_state: index,
donedata: true,
}];
let grandparent = chart.states[parent].parent;
if let Some(grandparent) = grandparent.filter(|&state| chart.is_parallel(state)) {
let complete = chart
.child_states(grandparent)
.all(|region| is_in_final_state(snapshot, region));
if let Some(grandparent_id) = id(snapshot, grandparent).filter(|_| complete) {
completions.push(Completion {
name: format!("done.state.{grandparent_id}"),
final_state: index,
donedata: false,
});
}
}
completions
}
fn id<'a>(snapshot: &Snapshot<'a>, index: usize) -> Option<&'a str> {
snapshot.chart.states[index]
.id
.as_deref()
.filter(|id| !id.is_empty())
}
pub(crate) fn is_in_final_state(snapshot: &Snapshot<'_>, index: usize) -> bool {
let chart = snapshot.chart;
if chart.is_compound(index) {
chart
.child_states(index)
.any(|child| snapshot.is_active(child) && chart.is_final(child))
} else if chart.is_parallel(index) {
chart
.child_states(index)
.all(|region| is_in_final_state(snapshot, region))
} else {
false
}
}