Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

OTExample ATM/SpecialConditions


Team SpecialConditions

  1. /**
  2.  * This team realizes special conditions for selected accounts.
  3.  * If an account is registered to collect a special bonus, every time
  4.  * an amount of more than 1000 is deposited, additionaly, 1% of the amount
  5.  * is credited.
  6.  *
  7.  */
  8. public team class SpecialConditions {
  9.  
  10.     /**
  11.      * This team provides a registration method participate which
  12.      * is used to register an Account for the special conditions.
  13.      * Here, the explicit role creation mechanism is used.
  14.      */
  15.     public void participate(Account as BonusAccount ba) {}
  16.  
  17.  
  18.     /**
  19.      * The base guard predicate at the BonusAccount role checks, if the base
  20.      * object already has a role in this team. If this is not the case it prevents
  21.      * lifting (and thus role creation). In combination with the registration method
  22.      * this means that BonusAccountroles are never created automatically via lifting but
  23.      * have to be explicitly registered first.
  24.      */
  25.     public class BonusAccount playedBy Account
  26.         base when(SpecialConditions.this.hasRole(base, BonusAccount.class))
  27.     {
  28.         /**
  29.          * This callin method implements the collection of the bonus.
  30.          * It replaces the original Account.credit method and performs a base call with the
  31.          * increased amount of money.
  32.          */
  33.         callin void creditBonus(int amount)
  34.         {
  35.             int bonus = amount/100;
  36.             base.creditBonus(amount+bonus);
  37.             System.out.println("You will gain a bonus of "+ bonus + " Euro!");
  38.         }
  39.  
  40.         /**
  41.          * In the method binding we use an additional predicate to ensure that
  42.          * bonus is only credited for amounts greater than 1000.
  43.          */
  44.         void creditBonus(int amount) <- replace void credit(int i)
  45.             base when (i > 1000);
  46.     }
  47. }

Explanation

This class implements the following conditions for registered Accounts:

  • When crediting a large amount to the Account an extra bonus of 1% will be added.
  • Any amount greater than 1000 will be considered large.

This logic is applied using two different guards (OTJLD §4.5):

  • The role level guard (line 26) ensures that only registered roles are considered. It does so by looking up the current base object (of type Account) in the internal registry of the team (method hasRole(Object) OTJLD §6.1).
    • The only way a BonusAccount role will be attached to an Account is by calling method participate. This method applies declared lifting (OTJLD §2.3.2) thereby creating a role if needed (the empty method body is not a mistake - other than lifting no action is required).
  • The binding level guard (line 45) blocks all calls where the amount is not greater than 1000. It is important that also this guard is specified as a base guard, because otherwise lifting would create a role before the guard is evaluated, which would interfere with the registration mechanism.

Back to the top