This action type can be used to assign a new value to a variable definition. The new value could be the result of an arithmetic calculation or method execution.
Assign :: variable definition = new value
Assign :: x = x + 5
Assign :: t = perform some Action (100,10)
After you assign a new value to a variable definition, the new value will be reflected in subsequent uses of the variable definition. Thus the newly assigned values can be used in other actions in the same or different rules.
If the variable definition is used in some conditions, those conditions are re-evaluated to verify if their outcome has changed. When the outcome of the condition is different from the earlier outcome, it may result in additions or deletions to the firing queue.
When the action method returns a value that is required in a subsequent action you might want to use Assign actions.
If |
|
... |
|
Then |
|
Assign :: x = perform calculation |
|
Perform the second action (x) |
When rules cause many changes to a parameter which finally needs to be set on the business object, you might want to use a temporary variable definition to store the value instead of directly modifying the business object.
Rule A |
|
---|---|
If |
|
... |
|
Then |
|
Assign :: rate = 10.5 |
Rule B |
|
---|---|
If |
|
... |
|
Then |
|
Assign :: rate = rate + 1.5 |
Rule C Priority = Minimum |
|
---|---|
If |
|
Condition always evaluates true |
|
Then |
|
LoanApplication.setInterestRate(rate) |
When there are multiple objects of a type and you would like to check some rules which depend on the cumulative values of these objects.
To grant a discount based on the total value of the order (which is obtained by the sum of all items in the order), you can define a variable definition as follows:
Name |
Type |
Initial Value |
---|---|---|
order Total |
Double |
0 |
number of items |
int |
0 |
You can create rules as shown below:
Rule FetchOrderItems |
|
---|---|
If |
|
Order.getOrderId != -1 |
|
Then |
|
Assert :: Order.getOrderItems |
Rule AddItemToTotal |
|
---|---|
If |
|
OrderItem.getItemId != -1 |
|
Then |
|
Assign :: order Total = order Total + OrderItem.getCost |
|
Assign :: number of items = number of items + 1 |
Rule checkOrderTotal |
|
---|---|
If |
|
order Total > 500 |
|
Then |
|
Then grant a discount of 10% for the order |