fix(template-engine): resolve TemplateEngine-001/003/004/005, re-triage 002 — recursive composed flattening, fixed-field guard, alarm script refs, dead collision query
This commit is contained in:
@@ -78,17 +78,23 @@ public class FlatteningService
|
||||
// Step 4: Apply connection bindings
|
||||
ApplyConnectionBindings(instance.ConnectionBindings, attributes, dataConnections);
|
||||
|
||||
// Step 5: Resolve alarms from inheritance chain
|
||||
var alarms = ResolveInheritedAlarms(templateChain);
|
||||
ResolveComposedAlarms(templateChain, compositionMap, composedTemplateChains, alarms);
|
||||
// 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).
|
||||
var alarmScriptIds = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
var alarms = ResolveInheritedAlarms(templateChain, prefix: null, alarmScriptIds);
|
||||
ResolveComposedAlarms(templateChain, compositionMap, composedTemplateChains, alarms, alarmScriptIds);
|
||||
ApplyInstanceAlarmOverrides(instance.AlarmOverrides, alarms);
|
||||
|
||||
// Step 6: Resolve scripts from inheritance chain
|
||||
var scripts = ResolveInheritedScripts(templateChain);
|
||||
ResolveComposedScripts(templateChain, compositionMap, composedTemplateChains, scripts);
|
||||
// Step 6: Resolve scripts from inheritance chain.
|
||||
// scriptCanonicalById maps a TemplateScript.Id to its resolved
|
||||
// canonical name, used to wire up alarm on-trigger script refs.
|
||||
var scriptCanonicalById = new Dictionary<int, string>();
|
||||
var scripts = ResolveInheritedScripts(templateChain, prefix: null, scriptCanonicalById);
|
||||
ResolveComposedScripts(templateChain, compositionMap, composedTemplateChains, scripts, scriptCanonicalById);
|
||||
|
||||
// Step 7: Resolve alarm on-trigger script references to canonical names
|
||||
ResolveAlarmScriptReferences(alarms, scripts);
|
||||
ResolveAlarmScriptReferences(alarms, alarmScriptIds, scriptCanonicalById);
|
||||
|
||||
// Step 8: Collect connection configurations for deployment packaging
|
||||
var connections = new Dictionary<string, ConnectionConfig>();
|
||||
@@ -221,57 +227,56 @@ public class FlatteningService
|
||||
continue;
|
||||
|
||||
foreach (var composition in compositions)
|
||||
ResolveComposedAttributesRecursive(
|
||||
composition, composition.InstanceName,
|
||||
compositionMap, composedTemplateChains, attributes, new HashSet<int>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively resolves the attributes of a composed module and every
|
||||
/// module nested inside it (to arbitrary depth), path-qualifying each
|
||||
/// canonical name with the accumulated <paramref name="prefix"/>.
|
||||
/// </summary>
|
||||
private static void ResolveComposedAttributesRecursive(
|
||||
TemplateComposition composition,
|
||||
string prefix,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<Template>> composedTemplateChains,
|
||||
Dictionary<string, ResolvedAttribute> attributes,
|
||||
HashSet<int> visited)
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
return;
|
||||
|
||||
var composedAttrs = ResolveInheritedAttributes(composedChain);
|
||||
foreach (var (name, attr) in composedAttrs)
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
// Don't overwrite if already defined (most-derived wins)
|
||||
if (!attributes.ContainsKey(canonicalName))
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
continue;
|
||||
|
||||
var prefix = composition.InstanceName;
|
||||
var composedAttrs = ResolveInheritedAttributes(composedChain);
|
||||
|
||||
foreach (var (name, attr) in composedAttrs)
|
||||
attributes[canonicalName] = attr with
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
// Don't overwrite if already defined (most-derived wins)
|
||||
if (!attributes.ContainsKey(canonicalName))
|
||||
{
|
||||
attributes[canonicalName] = attr with
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
Source = "Composed"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into nested compositions
|
||||
foreach (var composedTemplate in composedChain)
|
||||
{
|
||||
if (!compositionMap.TryGetValue(composedTemplate.Id, out var nestedCompositions))
|
||||
continue;
|
||||
|
||||
foreach (var nested in nestedCompositions)
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(nested.ComposedTemplateId, out var nestedChain))
|
||||
continue;
|
||||
|
||||
var nestedPrefix = $"{prefix}.{nested.InstanceName}";
|
||||
var nestedAttrs = ResolveInheritedAttributes(nestedChain);
|
||||
|
||||
foreach (var (name, attr) in nestedAttrs)
|
||||
{
|
||||
var canonicalName = $"{nestedPrefix}.{name}";
|
||||
if (!attributes.ContainsKey(canonicalName))
|
||||
{
|
||||
attributes[canonicalName] = attr with
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
Source = "Composed"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CanonicalName = canonicalName,
|
||||
Source = "Composed"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Descend into nested compositions of every template in the chain.
|
||||
foreach (var composedTemplate in composedChain)
|
||||
{
|
||||
if (!visited.Add(composedTemplate.Id))
|
||||
continue;
|
||||
if (!compositionMap.TryGetValue(composedTemplate.Id, out var nestedCompositions))
|
||||
continue;
|
||||
|
||||
foreach (var nested in nestedCompositions)
|
||||
ResolveComposedAttributesRecursive(
|
||||
nested, $"{prefix}.{nested.InstanceName}",
|
||||
compositionMap, composedTemplateChains, attributes, visited);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyInstanceOverrides(
|
||||
@@ -356,10 +361,22 @@ public class FlatteningService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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>).
|
||||
/// </summary>
|
||||
private static Dictionary<string, ResolvedAlarm> ResolveInheritedAlarms(
|
||||
IReadOnlyList<Template> templateChain)
|
||||
IReadOnlyList<Template> templateChain,
|
||||
string? prefix,
|
||||
Dictionary<string, int> alarmScriptIds)
|
||||
{
|
||||
var result = new Dictionary<string, ResolvedAlarm>(StringComparer.Ordinal);
|
||||
var scriptIdByName = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
|
||||
for (int i = templateChain.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@@ -394,9 +411,20 @@ public class FlatteningService
|
||||
OnTriggerScriptCanonicalName = null, // Resolved later
|
||||
Source = source
|
||||
};
|
||||
|
||||
if (alarm.OnTriggerScriptId.HasValue)
|
||||
scriptIdByName[alarm.Name] = alarm.OnTriggerScriptId.Value;
|
||||
else
|
||||
scriptIdByName.Remove(alarm.Name);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (name, scriptId) in scriptIdByName)
|
||||
{
|
||||
var canonical = prefix == null ? name : $"{prefix}.{name}";
|
||||
alarmScriptIds[canonical] = scriptId;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -536,7 +564,8 @@ public class FlatteningService
|
||||
IReadOnlyList<Template> templateChain,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<Template>> composedTemplateChains,
|
||||
Dictionary<string, ResolvedAlarm> alarms)
|
||||
Dictionary<string, ResolvedAlarm> alarms,
|
||||
Dictionary<string, int> alarmScriptIds)
|
||||
{
|
||||
foreach (var template in templateChain)
|
||||
{
|
||||
@@ -544,34 +573,74 @@ public class FlatteningService
|
||||
continue;
|
||||
|
||||
foreach (var composition in compositions)
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
continue;
|
||||
|
||||
var prefix = composition.InstanceName;
|
||||
var composedAlarms = ResolveInheritedAlarms(composedChain);
|
||||
|
||||
foreach (var (name, alarm) in composedAlarms)
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
if (!alarms.ContainsKey(canonicalName))
|
||||
{
|
||||
alarms[canonicalName] = alarm with
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
TriggerConfiguration = PrefixTriggerAttribute(alarm.TriggerConfiguration, prefix),
|
||||
Source = "Composed"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
ResolveComposedAlarmsRecursive(
|
||||
composition, composition.InstanceName,
|
||||
compositionMap, composedTemplateChains, alarms, alarmScriptIds,
|
||||
new HashSet<int>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively resolves the alarms of a composed module and every module
|
||||
/// nested inside it, path-qualifying each canonical name with the
|
||||
/// accumulated <paramref name="prefix"/>.
|
||||
/// </summary>
|
||||
private static void ResolveComposedAlarmsRecursive(
|
||||
TemplateComposition composition,
|
||||
string prefix,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<Template>> composedTemplateChains,
|
||||
Dictionary<string, ResolvedAlarm> alarms,
|
||||
Dictionary<string, int> alarmScriptIds,
|
||||
HashSet<int> visited)
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
return;
|
||||
|
||||
var composedAlarms = ResolveInheritedAlarms(composedChain, prefix, alarmScriptIds);
|
||||
foreach (var (name, alarm) in composedAlarms)
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
if (!alarms.ContainsKey(canonicalName))
|
||||
{
|
||||
alarms[canonicalName] = alarm with
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
TriggerConfiguration = PrefixTriggerAttribute(alarm.TriggerConfiguration, prefix),
|
||||
Source = "Composed"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Descend into nested compositions of every template in the chain.
|
||||
foreach (var composedTemplate in composedChain)
|
||||
{
|
||||
if (!visited.Add(composedTemplate.Id))
|
||||
continue;
|
||||
if (!compositionMap.TryGetValue(composedTemplate.Id, out var nestedCompositions))
|
||||
continue;
|
||||
|
||||
foreach (var nested in nestedCompositions)
|
||||
ResolveComposedAlarmsRecursive(
|
||||
nested, $"{prefix}.{nested.InstanceName}",
|
||||
compositionMap, composedTemplateChains, alarms, alarmScriptIds, visited);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves scripts from an inheritance chain. The returned dictionary is
|
||||
/// keyed by bare script name. <paramref name="scriptCanonicalById"/> is
|
||||
/// populated with each resolved script's <see cref="TemplateScript.Id"/>
|
||||
/// mapped to the canonical name it will ultimately carry (bare when
|
||||
/// <paramref name="prefix"/> is null, otherwise <c>prefix.name</c>).
|
||||
/// </summary>
|
||||
private static Dictionary<string, ResolvedScript> ResolveInheritedScripts(
|
||||
IReadOnlyList<Template> templateChain)
|
||||
IReadOnlyList<Template> templateChain,
|
||||
string? prefix,
|
||||
Dictionary<int, string> scriptCanonicalById)
|
||||
{
|
||||
var result = new Dictionary<string, ResolvedScript>(StringComparer.Ordinal);
|
||||
var idByName = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
|
||||
for (int i = templateChain.Count - 1; i >= 0; i--)
|
||||
{
|
||||
@@ -600,9 +669,17 @@ public class FlatteningService
|
||||
MinTimeBetweenRuns = script.MinTimeBetweenRuns,
|
||||
Source = source
|
||||
};
|
||||
idByName[script.Name] = script.Id;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (name, id) in idByName)
|
||||
{
|
||||
if (id == 0) continue; // unsaved row — no stable id to map
|
||||
var canonical = prefix == null ? name : $"{prefix}.{name}";
|
||||
scriptCanonicalById[id] = canonical;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -610,7 +687,8 @@ public class FlatteningService
|
||||
IReadOnlyList<Template> templateChain,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<Template>> composedTemplateChains,
|
||||
Dictionary<string, ResolvedScript> scripts)
|
||||
Dictionary<string, ResolvedScript> scripts,
|
||||
Dictionary<int, string> scriptCanonicalById)
|
||||
{
|
||||
foreach (var template in templateChain)
|
||||
{
|
||||
@@ -618,28 +696,58 @@ public class FlatteningService
|
||||
continue;
|
||||
|
||||
foreach (var composition in compositions)
|
||||
ResolveComposedScriptsRecursive(
|
||||
composition, composition.InstanceName,
|
||||
compositionMap, composedTemplateChains, scripts, scriptCanonicalById,
|
||||
new HashSet<int>());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Recursively resolves the scripts of a composed module and every module
|
||||
/// nested inside it, path-qualifying each canonical name with the
|
||||
/// accumulated <paramref name="prefix"/>.
|
||||
/// </summary>
|
||||
private static void ResolveComposedScriptsRecursive(
|
||||
TemplateComposition composition,
|
||||
string prefix,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<Template>> composedTemplateChains,
|
||||
Dictionary<string, ResolvedScript> scripts,
|
||||
Dictionary<int, string> scriptCanonicalById,
|
||||
HashSet<int> visited)
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
return;
|
||||
|
||||
var composedScripts = ResolveInheritedScripts(composedChain, prefix, scriptCanonicalById);
|
||||
foreach (var (name, script) in composedScripts)
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
if (!scripts.ContainsKey(canonicalName))
|
||||
{
|
||||
if (!composedTemplateChains.TryGetValue(composition.ComposedTemplateId, out var composedChain))
|
||||
continue;
|
||||
|
||||
var prefix = composition.InstanceName;
|
||||
var composedScripts = ResolveInheritedScripts(composedChain);
|
||||
|
||||
foreach (var (name, script) in composedScripts)
|
||||
scripts[canonicalName] = script with
|
||||
{
|
||||
var canonicalName = $"{prefix}.{name}";
|
||||
if (!scripts.ContainsKey(canonicalName))
|
||||
{
|
||||
scripts[canonicalName] = script with
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
Source = "Composed",
|
||||
Scope = new Commons.Types.Scripts.ScriptScope(SelfPath: prefix, ParentPath: "")
|
||||
};
|
||||
}
|
||||
}
|
||||
CanonicalName = canonicalName,
|
||||
Source = "Composed",
|
||||
Scope = new Commons.Types.Scripts.ScriptScope(SelfPath: prefix, ParentPath: "")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Descend into nested compositions of every template in the chain.
|
||||
foreach (var composedTemplate in composedChain)
|
||||
{
|
||||
if (!visited.Add(composedTemplate.Id))
|
||||
continue;
|
||||
if (!compositionMap.TryGetValue(composedTemplate.Id, out var nestedCompositions))
|
||||
continue;
|
||||
|
||||
foreach (var nested in nestedCompositions)
|
||||
ResolveComposedScriptsRecursive(
|
||||
nested, $"{prefix}.{nested.InstanceName}",
|
||||
compositionMap, composedTemplateChains, scripts, scriptCanonicalById, visited);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -688,18 +796,30 @@ public class FlatteningService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves alarm on-trigger script references from script IDs to canonical names.
|
||||
/// This is done by finding the script in the template chain whose ID matches the alarm's OnTriggerScriptId,
|
||||
/// then mapping to the corresponding canonical name in the resolved scripts.
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private static void ResolveAlarmScriptReferences(
|
||||
Dictionary<string, ResolvedAlarm> alarms,
|
||||
Dictionary<string, ResolvedScript> scripts)
|
||||
Dictionary<string, int> alarmScriptIds,
|
||||
Dictionary<int, string> scriptCanonicalById)
|
||||
{
|
||||
// Build a lookup of script names (we only have canonical names at this point)
|
||||
// The alarm's OnTriggerScriptCanonicalName will be set by the caller or validation step
|
||||
// For now, this is a placeholder — the actual resolution depends on how alarm trigger configs
|
||||
// reference scripts (by name within the same scope).
|
||||
// The trigger configuration JSON may contain a "scriptName" field.
|
||||
foreach (var (alarmCanonicalName, scriptId) in alarmScriptIds)
|
||||
{
|
||||
if (!alarms.TryGetValue(alarmCanonicalName, out var alarm))
|
||||
continue;
|
||||
|
||||
scriptCanonicalById.TryGetValue(scriptId, out var scriptCanonicalName);
|
||||
alarms[alarmCanonicalName] = alarm with
|
||||
{
|
||||
OnTriggerScriptCanonicalName = scriptCanonicalName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,15 +51,12 @@ public class TemplateService
|
||||
FolderId = folderId
|
||||
};
|
||||
|
||||
// Check acyclicity (inheritance) — for new templates this is mostly a parent-exists check,
|
||||
// but we validate anyway for consistency
|
||||
if (parentTemplateId.HasValue)
|
||||
{
|
||||
var allTemplates = await _repository.GetAllTemplatesAsync(cancellationToken);
|
||||
// The new template doesn't exist yet, so we simulate by adding it to the list
|
||||
// with a temporary ID. Since it has no children yet, the only cycle would be
|
||||
// if parentTemplateId somehow pointed at itself (already handled above).
|
||||
}
|
||||
// No collision or acyclicity check is needed here: a freshly created
|
||||
// template has no members of its own, the parent (validated above to
|
||||
// exist) was already collision-checked when its members were added,
|
||||
// and a brand-new child cannot be an ancestor of its parent. Naming
|
||||
// collisions are enforced on every member-mutating call (AddAttribute,
|
||||
// AddAlarm, AddScript, AddComposition) and on rename in UpdateTemplate.
|
||||
|
||||
await _repository.AddTemplateAsync(template, cancellationToken);
|
||||
await _auditService.LogAsync(user, "Create", "Template", "0", name, template, cancellationToken);
|
||||
@@ -281,17 +278,19 @@ public class TemplateService
|
||||
if (lockError != null)
|
||||
return Result<TemplateAttribute>.Failure(lockError);
|
||||
|
||||
// Validate fixed-field granularity
|
||||
// Validate fixed-field granularity. DataType and DataSourceReference are
|
||||
// fixed by the defining level for every attribute — locked or not — so
|
||||
// the error is always honoured (a locked attribute is already rejected
|
||||
// earlier inside the helper).
|
||||
var granularityError = LockEnforcer.ValidateAttributeOverride(existing, proposed);
|
||||
if (granularityError != null && existing.IsLocked)
|
||||
if (granularityError != null)
|
||||
return Result<TemplateAttribute>.Failure(granularityError);
|
||||
|
||||
// Apply overridable fields
|
||||
// Apply overridable fields. DataType / DataSourceReference are fixed and
|
||||
// are deliberately not copied from the proposed attribute.
|
||||
existing.Value = proposed.Value;
|
||||
existing.Description = proposed.Description;
|
||||
existing.IsLocked = proposed.IsLocked;
|
||||
existing.DataType = proposed.DataType;
|
||||
existing.DataSourceReference = proposed.DataSourceReference;
|
||||
if (template?.IsDerived == true)
|
||||
existing.IsInherited = proposed.IsInherited;
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user