11 Replies Latest reply on Jan 7, 2013 6:53 AM by Anwar005

    Simplest  method to get the PI tag name for an attribute

    K@8983

      Getting the PI tag from the configuration string seems cumbersome. Is there a simple method that will give me the PI tag for a data reference.

        • Re: Simplest  method to get the PI tag name for an attribute
          MvanderVeeken

          Have a look at the AFAttribute.RawPIPoint property. This returns the PISDK.PIPoint for a PIPointDataReference

            • Re: Simplest  method to get the PI tag name for an attribute
              andreas

              Keyur, what is the reason for getting the tagname? You may want to take a look at the AF CTP that includes RDA - maybe this neglects the need for getting the PI tag.

                • Re: Simplest  method to get the PI tag name for an attribute
                  cmanhard

                  RawPIPoint will work but has a couple downsides:

                  • If the tag cannot be resolved (for example, no communications to the PI Server), it will fail
                  • If not previously resolved, it could have an expensive as it may require a call to the server to resolve the name

                  AFAttribute.RawPIPointPath(AFEncodeType.Name) will return a string in the format of "\\server\tag", with substitution parameters resolved.  You would still have to strip the tag name off. 

                   
                                  string name = att11.RawPIPointPath(AFEncodeType.Name);
                                  int index = name.LastIndexOf('\\');
                                  if (index >= 0)
                                      name = name.Substring(index + 1);
                  

                   

                   

                   

                    • Re: Simplest  method to get the PI tag name for an attribute
                      MvanderVeeken

                      Wanted to find out what the smallest piece of code was to get the name using the RawPIPointPath like Chris indicated. I came up with this:

                       

                       

                       
                      Regex.Match(att.RawPIPointPath(AFEncodeType.Name), @"\\\\(.*)\\(.*)").Groups[2].Value;
                      

                       

                       

                       

                       

                       

                        • Re: Simplest  method to get the PI tag name for an attribute
                          Rick Davin

                          While the original post has been answered with RawPIPointPath, I’d like to belabor the topic in my quest to be King of Minutia.  Obviously, the code posted by both Chris and Michael only works if you have the a priori knowledge that you are dealing with a PI Point.  More precisely, it works for attributes that have a PI Point data reference … but not necessarily a validated PI Point assigned to the ConfigString.  If you rely upon templates and a wicked amount of substitution parameters, then the fully resolved ConfigString may refer to a PI Point that doesn't exist.

                           

                          If your code checks for an attribute that has a PI Point data reference, both code snippets from Chris and Michael work.   If not, an exception will be thrown along the lines of:

                           

                          Failed to extract PI Point from 'Attribute 'your attribute' ...

                           

                          However, a minor change to each can get it working for non-PI Point attributes.  By ‘working’, I mean you would get an empty string for non-PI Points, but it would do so without throwing an exception.

                           

                          LastIndexOf Snippet

                           

                          string name = attr.RawPIPointPath(AFEncodeType.Name);
                          if (name != null) {
                               int index = name.LastIndexOf("\\");
                               if (index >= 0)
                                    name = name.Substring(index + 1);
                          }
                          

                          Regex Snippet

                          string Name = attr.RawPIPointPath(AFEncodeType.Name);
                          if (Name != null)
                          Name = Regex.Match(Name, @"\\\\(.*)\\(.*)").Groups(2).Value();
                          

                           RawPIPoint Object Snippet (expensive)

                          string Name = null;
                          object TagObj = attr.RawPIPoint;
                          if (TagObj != null)
                               Name = ((PISDK.PIPoint)TagObj).Name;
                          

                          So let’s look at some relative performance numbers.  Remember your actual results will vary based on a dizzying array of factors.  Don’t focus on the absolute numbers as much as the relative difference among them as we attempt to fetch the name:

                          (A) an actual PI Point,
                          (B) a bogus PI Point that doesn’t exist, and
                          (C) something else that doesn’t use a PI Point DR.

                          As for performance differences between the LastIndexOf versus Regex, they were quite similar in timing.  Whichever one you chose it a matter of aesthetics.  In my testing, their performance was equivalent in each respective instance.

                          For our baseline factor of 1X or simply X, we will use the RawPIPointPath in instance (A).  In other words, our baseline time of X equals how long it takes to fetch the RawPIPointPath from a real PI Point.  So how does that compare to having (B) a bogus PI Point or (C) something that isn’t a PI Point?  And how expensive is the RawPIPoint call compared to the RawPIPointPath?

                           

                           

                           

                           

                           

                           

                           

                           

                           

                           

                          RawPIPointPath Results:

                           

                          A -   X  (baseline)
                          B -   (2/3) X
                          C -   ~0 ms

                           

                          RawPIPoint Results:

                           

                          A -    2 X
                          B -    1.5 X
                          C -    0.5 X

                           

                          Your actual results may vary. May cause abdominal distension.

                           

                          So if all you want is the tag name, avoid RawPIPoint and instead use RawPIPointPath.