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:
@@ -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<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);
|
||||
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 <paramref name="prefix"/>
|
||||
/// is non-null the alarm names are returned bare (caller path-qualifies);
|
||||
/// the keys of the returned dictionary are always bare alarm names.
|
||||
/// <paramref name="alarmScriptIds"/> 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 <paramref name="prefix"/> is null,
|
||||
/// otherwise <c>prefix.name</c>).
|
||||
/// <para>
|
||||
/// Each alarm's on-trigger script is resolved <b>within this chain</b>: the
|
||||
/// <c>OnTriggerScriptId</c> is looked up among the script rows of every
|
||||
/// 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>
|
||||
private static Dictionary<string, ResolvedAlarm> ResolveInheritedAlarms(
|
||||
IReadOnlyList<Template> templateChain,
|
||||
string? prefix,
|
||||
Dictionary<string, int> alarmScriptIds)
|
||||
out Dictionary<string, int> unresolvedScriptIds)
|
||||
{
|
||||
var result = new Dictionary<string, ResolvedAlarm>(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--)
|
||||
{
|
||||
var template = templateChain[i];
|
||||
@@ -468,10 +492,20 @@ public class FlatteningService
|
||||
}
|
||||
}
|
||||
|
||||
unresolvedScriptIds = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
foreach (var (name, scriptId) in scriptIdByName)
|
||||
{
|
||||
var canonical = prefix == null ? name : $"{prefix}.{name}";
|
||||
alarmScriptIds[canonical] = scriptId;
|
||||
if (chainScriptNameById.TryGetValue(scriptId, out var scriptName))
|
||||
{
|
||||
result[name] = result[name] with
|
||||
{
|
||||
OnTriggerScriptCanonicalName = prefix == null ? scriptName : $"{prefix}.{scriptName}"
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
unresolvedScriptIds[name] = scriptId;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -652,7 +686,7 @@ public class FlatteningService
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
return;
|
||||
|
||||
var composedAlarms = ResolveInheritedAlarms(composedChain, prefix, alarmScriptIds);
|
||||
var composedAlarms = ResolveInheritedAlarms(composedChain, prefix, out var unresolvedScriptIds);
|
||||
foreach (var (name, alarm) in composedAlarms)
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
@@ -664,6 +698,10 @@ public class FlatteningService
|
||||
TriggerConfiguration = PrefixTriggerAttribute(alarm.TriggerConfiguration, prefix),
|
||||
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>
|
||||
/// Resolves alarm on-trigger script references from <see cref="TemplateScript.Id"/>
|
||||
/// values to the canonical (path-qualified) names of the corresponding
|
||||
/// resolved scripts. <paramref name="alarmScriptIds"/> maps an alarm's
|
||||
/// canonical name to the id of its on-trigger script; <paramref name="scriptCanonicalById"/>
|
||||
/// maps a script id to the canonical name it carries in the flattened
|
||||
/// configuration. An alarm whose on-trigger script id has no matching
|
||||
/// resolved script is left with a <c>null</c> reference — semantic
|
||||
/// validation then reports the dangling reference.
|
||||
/// Finalises alarm on-trigger script references. Most were resolved by name
|
||||
/// within the alarm's own chain in <see cref="ResolveInheritedAlarms"/>; this
|
||||
/// pass (1) resolves the legacy fallback — an on-trigger script id that is not
|
||||
/// a script row of the alarm's chain — through the flatten-wide
|
||||
/// <paramref name="scriptCanonicalById"/> map, and (2) clears any reference
|
||||
/// whose canonical name is not a resolved script, so a dangling reference is
|
||||
/// left <c>null</c> exactly as before.
|
||||
/// </summary>
|
||||
private static void ResolveAlarmScriptReferences(
|
||||
Dictionary<string, ResolvedAlarm> alarms,
|
||||
Dictionary<string, int> alarmScriptIds,
|
||||
Dictionary<int, string> scriptCanonicalById)
|
||||
Dictionary<int, string> scriptCanonicalById,
|
||||
IReadOnlyDictionary<string, ResolvedScript> scripts)
|
||||
{
|
||||
foreach (var (alarmCanonicalName, scriptId) in alarmScriptIds)
|
||||
{
|
||||
@@ -1053,5 +1091,11 @@ public class FlatteningService
|
||||
OnTriggerScriptCanonicalName = scriptCanonicalName
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var (alarmCanonicalName, alarm) in alarms.ToList())
|
||||
{
|
||||
if (alarm.OnTriggerScriptCanonicalName is { } scriptName && !scripts.ContainsKey(scriptName))
|
||||
alarms[alarmCanonicalName] = alarm with { OnTriggerScriptCanonicalName = null };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user