Tutorial: Rule actions

As soon as a rule contains one condition, you can add actions to that rule. Actions define what happens when a rule is fired. There are two kinds of rules:

  • Find: determines which other entities or values, that are not yet available in the rule, will be searched for when the rule fires.
  • Create: determines which new entities, descriptors, associations and attributions will be created when the rule fires.

Find action

Find actions can be used to find elements that are not available in the when part of a rule, but are required in the then part. Most find actions are generated automatically, but there is one kind of find action you can explicitly make:

  • Find entity: finds existing but not yet known entity instances that are associated to entity instances that are available in the when part of the rule.

Create action

With create actions you can create new instances and make new relationships between them. When a rule fires because its conditions are met, then these actions create new data. These newly created data in their turn may trigger other rules to be fired.

Caution: Be aware of the (not always explicitly visible) action and reaction relationships between rules. Especially cyclic relationships between rules (where rule A triggers rule B which triggers rule A, etc.) may result in infinite loops.

There are three types of create actions:

  • Create element: creates a new instance of an entity or descriptor.
  • Make association: creates a new association between two entity instances.
  • Assign characteristic: assigns a characteristic (value or descriptor) to an entity or descriptor instance.

Create actions can take all elements, values and operation outcomes as inputs. This includes the following inputs from the three parts of a rule:

  • When part: constant values, as well as the elements (entity and descriptor instances) and the underlying descriptors and/or attributes that take part in equation conditions.
  • Do part: outcomes of all operations.
  • Then part: search results from all find actions, plus the elements (entity and descriptor instances) that are the result of the create new element actions.

Example app: Grocery store

For our example app we want to realize two different rules:

  • Rule 1: Calculate shopping cart total price: when a new product is added to the shopping cart, then the price of the product must be added to the total price of the shopping cart.
  • Rule 2: Calculate volume discount: when a customer checks out a shopping cart and the total price of the shopping cart exceeds 100 Euro, then the total amount should be reduced with 10%.

Rule 1: Calculate shopping cart total price

To implement rule 1, we need to implement the following assign value action:

  • Assign (the Amount that Shopping cart1 is totalled to [plus] the Amount that Product1 is valued at) to the Price that Shopping cart1 is totalled to as the Amount it consist of

Rule 2: Calculate volume discount

For rule 2 we first need to create a new price via a create new element action:

  • Create Price2

Then, we need to assign the new price to the shopping cart by writing the assign descriptor action:

  • Assign Price2 to Shopping cart1 as the Price it is reduced to

And finally we need to assign the outcome of the operation that calculates a 10% reduction to the reduced price of the shopping cart by writing the following assign value action:

  • Assign ((the Amount that Shopping cart1 is totalled to [divided by] 100) [multiplied by] 90) to Price2 as the Amount it contains of