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
- Right-click the StopWatch Class in the containment browser and select New Element > Operation.
- Name the new operation "increaseTime".
- Right-click the StopWatch Class in the containment browser and select New Element > Activity.
- Name the new Activity "increaseTime".
- 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 a 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
- Click Fork Vertical on the Activity diagram toolbar.
- Click the increaseTime Activity diagram to create a Fork.
- Click ObjectFlow on the Activity diagram toolbar to connect the fork and the self output pin.
(ii) Obtaining the current time value using a 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
- Click Action > Any Action on the Activity diagram toolbar. The Select Action MetaClass dialog will open.
- Select ReadStructuralFeatureAction and click OK.
- Click the increaseTime Activity diagram. A new ReadStructuralFeatureAction will be created on the diagram.
- Right-click the ReadStructuralFeatureAction symbol on the diagram and select Specification to open its Specification window.
- Select the StructuralFeature row and click the button. The SelectProperty dialog will open.
- Select the Attribute time:Integer[1] of the StopWatch Class and click the OK button.
- Click ObjectFlow on the Activity diagram toolbar to connect the Fork and the self input pin of ReadStructuralFeatureAction.
(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
- Right-click the increaseTime Activity in the containment browser and select New Element > Opaque Behavior. A new Opaque Behavior will be created under the increaseTime Activity.
- Name it "increase".
- Right-click the increase Opaque Behavior in the containment browser and select Specification to open its Specification window.
- 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.
- Name the parameter "ti", select Integer as its type, and select in as its direction.
- Click the Back button to return to the Specification window of the increase Opaque Behavior.
- 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.
- Name the return parameter "to", select Integer as its type, and select return as its direction.
- 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
- Open the increase Opaque Behavior's Specification window and select the increase Opaque Behavior node on the left-hand side of the dialog.
Type a JavaScript into the Body row, e.g., "to=ti+1".
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.
- Click Close to close the Specification window.
- 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.
- Name the created OpaqueBehaviorAction "inc".
- 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 .
(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.
To set the increased time value to the object of StopWatch
- Follow the same steps used to create the AddStructuralFeatureValueAction on the resetTime Activity diagram.
- 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.
- 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).
- 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.
(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
- Open the Specification window of the increaseTime Activity by right-clicking the increaseTime Activity in the containment browser and select Specification.
- Select the Specification row and click the button. The Select Element dialog will open.
- Select the increaseTime operation of the StopWatch Class as the specification of the Activity.
- Click OK to close the Select Element dialog.
- Click Close to close the Specification window.