From ee8a01b9cb7e27a697baeb4211ed50936c1b9ebe Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Thu, 17 Apr 2025 13:20:50 -0700 Subject: [PATCH] Let regulators handle their own activation This improves code organization at the cost of a little redundancy: the default implementation of `activate` doesn't do anything, and its implementation for `HalfCurvatureRegulator` redundantly accesses the set point signal and checks whether the regulator is set. --- app-proto/src/assembly.rs | 58 ++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 055b9a2..aedc344 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -145,6 +145,8 @@ pub trait Regulator: ProblemPoser + OutlineItem { fn subjects(&self) -> Vec; fn measurement(&self) -> ReadSignal; fn set_point(&self) -> Signal; + + fn activate(&self, _assembly: &Assembly) {} } pub struct ProductRegulator { @@ -202,6 +204,17 @@ impl Regulator for HalfCurvatureRegulator { fn set_point(&self) -> Signal { self.set_point } + + fn activate(&self, assembly: &Assembly) { + if let Some(half_curv) = self.set_point.with_untracked(|set_pt| set_pt.value) { + let representation = assembly.elements.with_untracked( + |elts| elts[self.subject].representation + ); + representation.update( + |rep| change_half_curvature(rep, half_curv) + ); + } + } } impl ProblemPoser for HalfCurvatureRegulator { @@ -313,7 +326,7 @@ impl Assembly { fn insert_regulator(&self, regulator: Rc) { let subjects = regulator.subjects(); let key = self.regulators.update( - |regs| regs.insert(regulator) + |regs| regs.insert(regulator.clone()) ); let subject_regulators: Vec<_> = self.elements.with_untracked( |elts| subjects.into_iter().map( @@ -324,6 +337,19 @@ impl Assembly { regulators.update(|regs| regs.insert(key)); } + // update the realization when the regulator becomes a constraint, or is + // edited while acting as a constraint + let self_for_effect = self.clone(); + create_effect(move || { + console::log_1(&JsValue::from( + format!("Updated regulator with subjects {:?}", regulator.subjects()) + )); + if regulator.set_point().with(|set_pt| set_pt.is_present()) { + regulator.activate(&self_for_effect); + self_for_effect.realize(); + } + }); + /* DEBUG */ // print an updated list of regulators console::log_1(&JsValue::from("Regulators:")); @@ -365,18 +391,6 @@ impl Assembly { measurement: measurement, set_point: set_point })); - - // update the realization when the regulator becomes a constraint, or is - // edited while acting as a constraint - let self_for_effect = self.clone(); - create_effect(move || { - console::log_1(&JsValue::from( - format!("Updated regulator with subjects {:?}", subjects) - )); - if set_point.with(|set_pt| set_pt.is_present()) { - self_for_effect.realize(); - } - }); } pub fn insert_new_half_curvature_regulator(&self, subject: ElementKey) { @@ -390,24 +404,6 @@ impl Assembly { measurement: measurement, set_point: set_point })); - - // update the realization when the regulator becomes a constraint, or is - // edited while acting as a constraint - let self_for_effect = self.clone(); - create_effect(move || { - console::log_1(&JsValue::from( - format!("Updated regulator with subjects [{}]", subject) - )); - if let Some(half_curv) = set_point.with(|set_pt| set_pt.value) { - let representation = self_for_effect.elements.with_untracked( - |elts| elts[subject].representation - ); - representation.update( - |rep| change_half_curvature(rep, half_curv) - ); - self_for_effect.realize(); - } - }); } // --- realization ---