Hans Karlsen (talk | contribs) |
No edit summary |
||
Line 1: | Line 1: | ||
There are other ways to introduce business rules in the model than using state machines and guards. You can use '''constraints'''. | There are other ways to introduce business rules in the model than using state machines and guards. You can use '''constraints'''. | ||
The model already has a lot of implicit constraints from the cardinalities of the association ends. | The model already has a lot of implicit constraints from the cardinalities of the association ends. If you have cardinality 1..4 and you have zero objects in that relation – then you have broken constraint. | ||
===== Define constraints ===== | ===== Define constraints ===== | ||
You may also define your own constraints: | |||
[[File:Model car constraints.png|frameless|397x397px]] | [[File:Model car constraints.png|frameless|397x397px]] | ||
You can choose if a broken constraint (a constraint that evaluates to false) should be treated as Information, Warning or | You can choose if a broken constraint (a constraint that evaluates to false) should be treated as Information, Warning, or an Error to the user. | ||
===== Delete constraints ===== | ===== Delete constraints ===== | ||
Line 15: | Line 15: | ||
[[File:Constraint wrappper.png|frameless|437x437px]] | [[File:Constraint wrappper.png|frameless|437x437px]] | ||
This way have explained at the model level that the domain does not consider it to be | This way, you have explained at the model level that the domain does not consider it to be acceptable to delete a Car-object as long as we have the deposit unless it is in state Scrapped. | ||
The delete constraints will be checked when | The delete constraints will be checked when Deleted by MDriven – as a result, the Delete operator is executed on the class. | ||
Other things that are checked when the Delete operator is run are the | Other things that are checked when the Delete operator is run are the Business Delete Rules that exist on all association endpoints: | ||
[[File:Business delete rule.png|frameless|438x438px]] | [[File:Business delete rule.png|frameless|438x438px]] | ||
We as modelers should decide | We as modelers should decide the best rule for each association end. In this case, is it acceptable to delete a Brand if Cars are left in the AllCarsOfThisBrand association? No, I think not. I am setting it to “MustBeEmpty.” | ||
The association in the other direct on the other hand | The association is in the other direct on the other hand. | ||
[[File:Business delete rule need not be empty.png|frameless|438x438px]] | [[File:Business delete rule need not be empty.png|frameless|438x438px]] | ||
I set that to “NeedNotBeEmptyNoWarning” – because deleting a car | I set that to “NeedNotBeEmptyNoWarning” – because deleting a car object is okay even if it has a brand. | ||
===== Constraints evaluation in OCL ===== | ===== Constraints evaluation in OCL ===== | ||
Line 35: | Line 35: | ||
===== Usage of constraints ===== | ===== Usage of constraints ===== | ||
Constraints automatically show up in ViewModels - but you may opt them out on a per | Constraints automatically show up in ViewModels - but you may opt them out on a per-ViewModel level: | ||
[[File:2021-04-08 10h46 32.png|none|thumb|484x484px]] | [[File:2021-04-08 10h46 32.png|none|thumb|484x484px]] | ||
You can also access Constraints and their " | You can also access Constraints and their "brokenness" via OCL Operators brokenConstraints and [[OCLOperators constraints|Constraints]]: | ||
[[File:2021-04-08 10h48 43.png|none|thumb|454x454px]]See also: [[OCLOperators_constraints]] | [[File:2021-04-08 10h48 43.png|none|thumb|454x454px]]See also: [[OCLOperators_constraints]] | ||
[[Category:UML]] | [[Category:UML]] |
Revision as of 07:06, 13 January 2023
There are other ways to introduce business rules in the model than using state machines and guards. You can use constraints.
The model already has a lot of implicit constraints from the cardinalities of the association ends. If you have cardinality 1..4 and you have zero objects in that relation – then you have broken constraint.
Define constraints
You may also define your own constraints:
You can choose if a broken constraint (a constraint that evaluates to false) should be treated as Information, Warning, or an Error to the user.
Delete constraints
You can also define the constraint as being a delete constraint only:
This way, you have explained at the model level that the domain does not consider it to be acceptable to delete a Car-object as long as we have the deposit unless it is in state Scrapped.
The delete constraints will be checked when Deleted by MDriven – as a result, the Delete operator is executed on the class.
Other things that are checked when the Delete operator is run are the Business Delete Rules that exist on all association endpoints:
We as modelers should decide the best rule for each association end. In this case, is it acceptable to delete a Brand if Cars are left in the AllCarsOfThisBrand association? No, I think not. I am setting it to “MustBeEmpty.”
The association is in the other direct on the other hand.
I set that to “NeedNotBeEmptyNoWarning” – because deleting a car object is okay even if it has a brand.
Constraints evaluation in OCL
If you want to evaluate constraints in OCL, use OCLOperators constraints for example.
Usage of constraints
Constraints automatically show up in ViewModels - but you may opt them out on a per-ViewModel level:
You can also access Constraints and their "brokenness" via OCL Operators brokenConstraints and Constraints:
See also: OCLOperators_constraints