In this lesson, we will talk about scopes and references. These will help you make rules and actions reusable and also access and manage data throughout your component.
There are two scopes in which you can declare rules, actions, and store data:
Additionally, there are three scopes available to store data:
<workflow [ATTRIBUTES]> <context> [...] </context> \ <actions> [...] </actions> -\ <rules> --\ <rule id="menu_button_selection"> ---\ <expression>[..]</expression> ----\ <actions> -----|--> Workflow scope [...] ----/ </actions> ---/ </rule> --/ </rules> -/ <steps> <step id="stepA" [ATTRIBUTES]> <context> [...] </context> ----\ <rules> [...] </rules> -----|--> Step scope <actions> [...] </actions> ----/ <states> <onevent> <rule ref="menu_button_selection"/> -------> Referencing a rule from the workflow scope <rule id="show_notification"> --\ <expression>[..]</expression> ---\ <actions> ----\ [...] -----|--> Direct definition (could still reference a pre-defined action) </actions> ----/ </rule> ---/ </onevent> </states> </step> <step id="stepB" [ATTRIBUTES]> <states> <onevent> <rule ref="menu_button_selection"/> -------> Referencing a rule from the workflow scope </onevent> </states> </step> </steps> </workflow>
In this example:
id="menu_button_selection"
is defined in the workflow scope and then referenced in both step id="stepA"
and step id="stepB"
using the ref
attribute.Note: Reusing rules and actions makes the behavior of your component consistent, improves maintainability, and minimizes the amount of code.
id="menu_button_selection"
is directly defined in the step scope it will be executed instead of any pre-defined rules with the same ID in the workflow scope.Note: IDs have to be unique within a scope. If you copy & paste an existing rule or action as a template when creating a new one, a typical mistake is forgetting to change the ID attribute. Make it a habit to change the ID first.
Refactor our choice component:
finish_workflow
action into the global scope and reference it in the rulemenu_button_selection
rule into the global scope and reference it in the stepDownload Component (Pre-Assignment)
Here are a few tips to make developing workflows and components a bit easier:
This mode allows you to immediately test your changes without having to publish the workflow. For more information, see the Preview Workflow section.
You can access the FCC and device logs by logging in with the sysadmin user or by directly accessing your UBIMAX_HOME\logs folder. The device logs are pushed to the server periodically, but you can request immediate upload as a sysadmin user.
If you did not have everything set up for development yet, that should have been the major task for this first practical assignment.
It might be noteworthy, that in the rule definition, you now reference a pre-defined action defined in the same scope. The order of the <actions>
and <rules>
tags do not matter for this to work. Here is what your component should look like:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <workflow xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" wfd_version="1.0" reporting="false" id="choice" name="choice" descriptor="Choice component" startstep="choose" xsi:noNamespaceSchemaLocation="../../../configuration/workflow.xsd"> <actions> <finish_workflow id="finish_workflow"> <output> <param name="selected_button" type="string">#{event:command}</param> </output> </finish_workflow> </actions> <rules> <rule id="menu_button_selection"> <expression>#{event:command} == 'APPLE' || #{event:command} == 'PEAR'</expression> <actions> <action ref="finish_workflow"/> </actions> </rule> </rules> <steps> <step id="choose" descriptor="the user selects between two options" uitemplate="ChoiceScreen"> <states> <onevent> <rule ref="menu_button_selection"/> </onevent> </states> </step> </steps> </workflow>
Download Component (Post-Assignment)
With this, you have finished the second lesson. The next lesson will be about data variables.