521a16e56f
Resolves the two spec_gaps from the cycle-0008 fieldtest
(docs/specs/fieldtest-0008-sum-combinators.md), the same class the 0007 fieldtest
raised for SimBroker (and which e9f4352 patched there) — the combinators shipped
without the equivalent note:
- per-input firing policy: both Add and every LinComb leg are Firing::Any
(mode-A as-of join) — the node fires on every cycle in which any leg is
fresh, once all legs have a value, pairing the fresh leg with the held value
of the others; None until all present (no cold-leg-as-0.0). A consumer
combining different-rate sources (e.g. an M5 signal with a daily-bias leg)
needs this to predict whether the bias is held-and-combined every cycle
(mode A, what ships) or only on rare both-fresh cycles (mode B).
- multi-row-per-timestamp: a fired node emits one row per *cycle*, and two
sources sharing a timestamp are two distinct cycles (C4 tie-break by source
order), so a recorded combined stream may carry >1 row per timestamp. Correct
and forced by C4/C5; ratified here, now stated on the surface so "one row per
timestamp" is not silently assumed.
Doc-only change; no behaviour change. Verified: RUSTDOCFLAGS="-D warnings" cargo
doc -p aura-std clean (intra-doc links to aura_core::Firing::Any resolve); clippy
--all-targets -D warnings clean; cargo test -p aura-std 14 passed.
refs #11
80 lines
2.5 KiB
Rust
80 lines
2.5 KiB
Rust
//! `Add` — two-input f64 sum (input 0 plus input 1), the companion to `Sub`.
|
|
//! Combines two signal streams into one — the most basic combinator for the
|
|
//! north-star "combine one signal with another" research move (C10).
|
|
|
|
use aura_core::{Ctx, FieldSpec, Firing, InputSpec, Node, NodeSchema, Scalar, ScalarKind};
|
|
|
|
/// Two-input f64 sum: input 0 plus input 1. Emits `None` until both inputs
|
|
/// have a value.
|
|
///
|
|
/// # Firing and warm-up
|
|
///
|
|
/// Both inputs are [`Firing::Any`](aura_core::Firing::Any) — a *mode-A as-of
|
|
/// join*: the node fires on every cycle in which either leg is fresh (once both
|
|
/// have produced a value), pairing the fresh leg with the held value of the
|
|
/// other. Until both legs have a value it emits `None` (no cold-leg-as-`0.0`).
|
|
/// With heterogeneous sources sharing a timestamp (same `ts` from two sources =
|
|
/// two distinct cycles, C4), a fired node emits one row per *cycle*, so a
|
|
/// recorded combined stream may carry more than one row per timestamp.
|
|
pub struct Add {
|
|
out: [Scalar; 1],
|
|
}
|
|
|
|
impl Add {
|
|
/// Build an `Add` node.
|
|
pub fn new() -> Self {
|
|
Self { out: [Scalar::F64(0.0)] }
|
|
}
|
|
}
|
|
|
|
impl Default for Add {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Node for Add {
|
|
fn schema(&self) -> NodeSchema {
|
|
NodeSchema {
|
|
inputs: vec![
|
|
InputSpec { kind: ScalarKind::F64, lookback: 1, firing: Firing::Any },
|
|
InputSpec { kind: ScalarKind::F64, lookback: 1, firing: Firing::Any },
|
|
],
|
|
output: vec![FieldSpec { name: "value", kind: ScalarKind::F64 }],
|
|
}
|
|
}
|
|
|
|
fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Scalar]> {
|
|
let a = ctx.f64_in(0);
|
|
let b = ctx.f64_in(1);
|
|
if a.is_empty() || b.is_empty() {
|
|
return None;
|
|
}
|
|
self.out[0] = Scalar::F64(a[0] + b[0]);
|
|
Some(&self.out)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use aura_core::{AnyColumn, Timestamp};
|
|
|
|
#[test]
|
|
fn add_is_sum_once_both_inputs_present() {
|
|
let mut add = Add::new();
|
|
let mut inputs = vec![
|
|
AnyColumn::with_capacity(ScalarKind::F64, 1),
|
|
AnyColumn::with_capacity(ScalarKind::F64, 1),
|
|
];
|
|
|
|
// only input 0 present -> None
|
|
inputs[0].push(Scalar::F64(10.0)).unwrap();
|
|
assert_eq!(add.eval(Ctx::new(&inputs, Timestamp(0))), None);
|
|
|
|
// both present -> a + b
|
|
inputs[1].push(Scalar::F64(4.0)).unwrap();
|
|
assert_eq!(add.eval(Ctx::new(&inputs, Timestamp(0))), Some([Scalar::F64(14.0)].as_slice()));
|
|
}
|
|
}
|