Workflow as a Block

This tutorial demonstrates one of the core pSeven features — the ability to use an existing workflow as a block in another workflow.

Note

This tutorial requires the workflow from Conditions and Error Handling.

Before You Begin

This tutorial requires an existing prjTutorials project. If you have not created this project yet, see Tutorial Project first.

  • Create a new empty workflow. If you have never created a workflow before, see the Simple Workflow tutorial.
  • Save the workflow as wfToySolverBlock.
  • Switch to Workspace and verify that the workflow file (ToySolverBlock.p7wf) is added to project. You can see it in the Project pane.
  • While in Workspace, verify that the wfErrorHandling workflow exists in your project. This workflow is explained in the Conditions and Error Handling tutorial and is required to continue.
  • Switch to Edit and select the workflow you have just created to continue with the tutorial.

Task

In Integration Basics and Conditions and Error Handling, the Toy Solver integration workflow was primarily discussed as a way to create a convenient interface to the program. A more interesting question, however, is how to integrate Toy Solver into workflows that require multiple evaluations during run-time — for example, output sampling, approximation, or optimization. All such workflows would have a common part, the same set of wrapper blocks. So, the task to begin with is to figure out how to re-use the existing integration workflow instead of configuring the same set of blocks for every new workflow.

Solution

As it was noted in the Workflow as a Tool tutorial (see section Workflow Inputs and Outputs), every pSeven workflow is contained inside a special root block. It is rather natural that the root block can be used as a new custom block, in fact including one workflow into another. General steps are:

  • Use the workflow import function to add an existing workflow as a block.
  • Review inputs and outputs of the new block to link it with other blocks.

Workflow Import

Workflow import itself is simple: in general, you only need to select a workflow and specify the name for a new block.

../_images/page_tutorials_workflow_block_01_menu.png
  • Select Import Workflow... from the edit toolbar context menu to open the Import Workflow dialog.
../_images/page_tutorials_workflow_block_02_import.png
  • Specify the name for a new block: Model.
  • Click b_browse and select the wfErrorHandling workflow from the project (ErrorHandling.p7wf).
  • Leave other settings default and click b_import. Note that you can also import parameter presets and monitoring settings, but it is not required at the moment.

As a result, a new Composite block named Model appears in the workflow. If you expand it, you can see that this block contains the wfErrorHandling workflow.

../_images/page_tutorials_workflow_block_03_model.png

Workflow structure (blocks and links) is preserved, and its root ports (that is, the workflow input and output) become ports of the Model block.

../_images/page_tutorials_workflow_block_04_mports.png

Further, to use the new Model block you will need to know its port properties. In this example they are already known from previous tutorials, but the next section provides an example of testing the block after import.

Block Review

To understand how a block is used in a workflow, it is important to consider the following properties:

  • The data types of its inputs and outputs.
  • What is the expected input and what data the block outputs.

Port data types can be found from the Ports tab in block configuration (both input and output are RealVector).

As for the expected input, note the value on Model.x which was added on import ((0.5, 0.5) in the example case; actually it will be the last input value you have specified in the Conditions and Error Handling tutorial). It already allows to guess that Model.x expects a 2-dimensional RealVector; also see the Ports tab in the Model configuration.

../_images/page_tutorials_workflow_block_05_mports_conf.png

To find the Model.f dimension, you can test the Model block with default input and note the output value.

  • Uplink Model.f.
../_images/page_tutorials_workflow_block_06_uplink.png
  • Run the workflow and see the Outputs pane in Run.
../_images/page_tutorials_workflow_block_07_testoutput.png

Note that Model.x is not connected, but the workflow runs because there is a value assigned to this port, which is accepted as input. Test output is a 2-dimensional RealVector (as expected).

  • Remove the root f port (created due to uplinking Model.f). The link will be removed automatically.

Another option is to monitor Model.f instead of uplinking it. This way you can find the output dimension from the project database record.

Workflow

Final workflow should contain a single Model block. Further it will be used as a starting point for new workflows.

../_images/page_tutorials_workflow_block_08_wf.png

Note that the Model block works the same as the Conditions and Error Handling workflow, but it can connect to other blocks instead of accepting inputs from the user.

Conclusion

This tutorial is an intermediate step intended to simplify several tutorials following next.

  • Save the workflow you have created in this tutorial: it will be required further.

The following tutorials use the new Model block to explain some of the advanced pSeven features:

  • Sampling Loop — explains how to perform batch calculations with Toy Solver and provides an example of creating loops.
  • Parallelization — an improved version of the batch calculation workflow providing an example of block parallelization.
  • Caching — an example of using block input-output cache for efficient calculations.

The following tutorials show using the Model block in typical tasks: