Adding support for generating network and rung connected to transitions into SFC
authorlbessard
Thu, 09 Aug 2007 18:06:54 +0200
changeset 66 fd138fc77510
parent 65 cb6bed0720f0
child 67 3a1b0afdaf84
Adding support for generating network and rung connected to transitions into SFC
PLCGenerator.py
--- a/PLCGenerator.py	Wed Aug 08 14:55:39 2007 +0200
+++ b/PLCGenerator.py	Thu Aug 09 18:06:54 2007 +0200
@@ -115,7 +115,7 @@
         elif body_type == "LD":
             for instance in body.getContentInstances():
                 if isinstance(instance, plcopen.coil):
-                    paths = self.GenerateLDPaths(instance, body)
+                    paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body)
                     variable = self.ExtractModifier(instance, instance.getVariable())
                     expression = self.ComputeLDExpression(paths, True)
                     self.Program += "  %s := %s;\n"%(variable, expression)
@@ -129,6 +129,14 @@
                     self.GenerateSFCTransition(instance, pou)
                 elif isinstance(instance, plcopen.jumpStep):
                     self.GenerateSFCJump(instance, pou)
+            if len(self.InitialSteps) > 0:
+                action_name = "COMPUTE_FUNCTION_BLOCKS"
+                action_infos = {"qualifier" : "S", "content" : action_name}
+                self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos)
+                self.SFCNetworks["Actions"][action_name] = ReIndentText(self.Program, 4)
+                self.Program = ""
+            else:
+                raise Exception
             for initialstep in self.InitialSteps:
                 self.ComputeSFCStep(initialstep)
     
@@ -170,32 +178,46 @@
                         return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter()))
                 raise ValueError, "No output variable found"
 
-    def GenerateLDPaths(self, instance, body):
+    def GenerateLDPaths(self, connections, body):
         paths = []
-        variable = self.ExtractModifier(instance, instance.getVariable())
-        connections = instance.connectionPointIn.getConnections()
         for connection in connections:
             localId = connection.getRefLocalId()
             next = body.getContentInstance(localId)
             if isinstance(next, plcopen.leftPowerRail):
                 paths.append(None)
             else:
-                paths.append(self.GenerateLDPaths(next, body))
-        if isinstance(instance, plcopen.coil):
-            if len(paths) > 1:
-                return tuple(paths)
-            else:
-                return paths
-        else:
-            if len(paths) > 1:
-                return [variable, tuple(paths)]
-            elif type(paths[0]) == ListType:
-                return [variable] + paths[0]
-            elif paths[0]:
-                return [variable, paths[0]]
-            else:
-                return variable
-
+                variable = self.ExtractModifier(next, next.getVariable())
+                result = self.GenerateLDPaths(next.connectionPointIn.getConnections(), body)
+                if len(result) > 1:
+                    paths.append([variable, tuple(result)])
+                elif type(result[0]) == ListType:
+                    paths.append([variable] + result[0])
+                elif result[0]:
+                    paths.append([variable, result[0]])
+                else:
+                    paths.append(variable)
+        return paths
+
+    def GetNetworkType(self, connections, body):
+        network_type = "FBD"
+        for connection in connections:
+            localId = connection.getRefLocalId()
+            next = body.getContentInstance(localId)
+            if isinstance(next, plcopen.leftPowerRail) or isinstance(next, plcopen.contact):
+                return "LD"
+            elif isinstance(next, plcopen.block):
+                 for variable in instance.inputVariables.getVariable():
+                     result = self.GetNetworkType(variable.connectionPointIn.getConnections(), body)
+                     if result != "FBD":
+                         return result
+            elif isinstance(next, plcopen.inVariable):
+                return "FBD"
+            elif isinstance(next, plcopen.inOutVariable):
+                return self.GetNetworkType(next.connectionPointIn.getConnections(), body)
+            else:
+                return None
+        return "FBD"
+    
     def ExtractDivergenceInput(self, divergence, pou):
         connectionPointIn = divergence.getConnectionPointIn()
         if connectionPointIn:
@@ -310,7 +332,7 @@
                 elif actionType == "LD":
                     for instance in actionbody.getContentInstances():
                         if isinstance(instance, plcopen.coil):
-                            paths = self.GenerateLDPaths(instance, actionBody)
+                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody)
                             variable = self.ExtractModifier(instance, instance.getVariable())
                             expression = self.ComputeLDExpression(paths, True)
                             self.SFCNetworks["Actions"][action_name] = self.Program + "  %s := %s;\n"%(variable, expression)
@@ -338,7 +360,7 @@
             transitionValues = transition.getConditionContent()
             if transitionValues["type"] == "inline":
                 transition_infos["content"] = "\n    := %s;\n"%transitionValues["value"]
-            else:
+            elif transitionValues["type"] == "reference":
                 transitionContent = pou.getTransition(transitionValues["value"])
                 transitionType = transitionContent.getBodyType()
                 transitionBody = transitionContent.getBody()
@@ -346,19 +368,32 @@
                     transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4)
                 elif transitionType == "ST":
                     transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4)
-                elif conditionType == "FBD":
+                elif transitionType == "FBD":
                     for instance in transitionBody.getContentInstances():
                         if isinstance(instance, plcopen.outVariable):
                             connections = instance.connectionPointIn.getConnections()
                             if connections and len(connections) == 1:
-                                expression = self.ComputeFBDExpression(actionBody, connections[0])
-                                transition_infos["content"] = "\n    := %s;\n"%(var, expression)
-                elif actionType == "LD":
-                    for instance in transitionbody.getContentInstances():
+                                expression = self.ComputeFBDExpression(transitionBody, connections[0])
+                                transition_infos["content"] = "\n    := %s;\n"%expression
+                elif transitionType == "LD":
+                    for instance in transitionBody.getContentInstances():
                         if isinstance(instance, plcopen.coil):
-                            paths = self.GenerateLDPaths(instance, conditionBody)
+                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody)
                             expression = self.ComputeLDExpression(paths, True)
                             transition_infos["content"] = "\n    := %s;\n"%expression
+            elif transitionValues["type"] == "connection":
+                body = pou.getBody()
+                connections = transition.getConnections()
+                network_type = self.GetNetworkType(connections, body)
+                if network_type == None:
+                    raise Exception
+                if len(connections) > 1 or network_type == "LD":
+                    paths = self.GenerateLDPaths(connections, body)
+                    expression = self.ComputeLDExpression(paths, True)
+                    transition_infos["content"] = "\n    := %s;\n"%expression
+                else:
+                    expression = self.ComputeFBDExpression(body, connections[0])
+                    transition_infos["content"] = "\n    := %s;\n"%expression
             for step in steps:
                 self.GenerateSFCStep(step, pou)
                 step_name = step.getName()