You can use numeric operators in OCL expressions to calculate values from Integer and Real attributes in your MDriven model.
Numeric values in OCL
This page uses Number as the common term for both Integer and Real values. Integer and Real are substitutable unless an operator specifies otherwise.
For example, if a class has numeric attributes quantity and unitPrice, you can calculate a total with:
self.quantity * self.unitPrice
Use numeric expressions where MDriven accepts OCL, such as a derived attribute or a ViewModel expression. OCL is a query language: an expression calculates or evaluates a value and does not change model data.
Basic arithmetic
Use the standard arithmetic operators to combine numeric values.
| Operator | Use | Example | Result |
|---|---|---|---|
+
|
Addition | 3 + 2
|
5
|
-
|
Subtraction | 7 - 2
|
5
|
*
|
Multiplication | 4 * 3
|
12
|
/
|
Division | 10 / 2
|
5
|
For a model-based expression, the same operators work with attribute values:
self.amount + self.tax
Negative values: unary minus
A unary minus is a minus sign placed before one numeric value or expression. It makes that value negative; it is different from the binary - operator, which subtracts one value from another.
-(3 + 2)
The expression evaluates to -5.
Unary minus has higher precedence than addition and binary subtraction. Therefore:
-3 + 2
is interpreted as (-3) + 2, which evaluates to -1, not as -(3 + 2). Use parentheses when you want the minus sign to apply to a complete calculation. For a focused explanation, see Documentation:OCLOperators unary-.
Find the available numeric operators
The operators available to an expression depend on the type of the value before the operator. Use the OCL Editor to inspect the operators MDriven offers for a numeric value.
- Open the OCL Editor where you are writing the expression.
- Enter a numeric expression or a numeric attribute, such as
self.quantity. - Inspect the operators offered by the editor for that value.
- Select an operator and verify the expression result in your model context.
For example, enter self.quantity when quantity is numeric, then use the editor's available operators to continue the expression. Documentation:OCL General Operators describes this discovery approach.
Choose the right operator category
Numeric operators calculate numeric results. Do not use this page as a reference for operators that work on other kinds of values:
- Use Boolean operators to combine or negate true/false conditions, for example a condition used to decide whether a calculated value is acceptable.
- Use collection operators after navigation produces a collection of objects.
- Use OCL Operators for the broader operator overview.
