Files
Aura/crates/aura-std/src/const_node.rs
T
Brummel 99237e1d0a feat(std): fill the by-chance vocabulary gaps — Const, Div, Abs, Max, Min
Five new rostered node types (count-pin 23 -> 28, both the in-crate
shape test and the cross-boundary CLI vocabulary e2e): Const is unary
with an f64 'value' param — the clock input drives it, its value is
ignored, since a zero-input node never evaluates in the total-push
engine — mirroring EqConst's constant-as-param pattern; Div is binary
IEEE-754 (x/0 -> signed inf, 0/0 -> NaN, unit-tested, no error
channel); Abs unary mirroring Sqrt; Max/Min binary pairwise, distinct
from the windowed RollingMax/RollingMin.

Acceptance proof: the committed executable spec composes an RSI-class
gain/loss-split-and-ratio signal purely from blueprint data through
std_vocabulary and runs it to hand-computed RS values — the r_meanrev
constant-folding workaround is no longer forced.

Verified: headline test green, aura-std 163/0, full workspace suite
green (independent mini-verify), clippy -D warnings clean.

closes #236
2026-07-10 19:59:10 +02:00

87 lines
3.0 KiB
Rust

//! `Const` — a constant-as-param source: emits a fixed `f64` value every cycle
//! its clock input fires, ignoring the clock's actual value. Mirrors `EqConst`'s
//! constant-as-param pattern (the constant is a bound param, not topology), but
//! as a *source*: a zero-input node never evaluates in the total-push engine
//! (C8), so `Const` still needs one input purely to be driven by the sim clock
//! — its value is discarded.
use aura_core::{
Cell, Ctx, FieldSpec, Firing, Node, NodeSchema, ParamSpec, PortSpec, PrimitiveBuilder,
ScalarKind,
};
/// Emits the bound `value` every cycle its clock input fires; the clock's own
/// value is ignored. Emits `None` until the clock input has fired at least once
/// (warm-up filter, C8) — same shape as every other stateless node here.
pub struct Const {
value: f64,
out: [Cell; 1],
}
impl Const {
/// Build a constant source bound to `value`.
pub fn new(value: f64) -> Self {
Self { value, out: [Cell::from_f64(value)] }
}
/// The param-generic recipe for a blueprint primitive: declares `value` and
/// one clock input, builds through `Const::new`.
pub fn builder() -> PrimitiveBuilder {
PrimitiveBuilder::new(
"Const",
NodeSchema {
inputs: vec![PortSpec { kind: ScalarKind::F64, firing: Firing::Any, name: "clock".into() }],
output: vec![FieldSpec { name: "value".into(), kind: ScalarKind::F64 }],
params: vec![ParamSpec { name: "value".into(), kind: ScalarKind::F64 }],
},
|p| Box::new(Const::new(p[0].f64())),
)
}
}
impl Node for Const {
fn lookbacks(&self) -> Vec<usize> {
vec![1]
}
fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Cell]> {
let w = ctx.f64_in(0);
if w.is_empty() {
return None; // not yet warmed up (C8 filter)
}
self.out[0] = Cell::from_f64(self.value);
Some(&self.out)
}
fn label(&self) -> String {
format!("Const({})", self.value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use aura_core::{AnyColumn, Scalar, Timestamp};
#[test]
fn const_emits_bound_value_regardless_of_clock_value() {
// The core property: out == value, always, once the clock has fired at
// least once — the clock's own reading is never observed in the output.
let mut node = Const::new(7.0);
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 1)];
inputs[0].push(Scalar::f64(-100.0)).unwrap();
assert_eq!(node.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(7.0)].as_slice()));
inputs[0].push(Scalar::f64(42.0)).unwrap();
assert_eq!(node.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(7.0)].as_slice()));
}
#[test]
fn const_is_none_until_clock_present() {
let mut node = Const::new(7.0);
let inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 1)];
assert_eq!(node.eval(Ctx::new(&inputs, Timestamp(0))), None);
}
}