An increaseTime operation is necessary for the StopWatch operation. It is used to increase the value of time by 1 whenever the operation is called. Therefore, we need to create an operation and Activity for the StopWatch Class to define this operation the same as we did for a resetTime operation and resetTime Activity.

To create an operation and an Activity for the StopWatch


  1. Right-click the StopWatch Class in the containment browser and select New Element > Operation.
  2. Name the new operation "increaseTime".
  3. Right-click the StopWatch Class in the containment browser and select New Element > Activity.
  4. Name the new Activity "increaseTime".
  5. Right-click the increaseTime Activity in the containment browser and select New Diagram > ActivityDiagram to add an Activity diagram to the increaseTime Activity. The new Activity diagram will be created under the increaseTime Activity. We will use the default name of this diagram, which is "increaseTime".

 

To increase the time values, the increaseTime Activity will do the following
(ii) get the object of the StopWatch using a ReadSelfAction
(ii) obtain the current time value using the ReadStructuralFeatureAction
(iii) increment the time value by one using an OpaqueBehaviorAction
(iv) set the increased time value to the object of the StopWatch using AddStructuralFeatureValueAction
(v) set the specification of the IncreaseTime Activity to the IncreaseTime Operation

(i) Getting the object of the StopWatch using ReadSelfAction

The Object of the StopWatch, which contains the time value, must be obtained by using a ReadSelfAction. To create the ReadSelfAction, you can follow the same steps used to create the ReadSelfAction in the resetTime Activity. In this example, the Object, which is the value of the output pin of the ReadSelfAction, needs to be supplied to multiple Actions (ReadStructuralFeatureAction and AddStructuralFeatureValueAction that will be created later). Therefore, a Fork must be added to this Activity.

To add a Fork to an Activity


  1. Click Fork Vertical on the Activity diagram toolbar.

    The Fork Vertical Button on the Activity Diagram Toolbar

  2. Click the increaseTime Activity diagram to create a Fork.
  3. Click ObjectFlow on the Activity diagram toolbar to connect the fork and the self output pin.

    Connecting the Output Pin and Fork


(ii) Obtaining the current time value using ReadStructuralFeatureAction

Once you have obtained the object of the Stopwatch, you will need to get the time value.

To get the current time value using a ReadStructuralFeatureAction


  1. Click Action > Any Action on the Activity diagram toolbar. The Select Action MetaClass dialog will open.
  2. Select ReadStructuralFeatureAction and click OK.
  3. Click the increaseTime Activity diagram. A new ReadStructuralFeatureAction will be created on the diagram.
  4. Right-click the ReadStructuralFeatureAction symbol on the diagram and select Specification to open its Specification window. 

    The Structural Feature Attribute of the ReadStructuralFeatureAction

  5. Select the StructuralFeature row and click the button. The SelectProperty dialog will open.

    Selecting a Structural Feature in the Select Property Dialog

  6. Select the Attribute time:Integer[1] of the StopWatch Class and click the OK button.
  7. Click ObjectFlow on the Activity diagram toolbar to connect the Fork and the self input pin of ReadStructuralFeatureAction.

    Connecting the ReadStructuralFeatureAction and the Fork



(iii) Incrementing the time value by one using an Opaque Behavior Action

We need an OpaqueBehaviorAction to increase the time value. An OpaqueBehaviorAction receives an integer value as its input parameter and returns the integer value, which is the value of the input parameter that has been incremented by one.

To create an Opaque Behavior for the increaseTime Activity


  1. Right-click the increaseTime Activity in the containment browser and select New ElementOpaque Behavior. A new Opaque Behavior will be created under the increaseTime Activity.
  2. Name it "increase".
  3. Right-click the increase Opaque Behavior in the containment browser and select Specification to open its Specification window.
  4. Select the Parameters node in the Specification window and click the Create button . A new parameter will be created for the increase Opaque Behavior, and the Parameter Specification window will open.

    Creating Parameters for the Opaque Behavior

  5. Name the parameter "ti", select Integer as its type, and select in as its direction.

    Specification of parameter

  6.  Click the Back button to return to the Specification window of the increase Opaque Behavior.
  7. Repeat the same steps used to create the parameter ti to create another parameter, which will be a return parameter of the increase Opaque Behavior.
  8. Name the return parameter "to", select Integer as its type, and select return as its direction.



  9. Click the Back button to return to the Specification window of the increase Opaque Behavior. 


Once the parameters "ti" and "to" have been created, we will write a simple JavaScript that will be executed during the simulation.

To create a simple JavaScript to be executed in the simulation


  1. Open the increase Opaque Behavior's Specification window and select the increase Opaque Behavior node on the left-hand side of the dialog.
  2. Type a JavaScript into the Body row, e.g., "to=ti+1".

    Typing a JavaScript for the Increase Opaque Behavior

    Note

    You can use any scripting language that is supported by JSR-223. To specify the scripting language that you are going to use, type it directly in the Language row. JavaScript will be used by default if no scripting language is specified.

  3. Click Close to close the Specification window. 
  4. Create an OpaqueBehaviorAction on the increaseTime Activity by dragging the created increase Opaque Behavior from the containment browser to the increaseTime Activity diagram. A new OpaqueBehaviorAction will be created with the pins that correspond to the defined parameters ti and to.
  5. Name the created OpaqueBehaviorAction "inc".
  6. Click ObjectFlow on the Activity diagram toolbar to connect the output pin t of the ReadStructuralFeatureAction and the input pin ti of the inc:increase OpaqueBehaviorAction .

    Adding the Increase Opaque Behavior to the IncreaseTime Activity



(iv) Setting the increased time value to the object of StopWatch using AddStructuralFeatureValueAction

Once the time value has been increased using the inc:increase OpaqueBehaviorAction, you need to set it back to the Object of StopWatch. To do this, you can follow the same steps used to create the AddStructuralFeatureValueAction on the resetTime Activity.


Adding an AddStructuralFeatureValueAction to Set the Increased Time Value


To set the increased time value to the object of StopWatch


  1. Follow the same steps used to create the AddStructuralFeatureValueAction on the resetTime Activity diagram.
  2. Get the AddStructuralFeatureValueAction to set the value to the time:Integer of StopWatch. Make sure that the IsReplaceAll of the AddStructuralFeatureValueAction is set to true.
  3. Click ObjectFlow on the Activity diagram toolbar to connect from the output pin to of inc:increase OpaqueBehaviorAction to the input pin t of the AddStructuralFeatureValueAction (see the steps required for resetting the time value of the StopWatch object to zero with AddStructuralFeatureValueAction in Creating resetTime Operation and resetTime Activity).
  4. Add an Initial Node and an Activity Final stage to the increaseTime Activity and click ControlFlow on the Activity diagram toolbar to connect between the Actions. 

    A Complete Model of the IncreaseTime Activity



(v)Setting the specification of the increase time Activity to the IncreaseTime Operation

To set the specification of the increaseTime Activity to the increaseTime operation


  1. Open the Specification window of the increaseTime Activity by right-clicking the increaseTime Activity in the containment browser and select Specification.
  2. Select the Specification row and click the button. The Select Element dialog will open.
  3. Select the increaseTime operation of the StopWatch Class as the specification of the Activity.
  4. Click OK to close the Select Element dialog.
  5. Click Close to close the Specification window.