PLCGenerator.py
changeset 78 049f2e7090a2
parent 72 73212220ad22
child 80 c798a68c5560
--- a/PLCGenerator.py	Thu Aug 23 09:26:02 2007 +0200
+++ b/PLCGenerator.py	Thu Aug 23 09:39:18 2007 +0200
@@ -53,6 +53,111 @@
                 compute += "\n"
     return compute
 
+
+def GeneratePouProgram(pou_name):
+    if not pouComputed.get(pou_name, True):
+        pouComputed[pou_name] = True
+        global currentProject, currentProgram
+        pou = currentProject.getPou(pou_name)
+        pou_type = pou.getPouType().getValue()
+        if pou_type in pouTypeNames:
+            pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
+        else:
+            raise ValueError, "Undefined pou type"
+        pou_program.GenerateInterface(pou.getInterface())
+        pou_program.GenerateProgram(pou)
+        currentProgram += pou_program.GenerateSTProgram()
+
+def GenerateConfiguration(configuration):
+    config = "\nCONFIGURATION %s\n"%configuration.getName()
+    for varlist in configuration.getGlobalVars():
+        config += "  VAR_GLOBAL"
+        if varlist.getRetain():
+            config += " RETAIN"
+        if varlist.getConstant():
+            config += " CONSTANT"
+        config += "\n"
+        for var in varlist.getVariable():
+            var_type = var.getType().getValue()
+            config += "    %s "%var.getName()
+            address = var.getAddress()
+            if address:
+                config += "AT %s "%address
+            config += ": %s"%var_type
+            initial = var.getInitialValue()
+            if initial:
+                value = str(initial.getValue())
+                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
+                if var_type == "STRING":
+                    config += " := '%s'"%value
+                elif var_type == "WSTRING":
+                    config += " := \"%s\""%value
+                else:
+                    config += " := %s"%value
+            config += ";\n"
+        config += "  END_VAR\n"
+    for resource in configuration.getResource():
+        config += GenerateResource(resource)
+    config += "END_CONFIGURATION\n"
+    return config
+    
+def GenerateResource(resource):
+    resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
+    for varlist in resource.getGlobalVars():
+        resrce += "    VAR_GLOBAL"
+        if varlist.getRetain():
+            resrce += " RETAIN"
+        if varlist.getConstant():
+            resrce += " CONSTANT"
+        resrce += "\n"
+        for var in varlist.getVariable():
+            var_type = var.getType().getValue()
+            resrce += "      %s "%var.getName()
+            address = var.getAddress()
+            if address:
+                resrce += "AT %s "%address
+            resrce += ": %s"%var.getType().getValue()
+            initial = var.getInitialValue()
+            if initial:
+                value = str(initial.getValue())
+                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
+                if var_type == "STRING":
+                    resrce += " := '%s'"%value
+                elif var_type == "WSTRING":
+                    resrce += " := \"%s\""%value
+                else:
+                    resrce += " := %s"%value
+            resrce += ";\n"
+        resrce += "    END_VAR\n"
+    tasks = resource.getTask()
+    for task in tasks:
+        resrce += "    TASK %s("%task.getName()
+        args = []
+        single = task.getSingle()
+        if single:
+            args.append("SINGLE := %s"%single)
+        interval = task.getInterval()
+        if interval:
+            text = "t#"
+            if interval.hour != 0:
+                text += "%dh"%interval.hour
+            if interval.minute != 0:
+                text += "%dm"%interval.minute
+            if interval.second != 0:
+                text += "%ds"%interval.second
+            if interval.microsecond != 0:
+                text += "%dms"%(interval.microsecond / 1000)
+            args.append("INTERVAL := %s"%text)
+        args.append("PRIORITY := %s"%str(task.priority.getValue()))
+        resrce += ",".join(args) + ");\n"
+    for task in tasks:
+        for instance in task.getPouInstance():
+            resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
+    for instance in resource.getPouInstance():
+        resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
+    resrce += "  END_RESOURCE\n"
+    return resrce
+
 """
 Module implementing methods for generating PLC programs in ST or IL
 """
@@ -120,7 +225,11 @@
                     if connections and len(connections) == 1:
                         expression = self.ComputeFBDExpression(body, connections[0])
                         self.Program += "  %s := %s;\n"%(var, expression)
-                if isinstance(instance, plcopen.connector):
+                elif isinstance(instance, plcopen.block):
+                    type = instance.getTypeName()
+                    block_infos = GetBlockType(type)
+                    block_infos["generate"](self, instance, body, None)
+                elif isinstance(instance, plcopen.connector):
                     connector = instance.getName()
                     if self.ComputedConnectors.get(connector, None):
                         continue 
@@ -161,36 +270,9 @@
         if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
             return instance.getExpression()
         elif isinstance(instance, plcopen.block):
-            name = instance.getInstanceName()
             type = instance.getTypeName()
             block_infos = GetBlockType(type)
-            if block_infos["type"] == "function":
-                GeneratePouProgram(type)
-                vars = []
-                for variable in instance.inputVariables.getVariable():
-                    connections = variable.connectionPointIn.getConnections()
-                    if connections and len(connections) == 1:
-                        value = self.ComputeFBDExpression(body, connections[0])
-                        vars.append(self.ExtractModifier(variable, value))
-                variable = instance.outputVariables.getVariable()[0]
-                return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars)))
-            elif block_infos["type"] == "functionBlock":
-                if not self.ComputedBlocks.get(name, False):
-                    vars = []
-                    for variable in instance.inputVariables.getVariable():
-                        connections = variable.connectionPointIn.getConnections()
-                        if connections and len(connections) == 1:
-                            parameter = variable.getFormalParameter()
-                            value = self.ComputeFBDExpression(body, connections[0])
-                            vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value)))
-                    self.Program += "  %s(%s);\n"%(name, ", ".join(vars))
-                    self.ComputedBlocks[name] = True
-                connectionPoint = link.getPosition()[-1]
-                for variable in instance.outputVariables.getVariable():
-                    blockPointx, blockPointy = variable.connectionPointOut.getRelPosition()
-                    if instance.getX() + blockPointx == connectionPoint.getX() and instance.getY() + blockPointy == connectionPoint.getY():
-                        return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter()))
-                raise ValueError, "No output variable found"
+            return block_infos["generate"](self, instance, body, link)
         elif isinstance(instance, plcopen.continuation):
             name = instance.getName()
             computed_value = self.ComputedConnectors.get(name, None)
@@ -557,109 +639,8 @@
         program += "END_%s\n\n"%self.Type
         return program
 
-def GeneratePouProgram(pou_name):
-    if not pouComputed.get(pou_name, True):
-        pouComputed[pou_name] = True
-        global currentProject, currentProgram
-        pou = currentProject.getPou(pou_name)
-        pou_type = pou.getPouType().getValue()
-        if pou_type in pouTypeNames:
-            pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
-        else:
-            raise ValueError, "Undefined pou type"
-        pou_program.GenerateInterface(pou.getInterface())
-        pou_program.GenerateProgram(pou)
-        currentProgram += pou_program.GenerateSTProgram()
-
-def GenerateConfiguration(configuration):
-    config = "\nCONFIGURATION %s\n"%configuration.getName()
-    for varlist in configuration.getGlobalVars():
-        config += "  VAR_GLOBAL"
-        if varlist.getRetain():
-            config += " RETAIN"
-        if varlist.getConstant():
-            config += " CONSTANT"
-        config += "\n"
-        for var in varlist.getVariable():
-            var_type = var.getType().getValue()
-            config += "    %s "%var.getName()
-            address = var.getAddress()
-            if address:
-                config += "AT %s "%address
-            config += ": %s"%var_type
-            initial = var.getInitialValue()
-            if initial:
-                value = str(initial.getValue())
-                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
-                if var_type == "STRING":
-                    config += " := '%s'"%value
-                elif var_type == "WSTRING":
-                    config += " := \"%s\""%value
-                else:
-                    config += " := %s"%value
-            config += ";\n"
-        config += "  END_VAR\n"
-    for resource in configuration.getResource():
-        config += GenerateResource(resource)
-    config += "END_CONFIGURATION\n"
-    return config
-    
-def GenerateResource(resource):
-    resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
-    for varlist in resource.getGlobalVars():
-        resrce += "    VAR_GLOBAL"
-        if varlist.getRetain():
-            resrce += " RETAIN"
-        if varlist.getConstant():
-            resrce += " CONSTANT"
-        resrce += "\n"
-        for var in varlist.getVariable():
-            var_type = var.getType().getValue()
-            resrce += "      %s "%var.getName()
-            address = var.getAddress()
-            if address:
-                resrce += "AT %s "%address
-            resrce += ": %s"%var.getType().getValue()
-            initial = var.getInitialValue()
-            if initial:
-                value = str(initial.getValue())
-                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
-                if var_type == "STRING":
-                    resrce += " := '%s'"%value
-                elif var_type == "WSTRING":
-                    resrce += " := \"%s\""%value
-                else:
-                    resrce += " := %s"%value
-            resrce += ";\n"
-        resrce += "    END_VAR\n"
-    tasks = resource.getTask()
-    for task in tasks:
-        resrce += "    TASK %s("%task.getName()
-        args = []
-        single = task.getSingle()
-        if single:
-            args.append("SINGLE := %s"%single)
-        interval = task.getInterval()
-        if interval:
-            text = "t#"
-            if interval.hour != 0:
-                text += "%dh"%interval.hour
-            if interval.minute != 0:
-                text += "%dm"%interval.minute
-            if interval.second != 0:
-                text += "%ds"%interval.second
-            if interval.microsecond != 0:
-                text += "%dms"%(interval.microsecond / 1000)
-            args.append("INTERVAL := %s"%text)
-        args.append("PRIORITY := %s"%str(task.priority.getValue()))
-        resrce += ",".join(args) + ");\n"
-    for task in tasks:
-        for instance in task.getPouInstance():
-            resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
-    for instance in resource.getPouInstance():
-        resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
-    resrce += "  END_RESOURCE\n"
-    return resrce
+    def GeneratePouProgram(self, pou_name):
+        GeneratePouProgram(pou_name)
 
 def GenerateCurrentProgram(project):
     global currentProject, currentProgram