Constraints
(Created page with "There are other ways to introduce business rules in the model than using state machines and guards. You can also use constraints. The model already has a lot of implicit const...")
 
No edit summary
 
(20 intermediate revisions by 5 users not shown)
Line 1: Line 1:
There are other ways to introduce business rules in the model than using state machines and guards. You can also use constraints. The model already has a lot of implicit constraints from the cardinalities of the association ends. Like if you have checcardinalityf 1..4 and you have zero objects in that relation – then you have broken constraint.
<message>Write the content here to display this box</message>
There are other ways to introduce business rules in the model than using state machines and guards. You can use '''Constraints'''.


But you may also define you own constraints:
The model already has several 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 a broken constraint.
 
=== 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 a Error to the user.
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.


In the Description for a constraint you can use <asString> to get the object that is brokens string representation.
=== Delete Constraints ===
You can also define the constraint as being a delete constraint only:
You can also define the constraint as being a delete constraint only:


[[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 ok to delete a Car-object as long as we have the deposit unless it is in state Scrapped.
This way, you have explained at the model level that the domain does not consider it 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 is Deleted by MDriven – as a result of the Delete operator is executed on the class.
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 BusinessDeleteRules that exists on all association endpoints:
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 what the best rule is for each association end. In this case is it ok to delete a Brand if there are Cars left in the AllCarsOfThisBrand association? No I think not. I am setting it to “MustBeEmpty”.
As modelers, we 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 direction 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-object is ok even if it has a brand.
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 [[Documentation:OCLOperators constraints|OCLOperators constraints]], for example.
 
=== Usage of Constraints ===
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]]
You can also access Constraints and their "brokenness" via OCL Operators brokenConstraints and [[Documentation:OCLOperators constraints|Constraints]]:
[[File:2021-04-08 10h48 43.png|none|thumb|454x454px]]'''<span style="color: #1a50ad">The MDriven Book</span>''' - '''Next Chapter:''' [[Training:The ViewModel|The ViewModel]]
 
'''See also:''' [[Documentation:OCLOperators constraints|OCLOperators_constraints]]


{{Edited|July|12|2025}}
[[Category:UML]]
[[Category:UML]]
[[Category:Constraints]]
[[Category:Constraints]]
[[Category:UI]]
[[Category:UI]]
[[Category:Expressions]]
[[Category:Expressions]]
[[Category:The MDriven Book]]

Latest revision as of 20:54, 13 February 2025

This page was created by Alexandra on 2017-05-14. Last edited by Lars.olofsson@mdriven.net on 2025-02-13.

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 several 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 a broken constraint.

Define Constraints

You may also define your own constraints:

Model car constraints.png

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.

In the Description for a constraint you can use <asString> to get the object that is brokens string representation.

Delete Constraints

You can also define the constraint as being a delete constraint only:

Constraint wrappper.png

This way, you have explained at the model level that the domain does not consider it 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:

Business delete rule.png

As modelers, we 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 direction on the other hand.

Business delete rule need not be empty.png

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:

2021-04-08 10h46 32.png

You can also access Constraints and their "brokenness" via OCL Operators brokenConstraints and Constraints:

2021-04-08 10h48 43.png

The MDriven Book - Next Chapter: The ViewModel

See also: OCLOperators_constraints