Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
using Aetheris.Kernel.Firmament.FirmamentV2.Syntax;
using System.Globalization;
using System.Text.RegularExpressions;
using Aetheris.Semantics;
using Aetheris.Surfacing;
using Aetheris.Kernel.Firmament.FirmamentV2;
namespace Aetheris.Kernel.Firmament.Assembly;
/// <summary>Definition-owned publication, evaluated after typed template specialization.</summary>
internal static class AssemblyPublishedPorts
{
// Publish semantic intent before a reusable assembly's local solve. This
// reads authored frames and uses the existing typed specialization/binder;
// it never materializes a body or imports STEP.
internal static IReadOnlyList<SemanticValue> BindAuthored(string identity, string declarations,
string sourceIdentity, List<AssemblyDiagnostic> diagnostics, FirmamentProjectSnapshot? project,
List<AssemblySourceDependencyIr> dependencies, DerivedValueCache<PublishedPortBinding>? cache = null)
{
declarations = FirmamentSourceSpelling.Normalize(declarations);
var file = Regex.Match(identity, "^(?:SectionChainFile|LoftFile)<\\\"(?<path>[^\\\"]+)\\\">$");
if (file.Success)
{
var requested = file.Groups["path"].Value;
string path, source;
if (project is not null)
{
try { path = FirmamentProjectSnapshot.NormalizePath(requested); }
catch (ArgumentException)
{ diagnostics.Add(new("assembly-profile-invalid-resource-path", $"Invalid port source path '{requested}'.")); return []; }
if (!path.EndsWith(".firmament", StringComparison.OrdinalIgnoreCase))
{ diagnostics.Add(new("assembly-profile-resource-kind-invalid", "Port sources must be .firmament documents.")); return []; }
if (!project.TryResolve(path, out source!))
{ diagnostics.Add(new("assembly-profile-unresolved-resource", $"Port source '{path}' was not found in the project snapshot.")); return []; }
}
else
{
path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(sourceIdentity))!, requested));
if (!File.Exists(path))
{ diagnostics.Add(new("assembly-profile-unresolved-resource", $"Port source '{requested}' was not found at '{path}'.")); return []; }
source = File.ReadAllText(path);
}
var resourceDependencies = new List<AssemblySourceDependencyIr>();
var loaded = AssemblyM0Parser.LoadResource(path, project, resourceDependencies, diagnostics);
if (loaded is null) return [];
source = loaded;
dependencies.AddRange(resourceDependencies.Where(d => !dependencies.Any(existing => existing.Path == d.Path))
.Select(d => d with { IsRoot = false }));
if (!dependencies.Any(d => d.Path == path))
dependencies.Add(new(path, Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(source))), false));
// Everything below is a function of the identity, the resource path and its loaded text.
return Memo(cache, "file\0" + identity + "\0" + path + "\0" + Hash(source), diagnostics, diagnostics =>
{
source = FirmamentSourceSpelling.Normalize(source);
if (!Regex.IsMatch(source, @"\bExpose\s*\{\s*Semantic\b")) return [];
var chain = SectionChainAuthoringParser.Compile(Strip(source, diagnostics), materialize: false);
if (!chain.IsSuccess || chain.Chain is null)
{
foreach (var error in chain.Diagnostics) diagnostics.Add(new("assembly-port-section-binding-failed", error));
return [];
}
var owner = Regex.Match(source, @"\bModel\s+(?<name>[A-Za-z_]\w*)\s*\{").Groups["name"].Value;
return Read(source, owner, identity, path, diagnostics, chain.Chain);
});
}
if (!identity.Contains('<')) return [];
// A specialization's ports are a function of the identity, the declarations and the source name.
var wholeKey = "template\0" + identity + "\0" + sourceIdentity + "\0" + DeclarationTextFacts.Of(declarations).Hash;
// BUILD-PERF-M2: like its definition, a specialization binds its ports from its declaration
// closure when that expands on its own, otherwise from the whole catalog. Keys follow the
// text actually bound (closure, or whole catalog after a failed closure), so a reused binding
// is always the one a fresh build would make.
if (AssemblyDefinitionMaterializer.ScopeDeclarations
&& AssemblyDeclarationScope.Closure(declarations, AssemblyDefinitionMaterializer.TemplateName(identity).Trim()) is { } scoped
&& !ReferenceEquals(scoped, declarations))
{
var fallbackKey = "template-scope-fallback\0" + wholeKey;
if (cache is null || !cache.Contains(fallbackKey))
{
var scopedKey = "template-scoped\0" + identity + "\0" + sourceIdentity + "\0" + Hash(scoped);
var attempt = new List<AssemblyDiagnostic>();
var ports = Memo(cache, scopedKey, attempt, diagnostics => BindTemplate(identity, scoped, sourceIdentity, diagnostics));
if (attempt.All(d => d.Severity != AssemblyDiagnosticSeverity.Error))
{
diagnostics.AddRange(attempt);
return ports;
}
cache?.Forget(scopedKey);
}
return Memo(cache, fallbackKey, diagnostics, diagnostics => BindTemplate(identity, declarations, sourceIdentity, diagnostics));
}
return Memo(cache, wholeKey, diagnostics, diagnostics => BindTemplate(identity, declarations, sourceIdentity, diagnostics));
}
private static IReadOnlyList<SemanticValue> BindTemplate(string identity, string declarations, string sourceIdentity,
List<AssemblyDiagnostic> diagnostics)
{
var templateName = identity[..identity.IndexOf('<')].Trim();
var header = Regex.Match(declarations, $@"\b(?:Struct|Model)\s+{Regex.Escape(templateName)}(?:\s*:\s*[A-Za-z_]\w*)?\s*\{{");
if (!header.Success) return [];
var body = AssemblyM0Parser.BalancedBody(declarations, header.Index + header.Length - 1, diagnostics, "port definition");
if (body is null || !Regex.IsMatch(body, @"\bExpose\s*\{\s*Semantic\b")) return [];
var inspectionDiagnostics = new List<string>();
var kind = FirmamentV2TemplateExpansion.Inspect(declarations, inspectionDiagnostics)
.SingleOrDefault(t => t.Name == templateName)?.TargetKind ?? "Struct";
var application = $"{kind} __AssemblyPart = {identity}\n";
var module = kind == "Model" ? declarations + "\n" + application
: "Model __AssemblyDefinition {\n Units: mm\n" + declarations + "\n" + application + "}\n";
var expansion = FirmamentTemplateSourceCompiler.Expand(module, out var errors);
if (expansion is null || errors.Count > 0)
{
foreach (var error in errors) diagnostics.Add(new("assembly-port-specialization-failed", error));
return [];
}
return Read(expansion.ExpandedSource, "__AssemblyPart", identity, sourceIdentity, diagnostics, includeWinding: true);
}
/// <summary>Reuse a binding from an earlier build of the session when its inputs are identical,
/// replaying its diagnostics. Only bindings the definition cache can copy are kept, and every
/// reuse returns copies.</summary>
private static IReadOnlyList<SemanticValue> Memo(DerivedValueCache<PublishedPortBinding>? cache, string key,
List<AssemblyDiagnostic> diagnostics, Func<List<AssemblyDiagnostic>, IReadOnlyList<SemanticValue>> bind)
{
if (cache is null) return bind(diagnostics);
var computed = false;
var binding = cache.GetOrAdd(key, () =>
{
computed = true;
var reported = new List<AssemblyDiagnostic>();
var ports = bind(reported);
return new(ports, reported);
});
diagnostics.AddRange(binding.Diagnostics);
if (!binding.Ports.All(AssemblyDefinitionCache.CanCopy))
{
// Not shareable: keep this build's values and do not offer them to the next build.
if (computed) { cache.Forget(key); return binding.Ports; }
var reported = new List<AssemblyDiagnostic>();
return bind(reported);
}
return binding.Ports.Select(AssemblyDefinitionCache.Copy).ToArray();
}
private static string Hash(string text) => Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(text)));
internal static string Strip(string source, List<AssemblyDiagnostic> diagnostics)
{
source = FirmamentSourceSpelling.Normalize(source);
ValidatePublicationSyntax(FirmamentSyntaxParser.Parse(source).Document, "<part>", diagnostics);
var chars = source.ToCharArray();
foreach (Match expose in Regex.Matches(source,@"\bExpose\s*\{"))
{
var body = AssemblyM0Parser.BalancedBody(source,expose.Index+expose.Length-1,diagnostics,"part Expose");
if (body is not null && Regex.IsMatch(body, @"^\s*Semantic\s+[A-Za-z_]\w*\s*\{"))
Array.Fill(chars,' ',expose.Index,expose.Length+body.Length+1);
}
return new(chars);
}
internal static void ValidatePublicationSyntax(DocumentSyntax syntax, string identity, List<AssemblyDiagnostic> diagnostics)
=> ValidatePublicationSyntax(FirmamentSyntaxTraversal.Declarations(syntax), identity, diagnostics);
internal static void ValidatePublicationSyntax(DeclarationSyntax syntax, string identity, List<AssemblyDiagnostic> diagnostics)
=> ValidatePublicationSyntax(FirmamentSyntaxTraversal.Declarations(syntax), identity, diagnostics);
private static void ValidatePublicationSyntax(IEnumerable<DeclarationSyntax> declarations, string identity, List<AssemblyDiagnostic> diagnostics)
{
foreach (var declaration in declarations)
{
if (declaration.Type.Name.Text == "Semantic")
foreach (var statement in declaration.Body?.Statements ?? [])
if (statement is not DeclarationSyntax { Type.Name.Text: "Axis" or "Plane" or "Point" or "DatumFrame" or "Dimension" })
diagnostics.Add(AssemblyInterfaceSyntax.Diagnostic(statement is DeclarationSyntax ? "assembly-semantic-unresolved-member" : "assembly-semantic-field-unknown",
$"Unrecognized member in Semantic '{declaration.Name?.Text}'; publish a typed Axis, Plane, Point, DatumFrame or Dimension.", identity, statement.Span));
if (declaration.Type.Name.Text == "Expose" && declaration.Body?.Statements.OfType<DeclarationSyntax>().Any(d => d.Type.Name.Text == "Semantic" && d.Body is not null) == true)
foreach (var statement in declaration.Body.Statements)
if (statement is not DeclarationSyntax { Type.Name.Text: "Semantic" })
diagnostics.Add(AssemblyInterfaceSyntax.Diagnostic("assembly-port-publication-unknown",
"This expose block publishes named Semantic blocks; unrecognized fields or statements cannot be discarded.", identity, statement.Span));
}
}
internal static IReadOnlyList<SemanticValue> Read(string source,string owner,string identity,
string sourceIdentity,List<AssemblyDiagnostic> diagnostics,SectionChain? chain = null, bool includeWinding = false)
{
source = FirmamentSourceSpelling.Normalize(source);
ValidatePublicationSyntax(FirmamentSyntaxParser.Parse(source).Document, sourceIdentity, diagnostics);
if (diagnostics.Count > 0) return [];
var valueErrors = new List<string>();
var valuesForPorts = new FirmamentCompileTimeValues(source);
_ = FirmamentV2TemplateExpansion.ReadDataCatalog(source, valueErrors, valuesForPorts, initialize: true);
if (valueErrors.Count > 0)
{ diagnostics.AddRange(valueErrors.Select(e => new AssemblyDiagnostic("assembly-port-value-invalid", e))); return []; }
source = FirmamentValueSubstitution.Apply(source, valuesForPorts.MaterialInputs(source)
.ToDictionary(v => v.Name, v => valuesForPorts.NominalExpression(v.Name), StringComparer.Ordinal));
var header = Regex.Match(source,$@"\b(?:Struct|Model)\s+{Regex.Escape(owner)}\s*\{{");
if (!header.Success) return [];
var body = AssemblyM0Parser.BalancedBody(source,header.Index+header.Length-1,diagnostics,"published part");
if (body is null) return [];
var result = new List<SemanticValue>();
// Winding ports are typed WireForm facts needed by a reusable unit's local
// solve, before M1 builds any bodies. Use the same AIR and datum factory
// as the post-build report; no authored copy of a coil axis is required.
if (includeWinding && Materializer.WireFormAuthoring.IsWireFormSource(source))
{
var stripped = Strip(source, diagnostics);
var values = new FirmamentCompileTimeValues(stripped);
var errors = new List<string>();
var expanded = FirmamentV2TemplateExpansion.Expand(stripped, errors, values);
if (expanded is null || errors.Count > 0)
diagnostics.AddRange(errors.Select(e => new AssemblyDiagnostic("assembly-port-specialization-failed", e)));
else if (Materializer.WireFormAuthoring.IsWireFormSource(expanded.Source))
{
var dataErrors = new List<string>();
var concrete = CanonicalStaticAuthoring.Expand(expanded.Source, dataErrors, values);
if (concrete is null || dataErrors.Count > 0)
{ diagnostics.AddRange(dataErrors.Select(e => new AssemblyDiagnostic("assembly-port-specialization-failed", e))); return []; }
var nominal = FirmamentValueSubstitution.Apply(concrete.Source, values.MaterialInputs(concrete.Source)
.ToDictionary(v => v.Name, v => values.NominalExpression(v.Name), StringComparer.Ordinal));
var wire = Materializer.WireFormAuthoring.Parse(nominal);
if (!wire.IsSuccess || wire.Value is null)
diagnostics.AddRange(wire.Diagnostics.Select(d => new AssemblyDiagnostic("assembly-port-wireform-invalid", d.Message)));
else result.AddRange(AssemblyDefinitionMaterializer.WindingSemantics(wire.Value,
[new("WireForm-AIR", identity, "definition-owned winding datum", SemanticSourceSpan.Generated(sourceIdentity)),
new("definition-port-publication", identity, "WireForm winding", SemanticSourceSpan.Generated(sourceIdentity))], sourceIdentity));
}
}
foreach (Match expose in Regex.Matches(body,@"\bExpose\s*\{"))
{
var ports = AssemblyM0Parser.BalancedBody(body,expose.Index+expose.Length-1,diagnostics,"part Expose");
if (ports is null) continue;
if (!Regex.IsMatch(ports, @"^\s*Semantic\s+[A-Za-z_]\w*\s*\{")) continue;
ports = Regex.Replace(ports,@"\bDatumFrame\s+(?<name>[A-Za-z_]\w*)\s*=\s*(?<chain>[A-Za-z_]\w*)\.Section\.(?<section>[A-Za-z_]\w*)\.Frame\s*;",m =>
{
var section = chain?.Sections.SingleOrDefault(s => s.SectionId == m.Groups["section"].Value);
if (chain is null || chain.StableId != m.Groups["chain"].Value || section is null)
{ diagnostics.Add(new("assembly-port-section-unresolved",$"Published port references missing authored section '{m.Groups["section"].Value}'.")); return m.Value; }
var f=section.Frame;
string V(double x,double y,double z) => FormattableString.Invariant($"[{x:R},{y:R},{z:R}]");
return $"DatumFrame {m.Groups["name"].Value} = {V(f.Origin.X,f.Origin.Y,f.Origin.Z)} x {V(f.XAxis.X,f.XAxis.Y,f.XAxis.Z)} y {V(f.YAxis.X,f.YAxis.Y,f.YAxis.Z)} z {V(f.Normal.X,f.Normal.Y,f.Normal.Z)};";
});
// Definition-owned origins use the same dimension-checked scalar
// evaluator as authored placement. Template dimensions may derive
// mounting stations; bare legacy numbers still inherit millimetres.
ports = Regex.Replace(ports, @"\bDatumFrame\s+(?<name>[A-Za-z_]\w*)\s*=\s*\[(?<origin>[^]]+)\]", match =>
{
var components = match.Groups["origin"].Value.Split(',');
var values = new double[3];
if (components.Length != 3 || components.Where((component, index) =>
!FirmamentNumericEvaluation.TryEvaluateScalar(component.Trim(), out values[index], out var unit)
|| (unit != "mm" && !(unit.Length == 0 && double.TryParse(component.Trim(), NumberStyles.Float,
CultureInfo.InvariantCulture, out _))) || !double.IsFinite(values[index])).Any())
{
diagnostics.Add(new("assembly-port-frame-origin-invalid", $"Published DatumFrame '{match.Groups["name"].Value}' requires three finite length components."));
return match.Value;
}
return $"DatumFrame {match.Groups["name"].Value} = [{string.Join(",", values.Select(value => value.ToString("R", CultureInfo.InvariantCulture)))}]";
});
// Remaining literal vectors retain the definition's millimetre contract.
ports = Regex.Replace(ports,@"\[[^]]+\]",m => Regex.Replace(m.Value,@"(?<number>[-+0-9.eE]+)mm\b", "${number}"));
var values = AssemblyM0Parser.ParseSemantics(ports,identity,sourceIdentity,diagnostics).ToArray();
foreach (var value in values)
{
if (value.ExposedMembers.Count == 0)
diagnostics.Add(new("assembly-port-empty-or-unresolved",$"Published Semantic '{value.ExposedName}' has no resolved supported members."));
result.Add(new SemanticValue(value.StableIdentity, value.Type, value.Capabilities.Values, value.Bindings,
value.ExposedMembers.Values, [.. value.Provenance, new("definition-port-publication", identity, value.ExposedName ?? "", SemanticSourceSpan.Generated(sourceIdentity))],
value.AuthoredSourceSpan, value.GeneratedSourceSpan, value.ExposedName));
}
if (!Regex.IsMatch(ports,@"\bSemantic\s+[A-Za-z_]\w*\s*\{"))
diagnostics.Add(new("assembly-port-invalid-publication","Part Expose requires named Semantic blocks."));
}
foreach (var group in result.GroupBy(p => p.ExposedName).Where(g => g.Count()>1))
diagnostics.Add(new("assembly-port-duplicate-name",$"Published Semantic '{group.Key}' is duplicated on '{identity}'."));
return result;
}
}