Code Generation For Business Rules

After you have modeled rules for all Rule Task elements in the Rule Flow diagram, the Business Domain model is ready for code transformation. The code templates for generating technology-specific rule code work hand-in-hand with the EASL code templates to generate the code for the Rule Flow diagram.

Return a value from the Rule Flow behavior

Step

Action

See also

1

Double-click on the last Rule Task element before the end node of the Rule Flow diagram.

The element's Properties dialog displays.

 

 

2

Click on the Effect tab.

 

 

3

In the Effect field, type the return statement; for example, return true.

 

 

4

Click on the Save button, and on the OK button to close the dialog.

 

Generate code for the Class containing the rule flow behavior (in our initial example, Rental System); the code for business rules logic is generated, with the rule statements expressed in natural language as comments.

 

Generate code for a single class

Create a Business Domain Model

Example

The following code snippet was generated from the Rental System Class element in the EAExample model:

 

///////////////////////////////////////////////////////////

//  RentalSystem.cs

//  Implementation of the Class RentalSystem

//  Generated by Enterprise Architect

//  Created on:      08-May-2009 2:39:23 PM

///////////////////////////////////////////////////////////

 

 

public class RentalSystem {

 

 public Customer m_Customer;

 public Car m_Car;

 public Rent m_Rent;

 

 public RentalSystem(){

 

 }

 

 ~RentalSystem(){

 

 }

 

 public virtual void Dispose(){

 

 }

 

 /* Begin - EA generated code for  Activities and Interactions */

 

 public bool ProcessApplication(Rent m_rent,Application m_application)

 {

         // behavior is an Activity

 

                 /*CAR MUST NOT BE RENTED TO CUSTOMERS WITHOUT A VALID LICENCE NUMBER*/

                 if( m_Customer.ValidLicenceNumber == "FALSE" )

                 {

                         m_application.Status = "Reject";

                         m_Customer.Eligibile = false;

                 }

                 /*CAR MUST NOT BE RENTED TO CUSTOMERS OF AGE LESS THAN 18*/

                 if( m_Customer.age < 18 )

                 {

                         m_application.Status = "Reject";

                         m_Customer.Eligibile = false;

                 }

                 /*CAR MUST NOT BE RENTED TO CUSTOMERS WITH BAD HISTORY LEVEL 3*/

                 if( m_Customer.BadHistoryLevel == 3 )

                 {

                         m_application.Status = "Reject";

                         m_Customer.Eligibile = false;

                 }

         if (Customer.Eligible == true)

         {

 

                         /*RENT FOR SMALL CARS IS 80 AUD PER DAY*/

                         if( m_Car.type == Small )

                         {

                                 m_rent.RentPerDay = 80;

                         }

                         /*RENT FOR AWD CARS IS 100 AUD PER DAY*/

                         if( m_Car.type == AWD )

                         {

                                 m_rent.RentPerDay = 100;

                         }

                         /*RENT FOR LUXURY CARS IS 150 AUD PER DAY*/

                         if( m_Car.type == Luxury )

                         {

                                 m_rent.RentPerDay = 150;

                         }

                         /*RENT PAYABLE IS CALCULATED AS THE PRODUCT OF RENTPERDAY AND RENTALPERIOD IN DAYS*/

                         m_rent.RentPayable = m_rent.RentPerDay * m_rent.No_of_rent_days;

                 if (CustomerBadHistoryLevel > 0)

                 {

 

                                 /*PENALTY OF 20 % OF RENT MUST BE APPLIED FOR CUSTOMERS WITH BAD HISTORY LEVEL 2*/

                                 if( m_Customer.BadHistoryLevel == 2 )

                                 {

                                         m_rent.PenaltyFee = m_rent.RentPayable * 0.2;

                                 }

                                 /*PENALTY OF 10 % OF RENT MUST BE APPLIED FOR CUSTOMERS WITH BAD HISTORY LEVEL 1*/

                                 if( m_Customer.BadHistoryLevel == 1 )

                                 {

                                         m_rent.PenaltyFee = m_rent.RentPayable * 0.1;

                                 }

                 }

                 else

                 {

 

                 }

 

                         /*TOTAL AMOUNT PAYABLE IS CALCULATED AS THE SUM OF RENT PAYABLE AND PENALTY IF ANY.*/

                         m_rent.TotalAmountPayable = m_rent.RentPerDay + m_rent.PenaltyFee;

         }

         else

         {

 

         }

         return m_application.Status;

 }

 

 /* End - EA generated code for  Activities and Interactions */

 

}//end RentalSystem

 

Learn more