60 throw new ArgumentNullException(nameof(code));
62 throw new ArgumentNullException(nameof(path));
64 throw new ArgumentNullException(nameof(message));
76 public string Code {
get; }
82 public string Path {
get; }
93 $
"[{Severity}] {Code} at {Path}: {Message}";
109 ArgumentNullException.ThrowIfNull(supplyChain);
111 var issues =
new List<ValidationIssue>();
113 ValidateRoot(supplyChain, issues);
114 ValidateLogicalSubsystem(supplyChain, issues);
115 ValidatePhysicalSubsystem(supplyChain, issues);
116 ValidateProductionRoutings(supplyChain, issues);
117 ValidateProductionCharacteristics(supplyChain, issues);
121 ValidateInventories(supplyChain, issues);
122 ValidateTransportCharacteristics(supplyChain, issues);
123 ValidateDemands(supplyChain, issues);
124 ValidateDistributionCenterSourcings(
127 ValidateSupplierDeliveries(supplyChain, issues);
129 return issues.AsReadOnly();
161 if (errors.Length == 0)
166 string message =
string.Join(
168 errors.Select(error => error.ToString()));
170 throw new InvalidOperationException(
171 "The supply-chain model is invalid." +
172 Environment.NewLine +
176 private static void ValidateRoot(
178 ICollection<ValidationIssue> issues)
185 "supplyChain.planningHorizon",
186 "The planning horizon must be strictly positive.");
189 if (supplyChain.
Items.Count == 0)
195 "The supply chain must contain at least one item.");
204 "At least one period-dependent object does not " +
205 "use the global planning horizon.");
209 private static void ValidateLogicalSubsystem(
210 SupplyChain supplyChain,
211 ICollection<ValidationIssue> issues)
213 ValidateIdentifiedCollection(
221 var requirementKeys =
222 new HashSet<string>(StringComparer.Ordinal);
232 $
"supplyChain.componentRequirements[{index}]";
235 $
"{requirement.ParentItemId}:" +
236 $
"{requirement.ComponentItemId}";
238 if (!requirementKeys.Add(key))
244 "The same parent-item/component-item " +
245 "requirement is defined more than once.");
250 requirement.ParentItemId))
255 path +
".parentItemId",
256 $
"Item {requirement.ParentItemId} does not exist.");
261 requirement.ComponentItemId))
266 path +
".componentItemId",
267 $
"Item {requirement.ComponentItemId} does not exist.");
270 if (requirement.ParentItemId ==
271 requirement.ComponentItemId)
277 "An item cannot be its own component.");
280 if (requirement.Quantity <= 0)
286 "A component quantity must be strictly positive.");
290 ValidateBillOfMaterialsCycles(
295 private static void ValidateBillOfMaterialsCycles(
296 SupplyChain supplyChain,
297 ICollection<ValidationIssue> issues)
299 Dictionary<int, List<int>> graph =
300 supplyChain.ComponentRequirements
303 requirement.ParentItemId)
309 requirement.ComponentItemId)
312 var states =
new Dictionary<int, int>();
314 foreach (
int itemId
in supplyChain.
Items.Select(
325 "supplyChain.componentRequirements",
326 "The bill of materials contains at least " +
327 "one circular dependency.");
334 private static bool ContainsCycle(
336 IReadOnlyDictionary<
int, List<int>> graph,
337 IDictionary<int, int> states)
339 if (states.TryGetValue(itemId, out
int state))
346 if (graph.TryGetValue(
348 out List<int>? components))
350 foreach (
int componentId
in components)
367 private static void ValidatePhysicalSubsystem(
368 SupplyChain supplyChain,
369 ICollection<ValidationIssue> issues)
371 ValidateIdentifiedCollection(
375 "supplyChain.plants",
379 ValidateIdentifiedCollection(
381 warehouse => warehouse.Id,
382 warehouse => warehouse.Name,
383 "supplyChain.standaloneWarehouses",
384 "standalone warehouse",
387 ValidateIdentifiedCollection(
389 supplier => supplier.Id,
390 supplier => supplier.Name,
391 "supplyChain.suppliers",
395 ValidateIdentifiedCollection(
398 center => center.Name,
399 "supplyChain.distributionCenters",
400 "distribution center",
403 ValidateIdentifiedCollection(
405 resource => resource.Id,
406 resource => resource.Name,
407 "supplyChain.transportResources",
408 "transport resource",
411 foreach (Plant plant
in supplyChain.
Plants)
414 $
"supplyChain.plants[id={plant.Id}]";
421 plantPath +
".workCenters",
422 "A plant must contain at least one work center.");
425 ValidateIdentifiedCollection(
427 workCenter => workCenter.Id,
428 workCenter => workCenter.Name,
429 plantPath +
".workCenters",
435 var seenItemIds =
new HashSet<int>();
439 string path = $
"{plantPath}.manufacturedItemIds[{index}]";
440 if (itemId <= 0 || !ItemExists(supplyChain, itemId))
442 AddError(issues,
"PHY002", path,
443 $
"Manufactured item {itemId} must identify an existing item.");
445 else if (!seenItemIds.Add(itemId))
447 AddError(issues,
"PHY003", path,
448 $
"Manufactured item {itemId} is declared more than once.");
453 ValidateWarehouseDecisionModel(
455 plantPath +
".warehouse",
459 foreach (WorkCenter workCenter
462 ValidateWorkCenterDecisionModel(
465 $
".workCenters[id={workCenter.Id}]",
474 ValidateWarehouseDecisionModel(
476 "supplyChain.standaloneWarehouses" +
477 $
"[id={warehouse.Id}]",
482 foreach (TransportResource resource
486 "supplyChain.transportResources" +
487 $
"[id={resource.Id}]";
489 ValidatePhysicalFleet(supplyChain, resource.
PhysicalFleet, path +
".physicalFleet", issues);
490 ValidateTransportResourceDecisionModel(
497 var laneIds =
new HashSet<int>();
498 var laneKeys =
new HashSet<string>(StringComparer.Ordinal);
501 string path = $
"supplyChain.transportLanes[id={lane.Id}]";
502 if (lane.
Id < 0 || !laneIds.Add(lane.
Id)) AddError(issues,
"TRN005", path,
"Invalid or duplicate lane identifier.");
503 ValidateWarehouseReference(supplyChain, lane.
Origin, path +
".origin", issues);
504 ValidateWarehouseReference(supplyChain, lane.
Destination, path +
".destination", issues);
505 if (SameWarehouse(lane.
Origin, lane.
Destination)) AddError(issues,
"TRN002", path,
"The origin and destination warehouses must be different.");
506 if (!laneKeys.Add(WarehouseKey(lane.
Origin) +
"->" + WarehouseKey(lane.
Destination)))
507 AddError(issues,
"TRN004", path,
"Duplicate directed transport lane.");
509 var assignments =
new HashSet<(int, int)>();
512 string path = $
"supplyChain.transportAssignments[lane={assignment.LaneId},resource={assignment.TransportResourceId}]";
514 AddError(issues,
"TRN006", path,
"An assignment references a missing lane or resource.");
516 AddError(issues,
"TRN007", path,
"Duplicate lane-resource assignment.");
517 if (assignment.
LeadTime < 0) AddError(issues,
"TRN003", path,
"A transport lead time cannot be negative.");
520 AddError(issues,
"TRN008",
"supplyChain.transportFormatVersion",
"Unsupported transport format version.");
523 private static void ValidatePhysicalFleet(SupplyChain chain, PhysicalTransportFleet? fleet,
string path, ICollection<ValidationIssue> issues)
529 AddError(issues,
"FLEET001", path,
"Le nombre de véhicules et la capacité unitaire doivent être positifs.");
531 var sites =
new HashSet<string>(StringComparer.Ordinal);
535 if (row is
null || row.Count <= 0 || ResolveWarehouse(chain, row.Warehouse) is
null ||
536 !sites.Add(WarehouseKey(row.Warehouse)))
537 AddError(issues,
"FLEET002", path,
"Utilisez des entrepôts existants et distincts pour la répartition initiale.");
538 total += row?.Count ?? 0;
541 AddError(issues,
"FLEET003", path,
"Le total de la répartition initiale doit égaler la taille de la flotte.");
543 var movements =
new HashSet<(string, string)>();
546 if (row is
null || row.Duration <= 0 || row.CostPerVehicle < 0 || SameWarehouse(row.Origin, row.Destination) ||
547 ResolveWarehouse(chain, row.Origin) is
null || ResolveWarehouse(chain, row.Destination) is
null ||
548 !movements.Add((WarehouseKey(row.Origin), WarehouseKey(row.Destination))))
549 AddError(issues,
"FLEET004", path,
"Chaque déplacement à vide doit relier deux entrepôts distincts, avec une durée positive et sans doublon.");
556 ArgumentNullException.ThrowIfNull(supplyChain);
557 var issues =
new List<ValidationIssue>();
565 private static void ValidateProductionRoutings(
567 ICollection<ValidationIssue> issues)
569 var routingIds =
new HashSet<int>();
579 $
"supplyChain.productionRoutings[{index}]";
587 "A production-routing identifier must be " +
588 "strictly positive.");
591 if (!routingIds.Add(routing.
Id))
597 $
"Production-routing identifier {routing.Id} " +
609 $
"Item {routing.ItemId} does not exist.");
623 $
"Plant {routing.PlantId} does not exist.");
629 AddError(issues,
"ROU011", path +
".itemId",
630 $
"Item {routing.ItemId} is not declared as manufactured by plant {routing.PlantId}.");
639 "A production lead time cannot be negative.");
647 path +
".workCenters",
648 "A production routing must use at least " +
653 new HashSet<string>(StringComparer.Ordinal);
655 foreach (WorkCenterReference reference
659 WorkCenterKey(reference);
661 if (!workCenterKeys.Add(key))
666 path +
".workCenters",
667 "The same work center is referenced more " +
668 "than once by the routing.");
676 path +
".workCenters",
677 "Every referenced work center must belong " +
678 "to the routing plant.");
681 if (ResolveWorkCenter(
688 path +
".workCenters",
689 $
"Work center {key} does not exist.");
692 bool characteristicExists =
695 characteristic.ItemId ==
698 characteristic.WorkCenter,
701 if (!characteristicExists)
706 path +
".workCenters",
707 $
"No production characteristic is defined " +
708 $
"for item {routing.ItemId} and work " +
713 ValidatePlanningHorizon(
723 private static void ValidateProductionCharacteristics(
724 SupplyChain supplyChain,
725 ICollection<ValidationIssue> issues)
728 new HashSet<string>(StringComparer.Ordinal);
735 ProductionCharacteristic characteristic =
739 "supplyChain.productionCharacteristics" +
750 $
"Item {characteristic.ItemId} does not exist.");
758 path +
".workCenter",
759 "A production characteristic must reference " +
766 $
"{characteristic.ItemId}:" +
775 "A production characteristic is duplicated " +
776 "for this item and work center.");
779 WorkCenter? workCenter =
784 if (workCenter is
null)
789 path +
".workCenter",
790 "The referenced work center does not exist.");
792 ValidatePlanningHorizon(
802 private static void ValidateInventories(
803 SupplyChain supplyChain,
804 ICollection<ValidationIssue> issues)
807 new HashSet<string>(StringComparer.Ordinal);
813 Inventory inventory =
817 $
"supplyChain.inventories[{index}]";
827 $
"Item {inventory.ItemId} does not exist.");
830 ValidateWarehouseReference(
837 $
"{inventory.ItemId}:" +
846 "An inventory is duplicated for this item " +
856 path +
".initialInventory",
857 "Initial inventory must be finite and " +
866 path +
".inventoryBalanceRule",
867 "Every inventory must contain an inventory-" +
877 "The dependencies between the inventory " +
878 "constraints and costs are invalid.");
881 Warehouse? warehouse =
887 inventory.UnitCapacityConsumption is not
null ||
888 inventory.SetupTime is not
null;
890 bool hasApplicableCapacity =
891 inventory.CapacityConstraint is not
null ||
894 if (usesCapacity && !hasApplicableCapacity)
900 "Unit-capacity consumption or setup time is " +
901 "defined, but neither the inventory nor its " +
902 "warehouse has a capacity constraint.");
905 ValidatePlanningHorizon(
915 private static void ValidateTransportCharacteristics(
916 SupplyChain supplyChain,
917 ICollection<ValidationIssue> issues)
920 new HashSet<string>(StringComparer.Ordinal);
927 TransportCharacteristic characteristic =
931 "supplyChain.transportCharacteristics" +
942 $
"Item {characteristic.ItemId} does not exist.");
945 TransportResource? resource =
946 FindTransportResource(
950 if (resource is
null)
955 path +
".transportResourceId",
956 "The referenced transport resource does not exist.");
960 $
"{characteristic.ItemId}:" +
961 $
"{characteristic.TransportResourceId}";
969 "A transport characteristic is duplicated " +
970 "for this item and transport resource.");
979 path +
".additionalCapacity",
980 "Item-specific additional capacity requires " +
981 "an item-specific capacity constraint.");
990 path +
".additionalCapacityCost",
991 "An additional-capacity cost requires an " +
992 "additional-capacity constraint.");
996 characteristic.UnitCapacityConsumption is not
null ||
997 characteristic.SetupTime is not
null;
999 bool hasApplicableCapacity =
1000 characteristic.CapacityConstraint is not
null ||
1002 (resource?.
PhysicalFleet is not
null && characteristic.SetupTime is
null);
1004 if (resource?.PhysicalFleet is not
null &&
1006 consumption.Values.Any(value => value <= 0.0))
1011 path +
".unitCapacityConsumption",
1012 "A physical fleet requires strictly positive unit-capacity consumption.");
1015 if (usesCapacity && !hasApplicableCapacity)
1021 "Unit-capacity consumption or setup time is " +
1022 "defined, but neither the characteristic nor " +
1023 "the transport resource has a capacity constraint.");
1026 ValidatePlanningHorizon(
1036 private static void ValidateDemands(
1037 SupplyChain supplyChain,
1038 ICollection<ValidationIssue> issues)
1041 new HashSet<string>(StringComparer.Ordinal);
1044 index < supplyChain.
Demands.Count;
1051 $
"supplyChain.demands[{index}]";
1061 $
"Item {demand.ItemId} does not exist.");
1064 if (!DistributionCenterExists(
1071 path +
".distributionCenterId",
1072 "The referenced distribution center does not exist.");
1076 $
"{demand.DistributionCenterId}:" +
1085 "A demand is duplicated for this distribution " +
1086 "center and item.");
1094 path +
".quantities",
1095 "Demand quantities must be finite and non-negative.");
1104 path +
".quantities",
1105 "The demand time series does not use the global " +
1106 "planning horizon.");
1111 private static void ValidateDistributionCenterSourcings(
1112 SupplyChain supplyChain,
1113 ICollection<ValidationIssue> issues)
1116 new HashSet<string>(StringComparer.Ordinal);
1123 DistributionCenterSourcing sourcing =
1127 "supplyChain.distributionCenterSourcings" +
1138 $
"Item {sourcing.ItemId} does not exist.");
1141 if (!DistributionCenterExists(
1148 path +
".distributionCenterId",
1149 "The referenced distribution center does not exist.");
1152 ValidateWarehouseReference(
1155 path +
".warehouse",
1159 $
"{sourcing.DistributionCenterId}:" +
1160 $
"{sourcing.ItemId}:" +
1169 "A sourcing relationship is duplicated.");
1172 bool inventoryExists =
1181 if (!inventoryExists)
1186 path +
".warehouse",
1187 "The sourcing relationship references an " +
1188 "item-warehouse inventory that does not exist.");
1205 "No demand is defined for this distribution " +
1206 "center and item.");
1215 "A backlog or shortage cost is defined without " +
1216 "the corresponding constraint.");
1219 ValidatePlanningHorizon(
1229 private static void ValidateSupplierDeliveries(
1230 SupplyChain supplyChain,
1231 ICollection<ValidationIssue> issues)
1234 new HashSet<string>(StringComparer.Ordinal);
1240 SupplierDelivery delivery =
1244 $
"supplyChain.supplierDeliveries[{index}]";
1246 if (!SupplierExists(
1253 path +
".supplierId",
1254 "The referenced supplier does not exist.");
1265 $
"Item {delivery.ItemId} does not exist.");
1268 ValidateWarehouseReference(
1271 path +
".warehouse",
1275 $
"{delivery.SupplierId}:" +
1276 $
"{delivery.ItemId}:" +
1285 "A supplier-delivery relationship is duplicated.");
1294 "A supplier-delivery lead time cannot be negative.");
1297 bool inventoryExists =
1306 if (!inventoryExists)
1311 path +
".warehouse",
1312 "The delivery references an item-warehouse " +
1313 "inventory that does not exist.");
1323 path +
".purchasePrice",
1324 "The purchase-price time series does not use " +
1325 "the global planning horizon.");
1330 private static void ValidateWarehouseDecisionModel(
1331 Warehouse warehouse,
1334 ICollection<ValidationIssue> issues)
1342 "The warehouse additional-capacity configuration " +
1346 ValidatePlanningHorizon(
1355 private static void ValidateWorkCenterDecisionModel(
1356 WorkCenter workCenter,
1359 ICollection<ValidationIssue> issues)
1367 "The work-center additional-capacity configuration " +
1371 ValidatePlanningHorizon(
1380 private static void ValidateTransportResourceDecisionModel(
1381 TransportResource resource,
1384 ICollection<ValidationIssue> issues)
1392 "The transport-resource additional-capacity " +
1393 "configuration is invalid.");
1396 ValidatePlanningHorizon(
1405 private static void ValidatePlanningHorizon(
1407 bool hasActiveParameters,
1409 bool internallyConsistent,
1411 ICollection<ValidationIssue> issues)
1413 if (!internallyConsistent)
1419 "The active time series do not all use the same " +
1420 "planning horizon.");
1423 if (hasActiveParameters &&
1424 localHorizon != globalHorizon)
1430 $
"The local planning horizon ({localHorizon}) " +
1431 $
"does not match the global planning horizon " +
1432 $
"({globalHorizon}).");
1436 private static void ValidateWarehouseReference(
1437 SupplyChain supplyChain,
1438 WarehouseReference? reference,
1440 ICollection<ValidationIssue> issues)
1442 if (reference is
null)
1448 "A warehouse reference is required.");
1458 path +
".referenceId",
1459 "A warehouse reference identifier must be " +
1460 "strictly positive.");
1463 if (ResolveWarehouse(
1471 $
"Warehouse {WarehouseKey(reference)} does not exist.");
1475 private static void ValidateIdentifiedCollection<T>(
1476 IEnumerable<T> values,
1477 Func<T, int> idSelector,
1478 Func<T, string> nameSelector,
1481 ICollection<ValidationIssue> issues)
1484 var identifiers =
new HashSet<int>();
1488 foreach (T value
in values)
1490 int id = idSelector(value);
1491 string name = nameSelector(value);
1502 $
"A {entityName} identifier must be " +
1503 "strictly positive.");
1506 if (!identifiers.Add(
id))
1512 $
"{entityName} identifier {id} is duplicated.");
1515 if (
string.IsNullOrWhiteSpace(name))
1520 entityPath +
".name",
1521 $
"A {entityName} name is required.");
1528 private static bool ItemExists(
1529 SupplyChain supplyChain,
1532 return supplyChain.
Items.Any(
1533 item => item.Id == itemId);
1536 private static bool SupplierExists(
1537 SupplyChain supplyChain,
1541 supplier => supplier.Id == supplierId);
1544 private static bool DistributionCenterExists(
1545 SupplyChain supplyChain,
1546 int distributionCenterId)
1550 center.Id == distributionCenterId);
1553 private static Plant? FindPlant(
1554 SupplyChain supplyChain,
1557 return supplyChain.
Plants.FirstOrDefault(
1558 plant => plant.
Id == plantId);
1561 private static TransportResource? FindTransportResource(
1562 SupplyChain supplyChain,
1563 int transportResourceId)
1567 resource.
Id == transportResourceId);
1570 private static WorkCenter? ResolveWorkCenter(
1571 SupplyChain supplyChain,
1572 WorkCenterReference? reference)
1574 if (reference is
null)
1590 private static Warehouse? ResolveWarehouse(
1591 SupplyChain supplyChain,
1592 WarehouseReference? reference)
1594 if (reference is
null)
1600 return reference.Kind
switch
1602 WarehouseReferenceKind.StandaloneWarehouse =>
1603 supplyChain.StandaloneWarehouses
1609 WarehouseReferenceKind.PlantWarehouse =>
1621 private static bool SameWarehouse(
1622 WarehouseReference? first,
1623 WarehouseReference? second)
1626 if (first is
null || second is
null)
1628 return first is
null && second is
null;
1631 return first.Kind == second.Kind &&
1635 private static bool SameWorkCenter(
1636 WorkCenterReference? first,
1637 WorkCenterReference? second)
1640 if (first is
null || second is
null)
1642 return first is
null && second is
null;
1645 return first.PlantId == second.PlantId &&
1649 private static string WarehouseKey(
1650 WarehouseReference? reference)
1652 return reference is
null
1654 : $
"{reference.Kind}:{reference.ReferenceId}";
1657 private static string WorkCenterKey(
1658 WorkCenterReference? reference)
1660 return reference is
null
1662 : $
"{reference.PlantId}:" +
1663 $
"{reference.WorkCenterId}";
1666 private static void AddError(
1667 ICollection<ValidationIssue> issues,
1680 private static void AddWarning(
1681 ICollection<ValidationIssue> issues,