7 Replies Latest reply on Jun 28, 2017 3:29 AM by neilg

    AFSDK FindElementsByAttribute not working

    neilg

      Hi All, Hoping someone can point out what I may be doing wrong.

       

      In an AF Database I have an element which has an "AssetID" attribute and a "ModelID" attribute. I am using AF SDK to search for this element by attribute but it is not giving me back the any results.

      This element drives from a template called "CB Gas Pressure and Quality" which in turn derives from a base template called "TestBaseTemplate". The AssetID and ModelID are attributes coming from the TestBaseTemplate.

       


      I have a WCF webservice which is making a call to AFSDK using the same code below. The Web Service was not working so I morphed it into a Windows Forms application to see if that was able to find the element but it is not able to do it as well. My initial thoughts were AFCache but I attempted to clear it without any luck.


      The code is as per screenshot below (hopefully screenshot comes out clear).

       

      In code I am getting a reference to the root AF element.I am doing a AFDB.Refresh and a AFCache.Clear (as a test). Next I am getting the "TestBaseTemplate". Next I am building a search query based on the base template attributes. Lastly I am making a call to AFElement.FindElementByAttribute but that always comes back with 0 elements. Any idea what I am doing wrong? Security is not an issue.

       

       

      CODE:

        • Re: AFSDK FindElementsByAttribute not working
          neilg

          Interestingly, The code is able to find an element one level higher up:

           

           

          This element derives from a template called "BAC" which derives from a template called "AssetBase" and AssetBase has the AssetID. One difference between this one and the one not working is that the only not working has string builder attributes but this one is a normal attribute. Would having String Builder attributes cause an issue?

          Code which works at the higher level:

           

            • Re: AFSDK FindElementsByAttribute not working
              neilg

              Hmm, after more playing around with this, If I add the AssetID and Model ID attributes directly onto the "CB Gas Pressure and Quality" template and change the code to do a search on this then I am able to get the right result.

               

              If it is at the Base template level then it returns 0 results. Any reason why this would happen? Does the method support search by Base Template attributes?

               

              I have the latest AF Server and the latest AFSDK installed.

               

              My workaround will be to change this by moving the attributes to the template level from the base but this does make it a bit untidy as I have 44 different templates that I have in this AF Database.

                • Re: AFSDK FindElementsByAttribute not working
                  pmartin

                  Neil,

                   

                  I've been playing around trying to replicate your issue but have been unsuccessful.

                  I have a template "Inheritance" which has a derived template "InheritanceDerived" which, in turn, has a derived template "InheritanceDerivedDerived".

                  An attribute "Constant" is defined in the Inheritance template.  In one instance of InheritanceDerivedDerived, I set the value to 115.  All other "Constant" attributes have a value of 100.

                   

                  This is the code I am using:

                  AFElementTemplate testTemplate = db.ElementTemplates["Inheritance"];
                   AFAttributeTemplate a = testTemplate.AttributeTemplates["Constant"];
                   Console.WriteLine(a.GetValue(a.DefaultUOM));
                  
                  
                    AFAttributeValueQuery[] q = new AFAttributeValueQuery[1];
                    q[0] = new AFAttributeValueQuery(a, AFSearchOperator.Equal, 115);
                  
                  
                    var assetTestElements = AFElement.FindElementsByAttribute(null, "*", q, true, AFSortField.Name, AFSortOrder.Ascending, 10);
                    foreach(AFElement e in assetTestElements) {
                         Console.WriteLine(e.Template);
                    }
                  

                   

                  The output is:

                  100

                  InheritanceDerivedDerived

                   

                  Is there something I am missing to replicate your issue?

                    • Re: AFSDK FindElementsByAttribute not working
                      neilg

                      Can you try this

                       

                      Setup AF like
                      Root - No attributes

                      ---> Sub Category - Add an AssetID attribute of type string. Set the value to 115.
                      --->---> Child (InheritanceDerivedDerived) - Add an AssetID attribute of type StringBuilder which references the parent Root attributes AssetID ('..\|AssetID';). This should be added to the "Inheritance" template.

                       

                      Now via code search for Child. What happens? In my testing if you have a StringBuilder it seems to trip the function up. What works is if instead of a StringBuilder looking up the value from the parent I put the attribute as a normal String attribute in Inheritance.

                       

                      One additional thing I have is I am searching on 2 attributes "AssetID" and "ModelID" both in Inheritance template. If AssetID was set to StringBuilder it failed. If it was set to normal attribute it worked. ModelID is a StringBuilder as well but difference is its looking at its own template (%Template%;).

                    • Re: AFSDK FindElementsByAttribute not working
                      Rick Davin

                      Hi Neil,

                       

                      Since my colleague Paul Martin  can't replicate your problem, it makes me wonder if the SQL Server indexes need maintenance.  This has been an occasional known issue with FindAttributesByPath.  I suggest you review my answer to this other post and see if that takes care of your issue.

                       

                      Good luck,

                      Rick

                    • Re: AFSDK FindElementsByAttribute not working
                      David Hearn

                      Does the attribute template for the attribute using the String Builder have a data reference defined in the template? If not, then the FindElementsByAttribute will always use the value stored in the SQL server and not know to evaluate the data reference for the element instances.

                       

                      Another possible issue is you may not have set your 'maxCount' parameter high enough. Since the search is using a data reference, it must evaluate it in the client and therefore your 'maxCount' parameter must be large enough to have enough elements returned from the server to have the data reference attribute filter not filter out all the desired results. If possible, you should switch to using the new AFElementSearch class which will not have this issue. See https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/T_OSIsoft_AF_Search_AFElementSearch.htm for more information about this new search.

                        • Re: AFSDK FindElementsByAttribute not working
                          neilg

                          Hi David, A bit of a late reply from me but what you mentioned around the maxCount is correct. When I change the maxCount to 100 from the 10 I had it starts returning the result as you say. Due to the results not having more than around 20 hits for the searches I am doing I will keep using the FindElementsByAttribute method for now.

                           

                          Thanks for the pointer.