fe60c4a1dc
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.
280 lines
13 KiB
C#
280 lines
13 KiB
C#
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);
|
|
}
|
|
}
|