temporal_control/stnu/reaction.rs
1//! Reaction-time variants: same-tick and next-tick reaction to observations.
2
3use super::Stnu;
4use crate::{Error, Ticks, TimePoint};
5
6/// When the agent may react to an observation of a contingent point.
7#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
8pub enum Reaction {
9 /// An observation at tick `t` may influence executions at `t` (the crate's semantics).
10 #[default]
11 SameTick,
12 /// An observation at tick `t` may only influence executions from `t + 1` on: decisions
13 /// at `t` see contingent events strictly before `t`.
14 NextTick,
15}
16
17impl Stnu {
18 /// The network whose same-tick dynamic controllability equals this network's under
19 /// `reaction`.
20 ///
21 /// For [`Reaction::SameTick`] this is a copy. For [`Reaction::NextTick`] every contingent
22 /// point `C` is replaced by its *observation* `C' = C + 1`, which the agent sees at tick
23 /// `C'`, i.e. one tick after `C` happened: a link from an executable point becomes
24 /// `[lo + 1, hi + 1]`, a link from a contingent point keeps its bounds (both ends shift),
25 /// and every requirement is rewritten with `C = C' - 1`. Under same-tick reaction an agent
26 /// of the new network sees exactly what a next-tick agent of the old one sees, so the
27 /// transformation is exact; the tests check it against the brute-force game with the
28 /// next-tick move order. Point and constraint indices are unchanged, so a
29 /// [`SemiReducibleCycle`](super::SemiReducibleCycle) of the result names the same
30 /// constraints and links.
31 ///
32 /// Dispatching the result needs an executive that reports each contingent event one tick
33 /// late (`C` observed at `t` is handed to the dispatcher at `t + 1`); the dispatcher itself
34 /// is unchanged.
35 ///
36 /// # Examples
37 ///
38 /// ```
39 /// use temporal_control::{Budget, Controllability, Reaction, Stnu, Ticks};
40 ///
41 /// // X exactly at the end of a [1, 10] activity: needs same-tick reaction.
42 /// let mut stnu = Stnu::new();
43 /// let a = stnu.add_time_point().expect("fits");
44 /// let c = stnu.add_contingent_link(a, Ticks(1), Ticks(10)).expect("valid");
45 /// let x = stnu.add_time_point().expect("fits");
46 /// stnu.add_constraint(c, x, Some(Ticks(0)), Some(Ticks(0))).expect("exists");
47 ///
48 /// let dc = |s: &Stnu| {
49 /// let verdict = s.check_controllability(&Budget::UNLIMITED).expect("fits");
50 /// matches!(verdict, Controllability::Controllable(()))
51 /// };
52 /// assert!(dc(&stnu));
53 /// assert!(!dc(&stnu.with_reaction(Reaction::NextTick).expect("no overflow")));
54 /// ```
55 ///
56 /// # Errors
57 ///
58 /// [`Error::Overflow`] if a shifted bound overflows `i64`.
59 pub fn with_reaction(&self, reaction: Reaction) -> Result<Self, Error> {
60 if reaction == Reaction::SameTick {
61 return Ok(self.clone());
62 }
63
64 let n = self.time_point_count();
65 let mut link_of = alloc::vec![None; n];
66
67 for (i, link) in self.links.iter().enumerate() {
68 link_of[link.contingent.0] = Some(i);
69 }
70
71 let shift = |p: TimePoint| i64::from(link_of[p.0].is_some());
72 let mut out = Self::new();
73
74 for link in &link_of {
75 let Some(i) = *link else {
76 out.add_time_point()?;
77 continue;
78 };
79
80 let link = self.links[i];
81 let d = Ticks(1 - shift(link.activation));
82 let lo = link.lo.checked_add(d).ok_or(Error::Overflow)?;
83 let hi = link.hi.checked_add(d).ok_or(Error::Overflow)?;
84 out.add_contingent_link(link.activation, lo, hi)?;
85 }
86
87 for c in self.requirements.constraints() {
88 let d = Ticks(shift(c.to) - shift(c.from));
89 let moved = |b: Option<Ticks>| {
90 b.map(|b| b.checked_add(d).ok_or(Error::Overflow))
91 .transpose()
92 };
93 out.add_constraint(c.from, c.to, moved(c.lo)?, moved(c.hi)?)?;
94 }
95
96 Ok(out)
97 }
98}