forked from glen/dyna3
Only sync regulator inputs on change
This lets us infer a regulator's role from whether it has a set point and what text specifies the set point.
This commit is contained in:
parent
b3e4e902f3
commit
fef4127f69
3 changed files with 30 additions and 57 deletions
|
@ -7,7 +7,6 @@ use crate::{
|
||||||
assembly::{
|
assembly::{
|
||||||
Assembly,
|
Assembly,
|
||||||
Regulator,
|
Regulator,
|
||||||
RegulatorRole,
|
|
||||||
Element
|
Element
|
||||||
},
|
},
|
||||||
engine::Q
|
engine::Q
|
||||||
|
@ -209,14 +208,12 @@ pub fn AddRemove() -> View {
|
||||||
reps.0.dot(&(&*Q * reps.1))
|
reps.0.dot(&(&*Q * reps.1))
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
let set_point = create_signal(0.0);
|
let set_point = create_signal(None);
|
||||||
let role = create_signal(RegulatorRole::Measurement);
|
|
||||||
state.assembly.insert_regulator(Regulator {
|
state.assembly.insert_regulator(Regulator {
|
||||||
subjects: subjects,
|
subjects: subjects,
|
||||||
measurement: measurement,
|
measurement: measurement,
|
||||||
set_point: set_point,
|
set_point: set_point,
|
||||||
set_point_text: create_signal(String::new()),
|
set_point_spec: create_signal(String::new())
|
||||||
role: role,
|
|
||||||
});
|
});
|
||||||
state.selection.update(|sel| sel.clear());
|
state.selection.update(|sel| sel.clear());
|
||||||
|
|
||||||
|
@ -241,8 +238,7 @@ pub fn AddRemove() -> View {
|
||||||
console::log_1(&JsValue::from(
|
console::log_1(&JsValue::from(
|
||||||
format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1)
|
format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1)
|
||||||
));
|
));
|
||||||
set_point.track();
|
if set_point.with(|set_pt| set_pt.is_some()) {
|
||||||
if role.with(|rl| rl.is_valid_constraint()) {
|
|
||||||
state.assembly.realize();
|
state.assembly.realize();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -111,32 +111,17 @@ impl Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum RegulatorRole {
|
#[derive(Clone, Copy)]
|
||||||
Measurement,
|
|
||||||
Constraint(bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RegulatorRole {
|
|
||||||
pub fn is_valid_constraint(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
RegulatorRole::Measurement => false,
|
|
||||||
RegulatorRole::Constraint(valid) => *valid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Regulator {
|
pub struct Regulator {
|
||||||
pub subjects: (ElementKey, ElementKey),
|
pub subjects: (ElementKey, ElementKey),
|
||||||
pub measurement: ReadSignal<f64>,
|
pub measurement: ReadSignal<f64>,
|
||||||
pub set_point: Signal<f64>,
|
pub set_point: Signal<Option<f64>>,
|
||||||
pub set_point_text: Signal<String>,
|
pub set_point_spec: Signal<String>
|
||||||
pub role: Signal<RegulatorRole>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Regulator {
|
impl Regulator {
|
||||||
fn role_is_valid_constraint_untracked(&self) -> bool {
|
pub fn has_no_set_point_spec(&self) -> bool {
|
||||||
self.role.with_untracked(|role| role.is_valid_constraint())
|
self.set_point_spec.with(|spec| spec.is_empty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,11 +235,14 @@ impl Assembly {
|
||||||
let mut gram_to_be = PartialMatrix::new();
|
let mut gram_to_be = PartialMatrix::new();
|
||||||
self.regulators.with_untracked(|regs| {
|
self.regulators.with_untracked(|regs| {
|
||||||
for (_, reg) in regs {
|
for (_, reg) in regs {
|
||||||
if reg.role_is_valid_constraint_untracked() {
|
match reg.set_point.get_untracked() {
|
||||||
let subjects = reg.subjects;
|
Some(set_pt) => {
|
||||||
let row = elts[subjects.0].column_index.unwrap();
|
let subjects = reg.subjects;
|
||||||
let col = elts[subjects.1].column_index.unwrap();
|
let row = elts[subjects.0].column_index.unwrap();
|
||||||
gram_to_be.push_sym(row, col, reg.set_point.get_untracked());
|
let col = elts[subjects.1].column_index.unwrap();
|
||||||
|
gram_to_be.push_sym(row, col, set_pt);
|
||||||
|
},
|
||||||
|
None => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use sycamore::prelude::*;
|
use sycamore::prelude::*;
|
||||||
use web_sys::{
|
use web_sys::{
|
||||||
Event,
|
|
||||||
HtmlInputElement,
|
|
||||||
KeyboardEvent,
|
KeyboardEvent,
|
||||||
MouseEvent,
|
MouseEvent,
|
||||||
wasm_bindgen::JsCast
|
wasm_bindgen::JsCast
|
||||||
|
@ -14,7 +12,6 @@ use crate::{
|
||||||
assembly::{
|
assembly::{
|
||||||
Regulator,
|
Regulator,
|
||||||
RegulatorKey,
|
RegulatorKey,
|
||||||
RegulatorRole::*,
|
|
||||||
ElementKey
|
ElementKey
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -22,25 +19,17 @@ use crate::{
|
||||||
// an editable view of a regulator
|
// an editable view of a regulator
|
||||||
#[component(inline_props)]
|
#[component(inline_props)]
|
||||||
fn RegulatorInput(regulator: Regulator) -> View {
|
fn RegulatorInput(regulator: Regulator) -> View {
|
||||||
|
let value = create_signal(regulator.set_point_spec.get_clone_untracked());
|
||||||
|
create_effect(move || value.set(regulator.set_point_spec.get_clone()));
|
||||||
view! {
|
view! {
|
||||||
input(
|
input(
|
||||||
r#type="text",
|
r#type="text",
|
||||||
placeholder=regulator.measurement.with(|result| result.to_string()),
|
placeholder=regulator.measurement.with(|result| result.to_string()),
|
||||||
bind:value=regulator.set_point_text,
|
bind:value=value,
|
||||||
on:change=move |event: Event| {
|
on:change=move |_| {
|
||||||
let target: HtmlInputElement = event.target().unwrap().unchecked_into();
|
let value_val = value.get_clone_untracked();
|
||||||
let value = target.value();
|
regulator.set_point.set(value_val.parse::<f64>().ok());
|
||||||
if value.is_empty() {
|
regulator.set_point_spec.set(value_val);
|
||||||
regulator.role.set(Measurement);
|
|
||||||
} else {
|
|
||||||
match target.value().parse::<f64>() {
|
|
||||||
Ok(set_pt) => batch(|| {
|
|
||||||
regulator.set_point.set(set_pt);
|
|
||||||
regulator.role.set(Constraint(true));
|
|
||||||
}),
|
|
||||||
Err(_) => regulator.role.set(Constraint(false))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -51,20 +40,20 @@ fn RegulatorInput(regulator: Regulator) -> View {
|
||||||
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View {
|
||||||
let state = use_context::<AppState>();
|
let state = use_context::<AppState>();
|
||||||
let assembly = &state.assembly;
|
let assembly = &state.assembly;
|
||||||
let regulator = assembly.regulators.with(|regs| regs[regulator_key].clone());
|
let regulator = assembly.regulators.with(|regs| regs[regulator_key]);
|
||||||
let other_subject = if regulator.subjects.0 == element_key {
|
let other_subject = if regulator.subjects.0 == element_key {
|
||||||
regulator.subjects.1
|
regulator.subjects.1
|
||||||
} else {
|
} else {
|
||||||
regulator.subjects.0
|
regulator.subjects.0
|
||||||
};
|
};
|
||||||
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
|
||||||
let class = regulator.role.map(
|
let class = create_memo(move || {
|
||||||
|role| match role {
|
match regulator.set_point.get() {
|
||||||
Measurement => "regulator",
|
None if regulator.has_no_set_point_spec() => "regulator",
|
||||||
Constraint(true) => "regulator valid-constraint",
|
None => "regulator invalid-constraint",
|
||||||
Constraint(false) => "regulator invalid-constraint"
|
Some(_) => "regulator valid-constraint"
|
||||||
}
|
}
|
||||||
);
|
});
|
||||||
view! {
|
view! {
|
||||||
li(class=class.get()) {
|
li(class=class.get()) {
|
||||||
div(class="regulator-label") { (other_subject_label) }
|
div(class="regulator-label") { (other_subject_label) }
|
||||||
|
|
Loading…
Add table
Reference in a new issue