3 Replies Latest reply on Jan 18, 2012 2:27 PM by pramod.gupta

    How to get State name and state code from notification condition

    pramod.gupta

      Hello Everyone,

       

      Is there anyway to get state name and state code from notification condition.

       

       In Following image, i have defined some conditions. i want to get Result column value using this condtion/Rule.

       

      Result value is basically a attribute name of AFattributes in AFElement. so if i know the state name i.e attribute name also . i can access the attribute value.

       

       

       

      1122.Scrrenshot.jpg

       

       

       

      Please help me on this.

       

      Thanks

        • Re: How to get State name and state code from notification condition
          Mike Zboray

          Hi Pramod,

           

          One question: Are you trying to get the state for a notification instance or just from the current configuration?

           

          In the first case, code like this would work (C#):

           

           

           
          ANInstance instance = ANNotification.GetLastInstance(notification);
          string name = instance.State.Name;
          int code = instance.State.Code;
          

          In the second case, there is a bit more work to do. Each notification has an analysis object. The analysis has an analysis rule, which in turn can have child analysis rules. The analysis normally has a top level analysis rule that is called 'Condition Group'. Then each trigger condition on the notification is (for analysis rules that are not SQC) represented as a 'Sequential' analysis rule in the AnalysisRules collection of the 'Condition Group'. Each 'Sequential' analysis rule then has three sub analysis rules. The first corresponds to the type of analysis rule ("Performance Equation" or "Comparison"), the second is the 'Time True' analysis rule, and the last is the 'StateAndPriority' analysis rule. The ConfigString of this analysis rule has the required information. You will need to parse the ConfigString which has the format "key1=value;key2=value2;". From there you can get the states/stategroups from the pisystem. Here is some code that does what I described:

           

           

           
          AFAnalysisRule conditionGroup = notification.Analysis.AnalysisRule; // top level analysis rule
          AFAnalysisRule sequential = conditionGroup.AnalysisRules[0]; // the first one in the list of trigger conditions. you may want to loop through these to get all the states
          AFAnalysisRule stateAndPriority = sequential.AnalysisRules[2]; // StateAndPriority analysis rule is third in this list.
          int code = int.Parse(Regex.Match(stateAndPriority.ConfigString, @"code\w*=\w*(\d+)\w*;", RegexOptions.IgnoreCase).Groups[1].Value); // parse out 'code = ###' from config string
          ANNotificationState state = ANNotificationStateGroups.GetGroups(system).GetState(code); // get the state from the code.
          

          I hope this helps.

           

          Mike

           

           

           

           

           

           

            • Re: How to get State name and state code from notification condition
              mhamel

              Hi Parmod,

               

              A notification is represented by an AFNotification object from the OSIsoft.AF.AFNotification namespace. The AFNotification object contains an AFAnalysis object to define how a notification is generated. The AFAnalysis owns different sets of rules to test the conditions of firing, to set a level of priority, etc. If you want to learn more on how a notification is built you can consult the PI Notification Programmer's guide which is available with the PI Notifications Developer Tools in the Download section. The PI Notification Programmer's guide presents a section on how to create notification rule programmatically.

               

              To achieve what you want, you will need to browse the nested analysis rules collection to find out the state code and name. There is no easy way as you have to dig into each analysis rules collection of every level. I propose you the example below that looks for 3 levels deep (in VB.NET) all the rules referring to the StateAndPriority rule and add it to an array for parsing at a later time. You can use regular expressions as suggested by Mike to extract easily the Code and Name attributes.

               

              Dim MyAnalysis As AFAnalysis = MyNotification.Analysis
              
              'This search goes to 3 levels deep only.
              For Each CurrentRule1stLevel As AFAnalysisRule In MyAnalysis.AnalysisRule.AnalysisRules
                   'Look for a StateAndPriority rule, if found get the configstring property.
                   If CurrentRule1stLevel.Name = "StateAndPriority" Then
                        Items += 1
                        Array.Resize(ConfigString, Items)
                        ConfigString(Items - 1) = CurrentRule1stLevel.ConfigString
                   End If
              
                   'Validate if there are children.
                   If CurrentRule1stLevel.HasChildren Then
                        For Each CurrentRule2ndLevel As AFAnalysisRule In CurrentRule1stLevel.AnalysisRules
                             'Look for a StateAndPriority rule, if found get the configstring property.
                             If CurrentRule2ndLevel.Name = "StateAndPriority" Then
                                  Items += 1
                                  Array.Resize(ConfigString, Items)
                                  ConfigString(Items - 1) = CurrentRule2ndLevel.ConfigString
                             End If
              
                             'Validate if there are children.
                             If CurrentRule2ndLevel.HasChildren Then
                                  'Continue with children...
                                  For Each CurrentRule3rdLevel As AFAnalysisRule In CurrentRule2ndLevel.AnalysisRules
                                       'Look for a StateAndPriority rule, if found get the configstring property.
                                       If CurrentRule3rdLevel.Name = "StateAndPriority" Then
                                            Items += 1
                                            Array.Resize(ConfigString, Items)
                                            ConfigString(Items - 1) = CurrentRule2ndLevel.ConfigString
                                       End If
                                  Next
                             End If
                        Next
                   End If
              Next
              
              'Parse the config. strings stored in the array, it is on the form of Code=x,Name=y,Priority=z
              

               Let me know if you that worked for you.

                • Re: How to get State name and state code from notification condition
                  pramod.gupta

                  Hello Mike and Matheiu for the quick repsonse.

                   

                  I am very new to OSI-PI . and just completed virtual training . There was a requirment in my project to get state name using notification condtion. I was trying to find solution my own. then I posted it on this Forum.

                   

                  I got the solution last night when i was debuging the app. but i was not sure why the state name, state code and priority comes at third index in analysis rule.

                   

                  I was glad to see the Mike's comment to find the state name.and it was exactly same approach i used.

                   

                  The code i have written is

                   

                  private string GetStateNameFromARule(AFAnalysisRule rule)

                   

                         {

                   

                              string stateName = "";

                   

                              if (rule.AnalysisRules.Count > 1)

                   

                              {

                   

                                  string configString = rule.AnalysisRules[2].ConfigString;

                   

                                  if (string.IsNullOrEmpty(configString))

                   

                                      return "";

                   

                                  string[] configValues = configString.Split(";".ToCharArray(), StringSplitOptions.None);

                   

                                  if (configValues != null && configValues.Count() > 2)

                   

                                  {

                   

                                     string state = configValues[1];

                   

                                     if (!string.IsNullOrEmpty(state))

                   

                                     {

                   

                                         string[] stateValue = state.Split("=".ToCharArray(), StringSplitOptions.None);

                   

                                         if (stateValue!= null && stateValue.Count() >0)

                   

                                             stateName = stateValue[1];

                   

                                     }

                   

                                  }

                   

                              }

                   

                             return stateName;

                   

                         }

                   

                  Thank you so much guys for your help. I really appreciate it.