LotSizingDataModel.Core 2.1.0
Core domain model, shared abstractions and XML-serializable entities.
Loading...
Searching...
No Matches
ProductionRouting.cs
Go to the documentation of this file.
1using System;
2using System.ComponentModel;
3using System.Collections.Generic;
4using System.Linq;
5using System.Xml.Serialization;
8
10
11/// <summary>
12/// Represents a production routing used to manufacture an item
13/// in a plant through one or more work centers.
14///
15/// Corresponds to the UML class "Gamme".
16/// </summary>
17[Serializable]
18[XmlType(TypeName = "productionRouting")]
19public sealed partial class ProductionRouting : ModelObject
20{
21 private int _id;
22 private int _itemId;
23 private int _plantId;
24 private int _leadTime;
25 private bool _operationOrderConfirmed;
26 private List<WorkCenterReference> _workCenters = new();
27
28 /// <summary>
29 /// Initializes an empty production routing.
30 ///
31 /// This public parameterless constructor is required
32 /// by <see cref="XmlSerializer"/>.
33 /// </summary>
35 {
36 }
37
38 /// <summary>
39 /// Initializes a production routing.
40 /// </summary>
41 /// <param name="id">
42 /// Routing identifier corresponding to g in the source model.
43 /// </param>
44 /// <param name="itemId">
45 /// Identifier of the manufactured item.
46 /// </param>
47 /// <param name="plantId">
48 /// Identifier of the plant using this routing.
49 /// </param>
50 /// <param name="leadTime">
51 /// Production lead time, expressed in planning periods.
52 /// Corresponds to l[g] in the source model.
53 /// </param>
55 int id,
56 int itemId,
57 int plantId,
58 int leadTime)
59 {
60 // Initialize all production routing properties (validation occurs in setters)
61 Id = id;
62 ItemId = itemId;
63 PlantId = plantId;
64 LeadTime = leadTime;
65 }
66
67 /// <summary>
68 /// Gets or sets the routing identifier.
69 ///
70 /// Corresponds to g in the source model.
71 /// </summary>
72 [XmlAttribute("id")]
73 public int Id
74 {
75 get => _id;
76 set
77 {
78 // Validate that the identifier is non-negative
79 if (value < 0)
80 {
81 throw new ArgumentOutOfRangeException(
82 nameof(value),
83 value,
84 "The production-routing identifier cannot be negative.");
85 }
86
87 // Update the backing field and notify property change if value differs
88 SetProperty(ref _id, value);
89 }
90 }
91
92 /// <summary>
93 /// Gets or sets the identifier of the item manufactured
94 /// through this routing.
95 /// </summary>
96 [XmlAttribute("itemId")]
97 public int ItemId
98 {
99 get => _itemId;
100 set
101 {
102 // Validate that the item identifier is non-negative
103 if (value < 0)
104 {
105 throw new ArgumentOutOfRangeException(
106 nameof(value),
107 value,
108 "The item identifier cannot be negative.");
109 }
110
111 // Update the backing field and notify property change if value differs
112 SetProperty(ref _itemId, value);
113 }
114 }
115
116 /// <summary>
117 /// Gets or sets the identifier of the plant
118 /// in which this routing is available.
119 /// </summary>
120 [XmlAttribute("plantId")]
121 public int PlantId
122 {
123 get => _plantId;
124 set
125 {
126 // Validate that the plant identifier is non-negative
127 if (value < 0)
128 {
129 throw new ArgumentOutOfRangeException(
130 nameof(value),
131 value,
132 "The plant identifier cannot be negative.");
133 }
134
135 // Update and notify dependent property if value changed
136 if (SetProperty(ref _plantId, value))
137 {
139 }
140 }
141 }
142
143 /// <summary>
144 /// Gets or sets the production lead time.
145 ///
146 /// Corresponds to l[g] in the source model.
147 /// The value is expressed as a number of planning periods.
148 /// </summary>
149 [XmlAttribute("leadTime")]
150 public int LeadTime
151 {
152 get => _leadTime;
153 set
154 {
155 // Validate that the lead time is non-negative
156 if (value < 0)
157 {
158 throw new ArgumentOutOfRangeException(
159 nameof(value),
160 value,
161 "The production lead time cannot be negative.");
162 }
163
164 // Update the backing field and notify property change if value differs
165 SetProperty(ref _leadTime, value);
166 }
167 }
168
169 /// <summary>
170 /// Gets or sets whether the operator confirmed that the work-center
171 /// references are listed in execution order. In an older instance,
172 /// the order of multiple references is not assumed to be confirmed.
173 /// </summary>
174 [DefaultValue(false)]
175 [XmlAttribute("operationOrderConfirmed")]
177 {
178 get => _operationOrderConfirmed;
179 set => SetProperty(ref _operationOrderConfirmed, value);
180 }
181
182 /// <summary>
183 /// Gets or sets the work centers used by this routing.
184 ///
185 /// The UML cardinality is 1..*.
186 /// The non-empty cardinality is checked by the global validator
187 /// after object construction or XML deserialization.
188 /// </summary>
189 [XmlArray("workCenters")]
190 [XmlArrayItem("workCenterReference")]
191 public List<WorkCenterReference> WorkCenters
192 {
193 get => _workCenters;
194 set
195 {
196 // Ensure the list is never null
197 List<WorkCenterReference> newValue =
198 value ?? new List<WorkCenterReference>();
199
200 // Avoid unnecessary notifications if the reference is the same
201 if (ReferenceEquals(_workCenters, newValue))
202 {
203 return;
204 }
205
206 _workCenters = newValue;
207
208 // Notify both properties that depend on work centers
211 }
212 }
213
214 /// <summary>
215 /// Gets a value indicating whether all referenced work centers
216 /// belong to the plant associated with this routing.
217 ///
218 /// This property is calculated and is therefore not serialized.
219 /// </summary>
220 [XmlIgnore]
222 _workCenters.All(
223 reference =>
224 reference.PlantId == PlantId);
225
226 /// <summary>
227 /// Adds a work center to the routing.
228 /// </summary>
229 /// <param name="workCenter">
230 /// Reference to the work center to add.
231 /// </param>
232 /// <exception cref="InvalidOperationException">
233 /// Thrown when the referenced work center belongs to another plant
234 /// or is already present in the routing.
235 /// </exception>
236 public void AddWorkCenter(
237 WorkCenterReference workCenter)
238 {
239 ArgumentNullException.ThrowIfNull(workCenter);
240
241 // Validate that the work center belongs to the same plant
242 if (PlantId > 0 &&
243 workCenter.PlantId > 0 &&
244 workCenter.PlantId != PlantId)
245 {
246 throw new InvalidOperationException(
247 $"Work center {workCenter.WorkCenterId} belongs to plant " +
248 $"{workCenter.PlantId}, whereas routing {Id} belongs " +
249 $"to plant {PlantId}.");
250 }
251
252 // Check if the work center is already in the routing
253 bool alreadyExists = _workCenters.Any(
254 current =>
255 current.RefersToSameWorkCenter(workCenter));
256
257 if (alreadyExists)
258 {
259 throw new InvalidOperationException(
260 $"Work center {workCenter.WorkCenterId} of plant " +
261 $"{workCenter.PlantId} is already assigned " +
262 $"to routing {Id}.");
263 }
264
265 // Add the work center and notify property changes
266 _workCenters.Add(workCenter);
267
270 }
271
272 /// <summary>
273 /// Adds a work center by its identifier.
274 ///
275 /// The work center is assumed to belong to the plant
276 /// associated with this routing.
277 /// </summary>
278 /// <param name="workCenterId">
279 /// Identifier of the work center inside the plant.
280 /// </param>
281 public void AddWorkCenter(int workCenterId)
282 {
283 // Create a reference for the work center in this routing's plant
286 PlantId,
287 workCenterId));
288 }
289
290 /// <summary>
291 /// Removes a work-center reference from the routing.
292 /// </summary>
293 public bool RemoveWorkCenter(
294 WorkCenterReference workCenter)
295 {
296 ArgumentNullException.ThrowIfNull(workCenter);
297
298 // Find the matching work center reference in the collection
299 WorkCenterReference? existingReference =
300 _workCenters.FirstOrDefault(
301 current =>
302 current.RefersToSameWorkCenter(workCenter));
303
304 if (existingReference is null)
305 {
306 return false;
307 }
308
309 // Remove the reference and notify property changes if successful
310 bool removed = _workCenters.Remove(existingReference);
311
312 if (removed)
313 {
317 }
318
319 return removed;
320 }
321
322 /// <summary>
323 /// Removes a work center by its identifier.
324 /// </summary>
325 public bool RemoveWorkCenter(int workCenterId)
326 {
327 // Find the work center reference matching the plant and work center ID
328 WorkCenterReference? existingReference =
329 _workCenters.FirstOrDefault(
330 reference =>
331 reference.PlantId == PlantId &&
332 reference.WorkCenterId == workCenterId);
333
334 if (existingReference is null)
335 {
336 return false;
337 }
338
339 // Delegate to the main remove method
340 return RemoveWorkCenter(existingReference);
341 }
342
343 /// <summary>
344 /// Determines whether this routing uses a given work center.
345 /// </summary>
346 public bool UsesWorkCenter(int workCenterId)
347 {
348 // Check if any work center reference matches the plant and work center ID
349 return _workCenters.Any(
350 reference =>
351 reference.PlantId == PlantId &&
352 reference.WorkCenterId == workCenterId);
353 }
354}
Base class for model objects that notify listeners when one of their properties changes.
virtual void OnPropertyChanged([CallerMemberName] string? propertyName=null)
Raises the PropertyChanged event.
Represents a serializable reference to a work center.
int PlantId
Gets or sets the identifier of the plant owning the referenced work center.
bool HasConsistentWorkCenterReferences
Gets a value indicating whether all referenced work centers belong to the plant associated with this ...
List< WorkCenterReference > WorkCenters
Gets or sets the work centers used by this routing.
ProductionRouting(int id, int itemId, int plantId, int leadTime)
Initializes a production routing.
bool UsesWorkCenter(int workCenterId)
Determines whether this routing uses a given work center.
ProductionRouting()
Initializes an empty production routing.
int ItemId
Gets or sets the identifier of the item manufactured through this routing.
bool OperationOrderConfirmed
Gets or sets whether the operator confirmed that the work-center references are listed in execution o...
bool RemoveWorkCenter(int workCenterId)
Removes a work center by its identifier.
bool RemoveWorkCenter(WorkCenterReference workCenter)
Removes a work-center reference from the routing.
int LeadTime
Gets or sets the production lead time.
void AddWorkCenter(int workCenterId)
Adds a work center by its identifier.
int PlantId
Gets or sets the identifier of the plant in which this routing is available.
void AddWorkCenter(WorkCenterReference workCenter)
Adds a work center to the routing.