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.
This commit is contained in:
@@ -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.
|
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.
|
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)
|
### Flatten-session caching and the graph watermark (WP2.5)
|
||||||
|
|
||||||
The flatten above is driven per instance, so an unmemoised implementation re-walks
|
The flatten above is driven per instance, so an unmemoised implementation re-walks
|
||||||
|
|||||||
@@ -78,11 +78,15 @@ public class FlatteningService
|
|||||||
// Step 4: Apply connection bindings
|
// Step 4: Apply connection bindings
|
||||||
ApplyConnectionBindings(instance.ConnectionBindings, attributes, dataConnections);
|
ApplyConnectionBindings(instance.ConnectionBindings, attributes, dataConnections);
|
||||||
|
|
||||||
// Step 5: Resolve alarms from inheritance chain.
|
// Step 5: Resolve alarms from inheritance chain. Each alarm's on-trigger
|
||||||
// alarmScriptIds maps a resolved alarm's canonical name to the
|
// script is resolved by NAME within the alarm's own chain and
|
||||||
// TemplateScript.Id of its on-trigger script (if any).
|
// 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<string, int>(StringComparer.Ordinal);
|
var alarmScriptIds = new Dictionary<string, int>(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);
|
ResolveComposedAlarms(templateChain, compositionMap, composedTemplateChains, alarms, alarmScriptIds);
|
||||||
ApplyInstanceAlarmOverrides(instance.AlarmOverrides, alarms);
|
ApplyInstanceAlarmOverrides(instance.AlarmOverrides, alarms);
|
||||||
|
|
||||||
@@ -94,7 +98,7 @@ public class FlatteningService
|
|||||||
ResolveComposedScripts(templateChain, compositionMap, composedTemplateChains, scripts, scriptCanonicalById);
|
ResolveComposedScripts(templateChain, compositionMap, composedTemplateChains, scripts, scriptCanonicalById);
|
||||||
|
|
||||||
// Step 7: Resolve alarm on-trigger script references to canonical names
|
// 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
|
// Step 7b: Resolve native alarm source bindings (connection + source
|
||||||
// reference). Conditions under each source are discovered at runtime;
|
// reference). Conditions under each source are discovered at runtime;
|
||||||
@@ -405,19 +409,39 @@ public class FlatteningService
|
|||||||
/// Resolves alarms from an inheritance chain. When <paramref name="prefix"/>
|
/// Resolves alarms from an inheritance chain. When <paramref name="prefix"/>
|
||||||
/// is non-null the alarm names are returned bare (caller path-qualifies);
|
/// is non-null the alarm names are returned bare (caller path-qualifies);
|
||||||
/// the keys of the returned dictionary are always bare alarm names.
|
/// the keys of the returned dictionary are always bare alarm names.
|
||||||
/// <paramref name="alarmScriptIds"/> is populated with the on-trigger
|
/// <para>
|
||||||
/// script id of each resolved alarm keyed by the canonical name the alarm
|
/// Each alarm's on-trigger script is resolved <b>within this chain</b>: the
|
||||||
/// will ultimately carry (bare name when <paramref name="prefix"/> is null,
|
/// <c>OnTriggerScriptId</c> is looked up among the script rows of every
|
||||||
/// otherwise <c>prefix.name</c>).
|
/// template in <paramref name="templateChain"/> and translated to that row's
|
||||||
|
/// NAME, which is then path-qualified with <paramref name="prefix"/> and set
|
||||||
|
/// as <see cref="ResolvedAlarm.OnTriggerScriptCanonicalName"/>. 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).
|
||||||
|
/// </para>
|
||||||
|
/// <paramref name="unresolvedScriptIds"/> 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.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static Dictionary<string, ResolvedAlarm> ResolveInheritedAlarms(
|
private static Dictionary<string, ResolvedAlarm> ResolveInheritedAlarms(
|
||||||
IReadOnlyList<Template> templateChain,
|
IReadOnlyList<Template> templateChain,
|
||||||
string? prefix,
|
string? prefix,
|
||||||
Dictionary<string, int> alarmScriptIds)
|
out Dictionary<string, int> unresolvedScriptIds)
|
||||||
{
|
{
|
||||||
var result = new Dictionary<string, ResolvedAlarm>(StringComparer.Ordinal);
|
var result = new Dictionary<string, ResolvedAlarm>(StringComparer.Ordinal);
|
||||||
var scriptIdByName = new Dictionary<string, int>(StringComparer.Ordinal);
|
var scriptIdByName = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||||
|
|
||||||
|
// Script row id -> script name, over every template of this chain
|
||||||
|
// (base rows, derived overrides and IsInherited placeholders alike).
|
||||||
|
var chainScriptNameById = new Dictionary<int, string>();
|
||||||
|
foreach (var t in templateChain)
|
||||||
|
foreach (var script in t.Scripts)
|
||||||
|
if (script.Id != 0)
|
||||||
|
chainScriptNameById.TryAdd(script.Id, script.Name);
|
||||||
|
|
||||||
for (int i = templateChain.Count - 1; i >= 0; i--)
|
for (int i = templateChain.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var template = templateChain[i];
|
var template = templateChain[i];
|
||||||
@@ -468,10 +492,20 @@ public class FlatteningService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unresolvedScriptIds = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||||
foreach (var (name, scriptId) in scriptIdByName)
|
foreach (var (name, scriptId) in scriptIdByName)
|
||||||
{
|
{
|
||||||
var canonical = prefix == null ? name : $"{prefix}.{name}";
|
if (chainScriptNameById.TryGetValue(scriptId, out var scriptName))
|
||||||
alarmScriptIds[canonical] = scriptId;
|
{
|
||||||
|
result[name] = result[name] with
|
||||||
|
{
|
||||||
|
OnTriggerScriptCanonicalName = prefix == null ? scriptName : $"{prefix}.{scriptName}"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unresolvedScriptIds[name] = scriptId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -652,7 +686,7 @@ public class FlatteningService
|
|||||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var composedAlarms = ResolveInheritedAlarms(composedChain, prefix, alarmScriptIds);
|
var composedAlarms = ResolveInheritedAlarms(composedChain, prefix, out var unresolvedScriptIds);
|
||||||
foreach (var (name, alarm) in composedAlarms)
|
foreach (var (name, alarm) in composedAlarms)
|
||||||
{
|
{
|
||||||
var canonicalName = $"{prefix}.{name}";
|
var canonicalName = $"{prefix}.{name}";
|
||||||
@@ -664,6 +698,10 @@ public class FlatteningService
|
|||||||
TriggerConfiguration = PrefixTriggerAttribute(alarm.TriggerConfiguration, prefix),
|
TriggerConfiguration = PrefixTriggerAttribute(alarm.TriggerConfiguration, prefix),
|
||||||
Source = "Composed"
|
Source = "Composed"
|
||||||
};
|
};
|
||||||
|
// Only the pass that adds the alarm records its fallback script
|
||||||
|
// id (TPL-5: a nested slot is visited twice under one prefix).
|
||||||
|
if (unresolvedScriptIds.TryGetValue(name, out var scriptId))
|
||||||
|
alarmScriptIds[canonicalName] = scriptId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1028,19 +1066,19 @@ public class FlatteningService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolves alarm on-trigger script references from <see cref="TemplateScript.Id"/>
|
/// Finalises alarm on-trigger script references. Most were resolved by name
|
||||||
/// values to the canonical (path-qualified) names of the corresponding
|
/// within the alarm's own chain in <see cref="ResolveInheritedAlarms"/>; this
|
||||||
/// resolved scripts. <paramref name="alarmScriptIds"/> maps an alarm's
|
/// pass (1) resolves the legacy fallback — an on-trigger script id that is not
|
||||||
/// canonical name to the id of its on-trigger script; <paramref name="scriptCanonicalById"/>
|
/// a script row of the alarm's chain — through the flatten-wide
|
||||||
/// maps a script id to the canonical name it carries in the flattened
|
/// <paramref name="scriptCanonicalById"/> map, and (2) clears any reference
|
||||||
/// configuration. An alarm whose on-trigger script id has no matching
|
/// whose canonical name is not a resolved script, so a dangling reference is
|
||||||
/// resolved script is left with a <c>null</c> reference — semantic
|
/// left <c>null</c> exactly as before.
|
||||||
/// validation then reports the dangling reference.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static void ResolveAlarmScriptReferences(
|
private static void ResolveAlarmScriptReferences(
|
||||||
Dictionary<string, ResolvedAlarm> alarms,
|
Dictionary<string, ResolvedAlarm> alarms,
|
||||||
Dictionary<string, int> alarmScriptIds,
|
Dictionary<string, int> alarmScriptIds,
|
||||||
Dictionary<int, string> scriptCanonicalById)
|
Dictionary<int, string> scriptCanonicalById,
|
||||||
|
IReadOnlyDictionary<string, ResolvedScript> scripts)
|
||||||
{
|
{
|
||||||
foreach (var (alarmCanonicalName, scriptId) in alarmScriptIds)
|
foreach (var (alarmCanonicalName, scriptId) in alarmScriptIds)
|
||||||
{
|
{
|
||||||
@@ -1053,5 +1091,11 @@ public class FlatteningService
|
|||||||
OnTriggerScriptCanonicalName = scriptCanonicalName
|
OnTriggerScriptCanonicalName = scriptCanonicalName
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (var (alarmCanonicalName, alarm) in alarms.ToList())
|
||||||
|
{
|
||||||
|
if (alarm.OnTriggerScriptCanonicalName is { } scriptName && !scripts.ContainsKey(scriptName))
|
||||||
|
alarms[alarmCanonicalName] = alarm with { OnTriggerScriptCanonicalName = null };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+279
@@ -0,0 +1,279 @@
|
|||||||
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
||||||
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
||||||
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
|
||||||
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||||
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Flattening;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WP2 tasks 2.2/2.3 — alarm on-trigger script references are resolved by NAME within
|
||||||
|
/// the alarm's own composition chain, not through a flatten-wide script-id map.
|
||||||
|
/// TPL-1: two slots of one module share the base script row id, so the global map made
|
||||||
|
/// every slot's alarm run the LAST slot's script. TPL-2: a derived script override has
|
||||||
|
/// a different row id, so the base alarm lost its on-trigger script. TPL-5: a nested
|
||||||
|
/// slot visited twice under one prefix let the base pass overwrite a slot-level
|
||||||
|
/// override. The fixes change the revision hash of affected configurations by design.
|
||||||
|
/// </summary>
|
||||||
|
public class AlarmOnTriggerScriptResolutionTests
|
||||||
|
{
|
||||||
|
private readonly FlatteningService _sut = new();
|
||||||
|
|
||||||
|
private static Instance CreateInstance(int templateId = 1) =>
|
||||||
|
new("TestInstance") { Id = 1, TemplateId = templateId, SiteId = 1 };
|
||||||
|
|
||||||
|
private static Template CreateTemplate(int id, string name, int? parentId = null) =>
|
||||||
|
new(name) { Id = id, ParentTemplateId = parentId };
|
||||||
|
|
||||||
|
private static TemplateAlarm Alarm(string name, int? scriptId, bool inherited = false) => new(name)
|
||||||
|
{
|
||||||
|
TriggerType = AlarmTriggerType.RangeViolation,
|
||||||
|
TriggerConfiguration = "{\"attributeName\":\"Temp\",\"high\":100}",
|
||||||
|
PriorityLevel = 1,
|
||||||
|
OnTriggerScriptId = scriptId,
|
||||||
|
IsInherited = inherited,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the slot-derived template the composition service creates for a slot:
|
||||||
|
/// a child of <paramref name="baseTemplate"/> whose members are IsInherited
|
||||||
|
/// placeholders (placeholder alarms keep pointing at the BASE script id).
|
||||||
|
/// </summary>
|
||||||
|
private static Template SlotDerived(int id, Template baseTemplate, int scriptPlaceholderId)
|
||||||
|
{
|
||||||
|
var t = CreateTemplate(id, $"{baseTemplate.Name}_slot{id}", baseTemplate.Id);
|
||||||
|
foreach (var s in baseTemplate.Scripts)
|
||||||
|
t.Scripts.Add(new TemplateScript(s.Name, s.Code) { Id = scriptPlaceholderId++, IsInherited = true });
|
||||||
|
foreach (var a in baseTemplate.Alarms)
|
||||||
|
t.Alarms.Add(Alarm(a.Name, a.OnTriggerScriptId, inherited: true));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>TPL-1: each slot's alarm runs its OWN slot's script.</summary>
|
||||||
|
[Fact]
|
||||||
|
public void TwoSlotsOfOneModule_EachAlarmResolvesItsOwnSlotScript()
|
||||||
|
{
|
||||||
|
var pump = CreateTemplate(10, "Pump");
|
||||||
|
pump.Scripts.Add(new TemplateScript("OnHigh", "// on high") { Id = 100 });
|
||||||
|
pump.Alarms.Add(Alarm("HighTemp", 100));
|
||||||
|
var pump1 = SlotDerived(11, pump, 110);
|
||||||
|
var pump2 = SlotDerived(12, pump, 120);
|
||||||
|
var skid = CreateTemplate(1, "Skid");
|
||||||
|
|
||||||
|
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition>
|
||||||
|
{
|
||||||
|
new("Pump1") { ComposedTemplateId = 11 },
|
||||||
|
new("Pump2") { ComposedTemplateId = 12 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var chains = new Dictionary<int, IReadOnlyList<Template>>
|
||||||
|
{
|
||||||
|
[11] = [pump1, pump],
|
||||||
|
[12] = [pump2, pump],
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [skid], compositions, chains, new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("Pump1.OnHigh", result.Value.Alarms.Single(a => a.CanonicalName == "Pump1.HighTemp").OnTriggerScriptCanonicalName);
|
||||||
|
Assert.Equal("Pump2.OnHigh", result.Value.Alarms.Single(a => a.CanonicalName == "Pump2.HighTemp").OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>TPL-1 with the base template composed directly into two slots (one shared chain).</summary>
|
||||||
|
[Fact]
|
||||||
|
public void SameTemplateComposedDirectlyTwice_EachAlarmResolvesItsOwnSlotScript()
|
||||||
|
{
|
||||||
|
var pump = CreateTemplate(10, "Pump");
|
||||||
|
pump.Scripts.Add(new TemplateScript("OnHigh", "// on high") { Id = 100 });
|
||||||
|
pump.Alarms.Add(Alarm("HighTemp", 100));
|
||||||
|
var skid = CreateTemplate(1, "Skid");
|
||||||
|
|
||||||
|
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition>
|
||||||
|
{
|
||||||
|
new("A") { ComposedTemplateId = 10 },
|
||||||
|
new("B") { ComposedTemplateId = 10 },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
var chains = new Dictionary<int, IReadOnlyList<Template>> { [10] = [pump] };
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [skid], compositions, chains, new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("A.OnHigh", result.Value.Alarms.Single(a => a.CanonicalName == "A.HighTemp").OnTriggerScriptCanonicalName);
|
||||||
|
Assert.Equal("B.OnHigh", result.Value.Alarms.Single(a => a.CanonicalName == "B.HighTemp").OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TPL-5: a slot-level override that clears a nested alarm's on-trigger script is
|
||||||
|
/// not overwritten by the second (base) visit of the same nested slot.
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void NestedSlotOverride_ClearingOnTriggerScript_IsNotOverwrittenByBasePass()
|
||||||
|
{
|
||||||
|
// Motor(5) has OnOverload(500) and alarm Overload -> 500.
|
||||||
|
var motor = CreateTemplate(5, "Motor");
|
||||||
|
motor.Scripts.Add(new TemplateScript("OnOverload", "// overload") { Id = 500 });
|
||||||
|
motor.Alarms.Add(Alarm("Overload", 500));
|
||||||
|
// Pump(3) composes Motor via its slot-derived Motor (6).
|
||||||
|
var pump = CreateTemplate(3, "Pump");
|
||||||
|
var pumpMotor = SlotDerived(6, motor, 600);
|
||||||
|
// Skid's slot Pump1 is a slot-derived Pump (2) with its own slot-derived Motor (7, child of 6),
|
||||||
|
// on which the operator overrode Overload to have NO on-trigger script.
|
||||||
|
var pump1 = CreateTemplate(2, "Pump_slot2", 3);
|
||||||
|
var pump1Motor = SlotDerived(7, pumpMotor, 700);
|
||||||
|
pump1Motor.ParentTemplateId = 6;
|
||||||
|
pump1Motor.Alarms.Clear();
|
||||||
|
pump1Motor.Alarms.Add(Alarm("Overload", scriptId: null, inherited: false));
|
||||||
|
var skid = CreateTemplate(1, "Skid");
|
||||||
|
|
||||||
|
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition> { new("Pump1") { ComposedTemplateId = 2 } },
|
||||||
|
[2] = new List<TemplateComposition> { new("Motor") { ComposedTemplateId = 7 } },
|
||||||
|
[3] = new List<TemplateComposition> { new("Motor") { ComposedTemplateId = 6 } },
|
||||||
|
};
|
||||||
|
var chains = new Dictionary<int, IReadOnlyList<Template>>
|
||||||
|
{
|
||||||
|
[2] = [pump1, pump],
|
||||||
|
[7] = [pump1Motor, pumpMotor, motor],
|
||||||
|
[6] = [pumpMotor, motor],
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [skid], compositions, chains, new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
var alarm = result.Value.Alarms.Single(a => a.CanonicalName == "Pump1.Motor.Overload");
|
||||||
|
Assert.Null(alarm.OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>TPL-5: a slot-level override that re-points the nested alarm keeps the new script.</summary>
|
||||||
|
[Fact]
|
||||||
|
public void NestedSlotOverride_RepointingOnTriggerScript_Wins()
|
||||||
|
{
|
||||||
|
var motor = CreateTemplate(5, "Motor");
|
||||||
|
motor.Scripts.Add(new TemplateScript("OnOverload", "// overload") { Id = 500 });
|
||||||
|
motor.Scripts.Add(new TemplateScript("OnOverloadAlt", "// alt") { Id = 501 });
|
||||||
|
motor.Alarms.Add(Alarm("Overload", 500));
|
||||||
|
var pump = CreateTemplate(3, "Pump");
|
||||||
|
var pumpMotor = SlotDerived(6, motor, 600);
|
||||||
|
var pump1 = CreateTemplate(2, "Pump_slot2", 3);
|
||||||
|
var pump1Motor = SlotDerived(7, pumpMotor, 700);
|
||||||
|
pump1Motor.ParentTemplateId = 6;
|
||||||
|
pump1Motor.Alarms.Clear();
|
||||||
|
pump1Motor.Alarms.Add(Alarm("Overload", scriptId: 501, inherited: false));
|
||||||
|
var skid = CreateTemplate(1, "Skid");
|
||||||
|
|
||||||
|
var compositions = new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition> { new("Pump1") { ComposedTemplateId = 2 } },
|
||||||
|
[2] = new List<TemplateComposition> { new("Motor") { ComposedTemplateId = 7 } },
|
||||||
|
[3] = new List<TemplateComposition> { new("Motor") { ComposedTemplateId = 6 } },
|
||||||
|
};
|
||||||
|
var chains = new Dictionary<int, IReadOnlyList<Template>>
|
||||||
|
{
|
||||||
|
[2] = [pump1, pump],
|
||||||
|
[7] = [pump1Motor, pumpMotor, motor],
|
||||||
|
[6] = [pumpMotor, motor],
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [skid], compositions, chains, new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("Pump1.Motor.OnOverloadAlt",
|
||||||
|
result.Value.Alarms.Single(a => a.CanonicalName == "Pump1.Motor.Overload").OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TPL-2: a derived template that overrides the on-trigger script (a new row with a
|
||||||
|
/// different id) keeps the base alarm's link, and the overridden code is what runs.
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DerivedScriptOverride_KeepsBaseAlarmOnTriggerLink()
|
||||||
|
{
|
||||||
|
var tank = CreateTemplate(1, "Tank");
|
||||||
|
tank.Scripts.Add(new TemplateScript("OnHighLevel", "// base") { Id = 10 });
|
||||||
|
tank.Alarms.Add(Alarm("HighLevel", 10));
|
||||||
|
var heater = CreateTemplate(2, "TankWithHeater", 1);
|
||||||
|
heater.Scripts.Add(new TemplateScript("OnHighLevel", "// derived") { Id = 20, IsInherited = false });
|
||||||
|
heater.Alarms.Add(Alarm("HighLevel", 10, inherited: true));
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(templateId: 2), [heater, tank],
|
||||||
|
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||||
|
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||||
|
new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("OnHighLevel", result.Value.Alarms.Single(a => a.CanonicalName == "HighLevel").OnTriggerScriptCanonicalName);
|
||||||
|
Assert.Equal("// derived", result.Value.Scripts.Single(s => s.CanonicalName == "OnHighLevel").Code);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>TPL-2 for a composed module: a derived-module override keeps the slot alarm's link.</summary>
|
||||||
|
[Fact]
|
||||||
|
public void DerivedScriptOverride_InComposedModule_KeepsLink()
|
||||||
|
{
|
||||||
|
var pump = CreateTemplate(10, "Pump");
|
||||||
|
pump.Scripts.Add(new TemplateScript("OnHigh", "// base") { Id = 100 });
|
||||||
|
pump.Alarms.Add(Alarm("HighTemp", 100));
|
||||||
|
var slot = SlotDerived(11, pump, 110);
|
||||||
|
slot.Scripts.Clear();
|
||||||
|
slot.Scripts.Add(new TemplateScript("OnHigh", "// slot override") { Id = 111, IsInherited = false });
|
||||||
|
var skid = CreateTemplate(1, "Skid");
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [skid],
|
||||||
|
new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition> { new("P") { ComposedTemplateId = 11 } },
|
||||||
|
},
|
||||||
|
new Dictionary<int, IReadOnlyList<Template>> { [11] = [slot, pump] },
|
||||||
|
new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("P.OnHigh", result.Value.Alarms.Single(a => a.CanonicalName == "P.HighTemp").OnTriggerScriptCanonicalName);
|
||||||
|
Assert.Equal("// slot override", result.Value.Scripts.Single(s => s.CanonicalName == "P.OnHigh").Code);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Legacy fallback kept: an alarm whose on-trigger script id is not a script row of its
|
||||||
|
/// own chain (here a root alarm pointing at a composed module's script) still resolves
|
||||||
|
/// through the flatten-wide id map, as before.
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void OutOfChainScriptId_StillResolvesThroughLegacyIdMap()
|
||||||
|
{
|
||||||
|
var pump = CreateTemplate(2, "Pump");
|
||||||
|
pump.Scripts.Add(new TemplateScript("Handler", "// h") { Id = 60 });
|
||||||
|
var station = CreateTemplate(1, "Station");
|
||||||
|
station.Alarms.Add(Alarm("StationAlarm", 60));
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [station],
|
||||||
|
new Dictionary<int, IReadOnlyList<TemplateComposition>>
|
||||||
|
{
|
||||||
|
[1] = new List<TemplateComposition> { new("Main") { ComposedTemplateId = 2 } },
|
||||||
|
},
|
||||||
|
new Dictionary<int, IReadOnlyList<Template>> { [2] = [pump] },
|
||||||
|
new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("Main.Handler", result.Value.Alarms.Single(a => a.CanonicalName == "StationAlarm").OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>A dangling on-trigger script id still resolves to null.</summary>
|
||||||
|
[Fact]
|
||||||
|
public void DanglingScriptId_ResolvesToNull()
|
||||||
|
{
|
||||||
|
var t = CreateTemplate(1, "T");
|
||||||
|
t.Alarms.Add(Alarm("A", 999));
|
||||||
|
|
||||||
|
var result = _sut.Flatten(CreateInstance(), [t],
|
||||||
|
new Dictionary<int, IReadOnlyList<TemplateComposition>>(),
|
||||||
|
new Dictionary<int, IReadOnlyList<Template>>(),
|
||||||
|
new Dictionary<int, DataConnection>());
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Null(result.Value.Alarms.Single().OnTriggerScriptCanonicalName);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user