Code Generation - Activity Diagrams

Note:

To be able to generate code from behavioral models, all behavioral constructs should be contained within a Class.

Code generation from an Activity diagram in a Class requires a validation phase, during which Enterprise Architect uses the system engineering graph optimizer to analyze the diagram and render it into various code-generatable constructs. Enterprise Architect also transforms the constructs into one of the various action types (if appropriate), similar to the Interaction diagram constructs. Enterprise Architect then uses the EASL code generation macros to generate code from these constructs.

For more information on the EASL code generation macros and templates Enterprise Architect uses to generate code from behavioral models see the EASL Code Generation Macros topic.

To provide a comprehensive analysis of these features several diagrams from the EAExample project are shown as examples.

Conditional Statements

To model a conditional statement, you use Decision/Merge nodes. Alternatively, you can imply Decisions/Merges internally. The graph optimizer expects an associated Merge node for each Decision node, to facilitate efficient tracking of various branches and analysis of the code constructs within them.

The following diagram is interpreted as a nested IF statement.

do ValidateUser

The Java code that might be generated from this diagram is as follows:

 

       public boolean doValidateUser(String Password,String UserName)

       {

               loadAccountDetails();

               boolean bRet;

               if (Username==name)

               {

                       if (Password == password)

                       {

                               bRet = true;

                               bValidUser = true;

                       }

                       else

                       {

                               bRet = false;

                       }

 

               }

               else

               {

                       bRet = false;

               }

 

               return bRet;

       }

Invocation Actions (Call Operation Action, Call Behavior Action)

Call Actions are handled more efficiently. Each action has arguments relating to the parameters of the associated behavior (use the Synchronize button of the Arguments dialog to synchronize arguments and parameters).

The following diagram demonstrates the use of a Call Behavior Action and a Call Operation Action interspersed with a conditional statement.

Do markAccountClosed

The generated Java code might appear as follows:

 

       public void doMarkAccountClosed()

       {

               doValidateUser(password,name);

               if (bValiduser)

               {

                       setClosed(true);

 

               }

               else

               {

                       System.out.println("Invalid user");

               }

               return;

       }

Loops

Enterprise Architect's system engineering graph optimizer is also capable of analyzing and identifying loops. An identified loop is internally rendered as an Action Loop, which is translated by the EASL code generation macros to generate the required code.

The following diagram demonstrates how a loop can be modeled.

do checkForOutstandingOrders

The generated Java code might appear as follows:

 

       public void doCheckForOutstandingOrders()

       {

               if (status != closed)

               {

                       initializeStateMachine();

                       while (status != closed)

                       {

                               runStateMachine();

                       }

               }

               else

               {

                       //No Outstanding orders;

               }

               return;

       }