OCL Example: Mortgages
This example describes the added precision in a UML model for a mortgage applications. Looking at the associated class diagram, it is possible to derive the following business rules:
- A customer can raise any number of credits
- A given credit is raised by exactly one customer
- A customer can own any number of securities
- A given security can be used to secure an arbitrary amount of credits
- A credit must be secured by at least one security
- A security is owned by exactly one customer
But these rules do not at all cover all requirements that apply to the software system. For example, the business analysts could want to document explicitly the following requirements:
The sum of all monthly rates of all credits of a customer may not surpass 50% of his monthly income and after paying all the rates the customer must have at least 1000 EUR available.
The sum of the values of all securities must be at least 20% more than the amount of the credit.
The amount of a credit must be at least 10.000,00 EUR.
A credit of a given customer can only be secured by securities that are owned by the given customer.
Now let's write these rules in OCL:
The sum of all monthly rates of all credits of a customer may not surpass 50% of his monthly income and after paying all the rates the customer must have at least 1000 EUR available.
context Customer
inv appropriateRates:
credits.MonthlyRate->sum() < MonthlyIncome / 2
inv minimalIncome:
MonthlyIncome - credits.MonthlyRate->sum() >= 1000
Note, that we have split the original rule in two different OCL expressions. This is good practice, for two reasons. First, we can name the various constraints and thus document there meaning better. Second and more important, failed assertions generated from these OCL expressions would yield a more precise error message.
The sum of the values of all securities must be at least 20% more than the amount of the credit.
context Credit
inv sufficientSecurities:
securities.Value->sum() > Amount * 1.2
The amount of a credit must be at least 10.000,00 EUR.
context Credit
inv minimalAmount: Amount >= 10000
A credit of a given customer can only be secured by securities that are owned by the given customer.
inv onlyOwnedSecurities:
securities.owner->forAll(owner | owner = customer)