Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Implement M7b placement selector root and contract validation
Yuechen Li3e6eed30959f
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPlacementValidationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPlacementValidationTests.csindex 7075e35d..7736b33f 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPlacementValidationTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPlacementValidationTests.cs@@ -25,6 +25,76 @@ public sealed class FirmamentPlacementValidationTestsAssert.True(result.Compilation.IsSuccess);}+ [Fact]+ public void Primitive_With_SelectorShaped_PlaceAnchor_Uses_Selector_Contracts()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-valid-placement-selector-anchors.firmament");+ Assert.True(result.Compilation.IsSuccess);+ }++ [Fact]+ public void Primitive_With_Sphere_Surface_PlaceAnchor_Is_Valid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-sphere.firmament");+ Assert.True(result.Compilation.IsSuccess);+ }++ [Fact]+ public void Primitive_With_Boolean_Edges_PlaceAnchor_Is_Valid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-boolean.firmament");+ Assert.True(result.Compilation.IsSuccess);+ }++ [Fact]+ public void Primitive_With_SelectorShaped_PlaceAnchor_Unknown_Root_Is_Invalid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-invalid-placement-selector-root-missing.firmament");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.ValidationTargetUnknownSelectorRootFeatureId.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'place.on'", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Primitive_With_SelectorShaped_PlaceAnchor_Forward_Root_Is_Invalid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-invalid-placement-selector-root-forward.firmament");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.ValidationTargetUnknownSelectorRootFeatureId.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'future'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'place.on'", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Primitive_With_SelectorShaped_PlaceAnchor_Illegal_Box_Port_Is_Invalid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-invalid-placement-selector-port-box.firmament");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.ValidationTargetSelectorPortNotAllowedForFeatureKind.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'circular_edges'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'box'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'place.on'", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Primitive_With_SelectorShaped_PlaceAnchor_Illegal_Sphere_Port_Is_Invalid()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m7b-invalid-placement-selector-port-sphere.firmament");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.ValidationTargetSelectorPortNotAllowedForFeatureKind.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'top_face'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'sphere'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("'place.on'", diagnostic.Message, StringComparison.Ordinal);+ }+[Fact]public void Primitive_With_Place_Missing_On_Is_Invalid(){diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentDocumentCoherenceValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentDocumentCoherenceValidator.csindex 648f49aa..174fce4a 100644--- a/Aetheris.Kernel.Firmament/Validation/FirmamentDocumentCoherenceValidator.cs+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentDocumentCoherenceValidator.cs@@ -31,6 +31,12 @@ internal static class FirmamentDocumentCoherenceValidatorcontinue;}+ ValidatePlacementSelectorReference(entry, index, featureIds, featureKindsById, out var placementDiagnostic);+ if (placementDiagnostic is not null)+ {+ return KernelResult<FirmamentParsedDocument>.Failure([placementDiagnostic]);+ }+updatedEntries.Add(entry);var featureId = entry.RawFields["id"];@@ -106,55 +112,112 @@ internal static class FirmamentDocumentCoherenceValidatorreturn;}- var rootFeatureId = ExtractSelectorRoot(targetRaw);- if (!featureIds.Contains(rootFeatureId))+ if (!TryValidateSelectorReference(+ targetRaw,+ featureIds,+ featureKindsById,+ $"Validation op '{entry.OpName}' at index {index}",+ "target",+ out var rootFeatureId,+ out var portToken,+ out var rootFeatureKind,+ out diagnostic)){- diagnostic = CreateDiagnostic(- FirmamentDiagnosticCodes.ValidationTargetUnknownSelectorRootFeatureId,- $"Validation op '{entry.OpName}' at index {index} references unknown selector root feature id '{rootFeatureId}' via field 'target'.");return;}- var portToken = ExtractSelectorPort(targetRaw);- if (!FirmamentValidationTargetClassifier.IsValidIdentifier(portToken))+ if (!FirmamentSelectorContracts.TryGetPortContract(rootFeatureKind, portToken, out var contract)){- diagnostic = CreateDiagnostic(- FirmamentDiagnosticCodes.ValidationTargetInvalidSelectorPortToken,- $"Validation op '{entry.OpName}' at index {index} has invalid selector port token '{portToken}' via field 'target'.");return;}- if (!featureKindsById.TryGetValue(rootFeatureId, out var rootFeatureKind))+ var classifiedFields = entry.ClassifiedFields is null+ ? new Dictionary<string, string>(StringComparer.Ordinal)+ : new Dictionary<string, string>(entry.ClassifiedFields, StringComparer.Ordinal);++ classifiedFields["selectorResultKind"] = contract.ResultKind.ToString();+ classifiedFields["selectorCardinality"] = contract.Cardinality.ToString();++ enrichedEntry = entry with { ClassifiedFields = classifiedFields };+ }++ private static void ValidatePlacementSelectorReference(+ FirmamentParsedOpEntry entry,+ int index,+ HashSet<string> featureIds,+ IReadOnlyDictionary<string, FirmamentKnownOpKind> featureKindsById,+ out KernelDiagnostic? diagnostic)+ {+ diagnostic = null;++ if (entry.Placement?.On is not FirmamentParsedPlacementSelectorAnchor selectorAnchor){return;}- if (!FirmamentSelectorContracts.TryGetAllowedPorts(rootFeatureKind, out var allowedPorts))+ TryValidateSelectorReference(+ selectorAnchor.Selector,+ featureIds,+ featureKindsById,+ $"Primitive op '{entry.OpName}' at index {index}",+ "place.on",+ out _,+ out _,+ out _,+ out diagnostic);+ }++ private static bool TryValidateSelectorReference(+ string selector,+ HashSet<string> featureIds,+ IReadOnlyDictionary<string, FirmamentKnownOpKind> featureKindsById,+ string sourcePrefix,+ string fieldName,+ out string rootFeatureId,+ out string portToken,+ out FirmamentKnownOpKind rootFeatureKind,+ out KernelDiagnostic? diagnostic)+ {+ rootFeatureId = ExtractSelectorRoot(selector);+ portToken = ExtractSelectorPort(selector);+ rootFeatureKind = default;+ diagnostic = null;++ if (!featureIds.Contains(rootFeatureId)){- return;+ diagnostic = CreateDiagnostic(+ FirmamentDiagnosticCodes.ValidationTargetUnknownSelectorRootFeatureId,+ $"{sourcePrefix} references unknown selector root feature id '{rootFeatureId}' via field '{fieldName}'.");+ return false;}- if (!allowedPorts.Contains(portToken))+ if (!FirmamentValidationTargetClassifier.IsValidIdentifier(portToken)){diagnostic = CreateDiagnostic(- FirmamentDiagnosticCodes.ValidationTargetSelectorPortNotAllowedForFeatureKind,- $"Validation op '{entry.OpName}' at index {index} has selector port '{portToken}' not allowed for feature kind '{rootFeatureKind.ToString().ToLowerInvariant()}' on feature id '{rootFeatureId}' via field 'target'.");- return;+ FirmamentDiagnosticCodes.ValidationTargetInvalidSelectorPortToken,+ $"{sourcePrefix} has invalid selector port token '{portToken}' via field '{fieldName}'.");+ return false;}- if (!FirmamentSelectorContracts.TryGetPortContract(rootFeatureKind, portToken, out var contract))+ if (!featureKindsById.TryGetValue(rootFeatureId, out rootFeatureKind)){- return;+ return true;}- var classifiedFields = entry.ClassifiedFields is null- ? new Dictionary<string, string>(StringComparer.Ordinal)- : new Dictionary<string, string>(entry.ClassifiedFields, StringComparer.Ordinal);+ if (!FirmamentSelectorContracts.TryGetAllowedPorts(rootFeatureKind, out var allowedPorts))+ {+ return true;+ }- classifiedFields["selectorResultKind"] = contract.ResultKind.ToString();- classifiedFields["selectorCardinality"] = contract.Cardinality.ToString();+ if (allowedPorts.Contains(portToken))+ {+ return true;+ }- enrichedEntry = entry with { ClassifiedFields = classifiedFields };+ diagnostic = CreateDiagnostic(+ FirmamentDiagnosticCodes.ValidationTargetSelectorPortNotAllowedForFeatureKind,+ $"{sourcePrefix} has selector port '{portToken}' not allowed for feature kind '{rootFeatureKind.ToString().ToLowerInvariant()}' on feature id '{rootFeatureId}' via field '{fieldName}'.");+ return false;}private static string ExtractSelectorRoot(string target)diff --git a/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-box.firmament b/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-box.firmamentnew file mode 100644index 00000000..f446cbb0--- /dev/null+++ b/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-box.firmament@@ -0,0 +1,29 @@+firmament:+ version: 1++model:+ name: m7b_invalid_placement_selector_port_box+ units: mm++ops[2]:+ -+ op: box+ id: anchor+ size[3]:+ 2+ 2+ 2++ -+ op: box+ id: base+ size[3]:+ 1+ 1+ 1+ place:+ on: anchor.circular_edges+ offset[3]:+ 0+ 0+ 0diff --git a/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-sphere.firmament b/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-sphere.firmamentnew file mode 100644index 00000000..b0982d9e--- /dev/null+++ b/testdata/firmament/fixtures/m7b-invalid-placement-selector-port-sphere.firmament@@ -0,0 +1,26 @@+firmament:+ version: 1++model:+ name: m7b_invalid_placement_selector_port_sphere+ units: mm++ops[2]:+ -+ op: sphere+ id: s+ radius: 2++ -+ op: box+ id: base+ size[3]:+ 1+ 1+ 1+ place:+ on: s.top_face+ offset[3]:+ 0+ 0+ 0diff --git a/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-forward.firmament b/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-forward.firmamentnew file mode 100644index 00000000..37bc3181--- /dev/null+++ b/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-forward.firmament@@ -0,0 +1,29 @@+firmament:+ version: 1++model:+ name: m7b_invalid_placement_selector_root_forward+ units: mm++ops[2]:+ -+ op: box+ id: late_anchor+ size[3]:+ 2+ 2+ 2+ place:+ on: future.top_face+ offset[3]:+ 0+ 0+ 0++ -+ op: box+ id: future+ size[3]:+ 3+ 3+ 3diff --git a/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-missing.firmament b/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-missing.firmamentnew file mode 100644index 00000000..562097ee--- /dev/null+++ b/testdata/firmament/fixtures/m7b-invalid-placement-selector-root-missing.firmament@@ -0,0 +1,21 @@+firmament:+ version: 1++model:+ name: m7b_invalid_placement_selector_root_missing+ units: mm++ops[1]:+ -+ op: box+ id: base+ size[3]:+ 2+ 2+ 2+ place:+ on: missing.top_face+ offset[3]:+ 0+ 0+ 0diff --git a/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-boolean.firmament b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-boolean.firmamentnew file mode 100644index 00000000..01feac54--- /dev/null+++ b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-boolean.firmament@@ -0,0 +1,38 @@+firmament:+ version: 1++model:+ name: m7b_valid_placement_selector_anchor_boolean+ units: mm++ops[3]:+ -+ op: box+ id: base+ size[3]:+ 4+ 4+ 4++ -+ op: add+ id: union+ to: base+ with:+ op: cylinder+ radius: 1+ height: 2++ -+ op: box+ id: placed+ size[3]:+ 1+ 1+ 1+ place:+ on: union.edges+ offset[3]:+ 0+ 0+ 0diff --git a/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-sphere.firmament b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-sphere.firmamentnew file mode 100644index 00000000..a7c75cc0--- /dev/null+++ b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchor-sphere.firmament@@ -0,0 +1,26 @@+firmament:+ version: 1++model:+ name: m7b_valid_placement_selector_anchor_sphere+ units: mm++ops[2]:+ -+ op: sphere+ id: sphere+ radius: 3++ -+ op: box+ id: base+ size[3]:+ 2+ 2+ 2+ place:+ on: sphere.surface+ offset[3]:+ 0+ 0+ 0diff --git a/testdata/firmament/fixtures/m7b-valid-placement-selector-anchors.firmament b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchors.firmamentnew file mode 100644index 00000000..4f179a13--- /dev/null+++ b/testdata/firmament/fixtures/m7b-valid-placement-selector-anchors.firmament@@ -0,0 +1,63 @@+firmament:+ version: 1++model:+ name: m7b_valid_placement_selector_anchors+ units: mm++ops[6]:+ -+ op: box+ id: base+ size[3]:+ 10+ 10+ 10++ -+ op: cylinder+ id: cyl+ radius: 2+ height: 5++ -+ op: sphere+ id: sphere+ radius: 3++ -+ op: add+ id: union+ to: base+ with:+ op: cylinder+ radius: 1+ height: 2++ -+ op: box+ id: b1+ size[3]:+ 1+ 1+ 1+ place:+ on: base.top_face+ offset[3]:+ 0+ 0+ 0++ -+ op: box+ id: b2+ size[3]:+ 1+ 1+ 1+ place:+ on: cyl.side_face+ offset[3]:+ 0+ 0+ 0