Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Firmament M0c: recognize known op kinds and reject unknown ops

Yuechen Li8f919041ced8
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
index 15c27fd3..ca421dd7 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
@@ -4,7 +4,7 @@ namespace Aetheris.Kernel.Firmament.Tests;
public sealed class FirmamentCorpusScaffoldTests
{
- private const string ManifestPath = "testdata/firmament/manifests/m0b.corpus.json";
+ private const string ManifestPath = "testdata/firmament/manifests/m0c.corpus.json";
[Fact]
public void Manifest_Exists_And_CanBeLoaded()
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
index fef35feb..d0c99156 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
@@ -1,5 +1,6 @@
using Aetheris.Kernel.Core.Diagnostics;
using Aetheris.Kernel.Firmament.Diagnostics;
+using Aetheris.Kernel.Firmament.ParsedModel;
namespace Aetheris.Kernel.Firmament.Tests;
@@ -33,7 +34,7 @@ public sealed class FirmamentScaffoldTests
Assert.True(result.Compilation.IsSuccess);
var artifact = result.Compilation.Value;
- Assert.Equal("firmament-ops-structure-parsed", artifact.ArtifactKind);
+ Assert.Equal("firmament-ops-structure-known-kind-parsed", artifact.ArtifactKind);
Assert.NotNull(artifact.ParsedDocument);
Assert.Equal("1", artifact.ParsedDocument!.Firmament.Version);
Assert.Equal("demo", artifact.ParsedDocument.Model.Name);
@@ -42,7 +43,7 @@ public sealed class FirmamentScaffoldTests
}
[Fact]
- public void Compiler_Accepts_StructurallyValidSingleOp() =>
+ public void Compiler_Accepts_KnownPrimitiveOp() =>
AssertValidOpsCount(
"""
{
@@ -56,7 +57,7 @@ public sealed class FirmamentScaffoldTests
1);
[Fact]
- public void Compiler_Accepts_StructurallyValidMultipleOps() =>
+ public void Compiler_Accepts_MultipleKnownOps() =>
AssertValidOpsCount(
"""
{
@@ -70,6 +71,58 @@ public sealed class FirmamentScaffoldTests
""",
2);
+
+ [Fact]
+ public void Compiler_Classifies_KnownOpKind_OnParsedEntry()
+ {
+ var compiler = new FirmamentCompiler();
+ var source = """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [
+ { "op": "sphere", "note": "x" }
+ ]
+ }
+ """;
+
+ var result = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+
+ Assert.True(result.Compilation.IsSuccess);
+ var op = Assert.Single(result.Compilation.Value.ParsedDocument!.Ops.Entries);
+ Assert.Equal("sphere", op.OpName);
+ Assert.Equal(FirmamentKnownOpKind.Sphere, op.KnownKind);
+ Assert.Equal("x", op.RawFields["note"]);
+ }
+
+ [Fact]
+ public void Compiler_Accepts_KnownBooleanOp() =>
+ AssertValidOpsCount(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [
+ { "op": "add" }
+ ]
+ }
+ """,
+ 1);
+
+ [Fact]
+ public void Compiler_Accepts_KnownValidationOp() =>
+ AssertValidOpsCount(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [
+ { "op": "expect_exists" }
+ ]
+ }
+ """,
+ 1);
+
[Fact]
public void Compiler_Rejects_OpEntry_MissingOp() =>
AssertSingleValidationError(
@@ -115,6 +168,51 @@ public sealed class FirmamentScaffoldTests
FirmamentDiagnosticCodes.StructureInvalidOpFieldValue,
"Operation entry at index 0 has invalid 'op' value; expected a non-empty scalar.");
+
+ [Fact]
+ public void Compiler_Rejects_UnknownOpKind() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [
+ { "op": "torus" }
+ ]
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureUnknownOpKind,
+ "Operation entry at index 0 has unknown op kind 'torus'.");
+
+ [Fact]
+ public void Compiler_Rejects_UnknownOpKind_Deterministically()
+ {
+ var compiler = new FirmamentCompiler();
+ var source = """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [
+ { "op": "torus" }
+ ]
+ }
+ """;
+
+ var first = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+ var second = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(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.Code, secondDiagnostic.Code);
+ Assert.Equal(firstDiagnostic.Severity, secondDiagnostic.Severity);
+ Assert.Equal(firstDiagnostic.Message, secondDiagnostic.Message);
+ Assert.Equal(firstDiagnostic.Source, secondDiagnostic.Source);
+ }
+
[Fact]
public void Compiler_Rejects_OpEntry_MissingOp_Deterministically()
{
@@ -261,6 +359,7 @@ public sealed class FirmamentScaffoldTests
Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureInvalidOpsEntryShape.Value);
Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureMissingRequiredOpField.Value);
Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureInvalidOpFieldValue.Value);
+ Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureUnknownOpKind.Value);
Assert.StartsWith("FIRM-REF", FirmamentDiagnosticCodes.ReferencePlaceholder.Value);
Assert.StartsWith("FIRM-SEL", FirmamentDiagnosticCodes.SelectorPlaceholder.Value);
Assert.StartsWith("FIRM-SCHEMA", FirmamentDiagnosticCodes.SchemaPlaceholder.Value);
diff --git a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
index c13a4861..5093f12a 100644
--- a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
+++ b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
@@ -11,6 +11,7 @@ public static class FirmamentDiagnosticCodes
public static readonly FirmamentDiagnosticCode StructureInvalidOpsEntryShape = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0005");
public static readonly FirmamentDiagnosticCode StructureMissingRequiredOpField = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0006");
public static readonly FirmamentDiagnosticCode StructureInvalidOpFieldValue = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0007");
+ public static readonly FirmamentDiagnosticCode StructureUnknownOpKind = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0008");
public static readonly FirmamentDiagnosticCode ReferencePlaceholder = new($"{FirmamentDiagnosticConventions.ReferencePrefix}-0001");
public static readonly FirmamentDiagnosticCode SelectorPlaceholder = new($"{FirmamentDiagnosticConventions.SelectorPrefix}-0001");
diff --git a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
index 3f65b44d..144334de 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-structure-parsed",
+ string ArtifactKind = "firmament-ops-structure-known-kind-parsed",
FirmamentParsedDocument? ParsedDocument = null);
diff --git a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs
new file mode 100644
index 00000000..1b7f5633
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentKnownOpKind.cs
@@ -0,0 +1,34 @@
+namespace Aetheris.Kernel.Firmament.ParsedModel;
+
+public enum FirmamentKnownOpKind
+{
+ Box,
+ Cylinder,
+ Sphere,
+ Add,
+ Subtract,
+ Intersect,
+ ExpectExists,
+ ExpectSelectable,
+ ExpectManifold
+}
+
+internal static class FirmamentKnownOpKinds
+{
+ private static readonly IReadOnlyDictionary<string, FirmamentKnownOpKind> Registry =
+ new Dictionary<string, FirmamentKnownOpKind>(StringComparer.Ordinal)
+ {
+ ["box"] = FirmamentKnownOpKind.Box,
+ ["cylinder"] = FirmamentKnownOpKind.Cylinder,
+ ["sphere"] = FirmamentKnownOpKind.Sphere,
+ ["add"] = FirmamentKnownOpKind.Add,
+ ["subtract"] = FirmamentKnownOpKind.Subtract,
+ ["intersect"] = FirmamentKnownOpKind.Intersect,
+ ["expect_exists"] = FirmamentKnownOpKind.ExpectExists,
+ ["expect_selectable"] = FirmamentKnownOpKind.ExpectSelectable,
+ ["expect_manifold"] = FirmamentKnownOpKind.ExpectManifold
+ };
+
+ public static bool TryParse(string opName, out FirmamentKnownOpKind kind) =>
+ Registry.TryGetValue(opName, out kind);
+}
diff --git a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs
index f93caa3d..ceffb79d 100644
--- a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs
+++ b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs
@@ -15,4 +15,5 @@ public sealed record FirmamentParsedOpsSection(IReadOnlyList<FirmamentParsedOpEn
public sealed record FirmamentParsedOpEntry(
string OpName,
+ FirmamentKnownOpKind KnownKind,
IReadOnlyDictionary<string, string> RawFields);
diff --git a/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs b/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs
index c211bf78..26d2145b 100644
--- a/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs
+++ b/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs
@@ -239,7 +239,12 @@ internal static class FirmamentTopLevelParser
rawFields[property.Name] = property.Value.ToString();
}
- entries.Add(new FirmamentParsedOpEntry(opName, rawFields));
+ if (!FirmamentKnownOpKinds.TryParse(opName, out var knownKind))
+ {
+ return UnknownOpKind(index, opName);
+ }
+
+ entries.Add(new FirmamentParsedOpEntry(opName, knownKind, rawFields));
index++;
}
@@ -267,7 +272,12 @@ internal static class FirmamentTopLevelParser
return InvalidOpFieldValue(index);
}
- entries.Add(new FirmamentParsedOpEntry(opName, new Dictionary<string, string>(opEntry.Fields, StringComparer.Ordinal)));
+ if (!FirmamentKnownOpKinds.TryParse(opName, out var knownKind))
+ {
+ return UnknownOpKind(index, opName);
+ }
+
+ entries.Add(new FirmamentParsedOpEntry(opName, knownKind, new Dictionary<string, string>(opEntry.Fields, StringComparer.Ordinal)));
}
return KernelResult<IReadOnlyList<FirmamentParsedOpEntry>>.Success(entries);
@@ -486,6 +496,15 @@ internal static class FirmamentTopLevelParser
$"Operation entry at index {index} has invalid 'op' value; expected a non-empty scalar.")
]);
+ private static KernelResult<IReadOnlyList<FirmamentParsedOpEntry>> UnknownOpKind(int index, string opName) =>
+ KernelResult<IReadOnlyList<FirmamentParsedOpEntry>>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureUnknownOpKind,
+ $"Operation entry at index {index} has unknown op kind '{opName}'.")
+ ]);
+
private static KernelDiagnostic CreateDiagnostic(KernelDiagnosticCode kernelCode, FirmamentDiagnosticCode firmamentCode, string message) =>
new(
kernelCode,
diff --git a/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament b/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament
new file mode 100644
index 00000000..160eae74
--- /dev/null
+++ b/testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament
@@ -0,0 +1,10 @@
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[1]:
+ -
+ op: torus
diff --git a/testdata/firmament/fixtures/m0c-valid-known-boolean-op.firmament b/testdata/firmament/fixtures/m0c-valid-known-boolean-op.firmament
new file mode 100644
index 00000000..b27c8fba
--- /dev/null
+++ b/testdata/firmament/fixtures/m0c-valid-known-boolean-op.firmament
@@ -0,0 +1,10 @@
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[1]:
+ -
+ op: add
diff --git a/testdata/firmament/fixtures/m0c-valid-known-validation-op.firmament b/testdata/firmament/fixtures/m0c-valid-known-validation-op.firmament
new file mode 100644
index 00000000..acf55abf
--- /dev/null
+++ b/testdata/firmament/fixtures/m0c-valid-known-validation-op.firmament
@@ -0,0 +1,10 @@
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[1]:
+ -
+ op: expect_manifold
diff --git a/testdata/firmament/manifests/m0c.corpus.json b/testdata/firmament/manifests/m0c.corpus.json
new file mode 100644
index 00000000..d1abeff7
--- /dev/null
+++ b/testdata/firmament/manifests/m0c.corpus.json
@@ -0,0 +1,79 @@
+{
+ "version": "1",
+ "entries": [
+ {
+ "id": "m0b-valid-empty-ops",
+ "name": "Valid empty ops section",
+ "fixturePath": "testdata/firmament/fixtures/m0b-valid-empty-ops.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0b-valid-one-op",
+ "name": "Valid single known primitive op",
+ "fixturePath": "testdata/firmament/fixtures/m0b-valid-one-op.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0b-valid-two-ops",
+ "name": "Valid multiple known ops",
+ "fixturePath": "testdata/firmament/fixtures/m0b-valid-two-ops.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0c-valid-known-boolean-op",
+ "name": "Valid known boolean op",
+ "fixturePath": "testdata/firmament/fixtures/m0c-valid-known-boolean-op.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0c-valid-known-validation-op",
+ "name": "Valid known validation op",
+ "fixturePath": "testdata/firmament/fixtures/m0c-valid-known-validation-op.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0b-invalid-op-missing-op",
+ "name": "Invalid op entry missing op field",
+ "fixturePath": "testdata/firmament/fixtures/m0b-invalid-op-missing-op.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0006"
+ }
+ },
+ {
+ "id": "m0b-invalid-op-entry-wrong-shape",
+ "name": "Invalid op entry wrong coarse shape",
+ "fixturePath": "testdata/firmament/fixtures/m0b-invalid-op-entry-wrong-shape.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0005"
+ }
+ },
+ {
+ "id": "m0b-invalid-op-value-shape",
+ "name": "Invalid op value shape",
+ "fixturePath": "testdata/firmament/fixtures/m0b-invalid-op-value-shape.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0007"
+ }
+ },
+ {
+ "id": "m0c-invalid-unknown-op",
+ "name": "Invalid unknown op kind",
+ "fixturePath": "testdata/firmament/fixtures/m0c-invalid-unknown-op.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0008"
+ }
+ }
+ ]
+}