#[cfg(test)]
mod tests;
use super::{Call, Invocation, Mail, RoundBudget, State};
use crate::datamodel::system_variables::{EventType, SCXML_EVENT_PROCESSOR, scxml_location};
use crate::effect::Effect;
use crate::interpreter::{self, Event, Routes, StartOptions, is_scxml_type};
use crate::machine::Machine;
use predicator_rs::{Key, Map, Value};
use std::collections::BTreeSet;
impl Call {
pub(super) fn within(&mut self, invoke_id: &str, run: impl FnOnce(&mut Call)) {
let Some(mut invocation) = self.state.invocations.remove(invoke_id) else {
return;
};
let mut child = invocation.child;
child.now_ms = self.state.now_ms;
child.sequence = self.state.sequence;
let mut call = Call::open(child);
call.first_sequence = self.first_sequence;
call.fired = self.fired;
run(&mut call);
self.fired = call.fired;
let (child, effects, outbox) = call.finish();
self.state.sequence = child.sequence;
self.effects.extend(effects);
invocation.child = child;
self.state
.invocations
.insert(invoke_id.to_owned(), invocation);
self.mailbox.extend(outbox);
}
pub(super) fn start_child(
&mut self,
invoke_id: &str,
invoke_type: Option<&Value>,
params: &Value,
content: Option<&Value>,
) -> Vec<Effect> {
if !is_scxml_type(invoke_type) {
return self.deliver_internal(Event::platform("error.execution", Value::Undefined));
}
let machine = match content {
Some(Value::String(source)) => crate::compile_inline(source),
_ => None,
};
let Some(machine) = machine else {
return self.deliver_internal(Event::platform("error.communication", Value::Undefined));
};
let session_id = format!("{}.{invoke_id}", self.state.session_id);
let send_types = self.state.core.send_types.clone();
let ioprocessors = send_types
.iter()
.map(|send_type| (send_type.as_str(), Value::Map(Map::new())))
.collect();
let started = interpreter::initialize(
&machine,
StartOptions {
session_id: session_id.clone(),
datamodel: seed(params, &machine),
send_types,
ioprocessors,
routes: Some(Routes {
sessions: BTreeSet::from([session_id.clone()]),
parent: true,
invokes: BTreeSet::new(),
}),
max_macrostep_rounds: RoundBudget::default().interpreter(),
},
);
let mut child = State::fresh(started.state, session_id, RoundBudget::default(), false);
child.invoked_as = Some(invoke_id.to_owned());
self.state.invocations.insert(
invoke_id.to_owned(),
Invocation {
completed: false,
child,
},
);
self.within(invoke_id, |call| {
call.perform(started.effects);
call.drain();
});
Vec::new()
}
pub(super) fn cancel_child(&mut self, invoke_id: &str) {
match self.state.invocations.get(invoke_id) {
Some(invocation) if !invocation.completed => {}
_ => return,
}
let Some(invocation) = self.state.invocations.remove(invoke_id) else {
return;
};
let mut child = invocation.child;
if !child.core.running {
return;
}
child.cancelled = true;
child.now_ms = self.state.now_ms;
child.sequence = self.state.sequence;
let Ok(stopped) = interpreter::cancel(&child.core) else {
return;
};
let mut call = Call::open(child);
call.first_sequence = self.first_sequence;
call.fired = self.fired;
call.state.core = stopped.state;
call.perform(stopped.effects);
self.fired = call.fired;
let (child, effects, _) = call.finish();
self.state.sequence = child.sequence;
self.effects.extend(effects);
}
pub(super) fn deliver(&mut self, invoke_id: &str, event: Event) {
let running = self
.state
.invocations
.get(invoke_id)
.is_some_and(|invocation| invocation.child.core.running);
if running {
self.within(invoke_id, |call| {
call.enqueue_own(event, false);
call.drain();
});
}
}
pub(super) fn post_to_parent(&mut self, mut event: Event, counted: bool) {
let Some(invoke_id) = self.state.invoked_as.clone() else {
return;
};
event.invokeid = Some(invoke_id.clone());
self.outbox
.push((Mail::Event { invoke_id, event }, counted));
}
pub(super) fn announce(&mut self, batch: &[Effect]) {
if self.state.cancelled
|| !batch
.iter()
.any(|effect| matches!(effect, Effect::Done { .. }))
{
return;
}
if let Some(invoke_id) = self.state.invoked_as.clone() {
self.outbox.push((Mail::Completed { invoke_id }, false));
}
}
pub(super) fn return_done(&mut self, donedata: Value) {
if self.state.cancelled {
return;
}
if let Some(invoke_id) = self.state.invoked_as.clone() {
self.outbox.push((
Mail::Done {
invoke_id,
donedata,
},
false,
));
}
}
pub(super) fn take_mail(&mut self, mail: Mail) {
let invoke_id = mail.invoke_id().to_owned();
if !self.state.invocations.contains_key(&invoke_id) {
return;
}
match mail {
Mail::Completed { .. } => {
if let Some(invocation) = self.state.invocations.get_mut(&invoke_id) {
invocation.completed = true;
}
}
Mail::Event { event, .. } => self.take(event),
Mail::Done { donedata, .. } => {
let event = done_event(&self.state.session_id, &invoke_id, donedata);
self.take(event);
self.state.invocations.remove(&invoke_id);
}
}
}
}
fn done_event(session_id: &str, invoke_id: &str, donedata: Value) -> Event {
Event {
name: format!("done.invoke.{invoke_id}"),
event_type: EventType::External,
sendid: None,
origin: Some(scxml_location(session_id)),
origintype: Some(SCXML_EVENT_PROCESSOR.to_owned()),
invokeid: Some(invoke_id.to_owned()),
data: donedata,
}
}
fn seed(params: &Value, machine: &Machine) -> Map {
let mut seeded = Map::new();
let Value::Map(params) = params else {
return seeded;
};
let chart = machine.chart();
let root_data = chart
.states
.first()
.map_or(&[][..], |root| root.data.as_slice());
for &index in root_data {
let Some(data) = chart.data.get(index) else {
continue;
};
let key = Key::from(data.id.as_str());
if let Some(value) = params.get(&key) {
seeded.insert(key, value.clone());
}
}
seeded
}