How to Create and Use a Rule

Rules are special objects that can be used to regulate automatically executed actions. These examples will show how a Rule can be used to filter and limit automatic object creation when browsing an OPC DA server’s namespace.

For more information on how rules work, see the Rules Jumpstart

General Rule Creation

  • Creating a Rule is an administrative task, so firstly log in to DataStudio as administrator.

  • Once logged in, navigate to the I/O Model panel and expand the object tree to the Connector level and right click the Connector object to open its context menu. In the menu select Admin > New > Rules > Rule Node.

Create Rule Node from Context Menu
Figure 1. Create Rule Node from Context Menu
  • For this purpose of this example we will create a Rule Node first, as this is a good place to store multiple rules in one place. This is optional however and rules can be created outside of Rule Nodes if needed.

  • In the Create Object wizard, enter a name for the Rule Node and click Create.

Create Rule Node
Figure 2. Create Rule Node
  • Right click on the newly created Rule Node and in the context menu select Admin > New > Rules > Rule.

Create New Rule Context Menu
Figure 3. Create New Rule Context Menu
  • In the Create Object wizard, enter an Object Name and Object Description and change the Target, via the drop down menu to IO-Item. Also change Action to Discard.

Create Rule Common Settings
Figure 4. Create Rule Common Settings
  • Click on the "…" button next to the Conditions property to edit the conditions that will trigger the rule.

  • In the Conditions Builder dialogue click Add Condition and set the conditions to those shown below:

Conditions Builder - Create New Rule
Figure 5. Conditions Builder - Create New Rule
  • This applied condition will trigger the rule to discard any I/O items that do not begin with "I".

  • Click Apply to return to the Create Rule common settings page, then click Create to create the Rule. It will then be visible in the I/O model panel.

Created Rule in I/O panel
Figure 6. Created Rule in I/O panel
  • To use the rule, delete the children underneath the OPC DA Data Source by selecting Admin > Delete > Children in the context menu of the data source, then browse again by selecting Admin > Advanced Functions > Browse Once.

  • Expand the Data Source object tree in the I/O Model panel to view the I/O items: the browse only returns the I/O items with object name beginning with "I".

Filtered Browse Results
Figure 7. Filtered Browse Results

The implementation of the Browse Rule has the same effect as the Leaf Filter example described in an earlier section. However, using rules to manage browsing provides more options and control of the filtering process.

Using a Rule to Set Object Properties

In the following example we will create a new rule that demonstrates how we can automate the discovery and change the properties of I/O items.

  • Right click on the Rules Node that you created in the last section and select Admin > New > Rule in the context menu.

  • In the Create Object wizard, enter an Object Name and Object Description and change the Target, via the drop down menu, to IO-Item. Also change Action to Script.

Create Rule Common Settings
Figure 8. Create Rule Common Settings
  • Click on the "…" button next to the Conditions property to edit the conditions that will trigger the rule.

  • In the Conditions Builder dialogue click Add Condition and set the conditions to those shown below (the Data Type option is under IO Item Server Information section of the Model Property menu). Select Apply to apply the conditions and return to the Common Properties Menu:

Conditions Builder - Create New Rule to Set Object Properties
Figure 9. Conditions Builder - Create New Rule to Set Object Properties
  • This applied condition will trigger when the selected rule target (a Discovered Object of the type I/O Item) has a Data Type containing VT_I (VT_I1, VT_I2, VT_I4 and VT_INT are all OPC Integer data Types)

  • In the Common Properties dialogue click on the "…" button next to the Lua Script Body to bring up a script editor window and enter the following code:

return function(obj) obj.ArchiveOptions.StorageStrategy = "STORE_RAW_HISTORY" obj.ObjectDescription
= "Integer Value" obj:commit() end
  • This script changes two of the properties of the discovered object (obj in the script). In the function, the first command turns on the archiving of the Raw History of the discovered object and the second sets the object description to Integer Value.

  • The script should be displayed in the script editor as shown below:

Lua Script for New Object Rule
Figure 10. Lua Script for New Object Rule
  • Click Ok in the Script Editor Window and then Create in the wizard to create the rule in the I/O Model tree.

Created Rules in I/O Model panel
Figure 11. Created Rules in I/O Model panel
  • Now, delete the children from the OPC DA data source using the context menu and browse the data source again.

  • The two rules present in the Rule Node filter and change the object properties of discovered items based on the conditions applied in each rule. Therefore the discovered objects are filtered to the "I" items (as before) and these items properties are changed to archive Raw History and the description change to read "Integer Value" (See figure below).

Browse Results - Browse and Object Properties Rules
Figure 12. Browse Results - Browse and Object Properties Rules

Rules can be disabled at any point (context menu Admin > Disable or shortcut F9), and only one or none of the rule conditions and actions can be enabled at a time. Browsing can be controlled using multiple rules if required.

Training Task: Create additional rule Create and additional rule using two conditions and combine them with the logical "OR" operator. Where can rules be created? Does the location make a difference?