Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add compiled Firmament schema artifacts

Yuechen Lif61edf3b7463
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentCompiledSchemaAttachmentTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentCompiledSchemaAttachmentTests.cs
new file mode 100644
index 00000000..0c96dd3f
--- /dev/null
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentCompiledSchemaAttachmentTests.cs
@@ -0,0 +1,210 @@
+using Aetheris.Kernel.Firmament.CompiledModel;
+using System.Linq;
+
+namespace Aetheris.Kernel.Firmament.Tests;
+
+public sealed class FirmamentCompiledSchemaAttachmentTests
+{
+ [Fact]
+ public void Compile_WithoutSchema_Preserves_ExplicitSchemaAbsence_InArtifact()
+ {
+ var result = Compile(BasicOpsSource);
+
+ Assert.True(result.Compilation.IsSuccess);
+ Assert.Null(result.Compilation.Value.CompiledSchema);
+ }
+
+ [Fact]
+ public void Compile_WithValidCncSchema_Attaches_CompiledSchemaArtifact()
+ {
+ var result = Compile($"""
+{Header}
+
+schema:
+ process: cnc
+ minimum_tool_radius: 1.5
+
+{OpsSingleBox}
+""");
+
+ Assert.True(result.Compilation.IsSuccess);
+ var compiledSchema = Assert.IsType<FirmamentCompiledSchema>(result.Compilation.Value.CompiledSchema);
+ Assert.Equal(FirmamentCompiledSchemaProcess.Cnc, compiledSchema.Process);
+
+ var payload = Assert.IsType<FirmamentCompiledCncSchema>(compiledSchema.Payload);
+ Assert.Equal(1.5d, payload.MinimumToolRadius);
+ }
+
+ [Fact]
+ public void Compile_WithValidInjectionMoldedSchema_Attaches_CompiledSchemaArtifact()
+ {
+ var result = Compile($"""
+{Header}
+
+schema:
+ process: injection_molded
+ parting_plane: xy
+ gate_location:
+ x: 1
+ y: 2
+ z: 3
+ draft_angle: 2
+
+{OpsSingleBox}
+""");
+
+ Assert.True(result.Compilation.IsSuccess);
+ var compiledSchema = Assert.IsType<FirmamentCompiledSchema>(result.Compilation.Value.CompiledSchema);
+ Assert.Equal(FirmamentCompiledSchemaProcess.InjectionMolded, compiledSchema.Process);
+
+ var payload = Assert.IsType<FirmamentCompiledInjectionMoldedSchema>(compiledSchema.Payload);
+ Assert.Equal("xy", payload.PartingPlane);
+ Assert.Equal(new FirmamentCompiledSchemaGateLocation(1d, 2d, 3d), payload.GateLocation);
+ Assert.Equal(2d, payload.DraftAngle);
+ }
+
+ [Fact]
+ public void Compile_WithValidAdditiveSchema_Attaches_CompiledSchemaArtifact()
+ {
+ var result = Compile($"""
+{Header}
+
+schema:
+ process: additive
+ printer_resolution: 0.1
+
+{OpsSingleBox}
+""");
+
+ Assert.True(result.Compilation.IsSuccess);
+ var compiledSchema = Assert.IsType<FirmamentCompiledSchema>(result.Compilation.Value.CompiledSchema);
+ Assert.Equal(FirmamentCompiledSchemaProcess.Additive, compiledSchema.Process);
+
+ var payload = Assert.IsType<FirmamentCompiledAdditiveSchema>(compiledSchema.Payload);
+ Assert.Equal(0.1d, payload.PrinterResolution);
+ }
+
+ [Fact]
+ public void Compile_WithSchema_DoesNotChange_Lowering_Execution_Placement_Validation_Or_Selectors()
+ {
+ const string baseline = """
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[3]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 4
+ 2
+ 1
+ -
+ op: cylinder
+ id: pin
+ radius: 0.5
+ height: 2
+ place:
+ on: base.top_face
+ offset[3]:
+ 0
+ 0
+ 0
+ -
+ op: expect_exists
+ target: base.top_face
+""";
+
+ const string withSchema = """
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+schema:
+ process: cnc
+ minimum_tool_radius: 1
+
+ops[3]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 4
+ 2
+ 1
+ -
+ op: cylinder
+ id: pin
+ radius: 0.5
+ height: 2
+ place:
+ on: base.top_face
+ offset[3]:
+ 0
+ 0
+ 0
+ -
+ op: expect_exists
+ target: base.top_face
+""";
+
+ var first = Compile(baseline);
+ var second = Compile(withSchema);
+
+ Assert.True(first.Compilation.IsSuccess);
+ Assert.True(second.Compilation.IsSuccess);
+
+ Assert.Equivalent(first.Compilation.Value.PrimitiveLoweringPlan, second.Compilation.Value.PrimitiveLoweringPlan);
+
+ var firstExecutionMetadata = first.Compilation.Value.PrimitiveExecutionResult!.ExecutedPrimitives
+ .Select(p => (p.OpIndex, p.FeatureId, Kind: p.Kind.ToString(), BodyCount: p.Body.Topology.Bodies.Count(), FaceCount: p.Body.Topology.Faces.Count()))
+ .Concat(first.Compilation.Value.PrimitiveExecutionResult.ExecutedBooleans
+ .Select(b => (b.OpIndex, b.FeatureId, Kind: b.Kind.ToString(), BodyCount: b.Body.Topology.Bodies.Count(), FaceCount: b.Body.Topology.Faces.Count())))
+ .OrderBy(entry => entry.OpIndex)
+ .ToArray();
+ var secondExecutionMetadata = second.Compilation.Value.PrimitiveExecutionResult!.ExecutedPrimitives
+ .Select(p => (p.OpIndex, p.FeatureId, Kind: p.Kind.ToString(), BodyCount: p.Body.Topology.Bodies.Count(), FaceCount: p.Body.Topology.Faces.Count()))
+ .Concat(second.Compilation.Value.PrimitiveExecutionResult.ExecutedBooleans
+ .Select(b => (b.OpIndex, b.FeatureId, Kind: b.Kind.ToString(), BodyCount: b.Body.Topology.Bodies.Count(), FaceCount: b.Body.Topology.Faces.Count())))
+ .OrderBy(entry => entry.OpIndex)
+ .ToArray();
+
+ Assert.Equal(firstExecutionMetadata, secondExecutionMetadata);
+ Assert.Equal(first.Compilation.Value.ValidationExecutionResult!.Validations, second.Compilation.Value.ValidationExecutionResult!.Validations);
+ }
+
+ private static FirmamentCompileResult Compile(string source)
+ {
+ var compiler = new FirmamentCompiler();
+ return compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+ }
+
+ private const string Header = """
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+""";
+
+ private const string OpsSingleBox = """
+ops[1]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 1
+ 2
+ 3
+""";
+
+ private const string BasicOpsSource = Header + "\n" + OpsSingleBox;
+}
diff --git a/Aetheris.Kernel.Firmament/CompiledModel/FirmamentCompiledSchema.cs b/Aetheris.Kernel.Firmament/CompiledModel/FirmamentCompiledSchema.cs
new file mode 100644
index 00000000..3d35bef0
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/CompiledModel/FirmamentCompiledSchema.cs
@@ -0,0 +1,28 @@
+namespace Aetheris.Kernel.Firmament.CompiledModel;
+
+public sealed record FirmamentCompiledSchema(
+ FirmamentCompiledSchemaProcess Process,
+ FirmamentCompiledSchemaPayload Payload);
+
+public enum FirmamentCompiledSchemaProcess
+{
+ Cnc,
+ InjectionMolded,
+ Additive
+}
+
+public abstract record FirmamentCompiledSchemaPayload;
+
+public sealed record FirmamentCompiledCncSchema(double MinimumToolRadius)
+ : FirmamentCompiledSchemaPayload;
+
+public sealed record FirmamentCompiledInjectionMoldedSchema(
+ string PartingPlane,
+ FirmamentCompiledSchemaGateLocation GateLocation,
+ double DraftAngle)
+ : FirmamentCompiledSchemaPayload;
+
+public sealed record FirmamentCompiledAdditiveSchema(double PrinterResolution)
+ : FirmamentCompiledSchemaPayload;
+
+public sealed record FirmamentCompiledSchemaGateLocation(double X, double Y, double Z);
diff --git a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
index 59ae9f6b..a0882f8e 100644
--- a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
@@ -1,5 +1,6 @@
-using Aetheris.Kernel.Firmament.Lowering;
+using Aetheris.Kernel.Firmament.CompiledModel;
using Aetheris.Kernel.Firmament.Execution;
+using Aetheris.Kernel.Firmament.Lowering;
using Aetheris.Kernel.Firmament.ParsedModel;
namespace Aetheris.Kernel.Firmament;
@@ -7,6 +8,7 @@ namespace Aetheris.Kernel.Firmament;
public sealed record FirmamentCompilationArtifact(
string ArtifactKind = "firmament-placement-executed",
FirmamentParsedDocument? ParsedDocument = null,
+ FirmamentCompiledSchema? CompiledSchema = null,
FirmamentPrimitiveLoweringPlan? PrimitiveLoweringPlan = null,
FirmamentPrimitiveExecutionResult? PrimitiveExecutionResult = null,
FirmamentValidationExecutionResult? ValidationExecutionResult = null);
diff --git a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
index bd2214e1..dd32039e 100644
--- a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
@@ -1,6 +1,7 @@
using Aetheris.Kernel.Core.Results;
using Aetheris.Kernel.Firmament.Execution;
using Aetheris.Kernel.Firmament.Lowering;
+using Aetheris.Kernel.Firmament.Mapping;
using Aetheris.Kernel.Firmament.Parsing;
using Aetheris.Kernel.Firmament.Validation;
@@ -62,6 +63,7 @@ public sealed class FirmamentCompiler
}
var validatedDocument = documentCoherenceValidationResult.Value;
+ var compiledSchema = FirmamentCompiledSchemaMapper.Map(validatedDocument.Schema);
var primitiveLoweringResult = FirmamentPrimitiveLowerer.Lower(validatedDocument);
if (!primitiveLoweringResult.IsSuccess)
@@ -89,6 +91,7 @@ public sealed class FirmamentCompiler
new FirmamentCompilationArtifact(
ArtifactKind: "firmament-placement-executed",
ParsedDocument: validatedDocument,
+ CompiledSchema: compiledSchema,
PrimitiveLoweringPlan: primitiveLoweringResult.Value,
PrimitiveExecutionResult: primitiveExecutionResult.Value,
ValidationExecutionResult: validationExecutionResult.Value)));
diff --git a/Aetheris.Kernel.Firmament/Mapping/FirmamentCompiledSchemaMapper.cs b/Aetheris.Kernel.Firmament/Mapping/FirmamentCompiledSchemaMapper.cs
new file mode 100644
index 00000000..48bf5e90
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/Mapping/FirmamentCompiledSchemaMapper.cs
@@ -0,0 +1,46 @@
+using System.Globalization;
+using Aetheris.Kernel.Firmament.CompiledModel;
+using Aetheris.Kernel.Firmament.ParsedModel;
+
+namespace Aetheris.Kernel.Firmament.Mapping;
+
+internal static class FirmamentCompiledSchemaMapper
+{
+ public static FirmamentCompiledSchema? Map(FirmamentParsedSchema? schema)
+ {
+ if (schema is null)
+ {
+ return null;
+ }
+
+ return schema.Process switch
+ {
+ FirmamentParsedSchemaProcess.Cnc => new FirmamentCompiledSchema(
+ FirmamentCompiledSchemaProcess.Cnc,
+ new FirmamentCompiledCncSchema(schema.MinimumToolRadius!.Value)),
+ FirmamentParsedSchemaProcess.InjectionMolded => new FirmamentCompiledSchema(
+ FirmamentCompiledSchemaProcess.InjectionMolded,
+ new FirmamentCompiledInjectionMoldedSchema(
+ schema.PartingPlane!,
+ new FirmamentCompiledSchemaGateLocation(
+ ParseRequiredDouble(schema.GateLocation!.XRaw),
+ ParseRequiredDouble(schema.GateLocation.YRaw),
+ ParseRequiredDouble(schema.GateLocation.ZRaw)),
+ schema.DraftAngle!.Value)),
+ FirmamentParsedSchemaProcess.Additive => new FirmamentCompiledSchema(
+ FirmamentCompiledSchemaProcess.Additive,
+ new FirmamentCompiledAdditiveSchema(schema.PrinterResolution!.Value)),
+ _ => throw new InvalidOperationException($"Cannot compile unsupported schema process '{schema.Process}'.")
+ };
+ }
+
+ private static double ParseRequiredDouble(string? raw)
+ {
+ if (!double.TryParse(raw, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var value))
+ {
+ throw new InvalidOperationException("Schema validation should guarantee numeric gate location coordinates.");
+ }
+
+ return value;
+ }
+}