fix(benchmark): use reference-based drawing identity in NestValidator, fix duplicate-sheet-size crash, document Engines/ plugin contract
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,7 +25,14 @@ namespace OpenNest.Benchmark
|
||||
/// </summary>
|
||||
public static class NestValidator
|
||||
{
|
||||
public static ValidationResult Validate(List<(Plate Plate, List<Part> Parts)> plateRuns, BenchmarkJob job)
|
||||
/// <summary>
|
||||
/// requirements maps each materialized part's BaseDrawing (by reference - materialized
|
||||
/// Drawing instances are freshly reconstructed per NestResultMaterializer.Materialize, so
|
||||
/// identity must never be inferred from Name, which is only incidentally seeded from the
|
||||
/// originating NestJobPart id) to its original quantity limit and display name.
|
||||
/// </summary>
|
||||
public static ValidationResult Validate(List<(Plate Plate, List<Part> Parts)> plateRuns,
|
||||
IReadOnlyDictionary<Drawing, (string Name, int Quantity)> requirements)
|
||||
{
|
||||
var result = new ValidationResult();
|
||||
var allParts = plateRuns.SelectMany(pr => pr.Parts).ToList();
|
||||
@@ -33,38 +40,33 @@ namespace OpenNest.Benchmark
|
||||
if (allParts.Count == 0)
|
||||
return result;
|
||||
|
||||
ValidateQuantities(allParts, job, result);
|
||||
ValidateQuantities(allParts, requirements, result);
|
||||
|
||||
foreach (var (plate, parts) in plateRuns)
|
||||
{
|
||||
if (parts.Count == 0)
|
||||
continue;
|
||||
|
||||
ValidateBounds(parts, plate, result);
|
||||
ValidateBounds(parts, plate, requirements, result);
|
||||
ValidateAreaBudget(parts, plate, result);
|
||||
ValidateSpacing(parts, plate.PartSpacing, result);
|
||||
ValidateSpacing(parts, plate.PartSpacing, requirements, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ValidateQuantities(List<Part> parts, BenchmarkJob job, ValidationResult result)
|
||||
private static void ValidateQuantities(List<Part> parts,
|
||||
IReadOnlyDictionary<Drawing, (string Name, int Quantity)> requirements, ValidationResult result)
|
||||
{
|
||||
// Materialized parts reference freshly reconstructed Drawing objects (NestResultMaterializer
|
||||
// rebuilds them via DrawingJobMapper.CreateDrawing, which sets the materialized Drawing's Name
|
||||
// to the originating NestJobPart id - a fresh, unrelated Drawing.Id gets auto-generated instead).
|
||||
// BuildNestJob sets each NestJobPart's id to the original Drawing.Id.ToString(), so that string -
|
||||
// materialized as BaseDrawing.Name - is the stable identity across the materialization boundary.
|
||||
var allowed = job.Requests.ToDictionary(r => r.Drawing.Id.ToString(), r => (r.Quantity, r.Drawing.Name));
|
||||
var placedCounts = parts
|
||||
.GroupBy(p => p.BaseDrawing.Name)
|
||||
.GroupBy<Part, Drawing>(p => p.BaseDrawing, ReferenceEqualityComparer.Instance)
|
||||
.ToDictionary(g => g.Key, g => g.Count());
|
||||
|
||||
foreach (var (partId, placed) in placedCounts)
|
||||
foreach (var (drawing, placed) in placedCounts)
|
||||
{
|
||||
if (!allowed.TryGetValue(partId, out var requirement))
|
||||
if (!requirements.TryGetValue(drawing, out var requirement))
|
||||
{
|
||||
result.Violations.Add($"Placed drawing id={partId} which was not requested for this job");
|
||||
result.Violations.Add($"Placed drawing '{drawing.Name}' which was not requested for this job");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -76,7 +78,8 @@ namespace OpenNest.Benchmark
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateBounds(List<Part> parts, Plate plate, ValidationResult result)
|
||||
private static void ValidateBounds(List<Part> parts, Plate plate,
|
||||
IReadOnlyDictionary<Drawing, (string Name, int Quantity)> requirements, ValidationResult result)
|
||||
{
|
||||
var workArea = plate.WorkArea();
|
||||
|
||||
@@ -92,7 +95,7 @@ namespace OpenNest.Benchmark
|
||||
if (outLeft || outBottom || outRight || outTop)
|
||||
{
|
||||
result.Violations.Add(
|
||||
$"'{part.BaseDrawing.Name}' at ({part.Location.X:F2},{part.Location.Y:F2}) falls outside the work area " +
|
||||
$"'{DisplayName(part, requirements)}' at ({part.Location.X:F2},{part.Location.Y:F2}) falls outside the work area " +
|
||||
$"of a {plate.Size} plate");
|
||||
}
|
||||
}
|
||||
@@ -121,7 +124,8 @@ namespace OpenNest.Benchmark
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateSpacing(List<Part> parts, double spacing, ValidationResult result)
|
||||
private static void ValidateSpacing(List<Part> parts, double spacing,
|
||||
IReadOnlyDictionary<Drawing, (string Name, int Quantity)> requirements, ValidationResult result)
|
||||
{
|
||||
var worldPolygons = new Polygon[parts.Count];
|
||||
var inflatedPolygons = new Polygon[parts.Count];
|
||||
@@ -145,12 +149,18 @@ namespace OpenNest.Benchmark
|
||||
if (Collision.HasOverlap(inflatedPolygons[i], worldPolygons[j]))
|
||||
{
|
||||
result.Violations.Add(
|
||||
$"'{parts[i].BaseDrawing.Name}' and '{parts[j].BaseDrawing.Name}' are closer than the required spacing ({spacing:F3})");
|
||||
$"'{DisplayName(parts[i], requirements)}' and '{DisplayName(parts[j], requirements)}' are closer than the required spacing ({spacing:F3})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Friendly name for a violation message, falling back to the materialized
|
||||
/// Drawing's own Name (the raw partId string) if this part wasn't in requirements at all -
|
||||
/// that mismatch is already reported by ValidateQuantities, so this is display-only.</summary>
|
||||
private static string DisplayName(Part part, IReadOnlyDictionary<Drawing, (string Name, int Quantity)> requirements) =>
|
||||
requirements.TryGetValue(part.BaseDrawing, out var requirement) ? requirement.Name : part.BaseDrawing.Name;
|
||||
|
||||
/// <summary>
|
||||
/// Extracts a part's perimeter as a world-space polygon, optionally inflated
|
||||
/// outward by the given spacing, mirroring Part.Intersects' own geometry
|
||||
|
||||
Reference in New Issue
Block a user