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.
This commit is contained in:
Aaron Fenyes 2025-04-17 13:20:50 -07:00
parent 955220c0bc
commit ee8a01b9cb

View file

@ -145,6 +145,8 @@ pub trait Regulator: ProblemPoser + OutlineItem {
fn subjects(&self) -> Vec<ElementKey>;
fn measurement(&self) -> ReadSignal<f64>;
fn set_point(&self) -> Signal<SpecifiedValue>;
fn activate(&self, _assembly: &Assembly) {}
}
pub struct ProductRegulator {
@ -202,6 +204,17 @@ impl Regulator for HalfCurvatureRegulator {
fn set_point(&self) -> Signal<SpecifiedValue> {
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<dyn Regulator>) {
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 ---