Expression simplification with refactoring
- Last UpdatedJan 08, 2025
- 2 minute read
- PI System
- PI Server 2018
- PI Server
In real-world applications, the expression in an analysis can be extremely complex; lengthy attribute names can add to the complexity. To simplify a complicated expression, refactor it into a group of smaller expressions assigned to variables.
Refactored analyses are easier to understand. Refactored analyses are also easier to test and debug: you can evaluate the smaller expressions one at a time and isolate any errors.
To refactor an analysis, define components of the analysis as separate expressions and reference by variable name. For example, you might define a separate expression that defines a long attribute name as a separate variable or that defines a particular calculation as a separate variable. To create these separate expressions, simply add new expression rows to the analysis and enter text in the Name and Expression columns.
To reduce typing, you can select a term in an expression, right-click the selection, and then click Assign to variable. In the row above the expression, PI System Explorer inserts a new row that maps the selected term to a new variable name. In the original expression, PI System Explorer replaces the term with the new variable name.
You can edit the name of any row. However, if you change the name of an expression row already used as a variable in other expressions, you break the connection between the expressions.
Example
Suppose you have a complex expression defined in a single row:
|
Name |
Expression |
|---|---|
|
Variable1 |
2*'LongAttributeName' + Avg('Att2', 'Att3', 'Att4') |
You can refactor the expression into the following three rows that contain simpler expressions. Note that the third row uses the names of the earlier rows as variables.
|
Name |
Expression |
|---|---|
|
Variable1 |
'LongAttributeName' |
|
Variable2 |
Avg('Att2', 'Att3', 'Att4') |
|
Variable3 |
2*Variable1 + Variable2 |