Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add reusable JudgmentEngine and When predicate layer

Yuechen Li9cc8eedf3bbe
diff --git a/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/JudgmentEngineTests.cs b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/JudgmentEngineTests.cs
new file mode 100644
index 00000000..a9e9c6fc
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/JudgmentEngineTests.cs
@@ -0,0 +1,116 @@
+using Aetheris.Kernel.Core.Brep.EdgeFinishing;
+
+namespace Aetheris.Kernel.Core.Tests.Brep.EdgeFinishing;
+
+public sealed class JudgmentEngineTests
+{
+ [Fact]
+ public void Evaluate_SelectsOnlyAdmissibleCandidate()
+ {
+ var engine = new JudgmentEngine<int>();
+ var candidates = new[]
+ {
+ new JudgmentCandidate<int>("non_positive", value => value <= 0, _ => 10d),
+ new JudgmentCandidate<int>("positive", value => value > 0, _ => 1d)
+ };
+
+ var result = engine.Evaluate(3, candidates);
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("positive", result.Selection!.Value.Candidate.Name);
+ }
+
+ [Fact]
+ public void Evaluate_SelectsHighestScoreAmongAdmissibleCandidates()
+ {
+ var engine = new JudgmentEngine<int>();
+ var candidates = new[]
+ {
+ new JudgmentCandidate<int>("low", _ => true, _ => 2d),
+ new JudgmentCandidate<int>("high", _ => true, _ => 5d)
+ };
+
+ var result = engine.Evaluate(0, candidates);
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("high", result.Selection!.Value.Candidate.Name);
+ Assert.Equal(5d, result.Selection.Value.Score);
+ }
+
+ [Fact]
+ public void Evaluate_UsesDeterministicTieBreakByPriorityThenName()
+ {
+ var engine = new JudgmentEngine<int>();
+ var candidates = new[]
+ {
+ new JudgmentCandidate<int>("zeta", _ => true, _ => 1d, TieBreakerPriority: 1),
+ new JudgmentCandidate<int>("alpha", _ => true, _ => 1d, TieBreakerPriority: 1),
+ new JudgmentCandidate<int>("beta", _ => true, _ => 1d, TieBreakerPriority: 0)
+ };
+
+ var first = engine.Evaluate(0, candidates);
+ var second = engine.Evaluate(0, candidates.Reverse().ToArray());
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal("beta", first.Selection!.Value.Candidate.Name);
+ Assert.Equal("beta", second.Selection!.Value.Candidate.Name);
+ }
+
+ [Fact]
+ public void Evaluate_ReturnsExplicitFailureRejectionsWhenNoCandidateIsAdmissible()
+ {
+ var engine = new JudgmentEngine<int>();
+ var candidates = new[]
+ {
+ new JudgmentCandidate<int>(
+ "too_small",
+ value => value > 10,
+ _ => 1d,
+ value => $"Expected > 10 but got {value}."),
+ new JudgmentCandidate<int>(
+ "even_only",
+ value => value % 2 == 0,
+ _ => 1d,
+ value => $"Expected even value but got {value}.")
+ };
+
+ var result = engine.Evaluate(3, candidates);
+
+ Assert.False(result.IsSuccess);
+ Assert.Null(result.Selection);
+ Assert.Collection(
+ result.Rejections,
+ rejection =>
+ {
+ Assert.Equal("too_small", rejection.CandidateName);
+ Assert.Contains("Expected > 10", rejection.Reason, StringComparison.Ordinal);
+ },
+ rejection =>
+ {
+ Assert.Equal("even_only", rejection.CandidateName);
+ Assert.Contains("Expected even", rejection.Reason, StringComparison.Ordinal);
+ });
+ }
+
+ [Fact]
+ public void When_Composition_AllAnyAndNot_BehaveAsExpected()
+ {
+ var positive = new Func<int, bool>(value => value > 0);
+ var even = new Func<int, bool>(value => value % 2 == 0);
+
+ var all = When.All(positive, even);
+ var any = When.Any(positive, even);
+ var not = When.Not(positive);
+
+ Assert.True(all(2));
+ Assert.False(all(3));
+
+ Assert.True(any(2));
+ Assert.True(any(3));
+ Assert.False(any(-3));
+
+ Assert.True(not(-1));
+ Assert.False(not(1));
+ }
+}
diff --git a/Aetheris.Kernel.Core/Brep/EdgeFinishing/JudgmentEngine.cs b/Aetheris.Kernel.Core/Brep/EdgeFinishing/JudgmentEngine.cs
new file mode 100644
index 00000000..63275593
--- /dev/null
+++ b/Aetheris.Kernel.Core/Brep/EdgeFinishing/JudgmentEngine.cs
@@ -0,0 +1,129 @@
+namespace Aetheris.Kernel.Core.Brep.EdgeFinishing;
+
+/// <summary>
+/// Tiny predicate combinator layer for readable bounded decision guards.
+/// </summary>
+public static class When
+{
+ public static Func<TContext, bool> All<TContext>(params Func<TContext, bool>[] predicates)
+ => context => predicates.All(predicate => predicate(context));
+
+ public static Func<TContext, bool> Any<TContext>(params Func<TContext, bool>[] predicates)
+ => context => predicates.Any(predicate => predicate(context));
+
+ public static Func<TContext, bool> Not<TContext>(Func<TContext, bool> predicate)
+ => context => !predicate(context);
+}
+
+/// <summary>
+/// Generic bounded candidate contract for deterministic judgment.
+/// </summary>
+public readonly record struct JudgmentCandidate<TContext>(
+ string Name,
+ Func<TContext, bool> IsAdmissible,
+ Func<TContext, double> Score,
+ Func<TContext, string>? RejectionReason = null,
+ int TieBreakerPriority = 0);
+
+public readonly record struct JudgmentRejection(string CandidateName, string Reason);
+
+public readonly record struct JudgmentSelection<TContext>(JudgmentCandidate<TContext> Candidate, double Score);
+
+public sealed class JudgmentResult<TContext>
+{
+ private JudgmentResult(JudgmentSelection<TContext>? selection, IReadOnlyList<JudgmentRejection> rejections)
+ {
+ Selection = selection;
+ Rejections = rejections;
+ }
+
+ public bool IsSuccess => Selection.HasValue;
+
+ public JudgmentSelection<TContext>? Selection { get; }
+
+ public IReadOnlyList<JudgmentRejection> Rejections { get; }
+
+ public static JudgmentResult<TContext> Success(JudgmentSelection<TContext> selection)
+ => new(selection, []);
+
+ public static JudgmentResult<TContext> Failure(IReadOnlyList<JudgmentRejection> rejections)
+ => new(null, rejections);
+}
+
+/// <summary>
+/// Deterministic bounded chooser over candidate admissibility and score.
+/// </summary>
+public sealed class JudgmentEngine<TContext>
+{
+ public JudgmentResult<TContext> Evaluate(TContext context, IReadOnlyList<JudgmentCandidate<TContext>> candidates)
+ {
+ if (candidates.Count == 0)
+ {
+ return JudgmentResult<TContext>.Failure([new JudgmentRejection("<none>", "No candidates were supplied.")]);
+ }
+
+ var rejections = new List<JudgmentRejection>();
+ JudgmentSelection<TContext>? best = null;
+ var bestIndex = -1;
+
+ for (var i = 0; i < candidates.Count; i++)
+ {
+ var candidate = candidates[i];
+ if (!candidate.IsAdmissible(context))
+ {
+ var reason = candidate.RejectionReason?.Invoke(context) ?? "Candidate predicates were not satisfied.";
+ rejections.Add(new JudgmentRejection(candidate.Name, reason));
+ continue;
+ }
+
+ var score = candidate.Score(context);
+ if (!double.IsFinite(score))
+ {
+ rejections.Add(new JudgmentRejection(candidate.Name, "Candidate produced a non-finite score."));
+ continue;
+ }
+
+ if (!best.HasValue || IsBetter(score, candidate, i, best.Value, bestIndex))
+ {
+ best = new JudgmentSelection<TContext>(candidate, score);
+ bestIndex = i;
+ }
+ }
+
+ return best.HasValue
+ ? JudgmentResult<TContext>.Success(best.Value)
+ : JudgmentResult<TContext>.Failure(rejections);
+ }
+
+ private static bool IsBetter(
+ double score,
+ JudgmentCandidate<TContext> candidate,
+ int index,
+ JudgmentSelection<TContext> currentBest,
+ int currentBestIndex)
+ {
+ const double epsilon = 1e-12;
+ if (score > currentBest.Score + epsilon)
+ {
+ return true;
+ }
+
+ if (score < currentBest.Score - epsilon)
+ {
+ return false;
+ }
+
+ if (candidate.TieBreakerPriority != currentBest.Candidate.TieBreakerPriority)
+ {
+ return candidate.TieBreakerPriority < currentBest.Candidate.TieBreakerPriority;
+ }
+
+ var nameOrder = StringComparer.Ordinal.Compare(candidate.Name, currentBest.Candidate.Name);
+ if (nameOrder != 0)
+ {
+ return nameOrder < 0;
+ }
+
+ return index < currentBestIndex;
+ }
+}