PLCGenerator.py
changeset 71 0578bc212c20
parent 70 0e48629c1e6d
child 72 73212220ad22
--- a/PLCGenerator.py	Mon Aug 13 18:06:50 2007 +0200
+++ b/PLCGenerator.py	Tue Aug 14 14:53:06 2007 +0200
@@ -32,6 +32,10 @@
 
 pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
 
+currentProject = None
+currentProgram = ""
+pouComputed = {}
+
 def ReIndentText(text, nb_spaces):
     compute = ""
     lines = text.splitlines()
@@ -88,6 +92,7 @@
                 type = var.getType().getValue()
                 if not isinstance(type, (StringType, UnicodeType)):
                     type = type.getName()
+                    GeneratePouProgram(type)
                 initial = var.getInitialValue()
                 if initial:
                     initial_value = initial.getValue()
@@ -152,6 +157,7 @@
             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()
@@ -528,101 +534,12 @@
         program += self.Program
         program += "END_%s\n\n"%self.Type
         return program
-    
-    def GenerateConfiguration(self, 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 += self.GenerateResource(resource)
-        config += "END_CONFIGURATION\n"
-        return config
-        
-    def GenerateResource(self, 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 GenerateCurrentProgram(project):
-    program = ""
-    for pou in project.getPous():
+
+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])
@@ -630,8 +547,107 @@
             raise ValueError, "Undefined pou type"
         pou_program.GenerateInterface(pou.getInterface())
         pou_program.GenerateProgram(pou)
-        program += pou_program.GenerateSTProgram()
+        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 GenerateCurrentProgram(project):
+    global currentProject, currentProgram
+    currentProject = project
+    currentProgram = ""
+    for pou in project.getPous():
+        pouComputed[pou.getName()] = False
+    for pou_name in pouComputed.keys():
+        GeneratePouProgram(pou_name)
     for config in project.getConfigurations():
-        program += pou_program.GenerateConfiguration(config)
-    return program
-
+        currentProgram += GenerateConfiguration(config)
+    return currentProgram
+