6 Replies Latest reply on Jun 14, 2018 10:11 PM by TuiBray

    PI AF - If then else statement

    TuiBray

      Hi.  I'm new at using PI AF so may be completely on the wrong track (I have done a course but many years ago and have not used it since). 

       

      I'm trying to determine the value of a newly created Tag (TagX) based on the value of several other tags.  In Attributes I have loaded the criteria tag values.  Then in Analyses I have created an IF then else statement as below to determine the value of TagX.  My problem is that the If Then Else statement is very large and difficult to edit.  Is there a better way of doing this?

       

      If 'TagA'>25 and 'TagB' < 35 and 'TagC' > 5 and 'TagD'<35 and 'TagE>25 then 'TagF+ 'TagG'- 'TagH'

      else (if 'TagA'>25 and 'TagB' < 35 and 'TagC' > 5 and 'TagD'<35 and 'TagE<25  then 'TagI

      else (if 'TagA'>25 and 'TagB'>35 and 'TagC'>5 and 'TagD'>35 and DigText('TagJ)="Active" then 'TagK'

      else (if 'TagA'>25 and 'TagB'<35 and 'TagC'>5 and 'TagD'>35 and 'TagE>25 and DigText('TagJ)="Active" then 'TagG'-'TagH'

      else (if 'TagA'>25 and 'TagB'<25 and 'TagC'<5 and 'TagE>25 and DigText('TagJ)="Active" then 'TagF+ 'TagG''TagH'

      else (if 'TagA'>25 and 'TagB'>35 and 'TagC'>5 and 'TagD'<35 and 'TagE>25 and DigText('TagJ)="Active" then 'TagF

      else (if 'TagA'<25 and DigText('TagJ)="Active" then 0 else NoOutput()))))))

        • Re: PI AF - If then else statement
          sraposo

          Hi Tui,

           

          I don't think there is a way in PI System Explorer to expand the expression box. You can copy/paste from a text editor, but then of course you would be missing out on the intellicense provided in the UI.

           

          I was going to go and make a user voice request on our feedback website. Before doing so, can you share how you think this could be made more user friendly? I'd include the details you provide in the request.

           

          Thanks,

          Seb

          • Re: PI AF - If then else statement
            Rick Davin

            Hello Tui,

             

            My Houston co-worker, Ales Soudek, wrote a fantastic paper called Tips and Tricks for Asset Based Analytics.  On page 9, he discusses breaking Analysis into many expressions, each with their own intermediate variable.  On page 10, he mentions how you may use SHIFT+ENTER to force a line break in the middle of an expression - this is great to do with a long IF-THEN-ELSE where you may break right before the THEN and again before the ELSE so that it appears as:

             

            If condition /* page 28 mentions inline comments */
            Then value1

            Else value2

             

            You can shorten somethings with its own variable expression:

             

            Variable: JActive    

            Expression: DigText('TagJ')="Active"

             

            You may consider different branching.  For instance, TagA'>25 and 'TagB' < 35 covers 3 long cases.  You could group them as one:

             

            If TagA'>25 and 'TagB' < 35 Then

            /* different conditions, shorter now because A>25 and B<35 is true */

            Else If TagA'>25 and 'TagB' > 35 Then

            /* other conditions */

            Else 'TagA'>25 and 'TagB'<25 Then

            /* still more conditions */

            Else If TagA'<25 And JActive Then 0 Else NoOutput() ))))

             

            Although I would also bring up that if 'TagA' was exactly equal to 25 that the NoOutput() would be assigned.

            2 of 2 people found this helpful
              • Re: PI AF - If then else statement
                TuiBray

                Thanks Rick.  That's really handy .  I had already shortened it from the 14 conditions I was given, but I can see there may be some more scope for doing this.  And great point about values that are exactly 25.  Thanks for your help.

                  • Re: PI AF - If then else statement
                    Rick Davin

                    Hello again Tui,

                     

                    Great that you understand that 'TagA' < 25 is not the opposite of 'TagA' > 25.  The opposite of 'TagA' > 25 is actually 'TagA' <= 25.

                     

                    With the above in mind, there may be an opportunity to make the expression more readable.  Let's say the main or good condition for TagA is when it is > 25.  And your good condition for TagB is when it is < 35, and so on.  If that is the case, then you can do something like:

                     

                    Variable
                    Expression
                    A'TagA' > 25
                    B'TagB' < 35
                    C'TagC' > 5
                    JDigText('TagJ') = "Active"

                     

                    This reduces the logic of the main expression to things like:

                     

                    If A and B and C etc.

                     

                    For the case where you want to check 'TagA' > 25' and 'TagB' >= 35, that is to say when you want the opposite of 'TagB' < 35, then the logic stub would be:

                     

                    If A and Not B and etc.

                     

                    By combining certain conditions into their own variable, you could end up with:

                     

                    2018-06-08 10_40_11-Window.png

                    3 of 3 people found this helpful