Global registry connecting parameters, probes and the sim clock. More...
| Import Statement: | import Clayground.Lab |
Every Parameter and Probe registers itself here on creation. UI components (ParamPanel, Plot2D) and agents (via eval on the inspector protocol) address them by name through this singleton.
Example usage:
import Clayground.Lab
// read a parameter value
property real g: Lab.p("gravity")
// set one (also the agent-facing entry point)
Component.onCompleted: Lab.set("gravity", 3.7)
See also Parameter, Probe, and SimClock.
flowIds : var |
Ids of all registered flows (registration order).
headless : bool |
No one is watching: skip audio and character travel.
Set by runFlow() for the length of a headless run and restored afterwards; a host may also set it by hand. What honours it is deliberately small and listed here rather than guessed at: Narrator hides, the professor kit's FlowGuide leaves the character where it is, and pre-rendered narration is not loaded. Nothing about the SIMULATION changes - a flow must traverse the same states headless as it does on screen, or running it as a check proves nothing.
host : var |
The sandbox root: what supplies scenarios() and applyScenario().
Set by LabKeys from its lab, which every lab has, and cleared when that LabKeys goes; a host without one sets it by hand. run() applies a scenario through it, because a lab's own applyScenario() does more than its ScenarioSet - street-network-101's frames the plan and resets the clock after the script.
modelStateNames : var |
Names of all registered ModelState hooks (registration order).
Empty means the lab's model offers no state: snapshot() says so, and a reload replays the run instead.
paramNames : var |
Names of all registered parameters (registration order).
probeNames : var |
Names of all registered probes (registration order).
scenario : string |
Name of the currently applied scenario ("" if none).
restored() |
sampled(real t) |
Emitted after all probes took a sample at sim time t.
Note: The corresponding handler is onSampled.
void applyViewState(var s) |
Restores parameters and sim time - from the snapshot, else by replay - then the running lessons.
Ordering contract: the Sandbox root MUST apply s.scenario BEFORE calling this. The scenario apply resets the clock + RNG.
With a s.snapshot (a lab whose model offers state) the run is restore()d from it: what it costs does not grow with sim time. A snapshot that does not restore - a code edit renamed a ModelState, say - is reported and the run is replayed instead.
Without one, and only when a SimClock exists, is world-less and s.time > 0, the run is replayed: the clock is re-stepped to s.time in fixed 1/60 s increments from the parameter values it had at its last reset, and each parameter change in s.paramLog is applied at the first step at or after the sim time it was made at - a replay that applied the last values from the start ran a different experiment. The replay re-runs the causal chain (takeSamples -> sampled -> sensors/filter); a run that was frame-driven comes back on the 1/60 s grid. World-driven (Box2D) clocks cannot be synchronously re-stepped and are left untouched. A payload without paramLog sets s.params and re-steps.
Then each flow in s.flows is loaded: the lesson that ran is at the step it was at, without that step's demo running again.
void clearProbes() |
Drops all probe samples (done automatically on SimClock.reset).
var labInfo() |
Standard lab state block for the flagInfo()/labInfo() convention.
real p(string name) |
Shorthand for the value of the named parameter.
Parameter parameter(string name) |
Returns the Parameter with the given name (null if unknown).
Probe probe(string name) |
Returns the Probe with the given name (null if unknown).
var probeSummary() |
Per-probe {first, last, min, max, count} map.
var readSnapshot(string path) |
Reads a snapshot writeSnapshot() wrote, in this process or another.
Returns the snapshot, ready for restore(), or {error}.
void resetParameters() |
Puts every registered parameter back to its defaultValue.
Touches parameters only: the clock, the probes and the scenario stay as they are. Applying a scenario resets the clock; this is the other half of a clean start without a reload.
See also Parameter::reset.
var restore(var snap) |
Puts a snapshot() back; the run goes on as if never left.
snap is what snapshot() or readSnapshot() returned, or the path of a file writeSnapshot() wrote. Order: the seed (changing it resets the clock), the scenario name, the parameters, each ModelState's state, the clock's values, the probe samples, the recorders and the parameter log. restored is emitted last.
Nothing is changed when the snapshot cannot be restored into this lab - not a snapshot, no clock, or model state that does not match: the snapshot must hold a state for exactly the ModelState names the lab registers, because restoring half a model is a run nobody made. The result is then {error}.
Otherwise it is {restored: true, time, scenario}, plus notes for what did not match and was let go: a parameter or probe the snapshot has and the lab does not (ignored), a probe the lab has and the snapshot does not (left empty), a recorder recording now that the snapshot had not (started again from here).
See also snapshot and readSnapshot.
var run(var opts) |
Runs one experiment in fixed steps and returns its probes' summaries.
The one-off form of a lab-sweep study: one call instead of a manifest. opts:
scenario - applied through host's applyScenario(). Omitted, the current scenario is applied again, so a run never starts from what the previous one left behind;seed - the clock's seed (default: the seed it has);params - {name: value}. Every parameter is put back to its defaultValue (resetParameters()) before the scenario, so one the run does not name starts where a fresh load has it;warmupSteps - steps advanced before recording starts (default 0);steps - steps recorded (default 600);stepHz - steps per sim second (default 60);probes - the probe names to report (default: all);series - true to add each probe's samples.The order is the one tools/lab-sweep/lab_sweep.py documents: seed, scenario, parameters, clock reset, warm-up, record. Parameters are set before the reset so that whatever the lab rebuilds on wasReset is built with them.
Returns {scenario, seed, warmupSteps, steps, stepSize, sampleInterval, duration, params, probes}: params is every parameter's value as run, probes maps a name to {unit, count, first, last, min, max, mean, stddev} over the RECORDED samples only - not the warm-up - computed the way a run record computes them, so a mean here is the mean a .labrec of the same run holds. The summaries are the run's own: a probe keeps its last 1200 samples and a longer run is still summarized in full. With series, series maps a name to [[t, v], ...], every sample of the run.
Nothing is run when something in opts is wrong - an unknown parameter or probe, a value set() refuses, a scenario the host does not have: the result is then {error}.
var runFlow(string flowId, var opts) |
Runs a registered Flow to its end with nobody watching.
The headless half of "every flow is also a test": it starts the flow through the lab's own startFlow() when the lab has one, forces pacing: "auto", advances clock in fixed 1/60 s steps and performs every task itself with Flow::solve(). Nothing is waited for in wall-clock time, so a whole lesson runs in well under a second.
Returns what broke rather than throwing:
flowId, steps - the id asked for and how many 1/60 s steps it took;unresolvedVerbs - verb names the flow asked the lab for and did not get (a missing verb otherwise fails silently);failedTasks - {index, step} for a task still unsatisfied opts.solveGrace sim seconds after its own solve() ran;failedExpects - {index, step} (plus error when the predicate threw) for every FlowStep::expect that did not hold, asserted the moment the flow leaves the step. An expect that names what it looks at through its second argument, saw(label, value), adds saw: {label: value} - the values it saw;finished - the flow reached its end. False means the bound was hit;error - the run never started (no such flow, no clock).opts.maxSteps (default 20000, i.e. ~5.5 sim minutes) bounds the run so a watch that never comes true is a false finished rather than a hang. opts.solveGrace (default 5 sim seconds) is how long a task gets after being solved before it counts as failed.
headless is true for the length of the run and restored afterwards.
var schema() |
What this lab can be asked: its parameters, probes, scenarios and flows.
Returns {params, probes, scenarios, flows, modelStates, clock}:
params - one {name, value, defaultValue, from, to, stepSize, unit, description} per parameter, in registration order. from and to are the range set() and run() hold a value to;probes - one {name, unit} per probe;scenarios - the names host's scenarios() returns (empty without a host);flows - the registered Flow ids;modelStates - the registered ModelState names; empty when the model offers no state and snapshot() cannot be taken;clock - {seed, sampleInterval, fixedStep} of clock, or null without one.description is the text the lab declared, in whatever language it declared it in; labInfo() stays ids and values only.
bool set(string name, real value) |
Sets a parameter value; refuses one outside its range.
Returns false and logs a warning naming the range when value is not a number or lies outside [from, to] of a parameter that has a range (to > from); the value is left as it was. It used to clamp without a word, so an experiment asked for a GPS noise of 12 on a 0.5 to 10 slider ran at 10 and its record said nothing about it.
var snapshot() |
The state of the running lab as plain data, for restore().
Everything a run needs to go on from here exactly as it would have without being interrupted:
clock - the seed and the clock's five values: time, random (the generator's state), nextSample, stepRemainder (the part of a fixed step still to come) and lastStepTime;scenario and params - every registered parameter's value;paramLog - the parameter values at the clock's last reset and every change since, with the sim time it was made at;probes - each probe's retained samples and last value;model - what every registered ModelState saves, by name;recorders - for each DataRecorder in registration order, the run it is recording (null when it is not), so a record finished after a restore holds the whole run.Non-finite numbers are written as {"$num": "NaN"} and the like, so the result goes through JSON unchanged: writeSnapshot() and readSnapshot() take it to a file and back.
What it does not hold: how the frame ticker is paced (timeScale, Clayground.paused, a resting clock), the camera and what the sandbox shows - that is viewState()'s - and a recorder writing CSV, whose rows are on disk already (listed in notes).
Returns {error} instead when there is nothing it could restore faithfully: no clock, a clock driven by a physics world, or a model that offers no state - a lab that registers no ModelState.
See also restore, ModelState, and writeSnapshot.
var viewState() |
JSON-serializable lab state for the live-loader reload convention.
Returns { scenario, time, params, paramLog, snapshot, flows }: the current scenario name (key omitted while empty), the sim-clock time, every registered parameter value, the parameter changes since the clock's last reset ({start, changes}, see snapshot()), - when the lab's model offers state - a snapshot(), and - while a lesson runs - flows, each running Flow's state() under its flowId. Sandbox roots merge their own camera/toggle state on top and hand the result to the dojo, which re-applies it after the next reload.
The loader's inspector leaves snapshot out of the view state it shows in a snapshot response or a flag: it is there for the reload.
See also applyViewState.
var writeSnapshot(string path, var snap) |
Writes snap (default: snapshot() now) to path as JSON.
Returns {written: path, bytes} or {error}. A relative path resolves against the process working directory.
See also readSnapshot.