LotSizingDataModel.Core 2.0.1
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 PlantWarehouse _warehouse = new();
21 private List<WorkCenter> _workCenters = new();
22
23 /// <summary>
24 /// Initializes an empty plant.
25 ///
26 /// This public parameterless constructor is required
27 /// by <see cref="XmlSerializer"/>.
28 /// </summary>
29 public Plant()
30 {
31 }
32
33 /// <summary>
34 /// Initializes a plant with an identifier, a name
35 /// and its associated warehouse.
36 /// </summary>
37 /// <param name="id">
38 /// Numerical identifier corresponding to u in the source model.
39 /// </param>
40 /// <param name="name">
41 /// Plant name.
42 /// </param>
43 /// <param name="warehouse">
44 /// Warehouse attached to the plant.
45 /// </param>
46 public Plant(
47 int id,
48 string name,
49 PlantWarehouse warehouse)
50 {
51 // Initialize all plant properties
52 Id = id;
53 Name = name;
54 Warehouse = warehouse; // Validation occurs in the setter
55 }
56
57 /// <summary>
58 /// Gets or sets the warehouse attached to the plant.
59 ///
60 /// The source UML model defines exactly one non-standalone
61 /// warehouse for each plant.
62 /// </summary>
63 [XmlElement("warehouse")]
65 {
66 get => _warehouse;
67 set
68 {
69 // Validate that the warehouse is not null
70 ArgumentNullException.ThrowIfNull(value);
71
72 // Update the backing field and notify property change if value differs
73 SetProperty(
74 ref _warehouse,
75 value);
76 }
77 }
78
79 /// <summary>
80 /// Gets or sets the work centers belonging to the plant.
81 ///
82 /// The source UML cardinality is 1..*.
83 /// This cardinality will be checked by the global validator.
84 /// </summary>
85 [XmlArray("workCenters")]
86 [XmlArrayItem("workCenter")]
87 public List<WorkCenter> WorkCenters
88 {
89 get => _workCenters;
90 set
91 {
92 // Ensure the list is never null
93 List<WorkCenter> newValue =
94 value ?? new List<WorkCenter>();
95
96 // Avoid unnecessary notifications if the reference is the same
97 if (ReferenceEquals(_workCenters, newValue))
98 {
99 return;
100 }
101
102 _workCenters = newValue;
103
105 }
106 }
107
108 /// <summary>
109 /// Adds a work center to the plant.
110 /// </summary>
111 /// <param name="workCenter">
112 /// Work center to add.
113 /// </param>
114 /// <exception cref="InvalidOperationException">
115 /// Thrown when another work center already uses the same
116 /// strictly positive identifier in this plant.
117 /// </exception>
118 public void AddWorkCenter(WorkCenter workCenter)
119 {
120 ArgumentNullException.ThrowIfNull(workCenter);
121
122 // Check if a work center with the same strictly positive ID already exists
123 bool identifierAlreadyUsed =
124 workCenter.Id > 0 &&
125 _workCenters.Any(
126 current => current.Id == workCenter.Id);
127
128 if (identifierAlreadyUsed)
129 {
130 throw new InvalidOperationException(
131 $"A work center with identifier {workCenter.Id} " +
132 $"already exists in plant {Id}.");
133 }
134
135 // Add the work center and notify property change
136 _workCenters.Add(workCenter);
137
139 }
140
141 /// <summary>
142 /// Removes a work center from the plant.
143 /// </summary>
144 /// <param name="workCenter">
145 /// Work center to remove.
146 /// </param>
147 /// <returns>
148 /// <see langword="true"/> when the work center was found
149 /// and removed; otherwise, <see langword="false"/>.
150 /// </returns>
151 public bool RemoveWorkCenter(WorkCenter workCenter)
152 {
153 ArgumentNullException.ThrowIfNull(workCenter);
154
155 // Attempt to remove the work center from the collection
156 bool removed = _workCenters.Remove(workCenter);
157
158 // Notify property change only if removal was successful
159 if (removed)
160 {
162 }
163
164 return removed;
165 }
166
167 /// <summary>
168 /// Finds a work center by its identifier.
169 /// </summary>
170 /// <param name="workCenterId">
171 /// Identifier of the requested work center.
172 /// </param>
173 /// <returns>
174 /// The matching work center, or <see langword="null"/>
175 /// when no match exists.
176 /// </returns>
177 public WorkCenter? FindWorkCenter(int workCenterId)
178 {
179 return _workCenters.FirstOrDefault(
180 workCenter => workCenter.Id == workCenterId);
181 }
182}
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:88
void AddWorkCenter(WorkCenter workCenter)
Adds a work center to the plant.
Definition Plant.cs:118
Plant(int id, string name, PlantWarehouse warehouse)
Initializes a plant with an identifier, a name and its associated warehouse.
Definition Plant.cs:46
Plant()
Initializes an empty plant.
Definition Plant.cs:29
bool RemoveWorkCenter(WorkCenter workCenter)
Removes a work center from the plant.
Definition Plant.cs:151
WorkCenter? FindWorkCenter(int workCenterId)
Finds a work center by its identifier.
Definition Plant.cs:177
PlantWarehouse Warehouse
Gets or sets the warehouse attached to the plant.
Definition Plant.cs:65
Represents a warehouse attached to a plant.
Represents a production work center belonging to a plant.
Definition WorkCenter.cs:15