Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Implement Firmament M2b validation target shape classification
Yuechen Li15af3f88a462
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.csindex ab7fc869..8415ecb1 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs@@ -37,7 +37,7 @@ public sealed class FirmamentScaffoldTestsAssert.True(result.Compilation.IsSuccess);var artifact = result.Compilation.Value;- Assert.Equal("firmament-ops-document-coherence-validated", artifact.ArtifactKind);+ Assert.Equal("firmament-validation-target-shape-classified", artifact.ArtifactKind);Assert.NotNull(artifact.ParsedDocument);Assert.Equal("1", artifact.ParsedDocument!.Firmament.Version);Assert.Equal("demo", artifact.ParsedDocument.Model.Name);diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetShapeClassificationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetShapeClassificationTests.csnew file mode 100644index 00000000..afce9611--- /dev/null+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentValidationTargetShapeClassificationTests.cs@@ -0,0 +1,104 @@+using Aetheris.Kernel.Firmament.Diagnostics;++namespace Aetheris.Kernel.Firmament.Tests;++public sealed class FirmamentValidationTargetShapeClassificationTests+{+ [Theory]+ [InlineData("expect_exists", "base", "FeatureId")]+ [InlineData("expect_exists", "base.top_face", "SelectorShaped")]+ [InlineData("expect_selectable", "mount_hole", "FeatureId")]+ [InlineData("expect_selectable", "mount_hole.entry_face", "SelectorShaped")]+ public void Compiler_Classifies_ValidationTargets_BySurfaceShape(string opName, string target, string expectedShape)+ {+ var source = $$"""+ firmament:+ version: 1++ model:+ name: demo+ units: mm++ ops[1]:+ -+ op: {{opName}}+ target: {{target}}+ {{(opName == "expect_selectable" ? "count: 1" : string.Empty)}}+ """;++ var result = Compile(source);++ Assert.True(result.Compilation.IsSuccess);+ var op = Assert.Single(result.Compilation.Value.ParsedDocument!.Ops.Entries);+ Assert.NotNull(op.ClassifiedFields);+ Assert.Equal(expectedShape, op.ClassifiedFields!["targetShape"]);+ }++ [Theory]+ [InlineData("expect_exists", ".top_face")]+ [InlineData("expect_exists", "base.")]+ [InlineData("expect_exists", "base.top.face")]+ [InlineData("expect_selectable", "base top_face")]+ [InlineData("expect_selectable", "1base")]+ public void Compiler_Rejects_MalformedValidationTargetShape_Deterministically(string opName, string target)+ {+ var source = $$"""+ firmament:+ version: 1++ model:+ name: demo+ units: mm++ ops[1]:+ -+ op: {{opName}}+ target: {{target}}+ {{(opName == "expect_selectable" ? "count: 1" : string.Empty)}}+ """;++ var first = Compile(source);+ var second = Compile(source);++ Assert.False(first.Compilation.IsSuccess);+ Assert.False(second.Compilation.IsSuccess);++ var firstDiagnostic = Assert.Single(first.Compilation.Diagnostics);+ var secondDiagnostic = Assert.Single(second.Compilation.Diagnostics);++ Assert.Equal(firstDiagnostic.Message, secondDiagnostic.Message);+ Assert.Contains($"[{FirmamentDiagnosticCodes.ValidationInvalidTargetShape.Value}]", firstDiagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Compiler_DoesNotResolve_ValidationTargets_Semantically()+ {+ const string source = """+ firmament:+ version: 1++ model:+ name: demo+ units: mm++ ops[2]:+ -+ op: expect_exists+ target: future_feature.top_face+ -+ op: expect_selectable+ target: ghost_feature+ count: 1+ """;++ var result = Compile(source);++ Assert.True(result.Compilation.IsSuccess);+ }++ private static FirmamentCompileResult Compile(string source)+ {+ var compiler = new FirmamentCompiler();+ return compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));+ }+}diff --git a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.csindex 70553999..7f4af366 100644--- a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs+++ b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs@@ -21,6 +21,7 @@ public static class FirmamentDiagnosticCodespublic static readonly FirmamentDiagnosticCode ValidationMissingRequiredField = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0014");public static readonly FirmamentDiagnosticCode ValidationInvalidFieldTypeOrShape = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0015");public static readonly FirmamentDiagnosticCode ValidationInvalidFieldValue = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0016");+ public static readonly FirmamentDiagnosticCode ValidationInvalidTargetShape = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0017");public static readonly FirmamentDiagnosticCode ReferenceDuplicateFeatureId = new($"{FirmamentDiagnosticConventions.ReferencePrefix}-0001");public static readonly FirmamentDiagnosticCode ReferenceUnknownFeatureId = new($"{FirmamentDiagnosticConventions.ReferencePrefix}-0002");diff --git a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.csindex 77de5a32..c845a930 100644--- a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs+++ b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs@@ -3,5 +3,5 @@ using Aetheris.Kernel.Firmament.ParsedModel;namespace Aetheris.Kernel.Firmament;public sealed record FirmamentCompilationArtifact(- string ArtifactKind = "firmament-ops-document-coherence-validated",+ string ArtifactKind = "firmament-validation-target-shape-classified",FirmamentParsedDocument? ParsedDocument = null);diff --git a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs b/Aetheris.Kernel.Firmament/FirmamentCompiler.csindex 38458c60..c3bd7e5c 100644--- a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs+++ b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs@@ -45,10 +45,17 @@ public sealed class FirmamentCompilerKernelResult<FirmamentCompilationArtifact>.Failure(documentCoherenceValidationResult.Diagnostics));}+ var targetShapeValidationResult = FirmamentValidationTargetShapeValidator.Validate(parseResult.Value);+ if (!targetShapeValidationResult.IsSuccess)+ {+ return new FirmamentCompileResult(+ KernelResult<FirmamentCompilationArtifact>.Failure(targetShapeValidationResult.Diagnostics));+ }+return new FirmamentCompileResult(KernelResult<FirmamentCompilationArtifact>.Success(new FirmamentCompilationArtifact(- ArtifactKind: "firmament-ops-document-coherence-validated",- ParsedDocument: parseResult.Value)));+ ArtifactKind: "firmament-validation-target-shape-classified",+ ParsedDocument: targetShapeValidationResult.Value)));}}diff --git a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.csindex 87055c48..cf1863c0 100644--- a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs+++ b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs@@ -17,4 +17,5 @@ public sealed record FirmamentParsedOpEntry(string OpName,FirmamentKnownOpKind KnownKind,FirmamentOpFamily Family,- IReadOnlyDictionary<string, string> RawFields);+ IReadOnlyDictionary<string, string> RawFields,+ IReadOnlyDictionary<string, string>? ClassifiedFields = null);diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetClassifier.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetClassifier.csnew file mode 100644index 00000000..3b308e1a--- /dev/null+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetClassifier.cs@@ -0,0 +1,92 @@+namespace Aetheris.Kernel.Firmament.Validation;++internal static class FirmamentValidationTargetClassifier+{+ public static bool TryClassify(string rawTarget, out FirmamentValidationTargetShape targetShape)+ {+ targetShape = default;++ if (string.IsNullOrWhiteSpace(rawTarget))+ {+ return false;+ }++ var trimmed = rawTarget.Trim();+ if (trimmed.Contains(' ', StringComparison.Ordinal)+ || trimmed.Contains('\t', StringComparison.Ordinal)+ || trimmed.Contains('\n', StringComparison.Ordinal)+ || trimmed.Contains('\r', StringComparison.Ordinal))+ {+ return false;+ }++ var dotCount = 0;+ foreach (var character in trimmed)+ {+ if (character == '.')+ {+ dotCount++;+ }+ }++ if (dotCount == 0)+ {+ if (!IsValidIdentifier(trimmed))+ {+ return false;+ }++ targetShape = FirmamentValidationTargetShape.FeatureId;+ return true;+ }++ if (dotCount != 1)+ {+ return false;+ }++ var separatorIndex = trimmed.IndexOf('.', StringComparison.Ordinal);+ var left = trimmed[..separatorIndex];+ var right = trimmed[(separatorIndex + 1)..];++ if (!IsValidIdentifier(left) || !IsValidIdentifier(right))+ {+ return false;+ }++ targetShape = FirmamentValidationTargetShape.SelectorShaped;+ return true;+ }++ private static bool IsValidIdentifier(string value)+ {+ if (value.Length == 0)+ {+ return false;+ }++ if (!IsIdentifierStart(value[0]))+ {+ return false;+ }++ for (var index = 1; index < value.Length; index++)+ {+ if (!IsIdentifierContinue(value[index]))+ {+ return false;+ }+ }++ return true;+ }++ private static bool IsIdentifierStart(char character) =>+ (character is >= 'a' and <= 'z')+ || (character is >= 'A' and <= 'Z')+ || character == '_';++ private static bool IsIdentifierContinue(char character) =>+ IsIdentifierStart(character)+ || (character is >= '0' and <= '9');+}diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShape.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShape.csnew file mode 100644index 00000000..552e7d73--- /dev/null+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShape.cs@@ -0,0 +1,7 @@+namespace Aetheris.Kernel.Firmament.Validation;++internal enum FirmamentValidationTargetShape+{+ FeatureId,+ SelectorShaped+}diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShapeValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShapeValidator.csnew file mode 100644index 00000000..eb14f500--- /dev/null+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentValidationTargetShapeValidator.cs@@ -0,0 +1,57 @@+using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Results;+using Aetheris.Kernel.Firmament.Diagnostics;+using Aetheris.Kernel.Firmament.ParsedModel;++namespace Aetheris.Kernel.Firmament.Validation;++internal static class FirmamentValidationTargetShapeValidator+{+ public static KernelResult<FirmamentParsedDocument> Validate(FirmamentParsedDocument parsedDocument)+ {+ ArgumentNullException.ThrowIfNull(parsedDocument);++ var updatedEntries = new List<FirmamentParsedOpEntry>(parsedDocument.Ops.Entries.Count);++ for (var index = 0; index < parsedDocument.Ops.Entries.Count; index++)+ {+ var entry = parsedDocument.Ops.Entries[index];+ if (entry.KnownKind is not (FirmamentKnownOpKind.ExpectExists or FirmamentKnownOpKind.ExpectSelectable))+ {+ updatedEntries.Add(entry);+ continue;+ }++ var targetRaw = entry.RawFields.TryGetValue("target", out var rawTarget) ? rawTarget : string.Empty;+ if (!FirmamentValidationTargetClassifier.TryClassify(targetRaw, out var targetShape))+ {+ return KernelResult<FirmamentParsedDocument>.Failure([+ CreateDiagnostic(+ FirmamentDiagnosticCodes.ValidationInvalidTargetShape,+ $"Validation op '{entry.OpName}' at index {index} has malformed target '{targetRaw}'. Expected 'feature_id' or 'feature_id.port_name'.")+ ]);+ }++ var classifiedFields = entry.ClassifiedFields is null+ ? new Dictionary<string, string>(StringComparer.Ordinal)+ : new Dictionary<string, string>(entry.ClassifiedFields, StringComparer.Ordinal);++ classifiedFields["targetShape"] = targetShape.ToString();+ updatedEntries.Add(entry with { ClassifiedFields = classifiedFields });+ }++ var updatedDocument = parsedDocument with+ {+ Ops = parsedDocument.Ops with { Entries = updatedEntries }+ };++ return KernelResult<FirmamentParsedDocument>.Success(updatedDocument);+ }++ private static KernelDiagnostic CreateDiagnostic(FirmamentDiagnosticCode code, string message) =>+ new(+ KernelDiagnosticCode.ValidationFailed,+ KernelDiagnosticSeverity.Error,+ $"[{code.Value}] {message}",+ FirmamentDiagnosticConventions.Source);+}diff --git a/testdata/firmament/fixtures/m2b-invalid-expect-exists-empty-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-exists-empty-target.firmamentnew file mode 100644index 00000000..84b7d6f1--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-exists-empty-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target:diff --git a/testdata/firmament/fixtures/m2b-invalid-expect-exists-leading-dot-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-exists-leading-dot-target.firmamentnew file mode 100644index 00000000..5c8cdbbc--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-exists-leading-dot-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target: .top_facediff --git a/testdata/firmament/fixtures/m2b-invalid-expect-exists-multi-dot-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-exists-multi-dot-target.firmamentnew file mode 100644index 00000000..1fbdcbfa--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-exists-multi-dot-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target: base.top.facediff --git a/testdata/firmament/fixtures/m2b-invalid-expect-exists-trailing-dot-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-exists-trailing-dot-target.firmamentnew file mode 100644index 00000000..72ffeabc--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-exists-trailing-dot-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target: base.diff --git a/testdata/firmament/fixtures/m2b-invalid-expect-selectable-invalid-id-start-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-selectable-invalid-id-start-target.firmamentnew file mode 100644index 00000000..5d172f07--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-selectable-invalid-id-start-target.firmament@@ -0,0 +1,12 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_selectable+ target: 1base+ count: 1diff --git a/testdata/firmament/fixtures/m2b-invalid-expect-selectable-whitespace-target.firmament b/testdata/firmament/fixtures/m2b-invalid-expect-selectable-whitespace-target.firmamentnew file mode 100644index 00000000..80ff5110--- /dev/null+++ b/testdata/firmament/fixtures/m2b-invalid-expect-selectable-whitespace-target.firmament@@ -0,0 +1,12 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_selectable+ target: base top_face+ count: 1diff --git a/testdata/firmament/fixtures/m2b-valid-expect-exists-bare-target.firmament b/testdata/firmament/fixtures/m2b-valid-expect-exists-bare-target.firmamentnew file mode 100644index 00000000..1adb3be4--- /dev/null+++ b/testdata/firmament/fixtures/m2b-valid-expect-exists-bare-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target: basediff --git a/testdata/firmament/fixtures/m2b-valid-expect-exists-selector-target.firmament b/testdata/firmament/fixtures/m2b-valid-expect-exists-selector-target.firmamentnew file mode 100644index 00000000..97f19112--- /dev/null+++ b/testdata/firmament/fixtures/m2b-valid-expect-exists-selector-target.firmament@@ -0,0 +1,11 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_exists+ target: base.top_facediff --git a/testdata/firmament/fixtures/m2b-valid-expect-selectable-bare-target.firmament b/testdata/firmament/fixtures/m2b-valid-expect-selectable-bare-target.firmamentnew file mode 100644index 00000000..0718cf90--- /dev/null+++ b/testdata/firmament/fixtures/m2b-valid-expect-selectable-bare-target.firmament@@ -0,0 +1,12 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_selectable+ target: mount_hole+ count: 1diff --git a/testdata/firmament/fixtures/m2b-valid-expect-selectable-selector-target.firmament b/testdata/firmament/fixtures/m2b-valid-expect-selectable-selector-target.firmamentnew file mode 100644index 00000000..29d4a5d1--- /dev/null+++ b/testdata/firmament/fixtures/m2b-valid-expect-selectable-selector-target.firmament@@ -0,0 +1,12 @@+firmament:+ version: 1++model:+ name: demo+ units: mm++ops[1]:+ -+ op: expect_selectable+ target: mount_hole.entry_face+ count: 2diff --git a/testdata/firmament/manifests/m0c.corpus.json b/testdata/firmament/manifests/m0c.corpus.jsonindex 4a4f8fb5..67c95d91 100644--- a/testdata/firmament/manifests/m0c.corpus.json+++ b/testdata/firmament/manifests/m0c.corpus.json@@ -536,6 +536,96 @@"name": "Valid M2a two primitives with distinct ids","fixturePath": "testdata/firmament/fixtures/m2a-valid-two-primitives-distinct-ids.firmament","expectedOutcome": "success"+ },+ {+ "id": "m2b-valid-expect-exists-bare-target",+ "name": "Valid M2b expect_exists bare feature target",+ "fixturePath": "testdata/firmament/fixtures/m2b-valid-expect-exists-bare-target.firmament",+ "expectedOutcome": "success"+ },+ {+ "id": "m2b-valid-expect-exists-selector-target",+ "name": "Valid M2b expect_exists selector-shaped target",+ "fixturePath": "testdata/firmament/fixtures/m2b-valid-expect-exists-selector-target.firmament",+ "expectedOutcome": "success"+ },+ {+ "id": "m2b-valid-expect-selectable-bare-target",+ "name": "Valid M2b expect_selectable bare feature target",+ "fixturePath": "testdata/firmament/fixtures/m2b-valid-expect-selectable-bare-target.firmament",+ "expectedOutcome": "success"+ },+ {+ "id": "m2b-valid-expect-selectable-selector-target",+ "name": "Valid M2b expect_selectable selector-shaped target",+ "fixturePath": "testdata/firmament/fixtures/m2b-valid-expect-selectable-selector-target.firmament",+ "expectedOutcome": "success"+ },+ {+ "id": "m2b-invalid-expect-exists-empty-target",+ "name": "Invalid M2b expect_exists empty target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-exists-empty-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0014"+ }+ },+ {+ "id": "m2b-invalid-expect-exists-leading-dot-target",+ "name": "Invalid M2b expect_exists leading-dot target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-exists-leading-dot-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0017"+ }+ },+ {+ "id": "m2b-invalid-expect-exists-trailing-dot-target",+ "name": "Invalid M2b expect_exists trailing-dot target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-exists-trailing-dot-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0017"+ }+ },+ {+ "id": "m2b-invalid-expect-exists-multi-dot-target",+ "name": "Invalid M2b expect_exists multi-dot target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-exists-multi-dot-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0017"+ }+ },+ {+ "id": "m2b-invalid-expect-selectable-whitespace-target",+ "name": "Invalid M2b expect_selectable whitespace target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-selectable-whitespace-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0017"+ }+ },+ {+ "id": "m2b-invalid-expect-selectable-invalid-id-start-target",+ "name": "Invalid M2b expect_selectable invalid identifier start target",+ "fixturePath": "testdata/firmament/fixtures/m2b-invalid-expect-selectable-invalid-id-start-target.firmament",+ "expectedOutcome": "failure",+ "expectedDiagnostic": {+ "source": "firmament",+ "kernelCode": "ValidationFailed",+ "firmamentCode": "FIRM-STRUCT-0017"+ }}]}