-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_model.hpp
More file actions
411 lines (358 loc) · 16.6 KB
/
Copy pathlab_model.hpp
File metadata and controls
411 lines (358 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SPDX-License-Identifier: Apache-2.0
#pragma once
/// @file
/// The demo model and its actions. Everything a client needs to render these
/// as forms is generated from the declarations below:
/// - field types/structure -> glaze reflection
/// - descriptions/bounds -> glz::json_schema<A>
/// - units + decimals -> the Quantity types (UnitTraits)
/// - required fields -> member types + `optionalFields` opt-out
/// - readiness (validate) -> morph::forms::allRequiredEngaged
#include <array>
#include <cstdint>
#include <format>
#include <morph/core/bridge.hpp>
#include <morph/core/registry.hpp>
#include <morph/forms/choice.hpp>
#include <morph/forms/forms.hpp>
#include <morph/util/datetime.hpp>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include "lab_units.hpp"
namespace lab {
/// @brief Compute dry density from oven-dry mass and mould volume.
struct ComputeDryDensity {
Mass massDry{};
/// Declared-precision override: this field is specified to 4 decimals,
/// while the m³ unit default is 3 — the schema advertises
/// `x-decimalPlaces: 4` and the form input adapts automatically.
Quantity<Unit::m3, 4> volume{};
[[nodiscard]] bool validate() const { return morph::forms::allRequiredEngaged(*this); }
};
/// @brief One selectable sample (a row of `ListSamples`' result), and the
/// row type `SamplesView` (lab_schemas.hpp) composes its columns from.
struct SampleInfo {
std::int64_t id = 0;
std::string name;
/// A nominal reference mass per sample — present purely so the derived
/// SamplesView table has one Quantity column to format (ExtUnits /
/// x-decimalPlaces), exercising the same path a Quantity form field uses.
Mass referenceMass{};
};
/// @brief Result of `ListSamples`.
struct SampleList {
std::vector<SampleInfo> samples;
};
/// @brief Options provider for `RecordMeasurement.sampleId` — a pure query
/// the form renderers execute to populate the sample combo box — and
/// `SamplesView`'s `v-query` (lab_schemas.hpp).
struct ListSamples {};
/// @brief Renames an existing sample. `SamplesView`'s `v-rowAction`
/// (lab_schemas.hpp): activating a row opens this form, prefilled
/// with the row's current id via `bind`.
struct EditSample {
std::int64_t id = 0;
std::string name;
[[nodiscard]] bool validate() const { return id != 0 && !name.empty(); }
};
/// @brief Removes an existing sample. `SamplesView`'s row-scope, confirm-guarded
/// `v-actions` entry.
struct DeleteSample {
std::int64_t id = 0;
[[nodiscard]] bool validate() const { return id != 0; }
};
/// @brief Result of `DeleteSample`.
struct DeleteSampleAck {
std::int64_t id = 0;
};
/// @brief Adds a new sample. `SamplesView`'s collection-scope `v-actions`
/// entry — fired with an empty body.
struct CreateSample {};
/// @brief Record a measured density (moisture and note are optional).
///
/// Deliberately the demo's kitchen-sink action: it combines a dependent-free
/// `Choice`, a `Timestamp`, an `optionalFields` opt-out, `fieldMetadata`
/// (placeholder + hidden), and `formLayout`/`fieldSpans` (sections + an
/// accordion + a spanning field) in one place, so a reader can see every
/// annotation feature interact on a single action. `ComputeDryDensity` above
/// is the plain, single-feature baseline to contrast it with.
struct RecordMeasurement {
/// Not free input: options come from executing `ListSamples` — the
/// schema carries x-optionsAction/x-optionValue/x-optionLabel and the
/// renderers build a combo box from the result rows.
morph::forms::Choice<std::int64_t, "ListSamples"> sampleId;
/// When the measurement was taken (ISO-8601 UTC on the wire; renderers
/// key on the schema's "format": "date-time").
morph::time::Timestamp measuredAt;
Density density{};
Percent moisture{};
std::optional<std::string> note{};
static constexpr std::array optionalFields{std::string_view{"moisture"}};
// Illustrative field metadata (docs/spec/forms/forms.md, "Field
// metadata"): density gets an in-control placeholder; note (already
// optional) is hidden entirely — carried in the payload if some other
// client sets it, never shown to this renderer's operator. sampleId,
// measuredAt, and moisture keep their inferred titles and their existing
// glz::json_schema<RecordMeasurement> descriptions untouched.
static constexpr std::array fieldMetadata{
morph::forms::FieldMeta{.field = "density", .placeholder = "e.g. 1650.0"},
morph::forms::FieldMeta{.field = "note", .hidden = true},
};
/// Visual structure (docs/spec/forms/forms.md, "Layout & grouping"): two
/// titled sections plus a collapsible notes panel, with the free-text
/// note spanning both grid columns. Purely additive — a renderer that
/// ignores `x-layout`/`x-colspan` still renders every field, flat, in
/// `x-order`.
static constexpr std::array<std::string_view, 2> kSample{std::string_view{"sampleId"},
std::string_view{"measuredAt"}};
static constexpr std::array<std::string_view, 2> kResult{std::string_view{"density"},
std::string_view{"moisture"}};
static constexpr std::array<std::string_view, 1> kNote{std::string_view{"note"}};
static constexpr std::array formLayout{
morph::forms::FieldGroup{.title = "Sample", .fields = kSample},
morph::forms::FieldGroup{.title = "Result", .fields = kResult},
morph::forms::FieldGroup{.title = "Notes", .kind = morph::forms::GroupKind::Accordion, .fields = kNote},
};
static constexpr std::array fieldSpans{
morph::forms::FieldSpan{.field = "note", .colspan = 2},
};
[[nodiscard]] bool validate() const { return morph::forms::allRequiredEngaged(*this); }
};
/// @brief Result of `RecordMeasurement`.
struct MeasurementAck {
std::int64_t sampleId = 0;
std::string summary;
};
/// @brief One selectable country (a row of `ListCountries`' result).
struct CountryInfo {
std::int64_t id = 0;
std::string name;
};
/// @brief Result of `ListCountries`.
struct CountryList {
std::vector<CountryInfo> countries;
};
/// @brief Options provider for `ShippingAddress.country` — independent, like
/// `ListSamples`.
struct ListCountries {};
/// @brief One selectable city (a row of `ListCities`' result).
struct CityInfo {
std::int64_t id = 0;
std::string name;
};
/// @brief Result of `ListCities`.
struct CityList {
std::vector<CityInfo> cities;
};
/// @brief Options provider for `ShippingAddress.city` — filtered by the
/// sibling `country` value the renderer sends as the request body
/// (declared via `Choice`'s `DependsOn`, surfaced as
/// `x-optionsDependsOn` in the schema).
struct ListCities {
std::int64_t country = 0;
};
/// @brief Ship to a city within a country: `city`'s options cascade from the
/// selected `country`.
struct ShippingAddress {
/// Independent Choice: options come from `ListCountries` with an empty body.
morph::forms::Choice<std::int64_t, "ListCountries"> country;
/// Dependent Choice: options come from `ListCities` with `{"country": <value>}`
/// as the body — the schema carries `x-optionsDependsOn: ["country"]`.
morph::forms::Choice<std::int64_t, "ListCities", "id", "name", "country"> city;
[[nodiscard]] bool validate() const { return morph::forms::allRequiredEngaged(*this); }
};
/// @brief Result of `ShippingAddress`.
struct ShippingAck {
std::int64_t countryId = 0;
std::int64_t cityId = 0;
std::string summary;
};
/// @brief Registers a new sample and returns its assigned id — step one of
/// the `IntakeWizard` (see lab_wizard.hpp): the result's `id`
/// prefills `RecordMeasurement.sampleId` in step two.
struct RegisterSample {
std::string name;
[[nodiscard]] bool validate() const { return !name.empty(); }
};
/// @brief Result of `RegisterSample`.
struct RegisterSampleAck {
std::int64_t id = 0;
std::string name;
};
/// @brief The business model behind the generated forms.
class LabModel {
public:
/// @brief The unit algebra runs in real model code: kg / m³ -> kg/m³.
Density execute(const ComputeDryDensity& action) { return action.massDry / action.volume; }
/// @brief Serves the sample combo-box options (a pure query) and
/// `SamplesView`'s table rows.
SampleList execute(const ListSamples& action) {
static_cast<void>(action);
return SampleList{.samples = _samples};
}
/// @brief Renames the sample named by `action.id`. Re-checks `validate()`
/// (the dispatcher does not run validators server-side over the
/// remote wire path — docs/spec/forms/forms.md, "Security / trust
/// boundary" — same guard `RecordMeasurement` already uses below).
SampleInfo execute(const EditSample& action) {
if (!action.validate()) {
throw std::invalid_argument{"EditSample: id and name are required"};
}
for (auto& sample : _samples) {
if (sample.id == action.id) {
sample.name = action.name;
return sample;
}
}
throw std::invalid_argument{std::format("EditSample: no sample with id {}", action.id)};
}
/// @brief Removes the sample named by `action.id`.
DeleteSampleAck execute(const DeleteSample& action) {
if (!action.validate()) {
throw std::invalid_argument{"DeleteSample: id is required"};
}
auto const before = _samples.size();
std::erase_if(_samples, [&](SampleInfo const& sample) { return sample.id == action.id; });
if (_samples.size() == before) {
throw std::invalid_argument{std::format("DeleteSample: no sample with id {}", action.id)};
}
return DeleteSampleAck{.id = action.id};
}
/// @brief Appends a new sample with an auto-assigned id and a default name.
SampleInfo execute(const CreateSample&) {
SampleInfo created{.id = _nextSampleId++, .name = "New sample"};
_samples.push_back(created);
return created;
}
/// @brief Registers a new sample under the caller-supplied name — step
/// one of `IntakeWizard` (lab_wizard.hpp). Shares the same id
/// counter and sample list as `CreateSample`/`ListSamples`, so a
/// registered sample is immediately selectable in
/// `RecordMeasurement.sampleId` and shows up in `SamplesView`.
RegisterSampleAck execute(const RegisterSample& action) {
if (!action.validate()) {
throw std::invalid_argument{"RegisterSample: name is required"};
}
SampleInfo created{.id = _nextSampleId++, .name = action.name};
_samples.push_back(created);
return RegisterSampleAck{.id = created.id, .name = created.name};
}
/// @brief Serves the country combo-box options (a pure query,
/// independent — like `ListSamples`).
CountryList execute(const ListCountries& action) {
static_cast<void>(action);
return CountryList{.countries = {{.id = 1, .name = "Wonderland"}, {.id = 2, .name = "Narnia"}}};
}
/// @brief Serves the city combo-box options, filtered by the sibling
/// `country` value the renderer sends as the request body — a
/// dependent `Choice`'s options action is an ordinary registered
/// action whose body happens to be the parent selection.
CityList execute(const ListCities& action) {
if (action.country == 1) {
return CityList{.cities = {{.id = 10, .name = "Looking-Glass City"}, {.id = 11, .name = "Tulgey Wood"}}};
}
if (action.country == 2) {
return CityList{.cities = {{.id = 20, .name = "Cair Paravel"}}};
}
return CityList{};
}
MeasurementAck execute(const RecordMeasurement& action) {
// The dispatcher does not run validators server-side (clients gate on
// validate() before submitting) — a model that dereferences required
// fields must enforce its own precondition. Same predicate as the
// GUI, so schema, form, and server agree by construction.
if (!action.validate()) {
throw std::invalid_argument{"RecordMeasurement: sampleId, measuredAt and density are required"};
}
auto summary = std::format("sample {} at {}: density {:.1f} {}", *action.sampleId, *action.measuredAt,
*action.density, Density::unitMeta().display);
if (action.moisture.hasValue()) {
summary += std::format(", moisture {:.1f} %", *action.moisture);
}
if (action.note && !action.note->empty()) {
summary += std::format(" ({})", *action.note);
}
return {.sampleId = *action.sampleId, .summary = std::move(summary)};
}
ShippingAck execute(const ShippingAddress& action) {
if (!action.validate()) {
throw std::invalid_argument{"ShippingAddress: country and city are required"};
}
return {.countryId = *action.country,
.cityId = *action.city,
.summary = std::format("ship to city {} in country {}", *action.city, *action.country)};
}
private:
// Serial per-model execution (docs/spec/core/bridge.md: each model runs
// on its own strand) makes plain, unsynchronised mutation of these two
// members safe.
std::vector<SampleInfo> _samples{
{.id = 1, .name = "Proctor A"},
{.id = 2, .name = "Proctor B"},
{.id = 7, .name = "Crushed aggregate 0/32"},
};
std::int64_t _nextSampleId = 100;
};
} // namespace lab
// glz::json_schema<A> supplies human-readable `description` text (and, for
// moisture, validation bounds) that schemaJson<A>() merges onto the
// generated schema -- see docs/spec/forms/forms.md, "schemaJson<A>() --
// schema generation". Only these four actions get a specialization: their
// free-text/numeric fields benefit from a description; the id-only actions
// below (EditSample, DeleteSample, CreateSample, the three List* queries)
// have nothing a description would add beyond their already-inferred titles.
template <>
struct glz::json_schema<lab::ComputeDryDensity> {
schema massDry{.description = "Oven-dry mass of the specimen"};
schema volume{.description = "Mould volume"};
};
template <>
struct glz::json_schema<lab::RecordMeasurement> {
schema sampleId{.description = "Sample under test"};
schema measuredAt{.description = "When the measurement was taken (UTC)"};
schema density{.description = "Dry density"};
schema moisture{.description = "Moisture content", .minimum = 0, .maximum = 100};
schema note{.description = "Free-text remark"};
};
template <>
struct glz::json_schema<lab::ShippingAddress> {
schema country{.description = "Destination country"};
schema city{.description = "Destination city (options depend on country)"};
};
template <>
struct glz::json_schema<lab::RegisterSample> {
schema name{.description = "Sample name/label"};
};
using lab::ComputeDryDensity;
using lab::CreateSample;
using lab::DeleteSample;
using lab::EditSample;
using lab::LabModel;
using lab::ListCities;
using lab::ListCountries;
using lab::ListSamples;
using lab::RecordMeasurement;
using lab::RegisterSample;
using lab::ShippingAddress;
// BRIDGE_REGISTER_MODEL/BRIDGE_REGISTER_ACTION (docs/spec/core/registry.md)
// specialize ModelTraits<LabModel>/ActionTraits<Action> and wire each
// action's dispatch entry at static-init time -- this is what makes
// schemaJson<Action>() and the bridge's executeJson path work for these
// types. Loggable::No opts the three pure-query/options-provider actions
// below (ListSamples, ListCountries, ListCities) out of the action log:
// they have no side effects worth journaling, unlike every other action
// registered here.
BRIDGE_REGISTER_MODEL(LabModel, "LabModel")
BRIDGE_REGISTER_ACTION(LabModel, ComputeDryDensity, "ComputeDryDensity")
BRIDGE_REGISTER_ACTION(LabModel, RecordMeasurement, "RecordMeasurement")
BRIDGE_REGISTER_ACTION(LabModel, ListSamples, "ListSamples", morph::model::Loggable::No)
BRIDGE_REGISTER_ACTION(LabModel, ListCountries, "ListCountries", morph::model::Loggable::No)
BRIDGE_REGISTER_ACTION(LabModel, ListCities, "ListCities", morph::model::Loggable::No)
BRIDGE_REGISTER_ACTION(LabModel, ShippingAddress, "ShippingAddress")
BRIDGE_REGISTER_ACTION(LabModel, EditSample, "EditSample")
BRIDGE_REGISTER_ACTION(LabModel, DeleteSample, "DeleteSample")
BRIDGE_REGISTER_ACTION(LabModel, CreateSample, "CreateSample")
BRIDGE_REGISTER_ACTION(LabModel, RegisterSample, "RegisterSample")