13 Replies Latest reply on May 19, 2014 9:23 AM by IPCOSRumaila

    Inconsistent behaviour through PI AF SDK when using models in PI/AF

    IPCOSRumaila

      Hi!

       

      We are using models for representing a piping network. Two connected nodes in the piping network always have an intermediate "valve" inbetween. This valve is an AFElement which has a status-attribute, which can be open or closed. So this valve has by design always one incoming connection and one outgoing connection. The problem we are confronted with is that while traversing the network with AF SDK some of these valves now and then apperently fail to have a outgoing connection! When looking at the same valve in Pi Explorer then the connection is actually there.

       

      So in the code below, it should never get into the else-branch that returns false. But actually it does. Without changing anything on the PI/AF tree this problem occurs, goes away and returns on other valves in the network. Does this somehow ring a bell  for anybody? Maybe something wrong in the way we work, or we need an update of some library because of a known issue? We are working with AF SDK 2.5.2 .  

       

      private bool SetValves(AFElement afmodel, AFElement fromNode, List<Tuple<string, string>> ToNodes, AFTime date)
      {
                    foreach (AFConnection afConnection in ((AFModel)afmodel).GetConnections(fromNode, AFPortType.Output))
                   {
                               AFElement valve = (AFElement)afConnection.Destination;
                               IEnumerable<AFConnection> targetConnections = ((AFModel)afmodel).GetConnections(valve,AFPortType.Output);
                               if (targetConnections.Count() != 1)
                               {
                                      return false;
                               }
                              AFElement target = (AFElement)targetConnections.ElementAt(0).Destination;

       

                             Tuple<string, string> rtn = (from item in ToNodes
                                                                             where item.Item1 == target.Name
                                                                             select new Tuple<string, string>(item.Item1, item.Item2)).SingleOrDefault();
                              if (rtn != null)  valve.Attributes[PIAFStrings.Status].SetValue(new AFValue(Convert.ToBoolean(rtn.Item2) ? 1 : 0, date));
                       }
                       return true;
      }

       

      Regards,

       

       

       

      Denis

        • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
          IPCOSRumaila

          Of course any hint on how to debug this further is welcome, With the intermittent behavior, this is not an easy one :-( .

           

          Denis

            • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
              David Hearn

              The GetConnections method will only return elements that are in the Model. It determines this by traversing up the 'Parent' property. Are any of your elements in more than one hierarchy? To test if this is the cause of your problem, check the 'valve.Parent' property before returning false and see if it is set to a different element than the 'afmodel'. If you have sub-elements within the model, you will need to traverse up the Parent path:

               

                     bool EqualsParentInModel(AFModel model, AFElement element)

               

                     {

               

                            while (element != null)

               

                             {

               

                                 if (model.Equals(element)) return true;

               

                                 if (element is AFModel) break;

               

                                 element = element.Parent;

               

                             }

               

                             return false;

               

                     }

                • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                  IPCOSRumaila

                  Hi David,

                   

                  This is an interesting thought. Indeed we have a double hierarchy. This is because models are not supported by all OsiSoft tools, e.g. OleDB Enterprise and DataLink refuse to see these model-elements. There is a master hierarchy which has models in there and then a parallel structure referring to the nodes below the models using weak references.  This makes everything available for all tools. This is something that would not have come to my mind since the tree is generated automatically by a tool, so it is unexpected that some of these valves would suddenly belong to a different tree and it certainly does not explain the intermittent behavior. But for whatever reason it could actually be the problem. So I will check on the parent structure for the problematic valves at the times I can reproduce the problem!  

                   

                  Thanks for the hint!

                   

                  Regards,

                   

                  Denis

                  • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                    David Hearn

                    Actually, it is the connection's Source or Destination elements must be within the Parent hierarchy of the specified element parameter in the GetConnections method call. Here is the how the GetConnections method is implemented, so you could see why one of the conditions is not being met when you code returns false:

                     

                               foreach (AFConnection connection in Connections)

                     

                               {

                     

                                   if ((connection.SourcePort != null && portType == connection.SourcePort.Type && EqualsParentInModel(element, connection.Source)) ||

                     

                                       (connection.DestinationPort != null && portType == connection.DestinationPort.Type && EqualsParentInModel(element, connection.Destination)))

                     

                                   {

                     

                                       collection.Add(connection);

                     

                                   }

                     

                               }

                     

                           bool EqualsParentInModel(AFElement parent, AFElement element)

                     

                           {

                     

                                  while ((object)element != null)

                     

                                   {

                     

                                       if (parent.Equals(element)) return true;

                     

                                       if (element is AFModel) break;

                     

                                       element = element.Parent;

                     

                                   }

                     

                                   return false;

                     

                           }

                      • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                        IPCOSRumaila

                        Hi David,

                         

                        We are aware of the constraint that connected elements should be within the tree below the model.  I thought we were "save" by having a double hierarchy. The first one with the models,  while the second hierarchy refers to the  nodes below the models using "weak references" . But now I wander if it is possible that the parent-method may return the parent following the weak reference?  Unfortunately currently I cannot reproduce the problem.  So it would mean that the parent-method is kind of undetermined in case of multiple parents. When requesting the parents of those elements that are referenced using weak references from PI AF, I get indeed two results: the model and the parent in the second hierarchy.  I'll start by checking the documentation on that.

                         

                        Regards,

                         

                        Denis

                          • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                            David Hearn

                            The Parent property should always return the primary parent of the element which should be the first parent element when the child element was added.

                              • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                                IPCOSRumaila

                                The element below the model is always created first and next we set up the weak reference:

                                 

                                              afGroupModel = new AFElement { Name = group };

                                 

                                               afDegasModel.Elements.Add(afGroupModel);

                                 

                                               /* first checkin, because creating the weak reference below will fail. */

                                 

                                               _afDatabase.CheckIn();

                                 

                                               AFReferenceType reference = _afDatabase.ReferenceTypes["Weak Reference"];

                                 

                                               afDegas.Elements.Add(afGroupModel,reference);

                                 

                                So we should be good, ... .

                                  • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                                    IPCOSRumaila

                                    Hi,

                                     

                                     

                                     

                                    I could reproduce the problem. I checked on the parent of the "valve" and this was OK. I investigated the data-structure. And I found something I believe is inconsistent.

                                     

                                    In the AFModel object in the Connections-Property I see an  _idDictonary and an  _itemsOrPages data-structure.

                                     

                                    In the _itemsOrPages I can find the connection that should be returned when asking for the outgoing connections of the "valve". But when looking in the _idDictionary the connection is there. but the source is missing:

                                     

                                    5850.Connection_5F00_is_5F00_there.png  

                                     

                                    7242.Connection_5F00_in_5F00_dictionary_5F00_messed_5F00_up.png

                                     

                                     

                                     

                                    I believe the internal data-structures are kind of messed up, causing the issue I'm dealing with?

                                     

                                    Without changing the connections or Pi/AF tree this problem comes and goes. At the time of reproduction you can reproduce it for a number of times on the same valves even after closing the connection to AF..  Wait a few hours and the problem is gone. But then occurs on other valves, ... .   Looks like AF Server is caching data. As long as the cache is used the problem stays, When caching is cleared, problem is gone? 

                                     

                                    Regards,

                                     

                                     

                                     

                                    Denis

                                      • Re: Inconsistent behaviour through PI AF SDK when using models in PI/AF
                                        IPCOSRumaila

                                        Hi,

                                         

                                        Actually I'm wrong when I write that the problem persists in between PI/AF sessions. It concerns a web-application and the connection with PIAF is kept alive with a static variable to avoid destroying the PIAF Cache in between 2 calls of WebMethods.   Our tree is quite big ( more than 25000 element), so we need that.

                                         

                                        The question is how thread-safe is this way of working? Can the PIAF-cache be shared between different threads or  should we use ThreadStatic instead ?

                                         

                                        I think I will play around with this a bit.

                                         

                                        I also noticed that the _sourcePort field in the data above is equal to null.  Given the code that David posted such a connection will never be returned. But in what circumstances can this _sourcePort be null?

                                         

                                        Regards,

                                         

                                        Denis