From fe60c4a1dc871e69b8cae583cb9536e1e52ac564 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:51:00 -0500 Subject: [PATCH] fix(template-engine): resolve alarm on-trigger scripts by name per composition chain (TPL-1, TPL-2, TPL-5, WP2 tasks 2.2+2.3) Alarm -> on-trigger script links went through one flatten-wide 'TemplateScript.Id -> canonical name' map. Slot-derived templates hold only IsInherited placeholders, so every slot of a module composed twice resolved to the base script row id and the LAST slot's canonical name won: pump 1's alarm ran pump 2's script in pump 2's scope (TPL-1). A derived script override has a different row id, so the base alarm's link resolved to null (TPL-2). And the base pass over a nested slot overwrote a slot-level on-trigger override (TPL-5). ResolveInheritedAlarms now looks the id up among the script rows of the alarm's own chain, takes the row's NAME and path-qualifies it with the alarm's prefix, carrying it on the ResolvedAlarm, so only the pass that actually adds the alarm decides it. An id outside the alarm's chain keeps the old flatten-wide fallback; a name with no resolved script is still nulled. BY DESIGN this changes the revision hash of multi-slot compositions (and of derived templates that override an alarm's on-trigger script, and nested slot overrides): affected instances read Out of date once and redeploy with the corrected on-trigger script. --- docs/requirements/Component-TemplateEngine.md | 2 + .../Flattening/FlatteningService.cs | 88 ++++-- .../AlarmOnTriggerScriptResolutionTests.cs | 279 ++++++++++++++++++ 3 files changed, 347 insertions(+), 22 deletions(-) create mode 100644 tests/ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests/Flattening/AlarmOnTriggerScriptResolutionTests.cs diff --git a/docs/requirements/Component-TemplateEngine.md b/docs/requirements/Component-TemplateEngine.md index 836b8925..a28f0c0f 100644 --- a/docs/requirements/Component-TemplateEngine.md +++ b/docs/requirements/Component-TemplateEngine.md @@ -156,6 +156,8 @@ When an instance is deployed, the Template Engine resolves the full configuratio 5. Resolve data connection bindings — replace connection name references with concrete connection details from the site. 6. Output a flat structure: list of attributes with resolved values and data source addresses, list of alarms with resolved trigger definitions, list of scripts with resolved code and triggers. +**Alarm on-trigger script resolution (2026-12-03, TPL-1/TPL-2/TPL-5).** An alarm's `OnTriggerScriptId` is resolved **by name within the alarm's own chain**: the id is looked up among the script rows of every template in that alarm's inheritance chain (base rows, derived overrides and `IsInherited` placeholders), and the row's *name* is path-qualified with the alarm's composition prefix (`Pump1.HighTemp` → `Pump1.OnHigh`). It used to go through one flatten-wide `script id → canonical name` map, which (a) made every slot of a module composed twice run the **last** slot's script, since all slots share the base script row id; (b) dropped the link (null on-trigger script) when a derived template overrode the script, since the override row has a different id; and (c) let the second visit of a nested slot overwrite a slot-level on-trigger override. The value is decided by the pass that actually adds the alarm (first wins, same as the alarm itself). An id that is not a script row of the alarm's own chain keeps the old flatten-wide fallback, and a reference that names no resolved script is still left null. **This changes the revision hash** of any instance whose flattened alarms were affected — such instances read *Out of date* once and redeploy with the corrected script link. + ### Flatten-session caching and the graph watermark (WP2.5) The flatten above is driven per instance, so an unmemoised implementation re-walks diff --git a/src/ZB.MOM.WW.ScadaBridge.TemplateEngine/Flattening/FlatteningService.cs b/src/ZB.MOM.WW.ScadaBridge.TemplateEngine/Flattening/FlatteningService.cs index 1fca35fb..06b7eea5 100644 --- a/src/ZB.MOM.WW.ScadaBridge.TemplateEngine/Flattening/FlatteningService.cs +++ b/src/ZB.MOM.WW.ScadaBridge.TemplateEngine/Flattening/FlatteningService.cs @@ -78,11 +78,15 @@ public class FlatteningService // Step 4: Apply connection bindings ApplyConnectionBindings(instance.ConnectionBindings, attributes, dataConnections); - // Step 5: Resolve alarms from inheritance chain. - // alarmScriptIds maps a resolved alarm's canonical name to the - // TemplateScript.Id of its on-trigger script (if any). + // Step 5: Resolve alarms from inheritance chain. Each alarm's on-trigger + // script is resolved by NAME within the alarm's own chain and + // path-qualified with the alarm's prefix (TPL-1/TPL-2/TPL-5). + // alarmScriptIds only carries the legacy fallback: an on-trigger script + // id that is not a script row of the alarm's own chain. var alarmScriptIds = new Dictionary(StringComparer.Ordinal); - var alarms = ResolveInheritedAlarms(templateChain, prefix: null, alarmScriptIds); + var alarms = ResolveInheritedAlarms(templateChain, prefix: null, out var rootUnresolvedScriptIds); + foreach (var (alarmName, scriptId) in rootUnresolvedScriptIds) + alarmScriptIds[alarmName] = scriptId; ResolveComposedAlarms(templateChain, compositionMap, composedTemplateChains, alarms, alarmScriptIds); ApplyInstanceAlarmOverrides(instance.AlarmOverrides, alarms); @@ -94,7 +98,7 @@ public class FlatteningService ResolveComposedScripts(templateChain, compositionMap, composedTemplateChains, scripts, scriptCanonicalById); // Step 7: Resolve alarm on-trigger script references to canonical names - ResolveAlarmScriptReferences(alarms, alarmScriptIds, scriptCanonicalById); + ResolveAlarmScriptReferences(alarms, alarmScriptIds, scriptCanonicalById, scripts); // Step 7b: Resolve native alarm source bindings (connection + source // reference). Conditions under each source are discovered at runtime; @@ -405,19 +409,39 @@ public class FlatteningService /// Resolves alarms from an inheritance chain. When /// is non-null the alarm names are returned bare (caller path-qualifies); /// the keys of the returned dictionary are always bare alarm names. - /// is populated with the on-trigger - /// script id of each resolved alarm keyed by the canonical name the alarm - /// will ultimately carry (bare name when is null, - /// otherwise prefix.name). + /// + /// Each alarm's on-trigger script is resolved within this chain: the + /// OnTriggerScriptId is looked up among the script rows of every + /// template in and translated to that row's + /// NAME, which is then path-qualified with and set + /// as . Resolving by + /// name per chain (not through a flatten-wide id map) is what keeps two + /// slots of the same module apart (TPL-1: both slots share the base script + /// row id, so a global map made every slot's alarm run the LAST slot's + /// script) and keeps the link when a derived template overrides the script + /// with a row of a different id (TPL-2). The value travels on the alarm, so + /// only the pass that actually adds the alarm decides it (TPL-5). + /// + /// receives, keyed by bare alarm name, + /// any on-trigger script id that is NOT a script row of this chain; the + /// caller records it for the legacy flatten-wide id fallback. /// private static Dictionary ResolveInheritedAlarms( IReadOnlyList