Trying an integration shape against synthetic data
A mapping decides what an event is: one per reading, one per alarm, one per change of state. Two mappings of the same source can make very different numbers of events, and which one suits a site is often a question a captured sample cannot settle, because the case that matters (a drift past a limit, a sensor that goes quiet, a burst of duplicates, a record the source garbles) is rare in what was captured, or nothing has been captured yet.
Two small commands help. python -m engine.scenario_cli makes a synthetic sample from a short
scenario file: signals, faults and a seed. python -m engine.shape_comparison_cli runs one sample
through two or more candidate configs and sets what each makes side by side. They build on the
mapping preview and the players of the fixture kit.
This is test tooling, not part of the public API. The options, the file formats and the exit codes may change in any release, and the stability policy (public-api-stability.md) does not cover them.
What this does not do. It judges the shape of a mapping, not a site.
- It does not prove a mapping right at a plant. Synthetic data shows what a mapping does with the cases you wrote into the scenario, and nothing more.
- It is not a load test and does not measure timing.
- It does not grade prompt text, and it does not say whether a client's real questions can be answered from the events. That stays a person's check against the Activity Log.
- It ranks nothing. The report puts the figures side by side and you decide.
Neither command opens a connection. Each writes only the files it is told to write, and the second writes none. Playing a generated file to a broker or a database is the job of the players, with the refusals they already have.
Setup and the commands in this document
Every command below runs as written once this block has been run in the same shell. It makes a work directory, a Flow Meter Node Type (the one Build a Node Type declares), two candidate configs, a scenario, a scenario with a mistake in it, and a profile for a broker on this machine's standard port. The commands run from the repository root with the project's Python environment active.
WORK=$(mktemp -d)
mkdir -p "$WORK/node-types" "$WORK/files"
cat > "$WORK/node-types/node_type.json" <<'JSON'
{
"identity": {"name": "Flow Meter", "version": "0.1.0", "relevant_peer_node_types": []},
"config_schema": {
"type": "object",
"properties": {
"polling": {
"type": "object",
"required": ["endpoint", "node_id"],
"properties": {
"endpoint": {"type": "string"},
"node_id": {"type": "string"},
"state_transition_rules": {"type": "array", "items": {"type": "object"}}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
"event_types": [
{
"name": "ReadingChanged",
"description": "The metered value changed.",
"fields": {
"type": "object",
"required": ["value", "source"],
"properties": {"value": {"type": "number"}, "source": {"type": "string"}}
}
}
]
}
JSON
cat > "$WORK/every-reading.json" <<'JSON'
{
"node_type": "Flow Meter", "node_type_version": "0.1.0", "config_version": 1,
"config": {"polling": {
"endpoint": "opc.tcp://meter.invalid:4840", "node_id": "ns=2;s=Flow",
"state_transition_rules": [
{"when": {"event": "data_change"},
"then": {"value": "reading", "meter": "meter_id"},
"literals": {"source": "polling", "kind": "reading"}}
]
}}
}
JSON
cat > "$WORK/high-only.json" <<'JSON'
{
"node_type": "Flow Meter", "node_type_version": "0.1.0", "config_version": 1,
"config": {"polling": {
"endpoint": "opc.tcp://meter.invalid:4840", "node_id": "ns=2;s=Flow",
"state_transition_rules": [
{"when": {"band": "high"},
"then": {"value": "reading", "meter": "meter_id"},
"literals": {"source": "polling", "kind": "alarm"}}
]
}}
}
JSON
cat > "$WORK/flow-meter-drift.jsonl" <<'JSON'
{"kind": "scenario", "name": "flow-meter-drift", "seed": 7, "start": "2027-03-08T06:00:00Z", "duration_seconds": 14400, "interval_seconds": 60, "fields": {"event": "data_change"}, "identity_field": "meter_id", "identities": ["FM-1", "FM-2"]}
{"kind": "signal", "field": "reading", "baseline": 12.5, "noise": 0.2, "drift": {"per_hour": 0.8, "from_seconds": 3600}, "state": {"field": "band", "default": "normal", "above": [{"value": 14, "name": "high"}]}}
{"kind": "fault", "fault": "dropout", "start_seconds": 7200, "length_seconds": 600, "identity": "FM-2"}
{"kind": "fault", "fault": "duplicate", "count": 6}
{"kind": "fault", "fault": "malformed", "count": 4, "field": "reading"}
{"kind": "mqtt", "topic": "kindred-sim/flow-meter/reading"}
JSON
cat > "$WORK/bad-scenario.jsonl" <<'JSON'
{"kind": "scenario", "name": "bad", "seed": 1, "start": "2027-03-08T06:00:00Z", "duration_seconds": 600, "interval_seconds": 60}
{"kind": "signal", "field": "reading", "baseline": 12.5}
{"kind": "fault", "fault": "meteor", "count": 1}
JSON
echo '{"host": "localhost", "port": 1883}' > "$WORK/broker.json"
echo '{"host": "localhost", "port": 1}' > "$WORK/nobroker.json"
The scenario file
JSON lines: one object per line, and a JSON array of the same objects is read too. The first line is
the scenario; the others are signals, faults and, if you want the other files, an mqtt or a
database line. The whole file is checked before anything is made, and an error names the line (not
counting blank lines) and never a value from it.
Line kind | Keys |
|---|---|
scenario |
name, seed, start, duration_seconds, interval_seconds, and optionally time_field, time_format, fields, identity_field, identities |
signal |
field, baseline, and optionally noise, drift, steps, cycle, decimals, state |
fault |
fault (one of the five below) and the keys that fault takes |
mqtt |
topic, and optionally retain, qos |
database |
table, columns, and optionally update_key |
The scenario line. name is a short lower-case word and goes in every file name. seed is a
whole number from 0 to 4294967295. start is an ISO 8601 time with a zone, such as
2027-03-08T06:00:00Z. One record is made for each interval_seconds across duration_seconds
(at most 31 days). time_field (default ts) holds each record's time, as ISO 8601 text, or as
seconds since 1970 with time_format set to epoch_seconds. fields are constants copied into every
record. With identity_field and identities (at most 50), each interval makes one record for each
identity, and each identity has noise of its own.
A signal is a baseline plus whichever of these it sets:
| Key | Meaning |
|---|---|
noise |
The standard deviation of random noise, never below 0. |
drift |
per_hour, a signed rate, and from_seconds where it starts. |
steps |
A list of at_seconds and delta: the value moves by delta from that second. |
cycle |
period_seconds and amplitude, and optionally phase_seconds: a sine, for a daily or a shift cycle. |
decimals |
How many decimals the value keeps (default 3). |
state |
field, default and above, a list of value and name: a second field naming the highest band the value is above, as a source that reports a state would. |
The state field is what lets a mapping act on a threshold. A mapping rule matches a field by
equality and sees one record at a time, so it cannot test a value against a limit. A source that
reports band: high can be matched.
The faults. Each is counted when it is applied, and the count is in the manifest.
fault | Keys | What it does |
|---|---|---|
dropout |
start_seconds, length_seconds, optionally identity |
Removes the records inside the window. |
burst |
start_seconds, count, spacing_seconds, and identity when there are several |
Adds count readings, one every spacing_seconds. |
duplicate |
count |
Repeats count records, each straight after itself. |
malformed |
count, field, optionally mode (text, the default, or missing) |
Replaces the field with the text n/a, or leaves it out. |
late |
count, by_records |
Moves count records by_records places later, so each is out of time order. |
Faults are applied in a fixed order whatever order the file lists them: dropouts, bursts, malformed records, duplicates, then late records. A window must lie inside the run, a burst may not fall inside a dropout of the same source, and a record is garbled or repeated at most once. The same scenario and seed always give the same files, and a different seed gives different values with the same number of each fault.
Making the files
python -m engine.scenario_cli check "$WORK/flow-meter-drift.jsonl"
check makes the records in memory and prints what it would write, without writing anything. It
builds each file's text too, so what would fail at write fails here.
Scenario flow-meter-drift (flow-meter-drift.jsonl), seed 7: synthetic data made from a scenario file; it is not a site's data and proves nothing about one.
records: 476 (240 ticks x 2 identities)
line 3: dropout removed 10 records
line 4: duplicate applied 6 of 6
line 5: malformed applied 4 of 4
python -m engine.scenario_cli write "$WORK/flow-meter-drift.jsonl" --out "$WORK/files"
Scenario flow-meter-drift (flow-meter-drift.jsonl), seed 7: synthetic data made from a scenario file; it is not a site's data and proves nothing about one.
records: 476 (240 ticks x 2 identities)
line 3: dropout removed 10 records
line 4: duplicate applied 6 of 6
line 5: malformed applied 4 of 4
wrote flow-meter-drift-seed-7.sample.jsonl
wrote flow-meter-drift-seed-7.mqtt.jsonl
wrote flow-meter-drift-seed-7.manifest.json
write puts these files in the directory, which must exist. Each name starts with the scenario's
name and the seed:
| File | Read by |
|---|---|
NAME-seed-N.sample.jsonl |
The mapping preview, and the comparison below. Always written. |
NAME-seed-N.mqtt.jsonl |
The MQTT player. Written when the scenario has an mqtt line. |
NAME-seed-N.statements.jsonl |
The database row player. Written when the scenario has a database line. |
NAME-seed-N.manifest.json |
People and the comparison: the scenario file's name, the seed, the number of records, a hash of the sample, how many times each fault was applied, and the words "synthetic data". |
The three data files hold only what their readers accept, so the label is in the file names and the
manifest, not in the records. Each message in the message file has a delay that is the time since
the one before. The statement file has an insert for each record and a delay before it where time
passes, and a database line maps record fields to the columns of one of the row player's
tables. For a table that is updated in place (one row for each order, as the row player's
order_status layout is), name a mapped field in update_key: the first record with each value of
it is an insert and every later one is an update of that row, setting the other columns it carries.
The key is the scenario's identity field or one of its constant fields, and a late fault cannot go
with it, since it would put an older reading over a newer one.
The MES as a database log uses this. A gap longer than a player takes in one pause (an hour) is written as an hour, and the
command says how many were; the records keep their true times. A statement file holds up to 10 000
lines, a delay before most statements, so a database scenario has room for about half the records of
the others.
A file that is already there is never written over unless --overwrite is given, and then every
file is made first and put in place together, so a failure part-way leaves nothing behind:
python -m engine.scenario_cli write "$WORK/flow-meter-drift.jsonl" --out "$WORK/files"
--seed N replaces the file's seed, so one scenario can be run with several. A mistake in the
scenario names its line:
python -m engine.scenario_cli check "$WORK/bad-scenario.jsonl" 2>&1
Scenario: cannot run: line 3: names a fault the generator does not know
| Exit code | Meaning |
|---|---|
| 0 | Done. |
| 2 | The scenario, the options or the output directory could not be used; nothing was written. |
Comparing shapes
python -m engine.shape_comparison_cli NODE_TYPE_DIR SAMPLE --config A --config B runs the sample
through each config with the mapping preview, and prints a table with a column for each config.
| Option | Meaning |
|---|---|
--config |
A candidate config. Give it at least twice. |
--section |
The config section to preview, as for the mapping preview. |
--identity-field |
The canonical identity field, if the Node Type fixes none. |
--event-type |
Apply that event type's schema to each event, and count the ones that fail it as flagged. |
--shared-dir |
The shared fragments directory, if a config uses one. |
--time-field |
The sample's own time field. The window counts need it, because a previewed event carries no time. |
--window-seconds |
A window this many seconds wide, at least 1. The default is 3600 once --time-field is given. |
--shift |
A named shift, NAME=HH:MM in UTC, given once for each shift. Not with --window-seconds. |
--type-field |
An event field that a rule sets as a literal: its values label the types. |
--manifest |
The manifest of the sample. It is looked for beside a .sample.jsonl file when not given. |
Each column holds:
- the preview's own counts for that config (records matched, unmatched and flagged), which are what the mapping preview reports for it alone;
- the events it makes and the events per record. A mapping makes at most one event for a record, so this is the share of records that matched;
- how many identities have events, how many events have none, and the most and fewest events for one identity. It counts identities and never names one, and an identity that a shape never makes an event for does not appear in it, so compare the count with the number of identities in the scenario;
- the events by type. A type is the value of the
--type-fieldfield the matching rule sets as a literal, and otherwise the rule's number (afield_mapis one type,field map); - the events by window, with every window of the run listed, empty ones included. A window is labelled
by its place in the run (
window 3 (+2 h),day 2 late), never by a date. A record on a boundary belongs to the window that starts there. A record whose time cannot be read is counted and left out of the windows.
A config that cannot be used has its column too, with a dash for each figure and its reason below
the table, and a config that matches nothing shows zero events. The report holds names and counts,
never a value of the sample or of a config. A sample made by scenario_cli write is reported as
synthetic, with the scenario file's name and the seed, when its manifest is beside it and its hash
matches. Any other sample is reported as of unstated origin, and so is one whose manifest does not
describe it.
| Exit code | Meaning |
|---|---|
| 0 | The configs were compared. The code says nothing about which shape is better. |
| 2 | A config, the Node Type directory, the sample, a manifest or the options could not be used. The table is still printed, to standard error. |
A worked example: two shapes of a Flow Meter's rules
The scenario above has two flow meters, each reporting a reading every minute for four hours. The
reading drifts upward from the second hour and crosses 14 after about three, where the source's own
band field changes from normal to high. One meter goes quiet for ten minutes, six records are
repeated and four readings are garbled. Two shapes of the Flow Meter's rules are set against it:
every-reading.jsonmakes an event for everydata_changerecord;high-only.jsonmakes an event only for a record whosebandishigh.
python -m engine.shape_comparison_cli "$WORK/node-types" "$WORK/files/flow-meter-drift-seed-7.sample.jsonl" \
--config "$WORK/every-reading.json" --config "$WORK/high-only.json" \
--section polling --identity-field meter --event-type ReadingChanged \
--time-field ts --window-seconds 3600 --type-field kind
Shape comparison of 2 configs over one sample (476 records).
The sample is synthetic: scenario flow-meter-drift (scenario file flow-meter-drift.jsonl), seed 7. It is made from a scenario file, not read from a site.
Figures only: nothing here says which shape is better or proves one right at a site.
config 1: every-reading.json config 2: high-only.json
records matched 476 134
records unmatched 0 342
records flagged 4 0
events 476 134
events per record 1.00 0.28
identities with events 2 2
events with no identity 0 0
most events for one identity 244 69
fewest events for one identity 232 65
events with no readable time 0 0
events by type:
config 1: every-reading.json config 2: high-only.json
alarm 0 134
reading 476 0
events by window (3600 s wide, UTC):
config 1: every-reading.json config 2: high-only.json
window 1 (+0 h) 121 0
window 2 (+1 h) 123 0
window 3 (+2 h) 110 17
window 4 (+3 h) 122 117
Reading the table:
- The first shape makes an event of every record, and the four garbled readings are flagged because
valueis no longer a number. The second makes events only from the third hour on, mostly in the fourth, after the drift has carried the reading over the limit. None of the four garbled readings fell there, so it flags none: the shape hides a fault that the scenario put in the quiet hours. - Which of these is right depends on what the client will ask. A per-reading shape answers "what was the flow at ten o'clock", at the price of an event for every minute. A state shape is quiet until there is something to say, but it cannot answer a question about a reading that was never over the limit. The report puts both next to each other; it does not choose.
- The windows show when each shape speaks. The second is silent for the first two hours.
Playing the scenario to a real Node
The message file is a message file like any other, so a Node under test can be fed the same scenario
through a stand-in broker with the MQTT player. Set up a broker for the Node
(mqtt-broker.md), put its address in a profile, and play the file. --rate ignores
the recorded delays and sends that many messages a second; without it, the player keeps the scenario's
own pace, scaled by --speed.
python -m engine.mqtt_player_cli "$WORK/files/flow-meter-drift-seed-7.mqtt.jsonl" --broker "$WORK/broker.json" --rate 1000
The player refuses what it always refuses, and a database statement file goes through the database row player the same way, with its refusal of a database that is not a stand-in. A message published this way is synthetic, and the Node that reads it does not know that, so play it only to a broker and a Node set up for testing, never to one that serves a site.
Limits
- A mapping cannot state a threshold crossing. A rule matches fields by equality and sees one
record at a time. The comparison covers the shapes a mapping can state, and a source that reports a
state (the
stateof a signal) is how a threshold gets into one. A shape that needs the Node Type's own evaluation of a value against a limit is not covered. - The data is the scenario. Noise, drift and faults are what you wrote, with a seeded random generator, not a recording. The same scenario gives the same files on the same version of Python.
- Size. A file holds at most 10 000 records and 10 MB, which is what the readers take, and a scenario that would write more is refused. A comparison spans at most 2000 windows.
- Identity. An identity is a string from the scenario. The generator has no hierarchy, no related orders and no other node types.
- OPC UA. It writes no node file for the OPC UA node server. That is added when an OPC UA shape is the one being tried.
- No history. Nothing keeps a record of runs or a difference between versions of a config. Keep the configs and scenarios in git.
Release: v0.11.0