PLCGenerator.py
changeset 526 79900abdfa3c
parent 507 42150e041dbe
child 527 e307e1b27828
equal deleted inserted replaced
525:e8d5ab0855d3 526:79900abdfa3c
   484         self.ActionNumber = 0
   484         self.ActionNumber = 0
   485         self.Program = []
   485         self.Program = []
   486         self.Errors = errors
   486         self.Errors = errors
   487         self.Warnings = warnings
   487         self.Warnings = warnings
   488     
   488     
   489     def GetBlockType(self, type):
   489     def GetBlockType(self, type, inputs=None):
   490         return self.ParentGenerator.Controler.GetBlockType(type)
   490         return self.ParentGenerator.Controler.GetBlockType(type, inputs)
   491     
   491     
   492     def IndentLeft(self):
   492     def IndentLeft(self):
   493         if len(self.CurrentIndent) >= 2:
   493         if len(self.CurrentIndent) >= 2:
   494             self.CurrentIndent = self.CurrentIndent[:-2]
   494             self.CurrentIndent = self.CurrentIndent[:-2]
   495     
   495     
   655         if isinstance(body, ListType):
   655         if isinstance(body, ListType):
   656             body = body[0]
   656             body = body[0]
   657         body_content = body.getcontent()
   657         body_content = body.getcontent()
   658         body_type = body_content["name"]
   658         body_type = body_content["name"]
   659         if body_type in ["FBD", "LD", "SFC"]:
   659         if body_type in ["FBD", "LD", "SFC"]:
       
   660             undefined_blocks = []
   660             for instance in body.getcontentInstances():
   661             for instance in body.getcontentInstances():
   661                 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
   662                 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
   662                     expression = instance.getexpression()
   663                     expression = instance.getexpression()
   663                     var_type = self.GetVariableType(expression)
   664                     var_type = self.GetVariableType(expression)
   664                     if pou.getpouType() == "function" and expression == pou.getname():
   665                     if pou.getpouType() == "function" and expression == pou.getname():
   739                             for connection in related:
   740                             for connection in related:
   740                                 self.ConnectionTypes[connection] = var_type
   741                                 self.ConnectionTypes[connection] = var_type
   741                     else:
   742                     else:
   742                         raise PLCGenException, _("No connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name)
   743                         raise PLCGenException, _("No connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name)
   743                 elif isinstance(instance, plcopen.fbdObjects_block):
   744                 elif isinstance(instance, plcopen.fbdObjects_block):
   744                     block_infos = self.GetBlockType(instance.gettypeName())
   745                     block_infos = self.GetBlockType(instance.gettypeName(), "undefined")
   745                     if block_infos is not None:
   746                     if block_infos is not None:
   746                         undefined = {}
   747                         self.ComputeBlockInputTypes(instance, block_infos, body)
   747                         for variable in instance.outputVariables.getvariable():
   748                     else:
   748                             output_name = variable.getformalParameter()
       
   749                             if output_name == "ENO":
       
   750                                 for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
       
   751                                     self.ConnectionTypes[connection] = "BOOL"
       
   752                             else:
       
   753                                 for oname, otype, oqualifier in block_infos["outputs"]:
       
   754                                     if output_name == oname:
       
   755                                         if otype.startswith("ANY"):
       
   756                                             if not undefined.has_key(otype):
       
   757                                                 undefined[otype] = []
       
   758                                             undefined[otype].append(variable.connectionPointOut)
       
   759                                         elif not self.ConnectionTypes.has_key(variable.connectionPointOut):
       
   760                                             for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
       
   761                                                 self.ConnectionTypes[connection] = otype
       
   762                         for variable in instance.inputVariables.getvariable():
   749                         for variable in instance.inputVariables.getvariable():
   763                             input_name = variable.getformalParameter()
   750                             connected = self.GetConnectedConnector(variable.connectionPointIn, body)
   764                             if input_name == "EN":
   751                             if connected is not None:
   765                                 for connection in self.ExtractRelatedConnections(variable.connectionPointIn):
   752                                 var_type = self.ConnectionTypes.get(connected, None)
   766                                     self.ConnectionTypes[connection] = "BOOL"
   753                                 if var_type is not None:
   767                             for iname, itype, iqualifier in block_infos["inputs"]:
   754                                     self.ConnectionTypes[variable.connectionPointIn] = var_type
   768                                 if input_name == iname:
       
   769                                     connected = self.GetConnectedConnector(variable.connectionPointIn, body)
       
   770                                     if itype.startswith("ANY"):
       
   771                                         if not undefined.has_key(itype):
       
   772                                             undefined[itype] = []
       
   773                                         undefined[itype].append(variable.connectionPointIn)
       
   774                                         if connected:
       
   775                                             undefined[itype].append(connected)
       
   776                                     else:
       
   777                                         self.ConnectionTypes[variable.connectionPointIn] = itype
       
   778                                         if connected and not self.ConnectionTypes.has_key(connected):
       
   779                                             for connection in self.ExtractRelatedConnections(connected):
       
   780                                                 self.ConnectionTypes[connection] = itype
       
   781                         for var_type, connections in undefined.items():
       
   782                             related = []
       
   783                             for connection in connections:
       
   784                                 if self.ConnectionTypes.has_key(connection):
       
   785                                     var_type = self.ConnectionTypes[connection]
       
   786                                 else:
   755                                 else:
   787                                     related.extend(self.ExtractRelatedConnections(connection))
   756                                     related = self.ExtractRelatedConnections(connected)
   788                             if var_type.startswith("ANY") and len(related) > 0:
   757                                     related.append(variable.connectionPointIn)
   789                                 self.RelatedConnections.append(related)
   758                                     self.RelatedConnections.append(related)
   790                             else:
   759                         undefined_blocks.append(instance)
   791                                 for connection in related:
   760             for instance in undefined_blocks:
   792                                     self.ConnectionTypes[connection] = var_type
   761                 block_infos = self.GetBlockType(instance.gettypeName(), tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable()]))
   793                     else:
   762                 if block_infos is not None:
   794                         raise PLCGenException, _("No informations found for \"%s\" block")%(instance.gettypeName())
   763                     self.ComputeBlockInputTypes(instance, block_infos, body)
       
   764                 else:
       
   765                     raise PLCGenException, _("No informations found for \"%s\" block")%(instance.gettypeName())
       
   766                 
       
   767     def ComputeBlockInputTypes(self, instance, block_infos, body):
       
   768         undefined = {}
       
   769         for variable in instance.outputVariables.getvariable():
       
   770             output_name = variable.getformalParameter()
       
   771             if output_name == "ENO":
       
   772                 for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
       
   773                     self.ConnectionTypes[connection] = "BOOL"
       
   774             else:
       
   775                 for oname, otype, oqualifier in block_infos["outputs"]:
       
   776                     if output_name == oname:
       
   777                         if otype.startswith("ANY"):
       
   778                             if not undefined.has_key(otype):
       
   779                                 undefined[otype] = []
       
   780                             undefined[otype].append(variable.connectionPointOut)
       
   781                         elif not self.ConnectionTypes.has_key(variable.connectionPointOut):
       
   782                             for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
       
   783                                 self.ConnectionTypes[connection] = otype
       
   784         for variable in instance.inputVariables.getvariable():
       
   785             input_name = variable.getformalParameter()
       
   786             if input_name == "EN":
       
   787                 for connection in self.ExtractRelatedConnections(variable.connectionPointIn):
       
   788                     self.ConnectionTypes[connection] = "BOOL"
       
   789             else:
       
   790                 for iname, itype, iqualifier in block_infos["inputs"]:
       
   791                     if input_name == iname:
       
   792                         connected = self.GetConnectedConnector(variable.connectionPointIn, body)
       
   793                         if itype.startswith("ANY"):
       
   794                             if not undefined.has_key(itype):
       
   795                                 undefined[itype] = []
       
   796                             undefined[itype].append(variable.connectionPointIn)
       
   797                             if connected:
       
   798                                 undefined[itype].append(connected)
       
   799                         else:
       
   800                             self.ConnectionTypes[variable.connectionPointIn] = itype
       
   801                             if connected and not self.ConnectionTypes.has_key(connected):
       
   802                                 for connection in self.ExtractRelatedConnections(connected):
       
   803                                     self.ConnectionTypes[connection] = itype
       
   804         for var_type, connections in undefined.items():
       
   805             related = []
       
   806             for connection in connections:
       
   807                 if self.ConnectionTypes.has_key(connection):
       
   808                     var_type = self.ConnectionTypes[connection]
       
   809                 else:
       
   810                     related.extend(self.ExtractRelatedConnections(connection))
       
   811             if var_type.startswith("ANY") and len(related) > 0:
       
   812                 self.RelatedConnections.append(related)
       
   813             else:
       
   814                 for connection in related:
       
   815                     self.ConnectionTypes[connection] = var_type
   795 
   816 
   796     def ComputeProgram(self, pou):
   817     def ComputeProgram(self, pou):
   797         body = pou.getbody()
   818         body = pou.getbody()
   798         if isinstance(body, ListType):
   819         if isinstance(body, ListType):
   799             body = body[0]
   820             body = body[0]
   855                         self.Program += expression
   876                         self.Program += expression
   856                         self.Program += [(";\n", ())]
   877                         self.Program += [(";\n", ())]
   857                 elif isinstance(instance, plcopen.fbdObjects_block):
   878                 elif isinstance(instance, plcopen.fbdObjects_block):
   858                     block_type = instance.gettypeName()
   879                     block_type = instance.gettypeName()
   859                     self.ParentGenerator.GeneratePouProgram(block_type)
   880                     self.ParentGenerator.GeneratePouProgram(block_type)
   860                     block_infos = self.GetBlockType(block_type)
   881                     block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable()]))
   861                     block_infos["generate"](self, instance, body, None)
   882                     block_infos["generate"](self, instance, block_infos, body, None)
   862                 elif isinstance(instance, plcopen.commonObjects_connector):
   883                 elif isinstance(instance, plcopen.commonObjects_connector):
   863                     connector = instance.getname()
   884                     connector = instance.getname()
   864                     if self.ComputedConnectors.get(connector, None):
   885                     if self.ComputedConnectors.get(connector, None):
   865                         continue 
   886                         continue 
   866                     self.ComputedConnectors[connector] = self.ComputeExpression(body, instance.connectionPointIn.getconnections())
   887                     self.ComputedConnectors[connector] = self.ComputeExpression(body, instance.connectionPointIn.getconnections())
   910             elif isinstance(next, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)):
   931             elif isinstance(next, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)):
   911                 paths.append(str([(next.getexpression(), (self.TagName, "io_variable", localId, "expression"))]))
   932                 paths.append(str([(next.getexpression(), (self.TagName, "io_variable", localId, "expression"))]))
   912             elif isinstance(next, plcopen.fbdObjects_block):
   933             elif isinstance(next, plcopen.fbdObjects_block):
   913                 block_type = next.gettypeName()
   934                 block_type = next.gettypeName()
   914                 self.ParentGenerator.GeneratePouProgram(block_type)
   935                 self.ParentGenerator.GeneratePouProgram(block_type)
   915                 block_infos = self.GetBlockType(block_type)
   936                 block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in next.inputVariables.getvariable()]))
   916                 paths.append(str(block_infos["generate"](self, next, body, connection, order)))
   937                 paths.append(str(block_infos["generate"](self, next, block_infos, body, connection, order)))
   917             elif isinstance(next, plcopen.commonObjects_continuation):
   938             elif isinstance(next, plcopen.commonObjects_continuation):
   918                 name = next.getname()
   939                 name = next.getname()
   919                 computed_value = self.ComputedConnectors.get(name, None)
   940                 computed_value = self.ComputedConnectors.get(name, None)
   920                 if computed_value != None:
   941                 if computed_value != None:
   921                     paths.append(str(computed_value))
   942                     paths.append(str(computed_value))