use super::Snapshot;
use super::name_match::{name_match, tokenize};
use crate::datamodel::{Failure, Scope, evaluate_cond};
use crate::machine::Transition;
use predicator_rs::Map;
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Selected {
pub(crate) transitions: Vec<usize>,
pub(crate) evaluations: Vec<Evaluation>,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Evaluation {
pub(crate) transition: usize,
pub(crate) outcome: Outcome,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Outcome {
Enabled,
Disabled,
Error(Failure),
}
pub(crate) fn get_effective_target_states(
snapshot: &Snapshot<'_>,
transition: &Transition,
) -> Vec<usize> {
let mut targets = Vec::new();
effective_targets(snapshot, &transition.targets, &mut Vec::new(), &mut targets);
targets
}
fn effective_targets(
snapshot: &Snapshot<'_>,
targets: &[usize],
visiting: &mut Vec<usize>,
out: &mut Vec<usize>,
) {
let chart = snapshot.chart;
for &target in targets {
if !chart.is_history(target) {
out.push(target);
} else if let Some(recorded) = snapshot.history.get(&target) {
out.extend(recorded.iter().copied());
} else if !visiting.contains(&target) {
if let Some(default) = chart.states[target].history_default {
visiting.push(target);
effective_targets(snapshot, &chart.transitions[default].targets, visiting, out);
visiting.pop();
}
}
}
}
pub(crate) fn get_transition_domain(
snapshot: &Snapshot<'_>,
transition: &Transition,
) -> Option<usize> {
let targets = get_effective_target_states(snapshot, transition);
if targets.is_empty() {
return None;
}
let chart = snapshot.chart;
let source = transition.source;
if transition.transition_type == crate::document::TransitionType::Internal
&& chart.is_compound(source)
&& targets
.iter()
.all(|&target| chart.is_descendant(target, source))
{
return Some(source);
}
let mut indexes = Vec::with_capacity(targets.len() + 1);
indexes.push(source);
indexes.extend(targets);
chart.lcca(&indexes)
}
pub(crate) fn compute_exit_set(snapshot: &Snapshot<'_>, transitions: &[usize]) -> BTreeSet<usize> {
let chart = snapshot.chart;
let mut exit_set = BTreeSet::new();
for &index in transitions {
let transition = &chart.transitions[index];
if transition.targets.is_empty() {
continue;
}
if let Some(domain) = get_transition_domain(snapshot, transition) {
exit_set.extend(
snapshot
.configuration
.iter()
.copied()
.filter(|&state| chart.is_descendant(state, domain)),
);
}
}
exit_set
}
pub(crate) fn select_transitions(snapshot: &Snapshot<'_>, data: &Map, name: &str) -> Selected {
let tokens = tokenize(name);
round(snapshot, data, Some(&tokens))
}
pub(crate) fn select_eventless_transitions(snapshot: &Snapshot<'_>, data: &Map) -> Selected {
round(snapshot, data, None)
}
pub(crate) fn remove_conflicting_transitions(
snapshot: &Snapshot<'_>,
enabled: &[usize],
) -> Vec<usize> {
let chart = snapshot.chart;
let mut filtered: Vec<usize> = Vec::new();
for &candidate in enabled {
let candidate_exit = compute_exit_set(snapshot, &[candidate]);
let mut preempted = Vec::new();
let mut kept = true;
for &other in &filtered {
let other_exit = compute_exit_set(snapshot, &[other]);
if candidate_exit.is_disjoint(&other_exit) {
continue;
}
if chart.is_descendant(
chart.transitions[candidate].source,
chart.transitions[other].source,
) {
preempted.push(other);
} else {
kept = false;
break;
}
}
if kept {
filtered.retain(|index| !preempted.contains(index));
filtered.push(candidate);
}
}
filtered
}
fn round(snapshot: &Snapshot<'_>, data: &Map, tokens: Option<&[&str]>) -> Selected {
let chart = snapshot.chart;
let scope = Scope {
data,
chart,
configuration: snapshot.configuration,
};
let mut evaluations = Vec::new();
let mut enabled: Vec<usize> = Vec::new();
for atomic in snapshot
.configuration
.iter()
.copied()
.filter(|&state| chart.is_atomic(state))
{
let found = std::iter::once(atomic)
.chain(chart.proper_ancestors(atomic))
.find_map(|state| first_matching_transition(&scope, state, tokens, &mut evaluations));
if let Some(transition) = found.filter(|transition| !enabled.contains(transition)) {
enabled.push(transition);
}
}
Selected {
transitions: remove_conflicting_transitions(snapshot, &enabled),
evaluations,
}
}
fn first_matching_transition(
scope: &Scope<'_>,
state: usize,
tokens: Option<&[&str]>,
evaluations: &mut Vec<Evaluation>,
) -> Option<usize> {
let chart = scope.chart;
chart.states[state]
.transitions
.iter()
.copied()
.find(|&index| is_enabled(scope, index, &chart.transitions[index], tokens, evaluations))
}
fn is_enabled(
scope: &Scope<'_>,
index: usize,
transition: &Transition,
tokens: Option<&[&str]>,
evaluations: &mut Vec<Evaluation>,
) -> bool {
let matches = match tokens {
None => transition.events.is_empty(),
Some(tokens) => !transition.events.is_empty() && name_match(&transition.events, tokens),
};
if !matches {
return false;
}
let Some(cond) = transition.cond.as_ref() else {
return true;
};
let (enabled, outcome) = match evaluate_cond(scope, Some(cond)) {
Ok(true) => (true, Outcome::Enabled),
Ok(false) => (false, Outcome::Disabled),
Err(failure) => (false, Outcome::Error(failure)),
};
evaluations.push(Evaluation {
transition: index,
outcome,
});
enabled
}