use super::content::{Counters, Raised, Routes, Sends, Site, execute_block};
use super::{History, Snapshot};
use crate::datamodel::Failure;
use crate::datamodel::system_variables::{self, EventFields, EventType};
use crate::effect::{BlockOwner, Effect};
use crate::machine::{Chart, Machine};
use predicator_rs::{Map, Value};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
pub(crate) const MAX_MACROSTEP_ROUNDS: u64 = 10_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RoundBudget {
Rounds(u64),
Unbounded,
}
impl Default for RoundBudget {
fn default() -> Self {
RoundBudget::Rounds(MAX_MACROSTEP_ROUNDS)
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Event {
pub(crate) name: String,
pub(crate) event_type: EventType,
pub(crate) sendid: Option<String>,
pub(crate) origin: Option<String>,
pub(crate) origintype: Option<String>,
pub(crate) invokeid: Option<String>,
pub(crate) data: Value,
}
impl Event {
pub(crate) fn internal(name: &str) -> Self {
Event::bare(name, EventType::Internal, Value::Undefined)
}
pub(crate) fn platform(name: &str, data: Value) -> Self {
Event::bare(name, EventType::Platform, data)
}
pub(crate) fn from_failure(failure: &Failure) -> Self {
let mut event = Event::platform(failure.event_name(), failure.data());
if let Failure::SendRejected { send_id, .. } = failure {
event.sendid = Some(send_id.clone());
}
event
}
fn bare(name: &str, event_type: EventType, data: Value) -> Self {
Event {
name: name.to_owned(),
event_type,
sendid: None,
origin: None,
origintype: None,
invokeid: None,
data,
}
}
pub(crate) fn value(&self) -> Value {
system_variables::event(&EventFields {
name: &self.name,
event_type: self.event_type,
sendid: self.sendid.as_deref(),
origin: self.origin.as_deref(),
origintype: self.origintype.as_deref(),
invokeid: self.invokeid.as_deref(),
data: self.data.clone(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Status {
Running,
Done,
}
#[derive(Debug, Clone, Default)]
pub(crate) struct StartOptions {
pub(crate) session_id: String,
pub(crate) datamodel: Map,
pub(crate) send_types: BTreeSet<String>,
pub(crate) ioprocessors: Map,
pub(crate) routes: Option<Routes>,
pub(crate) max_macrostep_rounds: RoundBudget,
}
#[derive(Debug, Clone)]
pub(crate) struct MachineState {
pub(crate) machine: Machine,
pub(crate) configuration: BTreeSet<usize>,
pub(crate) history: History,
pub(crate) datamodel: Map,
pub(crate) internal_queue: VecDeque<Event>,
pub(crate) entered_states: BTreeSet<usize>,
pub(crate) sends: Sends,
pub(crate) states_to_invoke: BTreeSet<usize>,
pub(crate) active_invocations: BTreeMap<(usize, u64), String>,
pub(crate) invoke_counter: u64,
pub(crate) send_types: BTreeSet<String>,
pub(crate) routes: Option<Routes>,
pub(crate) last_selection: Option<bool>,
pub(crate) running: bool,
pub(crate) status: Status,
pub(crate) counters: Counters,
pub(crate) max_macrostep_rounds: RoundBudget,
}
impl MachineState {
pub(crate) fn new(machine: &Machine, options: StartOptions) -> Self {
let chart = machine.chart();
let variables = system_variables::initial(
&options.session_id,
chart.name.as_deref(),
options.ioprocessors,
);
MachineState {
machine: machine.clone(),
configuration: BTreeSet::new(),
history: History::new(),
datamodel: system_variables::seed(options.datamodel, variables),
internal_queue: VecDeque::new(),
entered_states: BTreeSet::new(),
sends: Sends::default(),
states_to_invoke: BTreeSet::new(),
active_invocations: BTreeMap::new(),
invoke_counter: 0,
send_types: options.send_types,
routes: options.routes,
last_selection: None,
running: true,
status: Status::Running,
counters: Counters::default(),
max_macrostep_rounds: options.max_macrostep_rounds,
}
}
pub(crate) fn chart(&self) -> &Chart {
self.machine.chart()
}
pub(crate) fn snapshot(&self) -> Snapshot<'_> {
Snapshot {
chart: self.machine.chart(),
configuration: &self.configuration,
history: &self.history,
}
}
pub(crate) fn begin_macrostep(&mut self) {
self.counters = Counters {
macrostep: self.counters.macrostep.saturating_add(1),
microstep: 0,
round: 0,
};
}
pub(crate) fn begin_microstep(&mut self) {
self.counters.microstep = self.counters.microstep.saturating_add(1);
}
pub(crate) fn begin_round(&mut self) {
self.counters.round = self.counters.round.saturating_add(1);
}
pub(crate) fn enqueue(&mut self, event: Event) {
self.internal_queue.push_back(event);
}
pub(crate) fn raise_failure(&mut self, failure: &Failure) {
self.enqueue(Event::from_failure(failure));
}
pub(crate) fn put_event(&mut self, event: &Event) {
self.datamodel = system_variables::put_event(&self.datamodel, event.value());
}
pub(crate) fn run_block(
&mut self,
owner: BlockOwner,
content: &[usize],
effects: &mut Vec<Effect>,
) {
if content.is_empty() {
return;
}
let machine = self.machine.clone();
let site = Site {
chart: machine.chart(),
configuration: &self.configuration,
owner,
counters: self.counters,
send_types: &self.send_types,
routes: self.routes.as_ref(),
};
let ran = execute_block(
&site,
std::mem::take(&mut self.datamodel),
self.sends,
content,
);
self.datamodel = ran.data;
self.sends = ran.sends;
effects.extend(ran.effects);
for raised in ran.raised {
self.enqueue(match raised {
Raised::Event { name, .. } => Event::internal(&name),
Raised::Error { failure, .. } => Event::from_failure(&failure),
});
}
}
pub(crate) fn pending_internal_events(&self) -> Vec<Value> {
self.internal_queue.iter().map(Event::value).collect()
}
}