Adding support for generating FBD with connectors and continuations
authorlbessard
Thu, 16 Aug 2007 10:40:27 +0200
changeset 72 73212220ad22
parent 71 0578bc212c20
child 73 f2529e34e4b7
Adding support for generating FBD with connectors and continuations
PLCGenerator.py
--- a/PLCGenerator.py	Tue Aug 14 14:53:06 2007 +0200
+++ b/PLCGenerator.py	Thu Aug 16 10:40:27 2007 +0200
@@ -65,9 +65,10 @@
         self.ReturnType = None
         self.Interface = []
         self.InitialSteps = []
-        self.BlockComputed = {}
-        self.ComputedBlocks = ""
+        self.ComputedBlocks = {}
+        self.ComputedConnectors = {}
         self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
+        self.SFCComputedBlocks = ""
         self.ActionNumber = 0
         self.Program = ""
     
@@ -113,12 +114,19 @@
             self.Program = ReIndentText(body_content["value"].getText(), 2)
         elif body_type == "FBD":
             for instance in body.getContentInstances():
-                if isinstance(instance, plcopen.outVariable):
+                if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
                     var = instance.getExpression()
                     connections = instance.connectionPointIn.getConnections()
                     if connections and len(connections) == 1:
                         expression = self.ComputeFBDExpression(body, connections[0])
                         self.Program += "  %s := %s;\n"%(var, expression)
+                if isinstance(instance, plcopen.connector):
+                    connector = instance.getName()
+                    if self.ComputedConnectors.get(connector, None):
+                        continue 
+                    connections = instance.connectionPointIn.getConnections()
+                    if connections and len(connections) == 1:
+                        self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0])
         elif body_type == "LD":
             for instance in body.getContentInstances():
                 if isinstance(instance, plcopen.coil):
@@ -136,11 +144,11 @@
                     self.GenerateSFCTransition(instance, pou)
                 elif isinstance(instance, plcopen.jumpStep):
                     self.GenerateSFCJump(instance, pou)
-            if len(self.InitialSteps) > 0 and self.ComputedBlocks != "":
+            if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "":
                 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.ComputedBlocks, 4)
+                self.SFCNetworks["Actions"][action_name] = ReIndentText(self.SFCComputedBlocks, 4)
                 self.Program = ""
             else:
                 raise Exception
@@ -150,7 +158,7 @@
     def ComputeFBDExpression(self, body, link):
         localid = link.getRefLocalId()
         instance = body.getContentInstance(localid)
-        if isinstance(instance, plcopen.inVariable):
+        if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
             return instance.getExpression()
         elif isinstance(instance, plcopen.block):
             name = instance.getInstanceName()
@@ -167,7 +175,7 @@
                 variable = instance.outputVariables.getVariable()[0]
                 return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars)))
             elif block_infos["type"] == "functionBlock":
-                if not self.BlockComputed.get(name, False):
+                if not self.ComputedBlocks.get(name, False):
                     vars = []
                     for variable in instance.inputVariables.getVariable():
                         connections = variable.connectionPointIn.getConnections()
@@ -176,13 +184,27 @@
                             value = self.ComputeFBDExpression(body, connections[0])
                             vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value)))
                     self.Program += "  %s(%s);\n"%(name, ", ".join(vars))
-                    self.BlockComputed[name] = True
+                    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"
+        elif isinstance(instance, plcopen.continuation):
+            name = instance.getName()
+            computed_value = self.ComputedConnectors.get(name, None)
+            if computed_value != None:
+                return computed_value
+            for tmp_instance in body.getContentInstances():
+                if isinstance(tmp_instance, plcopen.connector):
+                    if tmp_instance.getName() == name:
+                        connections = tmp_instance.connectionPointIn.getConnections()
+                        if connections and len(connections) == 1:
+                            expression = self.ComputeFBDExpression(body, connections[0])
+                            self.ComputedConnectors[name] = expression
+                            return expression
+            raise ValueError, "No connector found"
 
     def GenerateLDPaths(self, connections, body):
         paths = []
@@ -383,7 +405,7 @@
                             if connections and len(connections) == 1:
                                 expression = self.ComputeFBDExpression(transitionBody, connections[0])
                                 transition_infos["content"] = "\n    := %s;\n"%expression
-                                self.ComputedBlocks += self.Program
+                                self.SFCComputedBlocks += self.Program
                                 self.Program = ""
                 elif transitionType == "LD":
                     for instance in transitionBody.getContentInstances():
@@ -391,7 +413,7 @@
                             paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody)
                             expression = self.ComputeLDExpression(paths, True)
                             transition_infos["content"] = "\n    := %s;\n"%expression
-                            self.ComputedBlocks += self.Program
+                            self.SFCComputedBlocks += self.Program
                             self.Program = ""
             elif transitionValues["type"] == "connection":
                 body = pou.getBody()
@@ -403,12 +425,12 @@
                     paths = self.GenerateLDPaths(connections, body)
                     expression = self.ComputeLDExpression(paths, True)
                     transition_infos["content"] = "\n    := %s;\n"%expression
-                    self.ComputedBlocks += self.Program
+                    self.SFCComputedBlocks += self.Program
                     self.Program = ""
                 else:
                     expression = self.ComputeFBDExpression(body, connections[0])
                     transition_infos["content"] = "\n    := %s;\n"%expression
-                    self.ComputedBlocks += self.Program
+                    self.SFCComputedBlocks += self.Program
                     self.Program = ""
             for step in steps:
                 self.GenerateSFCStep(step, pou)