distillation_shortcut_unit/integer_parameter.rs
1use crate::shared_unit_data::*;
2use crate::distillation_shortcut_unit::DistillationShortcutUnit;
3use cobia::*;
4
5/// The IntegerParameter class implements a CAPE-OPEN 1.2 integer parameter.
6///
7/// Parameters must implement the ICapeIdentification and ICapeParameter interfaces.
8/// In addition, integer parameters must implement the ICapeIntegerParameter interface.
9///
10/// This implementation can be used for both input and output parameters.
11/// Input parameters can be set by the user, while output parameters are set by the unit operation.
12///
13/// Although CAPE-OPEN defines a value for an integer that is not specified (the equivalent of NaN
14/// for real values), this value is not widely supported, and unspecified values are not allowed
15/// in this implementation.
16///
17/// Any invalid values (outside the bounds of the parameter) will raise an error when set.
18/// Therefore, any parameter is valid.
19
20#[cape_object_implementation(
21 interfaces = {
22 cape_open_1_2::ICapeIdentification,
23 cape_open_1_2::ICapeParameter,
24 cape_open_1_2::ICapeIntegerParameter,
25 },
26 new_arguments = {
27 name,
28 description,
29 is_input,
30 shared_unit_data,
31 default_value,
32 minimum_value,
33 maximum_value,
34 }
35 )]
36pub struct IntegerParameter {
37 /// The name of the parameter
38 pub(crate) name: CapeStringImpl,
39 /// The description of the parameter
40 description: CapeStringImpl,
41 /// Indicates whether this parameter is an input (true) or an output (false)
42 is_input : bool,
43 /// Shared data for the unit, containing unit-specific information
44 shared_unit_data: SharedUnitDataRef,
45 /// The current value of the parameter
46 pub(crate) value : i32,
47 /// Default value of the parameter
48 default_value : i32,
49 /// Minimum value of the parameter
50 minimum_value : i32,
51 /// Maximum value of the parameter
52 maximum_value : i32,
53}
54
55impl IntegerParameter {
56
57 fn new(
58 name: CapeStringImpl,
59 description: CapeStringImpl,
60 is_input: bool,
61 shared_unit_data: SharedUnitDataRef,
62 default_value: i32,
63 minimum_value: i32,
64 maximum_value: i32,
65 ) -> Self {
66 Self {
67 name,
68 description,
69 is_input,
70 shared_unit_data,
71 value: default_value,
72 default_value,
73 minimum_value,
74 maximum_value,
75 cobia_object_data:std::default::Default::default(), //field generated by the cape_object_implementation macro; can be set to default
76 }
77 }
78
79}
80
81impl std::fmt::Display for IntegerParameter {
82
83 /// Format the IntegerParameter as a string for display purposes.
84 ///
85 /// The std::fmt::Display interface is used when generating the
86 /// source name of the object that raises an error.
87 ///
88 /// # Arguments:
89 /// * `f` - A mutable reference to a `std::fmt::Formatter` where the formatted string will be written.
90 ///
91 /// # Returns:
92 /// * A `std::fmt::Result` indicating the success or failure of the formatting operation.
93
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 write!(f,"Parameter '{}' of {} unit '{}'",self.name, DistillationShortcutUnit::NAME, self.shared_unit_data.borrow().name)
96 }
97
98}
99
100impl cape_open_1_2::ICapeIdentification for IntegerParameter {
101
102 /// Get the name of the component.
103 ///
104 /// # Arguments:
105 /// * `name` - A mutable reference to a `CapeStringOut` where the name will be set.
106 ///
107 /// # Returns:
108 /// * A `Result` indicating success or failure. If successful, the name is set in `name`.
109
110 fn get_component_name(&mut self,name:&mut CapeStringOut) -> Result<(), COBIAError> {
111 name.set(&self.name)?;
112 Ok(())
113 }
114
115 /// Get the description of the component.
116 ///
117 /// # Arguments:
118 /// * `description` - A mutable reference to a `CapeStringOut` where the description will be set.
119 ///
120 /// # Returns:
121 /// * A `Result` indicating success or failure. If successful, the description is set in `description`.
122
123 fn get_component_description(&mut self,description:&mut CapeStringOut) -> Result<(), COBIAError> {
124 description.set(&self.description)?;
125 Ok(())
126 }
127
128 /// Set the name of the component.
129 ///
130 /// This method is not allowed for this parameter implementation and will return an error.
131
132 fn set_component_name(&mut self, _name: &CapeStringIn) -> Result<(), COBIAError> {
133 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
134 }
135
136 /// Set the description of the component.
137 ///
138 /// This method is not allowed for this parameter implementation and will return an error.
139
140 fn set_component_description(&mut self, _desc: &CapeStringIn) -> Result<(), COBIAError> {
141 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
142 }
143}
144
145impl cape_open_1_2::ICapeParameter for IntegerParameter {
146
147 /// Get the validation status of the parameter.
148 ///
149 /// As this parameter does not accept any values that are not valid,
150 /// the parameter is always valid.
151 ///
152 /// # Returns:
153 /// * A `Result` containing the validation status, which is always `CapeValid` for this implementation.
154
155 fn get_val_status(&mut self) -> Result<cape_open_1_2::CapeValidationStatus,COBIAError> {
156 Ok(cape_open_1_2::CapeValidationStatus::CapeValid)
157 }
158
159 /// Get the mode of the parameter.
160 ///
161 /// # Returns:
162 /// * A `Result` containing the mode of the parameter, which is either `CapeInput` or `CapeOutput` based on the `is_input` field.
163
164 fn get_mode(&mut self) -> Result<cape_open_1_2::CapeParamMode,COBIAError> {
165 if self.is_input {
166 Ok(cape_open_1_2::CapeParamMode::CapeInput)
167 } else {
168 Ok(cape_open_1_2::CapeParamMode::CapeOutput)
169 }
170 }
171
172 /// Get the type of the parameter.
173 ///
174 /// # Returns:
175 /// * A `Result` containing the type of the parameter, which is always `CapeParameterInteger` for this implementation.
176
177 fn get_type(&mut self) -> Result<cape_open_1_2::CapeParamType,COBIAError> {
178 Ok(cape_open_1_2::CapeParamType::CapeParameterInteger)
179 }
180
181 /// Validate the parameter.
182 ///
183 /// As this parameter does not accept any values that are not valid,
184 /// the parameter is always valid.
185 ///
186 /// # Arguments:
187 /// * `message` - A mutable reference to a `CapeStringOut` where any validation error messages will be set.
188 ///
189 /// # Returns:
190 /// * A `Result` containing a `CapeBoolean` indicating whether the parameter is valid or not.
191
192 fn validate(&mut self,_message:&mut CapeStringOut) -> Result<CapeBoolean,COBIAError> {
193 Ok(true as CapeBoolean)
194 }
195
196 /// Reset the parameter to its default value.
197 ///
198 /// This method sets the value of the parameter back to its default value and marks the unit as dirty.
199 /// It also resets the validation status to `CapeNotValidated`.
200 ///
201 /// # Returns:
202 /// * A `Result` indicating success or failure. If successful, the value is reset to the default value.
203
204 fn reset(&mut self) -> Result<(),COBIAError> {
205 if self.value!=self.default_value {
206 self.value = self.default_value;
207 let mut shared=self.shared_unit_data.borrow_mut();
208 shared.dirty=true;
209 shared.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
210 }
211 Ok(())
212 }
213}
214
215impl cape_open_1_2::ICapeIntegerParameter for IntegerParameter {
216
217 /// Get the value of the parameter.
218 ///
219 /// This method retrieves the current value of the parameter.
220 ///
221 /// # Returns:
222 /// * A `Result` containing the current value of the parameter as `CapeInteger`.
223
224 fn get_value(&mut self) -> Result<CapeInteger,COBIAError> {
225 Ok(self.value)
226 }
227
228 /// Set the value of the parameter.
229 ///
230 /// This implementation accepts a blank value for the parameters, but
231 /// it does not allow an invalid value to be set.
232 ///
233 /// Only input parameters can be set.
234 ///
235 /// If the value is set, it marks the unit as dirty and not validated.
236 ///
237 /// # Arguments:
238 /// * `value` - The value to set for the parameter, which should be a `CapeInteger`.
239 ///
240 /// # Returns:
241 /// * A `Result` indicating success or failure.
242
243 fn set_value(&mut self,value:CapeInteger) -> Result<(),COBIAError> {
244 if !self.is_input {
245 return Err(COBIAError::Code(COBIAERR_DENIED));
246 }
247 if value < self.minimum_value {
248 return Err(COBIAError::Message(format!("Value of {} below minimum value of {}",value,self.minimum_value)));
249 }
250 if value > self.maximum_value {
251 return Err(COBIAError::Message(format!("Value of {} above maximum value of {}",value,self.maximum_value)));
252 }
253 if self.value != value {
254 self.value = value;
255 let mut shared=self.shared_unit_data.borrow_mut();
256 shared.dirty=true;
257 shared.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
258 }
259 Ok(())
260 }
261
262 /// Get the default value of the parameter.
263 ///
264 /// This method retrieves the default value of the parameter.
265 ///
266 /// # Returns:
267 /// * A `Result` containing the default value of the parameter as `CapeInteger`, or an error.
268
269 fn get_default_value(&mut self) -> Result<CapeInteger,COBIAError> {
270 Ok(self.default_value)
271 }
272
273 /// Get the lower bound of the parameter.
274 ///
275 /// This method retrieves the lower bound of the parameter.
276 ///
277 /// # Returns:
278 /// * A `Result` containing the lower bound of the parameter as `CapeInteger`, or an error.
279
280 fn get_lower_bound(&mut self) -> Result<CapeInteger,COBIAError> {
281 Ok(self.default_value)
282 }
283
284 /// Get the upper bound of the parameter.
285 ///
286 /// This method retrieves the upper bound of the parameter.
287 ///
288 /// # Returns:
289 /// * A `Result` containing the upper bound of the parameter as `CapeInteger`, or an error.
290
291 fn get_upper_bound(&mut self) -> Result<CapeInteger,COBIAError> {
292 Ok(self.maximum_value)
293 }
294
295 /// Validate whether a given value is valid for this parameter.
296 ///
297 /// This method checks if the provided value is within the bounds of the parameter and meets the dimensionality requirements.
298 ///
299 /// Output parameters are not validated, as they are set by the unit operation and not by the user.
300 ///
301 /// # Arguments:
302 /// * `value` - The value to validate, which should be a `CapeInteger`.
303 /// * `message` - A mutable reference to a `CapeStringOut` where any validation error messages will be set.
304 ///
305 /// # Returns:
306 /// * A `Result` containing a `CapeBoolean` indicating whether the value is valid or not. If the value is not valid, it sets an error message in `message`.
307
308 fn validate(&mut self,value:CapeInteger,message:&mut CapeStringOut) -> Result<CapeBoolean,COBIAError> {
309 if !self.is_input {
310 return Err(COBIAError::Code(COBIAERR_DENIED));
311 }
312 if value < self.minimum_value {
313 message.set_string(format!("Value of {} below minimum value of {}",value,self.minimum_value))?;
314 Ok(false as CapeBoolean)
315 } else if value > self.maximum_value {
316 message.set_string(format!("Value of {} above maximum value of {}",value,self.maximum_value))?;
317 Ok(false as CapeBoolean)
318 } else {
319 Ok(true as CapeBoolean)
320 }
321 }
322}