hydr_structures/structures/actuator/snapshot/
restore.rs1use super::super::*;
4use super::compatibility::{as_definition, require};
5use super::receipt_bits::ReceiptBits;
6use crate::structures::StructureError;
7use hydr_core::contract::package_identity;
8use numr::dtype::DType;
9use numr::runtime::Runtime;
10use std::sync::Arc;
11
12impl<R: Runtime<DType = DType>> GateActuatorBatch<R> {
13 pub fn restore(&mut self, snapshot: &GateActuatorSnapshot<R>) -> Result<(), StructureError> {
15 super::compatibility::trusted(self, &snapshot.batch)?;
16 self.state = snapshot.batch.state.clone();
17 self.time = snapshot.batch.time;
18 self.next_target = snapshot.batch.next_target;
19 self.steps = snapshot.batch.steps;
20 self.last = snapshot.batch.last.clone();
21 Ok(())
22 }
23 pub fn restore_data(&mut self, data: &GateActuatorSnapshotData) -> Result<(), StructureError>
25 where
26 R::Client: numr::ops::BinaryOps<R>
27 + numr::ops::CompareOps<R>
28 + numr::ops::ConditionalOps<R>
29 + numr::ops::ReduceOps<R>
30 + numr::ops::ScalarOps<R>
31 + numr::ops::TwoSumOps<R>
32 + numr::ops::TypeConversionOps<R>
33 + numr::ops::UnaryOps<R>,
34 {
35 package_identity(
36 "gate actuator",
37 &data.schema,
38 GATE_ACTUATOR_SNAPSHOT_SCHEMA,
39 &data.contract,
40 GATE_ACTUATOR_CONTRACT_VERSION,
41 )
42 .map_err(StructureError::definition)?;
43 self.restore_checked(data).map_err(as_definition)
44 }
45 fn restore_checked(&mut self, data: &GateActuatorSnapshotData) -> Result<(), StructureError>
46 where
47 R::Client: numr::ops::BinaryOps<R>
48 + numr::ops::CompareOps<R>
49 + numr::ops::ConditionalOps<R>
50 + numr::ops::ReduceOps<R>
51 + numr::ops::ScalarOps<R>
52 + numr::ops::TwoSumOps<R>
53 + numr::ops::TypeConversionOps<R>
54 + numr::ops::UnaryOps<R>,
55 {
56 super::compatibility::check(self, data)?;
57 require(
58 data.time_s.is_finite()
59 && data.time_s >= self.schedule().initial_time_s
60 && data.time_s <= self.schedule().end_s,
61 "imported actuator time is outside original finite support",
62 )?;
63 let processed = self
64 .schedule()
65 .target_times_s
66 .partition_point(|t| *t <= data.time_s);
67 require(
68 data.next_target_index == processed
69 && data.accepted_steps >= processed
70 && (data.accepted_steps == 0) == data.last_step.is_none(),
71 "imported actuator event index, interval count, or last-step presence is inconsistent",
72 )?;
73 if data.accepted_steps == 0 {
74 require(
75 data.time_s.same_bits(&self.schedule().initial_time_s),
76 "zero-step actuator continuation changed its original initial time",
77 )?;
78 }
79 let state = super::state_check::check(
80 self,
81 &data.states,
82 data.time_s,
83 processed,
84 data.accepted_steps,
85 )?;
86 let mut candidate = Self {
87 context: Arc::clone(&self.context),
88 state,
89 time: data.time_s,
90 next_target: processed,
91 steps: data.accepted_steps,
92 last: None,
93 };
94 if let Some(last) = &data.last_step {
95 require(
96 last.start_s.is_finite()
97 && last.start_s >= self.schedule().initial_time_s
98 && last.start_s < last.end_s
99 && last.end_s.same_bits(&data.time_s),
100 "retained actuator immediate step does not end at accepted time with a valid original start",
101 )?;
102 require(
103 if data.accepted_steps == 1 {
104 last.start_s.same_bits(&self.schedule().initial_time_s)
105 } else {
106 last.start_s > self.schedule().initial_time_s
107 },
108 "retained actuator step start contradicts accepted interval count",
109 )?;
110 let prior_processed = self
111 .schedule()
112 .target_times_s
113 .partition_point(|t| *t <= last.start_s);
114 let prior_steps = data.accepted_steps.checked_sub(1).ok_or_else(|| {
115 StructureError::numerical("retained actuator step has no prior interval count")
116 })?;
117 require(
118 prior_steps >= prior_processed && last.receipts.len() == self.definitions().len(),
119 "retained actuator prior interval count or receipt vector is incomplete",
120 )?;
121 let rows = last
122 .receipts
123 .iter()
124 .map(|r| r.before.clone())
125 .collect::<Vec<_>>();
126 let before_state =
127 super::state_check::check(self, &rows, last.start_s, prior_processed, prior_steps)?;
128 let before = Self {
129 context: Arc::clone(&self.context),
130 state: before_state,
131 time: last.start_s,
132 next_target: prior_processed,
133 steps: prior_steps,
134 last: None,
135 };
136 let updates = last
137 .receipts
138 .iter()
139 .filter_map(|r| r.update.clone())
140 .collect::<Vec<_>>();
141 let trial = before
142 .trial_to(last.end_s, &updates)
143 .map_err(as_definition)?;
144 require(
145 trial.diagnostics()?.same_bits(last),
146 "retained actuator immediate diagnostics differ from recomputed original trial",
147 )?;
148 require(
149 trial.states()?.same_bits(&data.states),
150 "imported actuator state differs from recomputed immediate trial",
151 )?;
152 candidate.last = trial.into_next().last;
153 } else {
154 let initial = crate::structures::numerics::actuator::initial(&candidate.context)
155 .map_err(as_definition)?;
156 require(
157 super::super::projection::states(&candidate.context, &initial)?
158 .same_bits(&data.states),
159 "zero-step actuator continuation differs from canonical original initial state",
160 )?;
161 }
162 *self = candidate;
163 Ok(())
164 }
165}