LotSizingDataModel.Core 2.0.1
Core domain model, shared abstractions and XML-serializable entities.
Loading...
Searching...
No Matches
SupplyChain.Transport.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Xml.Serialization;
7
9
10public sealed partial class SupplyChain
11{
12 [XmlAttribute("transportFormatVersion")]
13 public int TransportFormatVersion { get; set; } = 2;
14
15 [XmlArray("transportLanes"), XmlArrayItem("transportLane")]
16 public List<TransportLane> TransportLanes { get; } = new();
17
18 [XmlArray("transportAssignments"), XmlArrayItem("transportAssignment")]
19 public List<TransportAssignment> TransportAssignments { get; } = new();
20
21 public bool ShouldSerializeTransportFormatVersion() => TransportResources.Count > 0 || TransportLanes.Count > 0 || TransportAssignments.Count > 0;
22 public bool ShouldSerializeTransportLanes() => TransportLanes.Count > 0;
23 public bool ShouldSerializeTransportAssignments() => TransportAssignments.Count > 0;
24
26 TransportLanes.SingleOrDefault(l => SameTransportWarehouse(l.Origin, origin) && SameTransportWarehouse(l.Destination, destination));
27
28 public TransportAssignment? FindTransportAssignment(int laneId, int resourceId) =>
29 TransportAssignments.SingleOrDefault(a => a.LaneId == laneId && a.TransportResourceId == resourceId);
30
31 public IEnumerable<AssignedTransportLane> GetTransportLanes(int resourceId) =>
32 TransportAssignments.Where(a => a.TransportResourceId == resourceId)
33 .Select(a => new AssignedTransportLane(TransportLanes.Single(l => l.Id == a.LaneId), a));
34
36 {
37 ArgumentNullException.ThrowIfNull(lane);
38 if (lane.Id < 0) throw new ArgumentOutOfRangeException(nameof(lane));
39 var index = new SupplyChainIndex(this);
40 index.GetRequiredWarehouse(lane.Origin);
41 index.GetRequiredWarehouse(lane.Destination);
42 if (SameTransportWarehouse(lane.Origin, lane.Destination))
43 throw new InvalidOperationException("A transport lane must connect different warehouses.");
44 if (TransportLanes.Any(l => l.Id == lane.Id) || FindTransportLane(lane.Origin, lane.Destination) is not null)
45 throw new InvalidOperationException("Duplicate transport lane identifier or directed warehouse pair.");
46 TransportLanes.Add(lane);
47 OnPropertyChanged(nameof(TransportLanes));
48 }
49
51 {
52 ArgumentNullException.ThrowIfNull(assignment);
53 if (!TransportLanes.Any(l => l.Id == assignment.LaneId) ||
54 !TransportResources.Any(r => r.Id == assignment.TransportResourceId))
55 throw new InvalidOperationException("The assignment references a missing lane or resource.");
56 if (FindTransportAssignment(assignment.LaneId, assignment.TransportResourceId) is not null)
57 throw new InvalidOperationException("Duplicate lane-resource assignment.");
58 TransportAssignments.Add(assignment);
59 OnPropertyChanged(nameof(TransportAssignments));
60 }
61
62 public bool RemoveTransportAssignment(int laneId, int resourceId)
63 {
64 var assignment = FindTransportAssignment(laneId, resourceId);
65 if (assignment is null) return false;
66 TransportAssignments.Remove(assignment);
67 OnPropertyChanged(nameof(TransportAssignments));
68 return true;
69 }
70
71 public bool RemoveTransportLane(int laneId)
72 {
73 if (TransportAssignments.Any(a => a.LaneId == laneId))
74 throw new InvalidOperationException("Remove referencing assignments explicitly before removing the lane.");
75 var lane = TransportLanes.SingleOrDefault(l => l.Id == laneId);
76 if (lane is null) return false;
77 TransportLanes.Remove(lane);
78 OnPropertyChanged(nameof(TransportLanes));
79 return true;
80 }
81
82 public bool RemoveTransportResource(int resourceId)
83 {
84 if (TransportAssignments.Any(a => a.TransportResourceId == resourceId) ||
85 TransportCharacteristics.Any(c => c.TransportResourceId == resourceId))
86 throw new InvalidOperationException("Remove referencing assignments and characteristics explicitly before removing the resource.");
87 var resource = TransportResources.SingleOrDefault(r => r.Id == resourceId);
88 if (resource is null) return false;
89 TransportResources.Remove(resource);
90 OnPropertyChanged(nameof(TransportResources));
91 return true;
92 }
93
94 private static bool SameTransportWarehouse(WarehouseReference first, WarehouseReference second) =>
95 first.Kind == second.Kind && first.ReferenceId == second.ReferenceId;
96}
int Id
Gets or sets the numerical identifier of the entity.
Provides fast access to the entities contained in a supply chain.
A runtime join of the central lane and assignment; stores no copied business data.
An authorized lane-resource pair, with its delay in periods.
Represents a transport lane between two warehouses.
WarehouseReference Destination
Gets or sets the destination warehouse.
WarehouseReference Origin
Gets or sets the origin warehouse.
Represents a serializable reference to a warehouse.
int ReferenceId
Gets or sets the referenced identifier.
Represents the complete supply-chain data model.
List< TransportResource > TransportResources
Gets the transport resources in the supply chain.
IEnumerable< AssignedTransportLane > GetTransportLanes(int resourceId)
TransportLane? FindTransportLane(WarehouseReference origin, WarehouseReference destination)
List< TransportCharacteristic > TransportCharacteristics
Gets the item-transport-resource characteristics.
void AddTransportAssignment(TransportAssignment assignment)
TransportAssignment? FindTransportAssignment(int laneId, int resourceId)
List< TransportAssignment > TransportAssignments
bool RemoveTransportAssignment(int laneId, int resourceId)