Add rule

It adds the value of one attribute to the value of another attribute, if you are inserting.
It subtracts the value of one attribute to the value of another attribute, if you are deleting.
It calculates the difference between the new and old value of the attribute you update and that difference is added to another attribute, if you are updating.

Syntax

Add(att1 , att2) [ if cond ] ;

In mode: 

  • Insert: the value of the att1 attribute is added to the value of the att1 attribute (if the specified condition is true).
  • Delete: the value of the att1 attribute is subtracted from the value of the att2 attribute (if the specified condition is true).
  • Update: the difference between the new and old value of the att1 attribute is added to the value of the att2 attribute (if the specified condition is true).

Let's see an example.

Customer                              Trip                           Country   
{                                     {                              { 
    CustomerId*                          TripId*                       CountryId*
    CustomerName                         TripDate                      CountryName
    CustomerLastName                     CountryId                     City
    CustomerPhone                        CountryName                   {
    CustomerTotalMiles                   CityId                          CityId*
    Trip                                 CityName                        CityName
    {                                    TripMiles                     } 
      TripId*                          }
      TripDate                                                       }
      CountryId
      CountryName
      CityId
      CityName
      TripMiles
    }
}

Suppose that a customer makes several trips and accrues miles.

To implement the customer’s mileage accrual, in the Customer transaction we define the following rule:

Add(TripMiles, CustomerTotalMiles);

What does this rule do?

The following behavior is incorporated into the Add rule:

  • If a new trip is added for the customer, the value of TripMiles is added to CustomerTotalMiles.
  • If a trip is deleted from the customer, the value of TripMiles is subtracted from  CustomerTotalMiles.
  • If the value of TripMiles associated with a customer’s trip is changed, its configured value is automatically subtracted and the new value is added to the customer’s total miles.

See also

Subtract rule