Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 2 Project Design Tutorial : Add ApplyDebit, BadApplyDebit, and CheckNegativeBalance Rules

Add ApplyDebit, BadApplyDebit, and CheckNegativeBalance Rules
The rules you add in this section work together as follows:
When a Debit event is asserted into the Rete network, TIBCO BusinessEvents checks rules with the Debit event in their scope.
The ApplyDebit rule has the Debit event in its scope, and also an account ID. It’s priority 1 so it will execute before any other lower priority rule. The engine checks the rule conditions.
If the Rete network also contains an Account instance whose ID matches the ID in the debit event, the rule executes. If the account is suspended, a message to that effect displays in the console. Otherwise, the account is debited. A message displays in the console to this effect.
If the Rete network does not contain a matching Account instance, then other rules in the agenda — those that have debit events in their scope — are eligible to execute. BadApplyDebit is the only rule with the Debit event in its scope, so it is eligible and it executes. It sends a message to the console that no matching account is found.
The CheckNegativeBalance rule has an Account concept in its scope. When an account is debited the Account concept is changed, and so the CheckNegativeBalance rule becomes "newly true." The effect is similar to assertion of a new entity into the Rete Network. If the debit has made the account balance negative, this rule sets the status to Suspended.
Learning Points
It’s important to understand how conflict resolution and run to completion (RTC) cycles work. If you understand what triggers rules to execute, and why a rule may not execute, you can design rules more effectively. For a reminder, carefully reread Understanding Conflict Resolution and Run to Completion Cycles in TIBCO BusinessEvents Architect’s Guide.
More Information
Chapter 5, Run-time Inferencing Behavior in TIBCO BusinessEvents Architect’s Guide.
Task N Add ApplyDebit, BadApplyDebit, and CheckNegativeBalance Rules
If you have completed Task L, Add the BadCreateAccount Rule and Task M, Add the CreateAccount Rule, you will be familiar with adding rules. This section shows the source code for the three rules you will add. As an extra hint, screenshot of the ApplyDebit rule is also shown below
ApplyDebit Rule Form View
The main purpose for showing the form view is so you can compare it with the source view when creating your own rule.
ApplyDebit Rule Source View Code

 
/**
* @description Debits the matching account by the specified amount
* @author
*/
rule Rules.ProcessDebits.ApplyDebit {
  attribute {
    priority = 1;
    forwardChain = true;
  }
  declare {
    Events.Debit  debit;
    Concepts.Account  account;
    
  }
  when  {
    //Checks whether the extId of an Account instance in working memory
    //matches the incoming event's account ID
    account@extId == debit.AccountId;
  }
  then {
    //If Account Status is not Suspended, debits the account
    if (account.Status !="Suspended") {
        account.Debits=debit.Amount;
        System.debugOut(
            "#### Debiting account " +account@extId+ " by $" +debit.Amount);
        account.Balance=account.Balance - debit.Amount;
        System.debugOut("#### New balance: $" + account.Balance);
        }
    else {
      System.debugOut(
          "#### Cannot debit the suspended account " +account@extId );
    }
    
    Event.consumeEvent(debit);
  }
}

 
BadApplyDebit Rule Source View Code

 
/**
* @description
* @author
*/
rule Rules.BadApplyDebit {
  attribute {
    priority = 5;
    forwardChain = true;
  }
  declare {
    Events.Debit  debit;
    
  }
  when {
 
  }
  then {
    System.debugOut(
       "#### Debit not applied, account not found: " + debit.AccountId);
  }
}

 
CheckNegativeBalance Rule Source View Code

 
/**
* @description
* @author
*/
rule Rules.CheckNegativeBalance {
  attribute {
    priority = 5;
    forwardChain = true;
  }
  declare {
    Concepts.Account  account;
  }
  when {
    //Checks that the balance is less than zero
    account.Balance < 0;
    //Checks that Account status is not set to Suspended
    account.Status!="Suspended";
  }
  then {
    account.Status="Suspended";
    System.debugOut(
        "#### Account ID "+account@extId+" STATUS set to Suspended. Balance" +account.Balance+" is less than zero");
  }
}

 

Copyright © TIBCO Software Inc. All Rights Reserved