10 Replies Latest reply on Jan 13, 2017 5:05 PM by pthivierge

    Created element with analyses is not allways seen thdough AF Sdk

    VarbanVarbanov

      Hello, I am crating AF Elements along with AF Analysis which target these elements. Elements are allways cretaed in same root element. However sometimes these element cannot be seen in Pi System Explorer and through search of an element children elements though AF Sdk (using element.Elements where element is the root element). At the same time these elements, which cannot be seen are present in the PIFD and their parents can be verified to be the correct ones using ownerid column. Also these elements can be found if the analyse which is targeting them is found and their parent (element.Parent) is the mentioned root. I hope the explanation is not confusing, in summary - using same code - sometimes elements can be seen, more often they cannot but still thei are present in the db and their parent can be verified even through sdk (i.e, they are not in some other place). The code I use for creating is as follows (some not important parts missing):

       

          AFElement element;
                     AFElement calculationRoot = GetOrCreateCalculationRoot();
                     element = new AFElement(an.Name);
                     if (calculationRoot.Elements[an.Name] != null)
                    {
                          throw new InvalidOperationException(
                              $@"Attempt for creating new analysis with name {an.Name} failed, because such already exists in default analysis root! 
                                 Consider changing the name or sending currently existing analysis element path to perform an update instead.");
                    }
                   calculationRoot.Elements.Add(element);
                      
                  AFAttribute att = element.Attributes.Add(AnalysisAttributeName);
                  var anPlugin = _connection.PISystem.DataReferencePlugIns["Analysis"];
                  if (anPlugin == null)
                  {
                      throw new InvalidOperationException(
                          $"An attempt was made for creating af analysis: {an.Name}, but analyses reference plugin was not installed on the server: {_connection.PISystem.Name}. Please check the af analysis installation on the server");
                  }
                  att.DataReferencePlugIn = anPlugin;
                  existingAnalyse = new AFAnalysis(_afDatabase, an.Name);
                  existingAnalyse.Target = element;
                  existingAnalyse.AnalysisRulePlugIn = _connection.PISystem.AnalysisRulePlugIns["PerformanceEquation"];
                  existingAnalyse.AnalysisRule.ConfigString = $"{OutputVariableName}{an.ExpressionString}";
                  existingAnalyse.TimeRulePlugIn = _connection.PISystem.TimeRulePlugIns["Natural"];
                  existingAnalyse.AnalysisRule.MapVariable("AnalysisOutput", AnalysisAttributeName);
      
                   _afDatabase.CheckIn(AFCheckedOutMode.ObjectsCheckedOutThisThread); 
      

       

       

      I tried some changes in code like checking the element separately, but none of them worked (and as i said , the elements are actually present in Database).

       

      Please help! What is wrong! I'll be very greatful for any fast suggestion.. Thank you in advance

        • Re: Created element with analyses is not allways seen thdough AF Sdk
          VarbanVarbanov

          Hi again, to be more clear and to give you some more info - I'm posting below some screenshots:

           

          1. Select from PIFD for element 1:

           

          Reaaa-seen.png

           

          2. Select from PIFD for element 2:

           

          a11.png

           

          Hoping that you can see something, look very small to me althogh files are not small. The only difference I see is that fkcheckoutinfoid of element 1 is null

           

          3. Screen shot from Pi System Explorer where element 1 can be ssen but element 2 cannot:

           

          AssetTree.png

           

          Please note, I don't care even if Pi System explorer is not showing the element as long as I can get it with Sdk. There are no checkout changes in PSMT. Both elemenets are created exactly the same way, using the code in the prebvious post.

           

          Thank you in advance!

            • Re: Created element with analyses is not allways seen thdough AF Sdk
              Roger Palmen

              screenshots of the queries 1 & 2 on PIFD seem to be the same?

              • Re: Created element with analyses is not allways seen thdough AF Sdk
                Roger Palmen

                Anyhow, if you do the same but without adding the analysis, what is the result?

                Is the analysis running on the analytics engine for the element you cannot see? (meaning the analytics engine can).

                 

                The only idea i have is that the objects are not cleaned in between subsequent creations? What happens if you create one element, restart the app, create the second?

                  • Re: Created element with analyses is not allways seen thdough AF Sdk
                    VarbanVarbanov

                    Hi Roger, thank you for your help!

                     

                    I'm sorry about the pictures, I edited the post, so now both screenshots are correct.

                     

                    To your questions:

                     

                    1. No difference with or without adding analuses
                    2. Sorry I don't undestand the question - how to check analytics engine. Please, explain what does that mean and how to check it. Apart from that i can see the analytics in Management section of System Explorer

                    3. If I create an element, restart and create next it works! Please keep in mind that the app is wcf service.

                     

                    However I noticed something else. It seems that it stops working (not 100 % sure that each time this is the reason, but ceirtanly helps to reproduce) when i perform an "Evaluate" functionality. I do that by creating dummy analytics and get its attribute values and delete after that (probably there is a better way , if you know such, please share). I create analysis exactly the same way as code above using unique, random generated name and after that i perform the following code:

                     

                     

                                string result;

                                var target = (AFElement)analyse.Target;

                                var att = target.Attributes[AnalysisAttributeName];

                                if (att == null)

                                {

                                    result = $"Cannot evaluete attribute on analyse {analyse.Name} because of missing analyse attribute." +

                                             $"If you are evaluating existing analyse it may be not properly created/edited, consider evaluating expression on a new anlyses";

                                }

                                else

                                {

                                    result = att.GetValue().ToString();

                                }

                                return result;

                     

                                    element.Analyses.Remove(analysis);

                                    analysis.Delete();

                                    element.Delete();

                      • Re: Created element with analyses is not allways seen thdough AF Sdk
                        VarbanVarbanov

                        One important note to the above answer:


                        I create analysis exactly the same way as code above using unique, random generated name

                        But without check in the "dummy" analysis

                        • Re: Created element with analyses is not allways seen thdough AF Sdk
                          Roger Palmen

                          Looking at the answers:

                          Because of 1) i do not expect this is related to Analysis on the elements. To simply your testing, maybe comment that out until..

                          Because of 3) i have the idea that this is related with the creation and cleanup of the objects involved in creating the objects

                           

                          With 2) i meant that if the elements and analytics were created correctly, and the problem was only visible in PSE, the Analytics service could see both, and would generate results for both. But that is not relevant anymore.

                           

                          I'm not a great programmer, and looking at your code i cannot find any obvious mistake. But the restart in between does suggest that this is related to the loops in the code. Could you post more of the surrounding code?

                          1 of 1 people found this helpful
                            • Re: Created element with analyses is not allways seen thdough AF Sdk
                              VarbanVarbanov

                              Thank you for your help, Roger!

                               

                              Actually there is nothing more to post.. I have posted all code which is related to the creation of element/analytics (I reproduce the behavior by executing only already posted code).  I achieve this by creating only one analytics and targeted element at a time, so no loops, no any additional cleanup.  However let me summarize a bit:

                               

                              1. I create an element and analysis with same names, create an attribute for output of the analysis and set the element as analysis target. All elements are always in same root, however sometimes they can be seen through AF Sdk and PSE, sometimes not. Analysis are always visible through SDK and PSE. The code to do this is posted in the first post

                               

                              2. It seems that the behavior where the elements are no longer shown, happens after I perform custom "Evaluate" functionality. For that purpose i just create element and analysis with random name same way as I do in 1, but without check in and get the output attribute value. After I do that i delete them. The code to do the second part is in my third post.

                               

                              I think we should focus on two things:

                               

                              1. If indeed 2 is provoking the behavior, how this could happen. Temporary creating some "dummy" elements/analysis logically should not inrerfere with creating new ones.
                              2. There is a screen shot in the second post where two almost identical records in the Db belong to element which is shown and element which is not. The only different column is checkoutinfotime (except from name and rid). Identical records in the db logically should lead to identical results in sdk.

                               

                              I think that someone who has an internal knowledge on how AF Sdk works could take these into account and give an explanation/solution.

                               

                              Thanks in advance!

                                • Re: Created element with analyses is not allways seen thdough AF Sdk
                                  Roger Palmen

                                  Hmm, in that case i am also left clueless. Never had these kind of issues before. Maybe have someone else look over the code and investigation?

                                   

                                  I do wonder about the things you are seeing. In your last response in 1) you state "All elements are always in same root, however sometimes they can be seen through AF Sdk and PSE, sometimes not. Analysis are always visible through SDK and PSE" but in your second post you say "creen shot from Pi System Explorer where element 1 can be ssen but element 2 cannot:".

                                  Although not contradictory, these are quite odd. Did you properly refresh the tree? Restarted PSE in between the creation of the two elements? Also removing the element & analysis complicates the testing further.

                                   

                                  On the difference in the screenshots: the ID is different in both records, thus i assume those are different elements (however i never look into PIFD).

                                   

                                  Your code looks sound, i doubt if the test sequence and expected test results is correct. In curious cases like this i always wonder if i am drawing the right conclusions based on my test results. It can be very tricky..

                                    • Re: Created element with analyses is not allways seen thdough AF Sdk
                                      VarbanVarbanov

                                      Hi, thanks for the reply,

                                       

                                      Yes, this is the odd thing: "screen shot from Pi System Explorer where element 1 can be ssen but element 2 cannot:" It proves that the element is actually written in the database, but still cannot be seen/retrieved through AF Sdk by using search by path or element.Elements or element.GetElements. It can be found as a target ot the analysis however and through  its Parent property its correct place in the tree can be verified (same can be done by just looking into the db, as i stated in the first post).
                                      Restarts of PSE did not help, also restarts of AF Service. Lets not forget that not only PSE does not show the elements but also AFSdk calls, using different instance.
                                      Regarding removing elements - lets not forget that I'm removing completely different elements, not the ones which cannot be seen.
                                      About the ID, yes it is different, but I did not mention it because it should be..
                                      Yes, the screen shots show different elements to illustrate the issue, one can be seen/returned by AF Sdk (raaa element) and the other cannot (a11) element.

                          • Re: Created element with analyses is not allways seen thdough AF Sdk
                            pthivierge

                            Hello Varban,

                             

                            Are you using several threads in your application?

                            This might cause some issues in case you are performing operations in different threads. Especially when comes time to do the Checkin.

                            The documentation has a lot of information about it: AFCheckin.

                            You should also make sure that the Checkin is made before you make you call to search the elements.

                             

                            Another thing that I can think of is your WCF service, is it spawning a new thread each time a request is made? Do you have a common class instance to all your WCF service calls?

                            this could introduce issues related to threads.

                             

                            My advice would be try to run your code in a simple console application to see how it goes, that will eliminate the complexity introduced by the WCF service.

                            And also if your code is simple enough you could share it with us and we could test it and compare our results.

                            From there we can probably work on a resolution of your issue in a better way.

                             

                            Another details, I typically personally use the root.elements.Add() method to create the new elements instead of using the new keyword.  I don't think this will change anything but this is my personal way of creating elements.

                             

                            Hope this helps a little bit going forward,

                            1 of 1 people found this helpful