LotSizingDataModel.Core 2.1.0
Core domain model, shared abstractions and XML-serializable entities.
Loading...
Searching...
No Matches
Plant.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Xml.Serialization;
6
8
9/// <summary>
10/// Represents a production plant.
11///
12/// A plant owns exactly one plant warehouse and one or more work centers.
13///
14/// Corresponds to the UML class "Usine".
15/// </summary>
16[Serializable]
17[XmlType(TypeName = "plant")]
18public sealed partial class Plant : IdentifiedEntity
19{
20 private bool _manufacturedItemIdsDeclared;
21 private List<int> _manufacturedItemIds = new();
22 private PlantWarehouse _warehouse = new();
23 private List<WorkCenter> _workCenters = new();
24
25 /// <summary>
26 /// Initializes an empty plant.
27 ///
28 /// This public parameterless constructor is required
29 /// by <see cref="XmlSerializer"/>.
30 /// </summary>
31 public Plant()
32 {
33 }
34
35 /// <summary>
36 /// Initializes a plant with an identifier, a name
37 /// and its associated warehouse.
38 /// </summary>
39 /// <param name="id">
40 /// Numerical identifier corresponding to u in the source model.
41 /// </param>
42 /// <param name="name">
43 /// Plant name.
44 /// </param>
45 /// <param name="warehouse">
46 /// Warehouse attached to the plant.
47 /// </param>
48 public Plant(
49 int id,
50 string name,
51 PlantWarehouse warehouse)
52 {
53 // Initialize all plant properties
54 Id = id;
55 Name = name;
56 Warehouse = warehouse; // Validation occurs in the setter
57 }
58
59 /// <summary>
60 /// Gets or sets the warehouse attached to the plant.
61 ///
62 /// The source UML model defines exactly one non-standalone
63 /// warehouse for each plant.
64 /// </summary>
65 [XmlElement("warehouse")]
67 {
68 get => _warehouse;
69 set
70 {
71 // Validate that the warehouse is not null
72 ArgumentNullException.ThrowIfNull(value);
73
74 // Update the backing field and notify property change if value differs
75 SetProperty(
76 ref _warehouse,
77 value);
78 }
79 }
80
81 /// <summary>
82 /// Gets or sets the work centers belonging to the plant.
83 ///
84 /// The source UML cardinality is 1..*.
85 /// This cardinality will be checked by the global validator.
86 /// </summary>
87 [XmlArray("workCenters")]
88 [XmlArrayItem("workCenter")]
89 public List<WorkCenter> WorkCenters
90 {
91 get => _workCenters;
92 set
93 {
94 // Ensure the list is never null
95 List<WorkCenter> newValue =
96 value ?? new List<WorkCenter>();
97
98 // Avoid unnecessary notifications if the reference is the same
99 if (ReferenceEquals(_workCenters, newValue))
100 {
101 return;
102 }
103
104 _workCenters = newValue;
105
107 }
108 }
109
110 /// <summary>
111 /// Gets or sets the identifiers of the items the plant is declared to
112 /// manufacture. An item may be declared before any routing is defined.
113 /// </summary>
114 [XmlArray("manufacturedItemIds")]
115 [XmlArrayItem("itemId")]
116 public List<int> ManufacturedItemIds
117 {
118 get => _manufacturedItemIds;
119 set => SetProperty(ref _manufacturedItemIds, value ?? new List<int>());
120 }
121
122 /// <summary>
123 /// Gets or sets whether the manufactured-item list was explicitly declared.
124 /// An empty declared list differs from an absent legacy declaration.
125 /// A nonempty list always constitutes a declaration.
126 /// </summary>
127 [XmlIgnore]
129 {
130 get => _manufacturedItemIdsDeclared || ManufacturedItemIds.Count > 0;
131 set => SetProperty(ref _manufacturedItemIdsDeclared, value);
132 }
133
134 /// <summary>
135 /// Adds a work center to the plant.
136 /// </summary>
137 /// <param name="workCenter">
138 /// Work center to add.
139 /// </param>
140 /// <exception cref="InvalidOperationException">
141 /// Thrown when another work center already uses the same
142 /// strictly positive identifier in this plant.
143 /// </exception>
144 public void AddWorkCenter(WorkCenter workCenter)
145 {
146 ArgumentNullException.ThrowIfNull(workCenter);
147
148 // Check if a work center with the same strictly positive ID already exists
149 bool identifierAlreadyUsed =
150 workCenter.Id > 0 &&
151 _workCenters.Any(
152 current => current.Id == workCenter.Id);
153
154 if (identifierAlreadyUsed)
155 {
156 throw new InvalidOperationException(
157 $"A work center with identifier {workCenter.Id} " +
158 $"already exists in plant {Id}.");
159 }
160
161 // Add the work center and notify property change
162 _workCenters.Add(workCenter);
163
165 }
166
167 /// <summary>
168 /// Removes a work center from the plant.
169 /// </summary>
170 /// <param name="workCenter">
171 /// Work center to remove.
172 /// </param>
173 /// <returns>
174 /// <see langword="true"/> when the work center was found
175 /// and removed; otherwise, <see langword="false"/>.
176 /// </returns>
177 public bool RemoveWorkCenter(WorkCenter workCenter)
178 {
179 ArgumentNullException.ThrowIfNull(workCenter);
180
181 // Attempt to remove the work center from the collection
182 bool removed = _workCenters.Remove(workCenter);
183
184 // Notify property change only if removal was successful
185 if (removed)
186 {
188 }
189
190 return removed;
191 }
192
193 /// <summary>
194 /// Finds a work center by its identifier.
195 /// </summary>
196 /// <param name="workCenterId">
197 /// Identifier of the requested work center.
198 /// </param>
199 /// <returns>
200 /// The matching work center, or <see langword="null"/>
201 /// when no match exists.
202 /// </returns>
203 public WorkCenter? FindWorkCenter(int workCenterId)
204 {
205 return _workCenters.FirstOrDefault(
206 workCenter => workCenter.Id == workCenterId);
207 }
208}
int Id
Gets or sets the numerical identifier of the entity.
IdentifiedEntity()
Initializes a new instance of the IdentifiedEntity class.
virtual void OnPropertyChanged([CallerMemberName] string? propertyName=null)
Raises the PropertyChanged event.
string Name
Gets or sets the entity name.
List< WorkCenter > WorkCenters
Gets or sets the work centers belonging to the plant.
Definition Plant.cs:90
List< int > ManufacturedItemIds
Gets or sets the identifiers of the items the plant is declared to manufacture. An item may be declar...
Definition Plant.cs:117
void AddWorkCenter(WorkCenter workCenter)
Adds a work center to the plant.
Definition Plant.cs:144
Plant(int id, string name, PlantWarehouse warehouse)
Initializes a plant with an identifier, a name and its associated warehouse.
Definition Plant.cs:48
bool ManufacturedItemIdsSpecified
Gets or sets whether the manufactured-item list was explicitly declared. An empty declared list diffe...
Definition Plant.cs:129
Plant()
Initializes an empty plant.
Definition Plant.cs:31
bool RemoveWorkCenter(WorkCenter workCenter)
Removes a work center from the plant.
Definition Plant.cs:177
WorkCenter? FindWorkCenter(int workCenterId)
Finds a work center by its identifier.
Definition Plant.cs:203
PlantWarehouse Warehouse
Gets or sets the warehouse attached to the plant.
Definition Plant.cs:67
Represents a warehouse attached to a plant.
Represents a production work center belonging to a plant.
Definition WorkCenter.cs:15