Rules & Scripts

Making OPI displays is flexible using the Palette and the Properties, but the resulting displays are still fairly static. But what if we want to make the position of a widget dynamic based on a PV? Or if we want to dynamically change widget colors?

Rules

You may have noticed that every widget has a Rules property. Rules are a user-friendly way for adding dynamic behaviour to widgets. They are most often used for changing the widget’s properties at runtime.

Example

This is best explained with an example. Suppose we have the bright idea to make an LED square when it is off, and round when it is on. The static properties would not allow for such a scenario, we therefore add a rule.

  1. Edit the Rules property to pop up this dialog.

    Attach a Rule

  2. Clicking the plus icon gives you this dialog:

    Edit Rule

  3. The first thing to choose is the rule’s target Property. So select Square LED.

  4. In the right Input PVs table add your input PV. In this example we chose to generate an alternating 0/1 value using a simulated PV. Notice the sequential number in the # column. The first PV is numbered 0. Make sure to check the Trigger checkbox as this will then trigger the execution of the rule whenever the PV’s value is updated.

  5. Now in the Expressions table, fill in your conditions in the Boolean Expression column, and add the desired value of the rule’s property in the Output Value column. The double value of the top-most right PV is available as the variable pv0. The next PV in the list (if applicable) is available as the variable pv1, etc.

    Example Rule

  6. Confirm your dialogs, save your display and refresh a runtime view of it. You should see the LED’s shape now alternating between square and ground.

One can see that this example could be made arbitrarily complex by adding more rules and/or expressions.

Boolean Expression

This input field needs to be expressed in JavaScript. The Input PVs are available in different formats:

Type Example
Double Value pv0 == 2.2
String Value pvStr0 != 'abc'
Integer Value pvInt0 >= 2

In addition, you can access the numeric alarm state of an input PV.

Alarm Example
Invalid pvSev0 == -1
No Alarm pvSev0 == 0
Minor Alarm pvSev0 == 1
Major Alarm pvSev0 == 2

If you wish to set a property value that always applies, use true (or 1==1) as the Boolean Expression.

Output Value

The exact form that the Output Value column adopts depends on the type of the property. Some properties are colors, so you would see a color picker, other properties expect text, and the above example was a boolean yes/no, so we got a checkbox.

Scripts

For more advanced dynamic runtime behaviour, we can write scripts (actually Rules are a thin layer on top of scripts). With scripts we can write arbitrary logic that can dynamically manipulate just about any combination of properties for a widget.

Yamcs Studio supports two dynamic languages: JavaScript and Python. Both languages can be used to the same effect, and are available without any external dependencies. As of now, there is no advanced editor support bundled with Yamcs Studio though, so scripts are edited with a plain text editor.