hydr_structures/structures/control/snapshot/
restore.rs1use super::super::OperatingControlBatch;
4use super::compatibility::require;
5use super::{
6 OPERATING_CONTROL_CONTRACT_VERSION, OPERATING_CONTROL_SNAPSHOT_SCHEMA,
7 OperatingControlSnapshot, OperatingControlSnapshotData,
8};
9use crate::structures::{StructureError, StructureInputErrorKind};
10use hydr_core::contract::package_identity;
11use numr::dtype::DType;
12use numr::runtime::Runtime;
13use std::sync::Arc;
14
15impl<R: Runtime<DType = DType>> OperatingControlBatch<R> {
16 pub fn restore(
18 &mut self,
19 snapshot: &OperatingControlSnapshot<R>,
20 ) -> Result<(), StructureError> {
21 super::compatibility::check_batch(self, &snapshot.batch)?;
22 self.state = snapshot.batch.state.clone();
23 self.time = snapshot.batch.time;
24 self.next_decision = snapshot.batch.next_decision;
25 self.steps = snapshot.batch.steps;
26 self.last = snapshot.batch.last.clone();
27 Ok(())
28 }
29 pub fn restore_data(
31 &mut self,
32 data: &OperatingControlSnapshotData,
33 ) -> Result<(), StructureError>
34 where
35 R::Client: numr::ops::BinaryOps<R>
36 + numr::ops::CompareOps<R>
37 + numr::ops::ConditionalOps<R>
38 + numr::ops::ReduceOps<R>
39 + numr::ops::ScalarOps<R>
40 + numr::ops::TwoSumOps<R>
41 + numr::ops::TypeConversionOps<R>
42 + numr::ops::UnaryOps<R>,
43 {
44 package_identity(
45 "operating control",
46 &data.schema,
47 OPERATING_CONTROL_SNAPSHOT_SCHEMA,
48 &data.contract,
49 OPERATING_CONTROL_CONTRACT_VERSION,
50 )
51 .map_err(StructureError::definition)?;
52 super::compatibility::check(self, data)?;
53 require(
54 data.time_s.is_finite()
55 && data.time_s >= self.schedule().initial_time_s
56 && data.time_s <= self.schedule().end_s,
57 "imported control time is outside original support",
58 )?;
59 let processed = self
60 .schedule()
61 .decision_times_s
62 .partition_point(|time| *time <= data.time_s);
63 require(
64 data.next_decision_index == processed && data.accepted_steps >= processed,
65 "imported decision index or accepted interval count disagrees with original schedule",
66 )?;
67 require(
68 (data.accepted_steps == 0) == data.last_step.is_none(),
69 "imported interval count disagrees with retained last step",
70 )?;
71 if data.accepted_steps == 0 {
72 require(
73 data.time_s.to_bits() == self.schedule().initial_time_s.to_bits(),
74 "zero-step control state changes its original initial time",
75 )?;
76 }
77 let state = super::state_check::check(self, &data.states, data.time_s, processed)?;
78 let mut candidate = Self {
79 context: Arc::clone(&self.context),
80 state,
81 time: data.time_s,
82 next_decision: processed,
83 steps: data.accepted_steps,
84 last: None,
85 };
86 if let Some(last) = &data.last_step {
87 require(
88 last.start_s.is_finite()
89 && last.start_s >= self.schedule().initial_time_s
90 && last.end_s.to_bits() == data.time_s.to_bits()
91 && last.start_s < last.end_s,
92 "retained control step does not end at accepted time with a valid original start",
93 )?;
94 require(
95 if data.accepted_steps == 1 {
96 last.start_s.to_bits() == self.schedule().initial_time_s.to_bits()
97 } else {
98 last.start_s > self.schedule().initial_time_s
99 },
100 "retained control step start contradicts accepted interval count",
101 )?;
102 let prior_processed = self
103 .schedule()
104 .decision_times_s
105 .partition_point(|time| *time <= last.start_s);
106 require(
107 data.accepted_steps
108 .checked_sub(1)
109 .is_some_and(|prior_steps| prior_steps >= prior_processed),
110 "retained prior interval count cannot cover its processed decisions",
111 )?;
112 let before = if last.decision_index.is_some() {
113 require(
114 last.decisions.len() == self.definitions().len(),
115 "retained control decision receipts are incomplete",
116 )?;
117 let values = last
118 .decisions
119 .iter()
120 .map(|receipt| receipt.before.clone())
121 .collect::<Vec<_>>();
122 super::state_check::check(self, &values, last.start_s, prior_processed)?
123 } else {
124 require(
125 last.decisions.is_empty(),
126 "non-decision control step contains decision receipts",
127 )?;
128 super::state_check::check(self, &data.states, last.start_s, prior_processed)?
129 };
130 let before = Self {
131 context: Arc::clone(&self.context),
132 state: before,
133 time: last.start_s,
134 next_decision: prior_processed,
135 steps: data.accepted_steps - 1,
136 last: None,
137 };
138 let samples = last
139 .decisions
140 .iter()
141 .map(|receipt| receipt.sample.clone())
142 .collect::<Vec<_>>();
143 let trial = before
144 .trial_to(last.end_s, &samples)
145 .map_err(super::state_check::as_definition)?;
146 require(
147 super::receipt_bits::step(&trial.diagnostics()?, last),
148 "retained last control diagnostics differ from recomputed immediate trial",
149 )?;
150 require(
151 super::receipt_bits::states(&trial.states()?, &data.states),
152 "imported accepted control state differs from retained last trial",
153 )?;
154 candidate.last = trial.into_next().last;
155 } else {
156 let expected = crate::structures::tensor::control::TensorControlState::initial(
157 &candidate.context,
158 )?;
159 require(
160 super::receipt_bits::states(
161 &super::super::projection::states(&candidate.context, &expected)?,
162 &data.states,
163 ),
164 "zero-step control values differ from original assumed initial state",
165 )?;
166 }
167 if candidate.next_decision != processed {
168 return Err(StructureError::input(
169 StructureInputErrorKind::Definition,
170 "imported control decision continuation differs after receipt checks",
171 ));
172 }
173 *self = candidate;
174 Ok(())
175 }
176}