Prev Next

Operation Behavior

You can define the behavior of an operation in one of two ways:

  • As behavior code to be inserted into an operation Body when that operation is generated to file
  • As a reference to a behavior element, such as an Activity or Interaction, or to another operation

Creation of the behavior code is discussed in the Behavior Code Help topic.

You can also provide a text description consisting of pseudo-code, structured text or just abbreviated notes, which is used as a comment on the behavior code. You have the option of displaying either this text or the reference on each depiction of the parent element on a diagram. In this State element example, the entry operation applies the behavior of the ActivityComp Activity, whilst the exit behavior is described in simple text.

A UML State element showing entry and exit behavior.

The operation Behavior is also applied during Interpreted Simulation and Executable StateMachine code generation and simulation, to provide 'entry/do/exit' behavior for States. In this case you create a short script in the 'Behavior' field, using JavaScript, to enact the operation behavior. For an example, access the EA.Example Model and open 'Model Simulation > State Machine Models > Nested Traces > Example Class > Nested Traces', then open the 'Nested Traces' State Machine diagram. Note the entry and exit scripts for each State. If you double-click on the 'entry' or 'exit' operations, the operation 'Properties' dialog opens and you will see the scripts in the 'Behavior' tab.

Access

In a diagram or the Browser window, click on an operation and then select one of the access methods identified here:

Ribbon

Design > Element > Editors | Properties Dialog > Behavior

Context Menu

Properties | Properties > Behavior

Keyboard Shortcuts

F10 > Behavior

Set Behavior

Either:

  • In the 'Behavior' field, type the text or pseudo code description of the behavior, or
  • Click on the Element button to display the 'Select Behavior' dialog, then browse for and select the appropriate element or operation; when you click on the OK button, the fully qualified object name displays in the 'Behavior' field

If you want the behavior text or reference to be shown on any diagram that the operation appears in, select the 'Show Behavior in Diagram' checkbox.

If you want to use behavior code, click on the 'Code' tab and enter the code in the field on that tab.

When you have defined the operation behavior, click on the Save button.

Delete behavior

If you have typed in a behavior description in free text, simply select and delete the text.

If you have selected another behavioral object, click on the Element button and select the '<none>' option at the foot of the 'Select Element' hierarchy.

Example of Associated Behavior

An operation of, for example, a Class is being associated with a behavior elsewhere in the model. The operation is then set as the specification of that associated behavior. In code generation, the behavior of the associated behavioral element is generated as the operation's code; in this illustration, op1 is associated with the Activity 'Activity'.

A screenshot from Sparx Systems Enterprise Architect showing a UML Class element that owns an Operation in the Project Browser.

A UML Activity diagram showing Action elements that are associated with behavior from Operations owned by Class elements.

The generated code for op1 is as shown:

     package Behavior;

     public class Container {

          public Container() {

          }

          public void finalize() throws Throwable {

          }

          public void op1() {

          /*Activity element(Activity)'s behavior rendered as

               operation(op1)'s code*/

               //Action1;

               if (cond1)

               {

                    //Action2;

               }

               else

               {

                     //Action3;

               }

               //Action4;

          }

          /*Activity element (Activity) not rendered*/

          public void Activity2()

          {

               // behavior is an Activity

          }

          public void Interaction()

          {

               // behavior is an Interaction

          }

     } //end Container

Learn more