Create a Custom Activity

VPL allows users to create custom activities, which you build by dragging blocks onto the activity design surface. To create a new activity, move an instance of the basic activity named Activity onto the main diagram. You can then double-click this activity to get to the activity design surface.

The activity design surface looks slightly different from the main diagram (see Figure 3-17). The square in the left border represents the input, and the square in the right border is the output. The circle in the right border relates to notifications, and an activity can have none, one, or several notifications associated with it.

The activity design surface includes orange squares and a circle to represent the input and output data values as well as notifications.

Figure 3-17. The activity design surface includes orange squares and a circle to represent the input and output data values as well as notifications.

You define one or more actions for each activity. For example, you could have an Add action and an Update action for the same activity. You access the Actions And Notifications dialog box (see Figure 3-18) by clicking the icon next to the Action drop-down list box. Actions are user-defined, and you can name them whatever you prefer. Each action can have one or more input and output values associated with it. The action is selected when you connect the activity to another block in the main diagram.

Use the Actions And Notifications dialog box to define input and output values associated with an activity’s user-defined action.

Figure 3-18. Use the Actions And Notifications dialog box to define input and output values associated with an activity’s user-defined action.

To demonstrate how this might work, consider a VPL program that simply counts to 10,000 and then logs that number as a trace message, as shown in Figure 3-19. This program is an example of a program that performs a loop.

Simple VPL program that counts to 10,000 and logs the result as a trace message.

Figure 3-19. Simple VPL program that counts to 10,000 and logs the result as a trace message.

A better way of performing this same task would be to use a concept known as recursion. Instead of using a loop, in which a block loops back and references itself, you could use a user-defined activity. Recursion is an alternative to looping, and it generally results in more readable code. For example, to replace the VPL program shown in Figure 3-19, you could create a user-defined activity named Increment. The block for the new activity would exist not only in the main program, but there would be a reference to it within the user-defined activity as well (see Figure 3-20).

User-defined activity named Increment. This activity was created to implement a process known as recursion.

Figure 3-20. User-defined activity named Increment. This activity was created to implement a process known as recursion.

Tip

Tip

When implementing loops or recursive activities, it is a good idea to run the program in debug mode. This ensures that you are able to step through the program and to ensure that no infinite loops exist. An infinite loop could potentially cause problems for your development machine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.4.221