use super::Snapshot;
use super::selection::{get_effective_target_states, get_transition_domain};
use crate::machine::Transition;
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct EntrySet {
pub(crate) states_to_enter: BTreeSet<usize>,
pub(crate) states_for_default_entry: BTreeSet<usize>,
pub(crate) default_history_content: BTreeMap<usize, usize>,
}
pub(crate) fn compute_entry_set(snapshot: &Snapshot<'_>, transitions: &[usize]) -> EntrySet {
let chart = snapshot.chart;
compute_entry_set_of(
snapshot,
transitions.iter().map(|&index| &chart.transitions[index]),
)
}
pub(crate) fn compute_entry_set_of<'t>(
snapshot: &Snapshot<'_>,
transitions: impl IntoIterator<Item = &'t Transition>,
) -> EntrySet {
let mut entry_set = EntrySet::default();
for transition in transitions {
for &target in &transition.targets {
add_descendant_states_to_enter(snapshot, target, &mut entry_set);
}
let domain = get_transition_domain(snapshot, transition);
for target in get_effective_target_states(snapshot, transition) {
add_ancestor_states_to_enter(snapshot, target, domain, &mut entry_set);
}
}
entry_set
}
pub(crate) fn add_descendant_states_to_enter(
snapshot: &Snapshot<'_>,
index: usize,
entry_set: &mut EntrySet,
) {
descend(snapshot, index, entry_set, &mut Vec::new());
}
fn descend(
snapshot: &Snapshot<'_>,
index: usize,
entry_set: &mut EntrySet,
visiting: &mut Vec<usize>,
) {
let chart = snapshot.chart;
if chart.is_history(index) {
enter_history_target(snapshot, index, entry_set, visiting);
return;
}
entry_set.states_to_enter.insert(index);
if chart.is_compound(index) {
entry_set.states_for_default_entry.insert(index);
let targets = initial_targets(snapshot, index);
enter_targets(snapshot, &targets, Some(index), entry_set, visiting);
} else if chart.is_parallel(index) {
enter_uncovered_regions(snapshot, index, entry_set, visiting);
}
}
pub(crate) fn add_ancestor_states_to_enter(
snapshot: &Snapshot<'_>,
index: usize,
ancestor: Option<usize>,
entry_set: &mut EntrySet,
) {
ascend(snapshot, index, ancestor, entry_set, &mut Vec::new());
}
fn ascend(
snapshot: &Snapshot<'_>,
index: usize,
ancestor: Option<usize>,
entry_set: &mut EntrySet,
visiting: &mut Vec<usize>,
) {
let chart = snapshot.chart;
for state in chart
.proper_ancestors(index)
.take_while(|&state| Some(state) != ancestor)
{
entry_set.states_to_enter.insert(state);
if chart.is_parallel(state) {
enter_uncovered_regions(snapshot, state, entry_set, visiting);
}
}
}
fn enter_history_target(
snapshot: &Snapshot<'_>,
history: usize,
entry_set: &mut EntrySet,
visiting: &mut Vec<usize>,
) {
let chart = snapshot.chart;
let parent = chart.states[history].parent;
if let Some(recorded) = snapshot.history.get(&history) {
let targets: Vec<usize> = recorded.iter().copied().collect();
enter_targets(snapshot, &targets, parent, entry_set, visiting);
return;
}
let Some(default) = chart.states[history].history_default else {
return;
};
if visiting.contains(&history) {
return;
}
if let Some(parent) = parent {
entry_set.default_history_content.insert(parent, default);
}
visiting.push(history);
let targets = &chart.transitions[default].targets;
enter_targets(snapshot, targets, parent, entry_set, visiting);
visiting.pop();
}
fn enter_targets(
snapshot: &Snapshot<'_>,
targets: &[usize],
ancestor: Option<usize>,
entry_set: &mut EntrySet,
visiting: &mut Vec<usize>,
) {
for &target in targets {
descend(snapshot, target, entry_set, visiting);
}
for &target in targets {
ascend(snapshot, target, ancestor, entry_set, visiting);
}
}
fn initial_targets(snapshot: &Snapshot<'_>, index: usize) -> Vec<usize> {
let chart = snapshot.chart;
let state = &chart.states[index];
match state.initial_transition {
Some(transition) => chart.transitions[transition].targets.clone(),
None => state.initial.clone(),
}
}
fn enter_uncovered_regions(
snapshot: &Snapshot<'_>,
index: usize,
entry_set: &mut EntrySet,
visiting: &mut Vec<usize>,
) {
let chart = snapshot.chart;
for region in chart.child_states(index) {
let covered = entry_set
.states_to_enter
.iter()
.any(|&state| chart.is_descendant(state, region));
if !covered {
descend(snapshot, region, entry_set, visiting);
}
}
}