PLCGenerator.py
changeset 257 d0a0ffbbd61c
parent 248 f7df265edd54
child 261 6fe3ee00f9a0
equal deleted inserted replaced
256:42dd57b23e36 257:d0a0ffbbd61c
   643                             self.RelatedConnections.append(related)
   643                             self.RelatedConnections.append(related)
   644                         else:
   644                         else:
   645                             for connection in related:
   645                             for connection in related:
   646                                 self.ConnectionTypes[connection] = var_type
   646                                 self.ConnectionTypes[connection] = var_type
   647     
   647     
   648     def GetNetworkType(self, connections, body):
       
   649         network_type = "FBD"
       
   650         for connection in connections:
       
   651             localId = connection.getrefLocalId()
       
   652             next = body.getcontentInstance(localId)
       
   653             if isinstance(next, plcopen.ldObjects_leftPowerRail) or isinstance(next, plcopen.ldObjects_contact):
       
   654                 return "LD"
       
   655             elif isinstance(next, plcopen.fbdObjects_block):
       
   656                  for variable in next.inputVariables.getvariable():
       
   657                      result = self.GetNetworkType(variable.connectionPointIn.getconnections(), body)
       
   658                      if result != "FBD":
       
   659                          return result
       
   660             elif isinstance(next, plcopen.fbdObjects_inVariable):
       
   661                 return "FBD"
       
   662             elif isinstance(next, plcopen.fbdObjects_inOutVariable):
       
   663                 return self.GetNetworkType(next.connectionPointIn.getconnections(), body)
       
   664             else:
       
   665                 return None
       
   666         return "FBD"
       
   667     
       
   668     def ComputeProgram(self, pou):
   648     def ComputeProgram(self, pou):
   669         body = pou.getbody()
   649         body = pou.getbody()
   670         body_content = body.getcontent()
   650         body_content = body.getcontent()
   671         body_type = body_content["name"]
   651         body_type = body_content["name"]
   672         if body_type in ["IL","ST"]:
   652         if body_type in ["IL","ST"]:
   779                 paths.append(str(block_infos["generate"](self, next, body, connection, order)))
   759                 paths.append(str(block_infos["generate"](self, next, body, connection, order)))
   780             elif isinstance(next, plcopen.commonObjects_continuation):
   760             elif isinstance(next, plcopen.commonObjects_continuation):
   781                 name = next.getname()
   761                 name = next.getname()
   782                 computed_value = self.ComputedConnectors.get(name, None)
   762                 computed_value = self.ComputedConnectors.get(name, None)
   783                 if computed_value != None:
   763                 if computed_value != None:
   784                     paths.append(computed_value)
   764                     paths.append(str(computed_value))
   785                 for tmp_instance in body.getcontentInstances():
   765                 else:
   786                     if isinstance(tmp_instance, plcopen.commonObjects_connector):
   766                     connector = None
   787                         if tmp_instance.getname() == name:
   767                     for instance in body.getcontentInstances():
   788                             connections = tmp_instance.connectionPointIn.getconnections()
   768                         if isinstance(instance, plcopen.commonObjects_connector) and instance.getname() == name:
   789                             if connections is not None:
   769                             if connector is not None:
   790                                 expression = str(self.ComputeExpression(body, connections, order))
   770                                 raise PLCGenException, "More than one connector found corresponding to \"%s\" continuation in \"%s\" POU"%(name, self.Name)
   791                                 self.ComputedConnectors[name] = expression
   771                             connector = instance
   792                                 paths.append(expression)
   772                     if connector is not None:
   793                 raise PLCGenException, "No connector found corresponding to \"%s\" continuation in \"%s\" POU"%(name, self.Name)
   773                         connections = connector.connectionPointIn.getconnections()
   794             else:
   774                         if connections is not None:
       
   775                             expression = self.ComputeExpression(body, connections, order)
       
   776                             self.ComputedConnectors[name] = expression
       
   777                             paths.append(str(expression))
       
   778                     else:
       
   779                         raise PLCGenException, "No connector found corresponding to \"%s\" continuation in \"%s\" POU"%(name, self.Name)
       
   780             elif isinstance(next, plcopen.ldObjects_contact):
   795                 contact_info = (self.TagName, "contact", next.getlocalId())
   781                 contact_info = (self.TagName, "contact", next.getlocalId())
   796                 variable = str(self.ExtractModifier(next, [(next.getvariable(), contact_info + ("reference",))], contact_info))
   782                 variable = str(self.ExtractModifier(next, [(next.getvariable(), contact_info + ("reference",))], contact_info))
   797                 result = self.GeneratePaths(next.connectionPointIn.getconnections(), body, order)
   783                 result = self.GeneratePaths(next.connectionPointIn.getconnections(), body, order)
   798                 if len(result) > 1:
   784                 if len(result) > 1:
   799                     factorized_paths = self.FactorizePaths(result)
   785                     factorized_paths = self.FactorizePaths(result)
   805                     paths.append([variable] + result[0])
   791                     paths.append([variable] + result[0])
   806                 elif result[0] is not None:
   792                 elif result[0] is not None:
   807                     paths.append([variable, result[0]])
   793                     paths.append([variable, result[0]])
   808                 else:
   794                 else:
   809                     paths.append(variable)
   795                     paths.append(variable)
       
   796             elif isinstance(next, plcopen.ldObjects_coil):
       
   797                 paths.append(str(self.GeneratePaths(next.connectionPointIn.getconnections(), body, order)))
   810         return paths
   798         return paths
   811 
   799 
   812     def ComputePaths(self, paths, first = False):
   800     def ComputePaths(self, paths, first = False):
   813         if type(paths) == TupleType:
   801         if type(paths) == TupleType:
   814             if None in paths:
   802             if None in paths:
   825         else:
   813         else:
   826             return eval(paths)
   814             return eval(paths)
   827 
   815 
   828     def ComputeExpression(self, body, connections, order = False):
   816     def ComputeExpression(self, body, connections, order = False):
   829         paths = self.GeneratePaths(connections, body, order)
   817         paths = self.GeneratePaths(connections, body, order)
       
   818         print paths
   830         if len(paths) > 1:
   819         if len(paths) > 1:
   831             factorized_paths = self.FactorizePaths(paths)
   820             factorized_paths = self.FactorizePaths(paths)
   832             if len(factorized_paths) > 1:
   821             if len(factorized_paths) > 1:
   833                 paths = tuple(factorized_paths)
   822                 paths = tuple(factorized_paths)
   834             else:
   823             else: