39 |
39 |
40 def ReIndentText(text, nb_spaces): |
40 def ReIndentText(text, nb_spaces): |
41 compute = "" |
41 compute = "" |
42 lines = text.splitlines() |
42 lines = text.splitlines() |
43 if len(lines) > 0: |
43 if len(lines) > 0: |
44 spaces = 0 |
44 line_num = 0 |
45 while lines[0][spaces] == " ": |
45 while line_num < len(lines) and len(lines[line_num].strip()) == 0: |
46 spaces += 1 |
46 line_num += 1 |
47 indent = "" |
47 if line_num < len(lines): |
48 for i in xrange(spaces, nb_spaces): |
48 spaces = 0 |
49 indent += " " |
49 while lines[line_num][spaces] == " ": |
50 for line in lines: |
50 spaces += 1 |
51 if line != "": |
51 indent = "" |
52 compute += "%s%s\n"%(indent, line) |
52 for i in xrange(spaces, nb_spaces): |
53 else: |
53 indent += " " |
54 compute += "\n" |
54 for line in lines: |
|
55 if line != "": |
|
56 compute += "%s%s\n"%(indent, line) |
|
57 else: |
|
58 compute += "\n" |
55 return compute |
59 return compute |
56 |
60 |
57 def GenerateDataType(datatype_name): |
61 def GenerateDataType(datatype_name): |
58 if not datatypeComputed.get(datatype_name, True): |
62 if not datatypeComputed.get(datatype_name, True): |
59 datatypeComputed[datatype_name] = True |
63 datatypeComputed[datatype_name] = True |
60 global currentProject, currentProgram |
64 global currentProject, currentProgram |
61 datatype = currentProject.getDataType(datatype_name) |
65 datatype = currentProject.getdataType(datatype_name) |
62 datatype_def = " %s :"%datatype.getName() |
66 datatype_def = " %s :"%datatype.getname() |
63 basetype_content = datatype.baseType.getContent() |
67 basetype_content = datatype.baseType.getcontent() |
64 if basetype_content["name"] in ["string", "wstring"]: |
68 if basetype_content["name"] in ["string", "wstring"]: |
65 datatype_def += " %s"%basetype_content["name"].upper() |
69 datatype_def += " %s"%basetype_content["name"].upper() |
66 elif basetype_content["name"] == "derived": |
70 elif basetype_content["name"] == "derived": |
67 basetype_name = basetype_content["value"].getName() |
71 basetype_name = basetype_content["value"].getname() |
68 GenerateDataType(basetype_name) |
72 GenerateDataType(basetype_name) |
69 datatype_def += " %s"%basetype_name |
73 datatype_def += " %s"%basetype_name |
70 elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: |
74 elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: |
71 base_type = basetype_content["value"].baseType.getContent() |
75 base_type = basetype_content["value"].baseType.getcontent() |
72 if base_type["name"] == "derived": |
76 if base_type["name"] == "derived": |
73 basetype_name = base_type["value"].getName() |
77 basetype_name = base_type["value"].getname() |
74 else: |
78 else: |
75 basetype_name = base_type["name"] |
79 basetype_name = base_type["name"] |
76 GenerateDataType(basetype_name) |
80 GenerateDataType(basetype_name) |
77 min_value = basetype_content["value"].range.getLower() |
81 min_value = basetype_content["value"].range.getlower() |
78 max_value = basetype_content["value"].range.getUpper() |
82 max_value = basetype_content["value"].range.getupper() |
79 datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value) |
83 datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value) |
80 elif basetype_content["name"] == "enum": |
84 elif basetype_content["name"] == "enum": |
81 values = [] |
85 values = [] |
82 for value in basetype_content["value"].values.getValue(): |
86 for value in basetype_content["value"].values.getvalue(): |
83 values.append(value.getName()) |
87 values.append(value.getname()) |
84 datatype_def += " (%s)"%", ".join(values) |
88 datatype_def += " (%s)"%", ".join(values) |
85 elif basetype_content["name"] == "array": |
89 elif basetype_content["name"] == "array": |
86 base_type = basetype_content["value"].baseType.getContent() |
90 base_type = basetype_content["value"].baseType.getcontent() |
87 if base_type["name"] == "derived": |
91 if base_type["name"] == "derived": |
88 basetype_name = base_type["value"].getName() |
92 basetype_name = base_type["value"].getname() |
89 elif base_type["name"] in ["string", "wstring"]: |
93 elif base_type["name"] in ["string", "wstring"]: |
90 basetype_name = base_type["name"].upper() |
94 basetype_name = base_type["name"].upper() |
91 else: |
95 else: |
92 basetype_name = base_type["name"] |
96 basetype_name = base_type["name"] |
93 GenerateDataType(basetype_name) |
97 GenerateDataType(basetype_name) |
94 dimensions = [] |
98 dimensions = [] |
95 for dimension in basetype_content["value"].getDimension(): |
99 for dimension in basetype_content["value"].getdimension(): |
96 dimensions.append("0..%d"%(dimension.getUpper() - 1)) |
100 dimensions.append("0..%d"%(dimension.getupper() - 1)) |
97 datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name) |
101 datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name) |
98 else: |
102 else: |
99 datatype_def += " %s"%basetype_content["name"] |
103 datatype_def += " %s"%basetype_content["name"] |
100 if datatype.initialValue is not None: |
104 if datatype.initialValue is not None: |
101 datatype_def += " := %s"%str(datatype.initialValue.getValue()) |
105 datatype_def += " := %s"%str(datatype.initialValue.getvalue()) |
102 currentProgram += "%s;\n"%datatype_def |
106 currentProgram += "%s;\n"%datatype_def |
103 |
107 |
104 def GeneratePouProgram(pou_name): |
108 def GeneratePouProgram(pou_name): |
105 if not pouComputed.get(pou_name, True): |
109 if not pouComputed.get(pou_name, True): |
106 pouComputed[pou_name] = True |
110 pouComputed[pou_name] = True |
107 global currentProject, currentProgram |
111 global currentProject, currentProgram |
108 pou = currentProject.getPou(pou_name) |
112 pou = currentProject.getpou(pou_name) |
109 pou_type = pou.getPouType().getValue() |
113 pou_type = pou.getpouType() |
110 if pou_type in pouTypeNames: |
114 if pou_type in pouTypeNames: |
111 pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type]) |
115 pou_program = PouProgram(pou.getname(), pouTypeNames[pou_type]) |
112 else: |
116 else: |
113 raise ValueError, "Undefined pou type" |
117 raise ValueError, "Undefined pou type" |
114 pou_program.GenerateInterface(pou.getInterface()) |
118 pou_program.GenerateInterface(pou.getinterface()) |
115 pou_program.GenerateConnectionTypes(pou) |
119 pou_program.GenerateConnectionTypes(pou) |
116 pou_program.GenerateProgram(pou) |
120 pou_program.GenerateProgram(pou) |
117 currentProgram += pou_program.GenerateSTProgram() |
121 currentProgram += pou_program.GenerateSTProgram() |
118 |
122 |
119 def GenerateConfiguration(configuration): |
123 def GenerateConfiguration(configuration): |
120 config = "\nCONFIGURATION %s\n"%configuration.getName() |
124 config = "\nCONFIGURATION %s\n"%configuration.getname() |
121 for varlist in configuration.getGlobalVars(): |
125 for varlist in configuration.getglobalVars(): |
122 config += " VAR_GLOBAL" |
126 config += " VAR_GLOBAL" |
123 if varlist.getRetain(): |
127 if varlist.getretain(): |
124 config += " RETAIN" |
128 config += " RETAIN" |
125 if varlist.getConstant(): |
129 if varlist.getconstant(): |
126 config += " CONSTANT" |
130 config += " CONSTANT" |
127 config += "\n" |
131 config += "\n" |
128 for var in varlist.getVariable(): |
132 for var in varlist.getvariable(): |
129 vartype_content = var.getType().getContent() |
133 vartype_content = var.gettype().getcontent() |
130 if vartype_content["name"] == "derived": |
134 if vartype_content["name"] == "derived": |
131 var_type = vartype_content["value"].getName() |
135 var_type = vartype_content["value"].getname() |
132 elif vartype_content["name"] in ["string", "wstring"]: |
136 elif vartype_content["name"] in ["string", "wstring"]: |
133 var_type = vartype_content["name"].upper() |
137 var_type = vartype_content["name"].upper() |
134 else: |
138 else: |
135 var_type = vartype_content["name"] |
139 var_type = vartype_content["name"] |
136 config += " %s "%var.getName() |
140 config += " %s "%var.getname() |
137 address = var.getAddress() |
141 address = var.getaddress() |
138 if address: |
142 if address: |
139 config += "AT %s "%address |
143 config += "AT %s "%address |
140 config += ": %s"%var_type |
144 config += ": %s"%var_type |
141 initial = var.getInitialValue() |
145 initial = var.getinitialValue() |
142 if initial: |
146 if initial: |
143 value = str(initial.getValue()) |
147 value = str(initial.getvalue()) |
144 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
148 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
145 if var_type == "STRING": |
149 if var_type == "STRING": |
146 config += " := '%s'"%value |
150 config += " := '%s'"%value |
147 elif var_type == "WSTRING": |
151 elif var_type == "WSTRING": |
148 config += " := \"%s\""%value |
152 config += " := \"%s\""%value |
149 else: |
153 else: |
150 config += " := %s"%value |
154 config += " := %s"%value |
151 config += ";\n" |
155 config += ";\n" |
152 config += " END_VAR\n" |
156 config += " END_VAR\n" |
153 for resource in configuration.getResource(): |
157 for resource in configuration.getresource(): |
154 config += GenerateResource(resource) |
158 config += GenerateResource(resource) |
155 config += "END_CONFIGURATION\n" |
159 config += "END_CONFIGURATION\n" |
156 return config |
160 return config |
157 |
161 |
158 def GenerateResource(resource): |
162 def GenerateResource(resource): |
159 resrce = "\n RESOURCE %s ON BEREMIZ\n"%resource.getName() |
163 resrce = "\n RESOURCE %s ON BEREMIZ\n"%resource.getname() |
160 for varlist in resource.getGlobalVars(): |
164 for varlist in resource.getglobalVars(): |
161 resrce += " VAR_GLOBAL" |
165 resrce += " VAR_GLOBAL" |
162 if varlist.getRetain(): |
166 if varlist.getretain(): |
163 resrce += " RETAIN" |
167 resrce += " RETAIN" |
164 if varlist.getConstant(): |
168 if varlist.getconstant(): |
165 resrce += " CONSTANT" |
169 resrce += " CONSTANT" |
166 resrce += "\n" |
170 resrce += "\n" |
167 for var in varlist.getVariable(): |
171 for var in varlist.getvariable(): |
168 vartype_content = var.getType().getContent() |
172 vartype_content = var.gettype().getcontent() |
169 if vartype_content["name"] == "derived": |
173 if vartype_content["name"] == "derived": |
170 var_type = vartype_content["value"].getName() |
174 var_type = vartype_content["value"].getname() |
171 elif vartype_content["name"] in ["string", "wstring"]: |
175 elif vartype_content["name"] in ["string", "wstring"]: |
172 var_type = vartype_content["name"].upper() |
176 var_type = vartype_content["name"].upper() |
173 else: |
177 else: |
174 var_type = vartype_content["name"] |
178 var_type = vartype_content["name"] |
175 resrce += " %s "%var.getName() |
179 resrce += " %s "%var.getname() |
176 address = var.getAddress() |
180 address = var.getaddress() |
177 if address: |
181 if address: |
178 resrce += "AT %s "%address |
182 resrce += "AT %s "%address |
179 resrce += ": %s"%var_type |
183 resrce += ": %s"%var_type |
180 initial = var.getInitialValue() |
184 initial = var.getinitialValue() |
181 if initial: |
185 if initial: |
182 value = str(initial.getValue()) |
186 value = str(initial.getvalue()) |
183 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
187 value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value) |
184 if var_type == "STRING": |
188 if var_type == "STRING": |
185 resrce += " := '%s'"%value |
189 resrce += " := '%s'"%value |
186 elif var_type == "WSTRING": |
190 elif var_type == "WSTRING": |
187 resrce += " := \"%s\""%value |
191 resrce += " := \"%s\""%value |
188 else: |
192 else: |
189 resrce += " := %s"%value |
193 resrce += " := %s"%value |
190 resrce += ";\n" |
194 resrce += ";\n" |
191 resrce += " END_VAR\n" |
195 resrce += " END_VAR\n" |
192 tasks = resource.getTask() |
196 tasks = resource.gettask() |
193 for task in tasks: |
197 for task in tasks: |
194 resrce += " TASK %s("%task.getName() |
198 resrce += " TASK %s("%task.getname() |
195 args = [] |
199 args = [] |
196 single = task.getSingle() |
200 single = task.getsingle() |
197 if single: |
201 if single: |
198 args.append("SINGLE := %s"%single) |
202 args.append("SINGLE := %s"%single) |
199 interval = task.getInterval() |
203 interval = task.getinterval() |
200 if interval: |
204 if interval: |
201 text = "t#" |
205 text = "t#" |
202 if interval.hour != 0: |
206 if interval.hour != 0: |
203 text += "%dh"%interval.hour |
207 text += "%dh"%interval.hour |
204 if interval.minute != 0: |
208 if interval.minute != 0: |
256 if name == var_name: |
260 if name == var_name: |
257 return var_type |
261 return var_type |
258 return None |
262 return None |
259 |
263 |
260 def GetConnectedConnection(self, connection, body): |
264 def GetConnectedConnection(self, connection, body): |
261 links = connection.getConnections() |
265 links = connection.getconnections() |
262 if links and len(links) == 1: |
266 if links and len(links) == 1: |
263 return self.GetLinkedConnection(links[0], body) |
267 return self.GetLinkedConnection(links[0], body) |
264 return None |
268 return None |
265 |
269 |
266 def GetLinkedConnection(self, link, body): |
270 def GetLinkedConnection(self, link, body): |
267 parameter = link.getFormalParameter() |
271 parameter = link.getformalParameter() |
268 instance = body.getContentInstance(link.getRefLocalId()) |
272 instance = body.getcontentInstance(link.getrefLocalId()) |
269 if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable, plcopen.continuation, plcopen.contact, plcopen.coil)): |
273 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable, plcopen.commonObjects_continuation, plcopen.ldObjects_contact, plcopen.ldObjects_coil)): |
270 return instance.connectionPointOut |
274 return instance.connectionPointOut |
271 elif isinstance(instance, plcopen.block): |
275 elif isinstance(instance, plcopen.fbdObjects_block): |
272 outputvariables = instance.outputVariables.getVariable() |
276 outputvariables = instance.outputVariables.getvariable() |
273 if len(outputvariables) == 1: |
277 if len(outputvariables) == 1: |
274 return outputvariables[0].connectionPointOut |
278 return outputvariables[0].connectionPointOut |
275 elif parameter: |
279 elif parameter: |
276 for variable in outputvariables: |
280 for variable in outputvariables: |
277 if variable.getFormalParameter() == parameter: |
281 if variable.getformalParameter() == parameter: |
278 return variable.connectionPointOut |
282 return variable.connectionPointOut |
279 else: |
283 else: |
280 point = link.getPosition()[-1] |
284 point = link.getPosition()[-1] |
281 for variable in outputvariables: |
285 for variable in outputvariables: |
282 relposition = variable.connectionPointOut.getRelPosition() |
286 relposition = variable.connectionPointOut.getrelPositionXY() |
283 blockposition = instance.getPosition() |
287 blockposition = instance.getposition() |
284 if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]: |
288 if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]: |
285 return variable.connectionPointOut |
289 return variable.connectionPointOut |
286 elif isinstance(instance, plcopen.leftPowerRail): |
290 elif isinstance(instance, plcopen.ldObjects_leftPowerRail): |
287 outputconnections = instance.getConnectionPointOut() |
291 outputconnections = instance.getconnectionPointOut() |
288 if len(outputconnections) == 1: |
292 if len(outputconnections) == 1: |
289 return outputconnections[0] |
293 return outputconnections[0] |
290 else: |
294 else: |
291 point = link.getPosition()[-1] |
295 point = link.getposition()[-1] |
292 for outputconnection in outputconnections: |
296 for outputconnection in outputconnections: |
293 relposition = outputconnection.getRelPosition() |
297 relposition = outputconnection.getrelPositionXY() |
294 powerrailposition = instance.getPosition() |
298 powerrailposition = instance.getposition() |
295 if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]: |
299 if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]: |
296 return outputconnection |
300 return outputconnection |
297 return None |
301 return None |
298 |
302 |
299 def ExtractRelatedConnections(self, connection): |
303 def ExtractRelatedConnections(self, connection): |
302 return self.RelatedConnections.pop(i) |
306 return self.RelatedConnections.pop(i) |
303 return [connection] |
307 return [connection] |
304 |
308 |
305 def GenerateInterface(self, interface): |
309 def GenerateInterface(self, interface): |
306 if self.Type == "FUNCTION": |
310 if self.Type == "FUNCTION": |
307 returntype_content = interface.getReturnType().getContent() |
311 returntype_content = interface.getreturnType().getcontent() |
308 if returntype_content["value"] is None: |
312 if returntype_content["value"] is None: |
309 self.ReturnType = returntype_content["name"] |
313 self.ReturnType = returntype_content["name"] |
310 else: |
314 else: |
311 self.ReturnType = returntype_content["value"].getName() |
315 self.ReturnType = returntype_content["value"].getname() |
312 for varlist in interface.getContent(): |
316 for varlist in interface.getcontent(): |
313 variables = [] |
317 variables = [] |
314 located = False |
318 located = False |
315 for var in varlist["value"].getVariable(): |
319 for var in varlist["value"].getvariable(): |
316 vartype_content = var.getType().getContent() |
320 vartype_content = var.gettype().getcontent() |
317 if vartype_content["name"] == "derived": |
321 if vartype_content["name"] == "derived": |
318 var_type = vartype_content["value"].getName() |
322 var_type = vartype_content["value"].getname() |
319 GeneratePouProgram(var_type) |
323 GeneratePouProgram(var_type) |
320 blocktype = GetBlockType(var_type) |
324 blocktype = GetBlockType(var_type) |
321 if blocktype is not None: |
325 if blocktype is not None: |
322 variables.extend(blocktype["initialise"](var_type, var.getName())) |
326 variables.extend(blocktype["initialise"](var_type, var.getname())) |
323 located = False |
327 located = False |
324 else: |
328 else: |
325 initial = var.getInitialValue() |
329 initial = var.getinitialValue() |
326 if initial: |
330 if initial: |
327 initial_value = initial.getValue() |
331 initial_value = initial.getvalue() |
328 else: |
332 else: |
329 initial_value = None |
333 initial_value = None |
330 address = var.getAddress() |
334 address = var.getaddress() |
331 if address: |
335 if address: |
332 located = True |
336 located = True |
333 variables.append((vartype_content["value"].getName(), var.getName(), address, initial_value)) |
337 variables.append((vartype_content["value"].getname(), var.getname(), address, initial_value)) |
334 else: |
338 else: |
335 initial = var.getInitialValue() |
339 initial = var.getinitialValue() |
336 if initial: |
340 if initial: |
337 initial_value = initial.getValue() |
341 initial_value = initial.getvalue() |
338 else: |
342 else: |
339 initial_value = None |
343 initial_value = None |
340 address = var.getAddress() |
344 address = var.getaddress() |
341 if address: |
345 if address: |
342 located = True |
346 located = True |
343 if vartype_content["name"] in ["string", "wstring"]: |
347 if vartype_content["name"] in ["string", "wstring"]: |
344 variables.append((vartype_content["name"].upper(), var.getName(), address, initial_value)) |
348 variables.append((vartype_content["name"].upper(), var.getname(), address, initial_value)) |
345 else: |
349 else: |
346 variables.append((vartype_content["name"], var.getName(), address, initial_value)) |
350 variables.append((vartype_content["name"], var.getname(), address, initial_value)) |
347 if len(variables) > 0: |
351 if len(variables) > 0: |
348 self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), |
352 self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), |
349 varlist["value"].getConstant(), located, variables)) |
353 varlist["value"].getconstant(), located, variables)) |
350 |
354 |
351 def GenerateConnectionTypes(self, pou): |
355 def GenerateConnectionTypes(self, pou): |
352 body = pou.getBody() |
356 body = pou.getbody() |
353 body_content = body.getContent() |
357 body_content = body.getcontent() |
354 body_type = body_content["name"] |
358 body_type = body_content["name"] |
355 if body_type in ["FBD", "LD", "SFC"]: |
359 if body_type in ["FBD", "LD", "SFC"]: |
356 for instance in body.getContentInstances(): |
360 for instance in body.getcontentInstances(): |
357 if isinstance(instance, (plcopen.inVariable, plcopen.outVariable, plcopen.inOutVariable)): |
361 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
358 expression = instance.getExpression() |
362 expression = instance.getexpression() |
359 var_type = self.GetVariableType(expression) |
363 var_type = self.GetVariableType(expression) |
360 if expression == pou.getName(): |
364 if expression == pou.getname(): |
361 returntype_content = pou.interface.getReturnType().getContent() |
365 returntype_content = pou.interface.getreturnType().getcontent() |
362 if returntype_content["name"] == "derived": |
366 if returntype_content["name"] == "derived": |
363 var_type = returntype_content["value"].getName() |
367 var_type = returntype_content["value"].getname() |
364 elif returntype_content["name"] in ["string", "wstring"]: |
368 elif returntype_content["name"] in ["string", "wstring"]: |
365 var_type = returntype_content["name"].upper() |
369 var_type = returntype_content["name"].upper() |
366 else: |
370 else: |
367 var_type = returntype_content["name"] |
371 var_type = returntype_content["name"] |
368 elif var_type is None: |
372 elif var_type is None: |
369 var_type = expression.split("#")[0] |
373 var_type = expression.split("#")[0] |
370 if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)): |
374 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)): |
371 self.ConnectionTypes[instance.connectionPointOut] = var_type |
375 self.ConnectionTypes[instance.connectionPointOut] = var_type |
372 if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)): |
376 if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
373 self.ConnectionTypes[instance.connectionPointIn] = var_type |
377 self.ConnectionTypes[instance.connectionPointIn] = var_type |
374 connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
378 connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
375 if connected and connected not in self.ConnectionTypes: |
379 if connected and connected not in self.ConnectionTypes: |
376 for connection in self.ExtractRelatedConnections(connected): |
380 for connection in self.ExtractRelatedConnections(connected): |
377 self.ConnectionTypes[connection] = var_type |
381 self.ConnectionTypes[connection] = var_type |
378 elif isinstance(instance, (plcopen.contact, plcopen.coil)): |
382 elif isinstance(instance, (plcopen.ldObjects_contact, plcopen.ldObjects_coil)): |
379 self.ConnectionTypes[instance.connectionPointOut] = "BOOL" |
383 self.ConnectionTypes[instance.connectionPointOut] = "BOOL" |
380 self.ConnectionTypes[instance.connectionPointIn] = "BOOL" |
384 self.ConnectionTypes[instance.connectionPointIn] = "BOOL" |
381 connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
385 connected = self.GetConnectedConnection(instance.connectionPointIn, body) |
382 if connected and connected not in self.ConnectionTypes: |
386 if connected and connected not in self.ConnectionTypes: |
383 for connection in self.ExtractRelatedConnections(connected): |
387 for connection in self.ExtractRelatedConnections(connected): |
384 self.ConnectionTypes[connection] = "BOOL" |
388 self.ConnectionTypes[connection] = "BOOL" |
385 elif isinstance(instance, plcopen.leftPowerRail): |
389 elif isinstance(instance, plcopen.ldObjects_leftPowerRail): |
386 for connection in instance.getConnectionPointOut(): |
390 for connection in instance.getconnectionPointOut(): |
387 self.ConnectionTypes[connection] = "BOOL" |
391 self.ConnectionTypes[connection] = "BOOL" |
388 elif isinstance(instance, plcopen.rightPowerRail): |
392 elif isinstance(instance, plcopen.ldObjects_rightPowerRail): |
389 for connection in instance.getConnectionPointIn(): |
393 for connection in instance.getconnectionPointIn(): |
390 self.ConnectionTypes[connection] = "BOOL" |
394 self.ConnectionTypes[connection] = "BOOL" |
391 connected = self.GetConnectedConnection(connection, body) |
395 connected = self.GetConnectedConnection(connection, body) |
392 if connected and connected not in self.ConnectionTypes: |
396 if connected and connected not in self.ConnectionTypes: |
393 for connection in self.ExtractRelatedConnections(connected): |
397 for connection in self.ExtractRelatedConnections(connected): |
394 self.ConnectionTypes[connection] = "BOOL" |
398 self.ConnectionTypes[connection] = "BOOL" |
395 elif isinstance(instance, plcopen.transition): |
399 elif isinstance(instance, plcopen.sfcObjects_transition): |
396 content = instance.condition.getContent() |
400 content = instance.condition.getcontent() |
397 if content["name"] == "connection" and len(content["value"]) == 1: |
401 if content["name"] == "connection" and len(content["value"]) == 1: |
398 connected = self.GetLinkedConnection(content["value"][0], body) |
402 connected = self.GetLinkedConnection(content["value"][0], body) |
399 if connected and connected not in self.ConnectionTypes: |
403 if connected and connected not in self.ConnectionTypes: |
400 for connection in self.ExtractRelatedConnections(connected): |
404 for connection in self.ExtractRelatedConnections(connected): |
401 self.ConnectionTypes[connection] = "BOOL" |
405 self.ConnectionTypes[connection] = "BOOL" |
402 elif isinstance(instance, plcopen.block): |
406 elif isinstance(instance, plcopen.fbdObjects_block): |
403 block_infos = GetBlockType(instance.getTypeName()) |
407 block_infos = GetBlockType(instance.gettypeName()) |
404 undefined = {} |
408 undefined = {} |
405 for variable in instance.outputVariables.getVariable(): |
409 for variable in instance.outputVariables.getvariable(): |
406 output_name = variable.getFormalParameter() |
410 output_name = variable.getformalParameter() |
407 for oname, otype, oqualifier in block_infos["outputs"]: |
411 for oname, otype, oqualifier in block_infos["outputs"]: |
408 if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes: |
412 if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes: |
409 if otype.startswith("ANY"): |
413 if otype.startswith("ANY"): |
410 if otype not in undefined: |
414 if otype not in undefined: |
411 undefined[otype] = [] |
415 undefined[otype] = [] |
412 undefined[otype].append(variable.connectionPointOut) |
416 undefined[otype].append(variable.connectionPointOut) |
413 else: |
417 else: |
414 for connection in self.ExtractRelatedConnections(variable.connectionPointOut): |
418 for connection in self.ExtractRelatedConnections(variable.connectionPointOut): |
415 self.ConnectionTypes[connection] = otype |
419 self.ConnectionTypes[connection] = otype |
416 for variable in instance.inputVariables.getVariable(): |
420 for variable in instance.inputVariables.getvariable(): |
417 input_name = variable.getFormalParameter() |
421 input_name = variable.getformalParameter() |
418 for iname, itype, iqualifier in block_infos["inputs"]: |
422 for iname, itype, iqualifier in block_infos["inputs"]: |
419 if input_name == iname: |
423 if input_name == iname: |
420 connected = self.GetConnectedConnection(variable.connectionPointIn, body) |
424 connected = self.GetConnectedConnection(variable.connectionPointIn, body) |
421 if itype.startswith("ANY"): |
425 if itype.startswith("ANY"): |
422 if itype not in undefined: |
426 if itype not in undefined: |
441 else: |
445 else: |
442 for connection in related: |
446 for connection in related: |
443 self.ConnectionTypes[connection] = var_type |
447 self.ConnectionTypes[connection] = var_type |
444 |
448 |
445 def GenerateProgram(self, pou): |
449 def GenerateProgram(self, pou): |
446 body = pou.getBody() |
450 body = pou.getbody() |
447 body_content = body.getContent() |
451 body_content = body.getcontent() |
448 body_type = body_content["name"] |
452 body_type = body_content["name"] |
449 if body_type in ["IL","ST"]: |
453 if body_type in ["IL","ST"]: |
450 self.Program = ReIndentText(body_content["value"].getText(), 2) |
454 self.Program = ReIndentText(body_content["value"].gettext(), 2) |
451 elif body_type == "FBD": |
455 elif body_type == "FBD": |
452 orderedInstances = [] |
456 orderedInstances = [] |
453 otherInstances = [] |
457 otherInstances = [] |
454 for instance in body.getContentInstances(): |
458 for instance in body.getcontentInstances(): |
455 if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable, plcopen.block)): |
459 if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable, plcopen.fbdObjects_block)): |
456 executionOrderId = instance.getExecutionOrderId() |
460 executionOrderId = instance.getexecutionOrderId() |
457 if executionOrderId > 0: |
461 if executionOrderId > 0: |
458 orderedInstances.append((executionOrderId, instance)) |
462 orderedInstances.append((executionOrderId, instance)) |
459 else: |
463 else: |
460 otherInstances.append(instance) |
464 otherInstances.append(instance) |
461 elif isinstance(instance, plcopen.connector): |
465 elif isinstance(instance, plcopen.commonObjects_connector): |
462 otherInstances.append(instance) |
466 otherInstances.append(instance) |
463 orderedInstances.sort() |
467 orderedInstances.sort() |
464 instances = [instance for (executionOrderId, instance) in orderedInstances] + otherInstances |
468 instances = [instance for (executionOrderId, instance) in orderedInstances] + otherInstances |
465 for instance in instances: |
469 for instance in instances: |
466 if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)): |
470 if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)): |
467 var = instance.getExpression() |
471 var = instance.getexpression() |
468 connections = instance.connectionPointIn.getConnections() |
472 connections = instance.connectionPointIn.getconnections() |
469 if connections and len(connections) == 1: |
473 if connections and len(connections) == 1: |
470 expression = self.ComputeFBDExpression(body, connections[0]) |
474 expression = self.ComputeFBDExpression(body, connections[0]) |
471 self.Program += " %s := %s;\n"%(var, expression) |
475 self.Program += " %s := %s;\n"%(var, expression) |
472 elif isinstance(instance, plcopen.block): |
476 elif isinstance(instance, plcopen.fbdObjects_block): |
473 block_type = instance.getTypeName() |
477 block_type = instance.gettypeName() |
474 self.GeneratePouProgram(block_type) |
478 self.GeneratePouProgram(block_type) |
475 block_infos = GetBlockType(block_type) |
479 block_infos = GetBlockType(block_type) |
476 block_infos["generate"](self, instance, body, None) |
480 block_infos["generate"](self, instance, body, None) |
477 elif isinstance(instance, plcopen.connector): |
481 elif isinstance(instance, plcopen.commonObjects_connector): |
478 connector = instance.getName() |
482 connector = instance.getname() |
479 if self.ComputedConnectors.get(connector, None): |
483 if self.ComputedConnectors.get(connector, None): |
480 continue |
484 continue |
481 connections = instance.connectionPointIn.getConnections() |
485 connections = instance.connectionPointIn.getconnections() |
482 if connections and len(connections) == 1: |
486 if connections and len(connections) == 1: |
483 self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0]) |
487 self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0]) |
484 elif body_type == "LD": |
488 elif body_type == "LD": |
485 for instance in body.getContentInstances(): |
489 for instance in body.getcontentInstances(): |
486 if isinstance(instance, plcopen.coil): |
490 if isinstance(instance, plcopen.ldObjects_coil): |
487 paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body) |
491 paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), body) |
488 if len(paths) > 0: |
492 if len(paths) > 0: |
489 paths = tuple(paths) |
493 paths = tuple(paths) |
490 else: |
494 else: |
491 paths = paths[0] |
495 paths = paths[0] |
492 variable = self.ExtractModifier(instance, instance.getVariable()) |
496 variable = self.ExtractModifier(instance, instance.getvariable()) |
493 expression = self.ComputeLDExpression(paths, True) |
497 expression = self.ComputeLDExpression(paths, True) |
494 self.Program += " %s := %s;\n"%(variable, expression) |
498 self.Program += " %s := %s;\n"%(variable, expression) |
495 elif body_type == "SFC": |
499 elif body_type == "SFC": |
496 for instance in body.getContentInstances(): |
500 for instance in body.getcontentInstances(): |
497 if isinstance(instance, plcopen.step): |
501 if isinstance(instance, plcopen.sfcObjects_step): |
498 self.GenerateSFCStep(instance, pou) |
502 self.GenerateSFCStep(instance, pou) |
499 elif isinstance(instance, plcopen.actionBlock): |
503 elif isinstance(instance, plcopen.commonObjects_actionBlock): |
500 self.GenerateSFCStepActions(instance, pou) |
504 self.GenerateSFCStepActions(instance, pou) |
501 elif isinstance(instance, plcopen.transition): |
505 elif isinstance(instance, plcopen.sfcObjects_transition): |
502 self.GenerateSFCTransition(instance, pou) |
506 self.GenerateSFCTransition(instance, pou) |
503 elif isinstance(instance, plcopen.jumpStep): |
507 elif isinstance(instance, plcopen.sfcObjects_jumpStep): |
504 self.GenerateSFCJump(instance, pou) |
508 self.GenerateSFCJump(instance, pou) |
505 if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "": |
509 if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "": |
506 action_name = "COMPUTE_FUNCTION_BLOCKS" |
510 action_name = "COMPUTE_FUNCTION_BLOCKS" |
507 action_infos = {"qualifier" : "S", "content" : action_name} |
511 action_infos = {"qualifier" : "S", "content" : action_name} |
508 self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) |
512 self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) |
510 self.Program = "" |
514 self.Program = "" |
511 for initialstep in self.InitialSteps: |
515 for initialstep in self.InitialSteps: |
512 self.ComputeSFCStep(initialstep) |
516 self.ComputeSFCStep(initialstep) |
513 |
517 |
514 def ComputeFBDExpression(self, body, link, order = False): |
518 def ComputeFBDExpression(self, body, link, order = False): |
515 localid = link.getRefLocalId() |
519 localid = link.getrefLocalId() |
516 instance = body.getContentInstance(localid) |
520 instance = body.getcontentInstance(localid) |
517 if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)): |
521 if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)): |
518 return instance.getExpression() |
522 return instance.getexpression() |
519 elif isinstance(instance, plcopen.block): |
523 elif isinstance(instance, plcopen.fbdObjects_block): |
520 block_type = instance.getTypeName() |
524 block_type = instance.gettypeName() |
521 self.GeneratePouProgram(block_type) |
525 self.GeneratePouProgram(block_type) |
522 block_infos = GetBlockType(block_type) |
526 block_infos = GetBlockType(block_type) |
523 return block_infos["generate"](self, instance, body, link, order) |
527 return block_infos["generate"](self, instance, body, link, order) |
524 elif isinstance(instance, plcopen.continuation): |
528 elif isinstance(instance, plcopen.commonObjects_continuation): |
525 name = instance.getName() |
529 name = instance.getname() |
526 computed_value = self.ComputedConnectors.get(name, None) |
530 computed_value = self.ComputedConnectors.get(name, None) |
527 if computed_value != None: |
531 if computed_value != None: |
528 return computed_value |
532 return computed_value |
529 for tmp_instance in body.getContentInstances(): |
533 for tmp_instance in body.getcontentInstances(): |
530 if isinstance(tmp_instance, plcopen.connector): |
534 if isinstance(tmp_instance, plcopen.commonObjects_connector): |
531 if tmp_instance.getName() == name: |
535 if tmp_instance.getname() == name: |
532 connections = tmp_instance.connectionPointIn.getConnections() |
536 connections = tmp_instance.connectionPointIn.getconnections() |
533 if connections and len(connections) == 1: |
537 if connections and len(connections) == 1: |
534 expression = self.ComputeFBDExpression(body, connections[0], order) |
538 expression = self.ComputeFBDExpression(body, connections[0], order) |
535 self.ComputedConnectors[name] = expression |
539 self.ComputedConnectors[name] = expression |
536 return expression |
540 return expression |
537 raise ValueError, "No connector found" |
541 raise ValueError, "No connector found" |
538 |
542 |
539 def GenerateLDPaths(self, connections, body): |
543 def GenerateLDPaths(self, connections, body): |
540 paths = [] |
544 paths = [] |
541 for connection in connections: |
545 for connection in connections: |
542 localId = connection.getRefLocalId() |
546 localId = connection.getrefLocalId() |
543 next = body.getContentInstance(localId) |
547 next = body.getcontentInstance(localId) |
544 if isinstance(next, plcopen.leftPowerRail): |
548 if isinstance(next, plcopen.ldObjects_leftPowerRail): |
545 paths.append(None) |
549 paths.append(None) |
546 elif isinstance(next, plcopen.block): |
550 elif isinstance(next, plcopen.fbdObjects_block): |
547 block_type = next.getTypeName() |
551 block_type = next.gettypeName() |
548 self.GeneratePouProgram(block_type) |
552 self.GeneratePouProgram(block_type) |
549 block_infos = GetBlockType(block_type) |
553 block_infos = GetBlockType(block_type) |
550 paths.append(block_infos["generate"](self, next, body, connection)) |
554 paths.append(block_infos["generate"](self, next, body, connection)) |
551 else: |
555 else: |
552 variable = self.ExtractModifier(next, next.getVariable()) |
556 variable = self.ExtractModifier(next, next.getvariable()) |
553 result = self.GenerateLDPaths(next.connectionPointIn.getConnections(), body) |
557 result = self.GenerateLDPaths(next.connectionPointIn.getconnections(), body) |
554 if len(result) > 1: |
558 if len(result) > 1: |
555 paths.append([variable, tuple(result)]) |
559 paths.append([variable, tuple(result)]) |
556 elif type(result[0]) == ListType: |
560 elif type(result[0]) == ListType: |
557 paths.append([variable] + result[0]) |
561 paths.append([variable] + result[0]) |
558 elif result[0]: |
562 elif result[0]: |
562 return paths |
566 return paths |
563 |
567 |
564 def GetNetworkType(self, connections, body): |
568 def GetNetworkType(self, connections, body): |
565 network_type = "FBD" |
569 network_type = "FBD" |
566 for connection in connections: |
570 for connection in connections: |
567 localId = connection.getRefLocalId() |
571 localId = connection.getrefLocalId() |
568 next = body.getContentInstance(localId) |
572 next = body.getcontentInstance(localId) |
569 if isinstance(next, plcopen.leftPowerRail) or isinstance(next, plcopen.contact): |
573 if isinstance(next, plcopen.ldObjects_leftPowerRail) or isinstance(next, plcopen.ldObjects_contact): |
570 return "LD" |
574 return "LD" |
571 elif isinstance(next, plcopen.block): |
575 elif isinstance(next, plcopen.fbdObjects_block): |
572 for variable in next.inputVariables.getVariable(): |
576 for variable in next.inputVariables.getvariable(): |
573 result = self.GetNetworkType(variable.connectionPointIn.getConnections(), body) |
577 result = self.GetNetworkType(variable.connectionPointIn.getconnections(), body) |
574 if result != "FBD": |
578 if result != "FBD": |
575 return result |
579 return result |
576 elif isinstance(next, plcopen.inVariable): |
580 elif isinstance(next, plcopen.fbdObjects_inVariable): |
577 return "FBD" |
581 return "FBD" |
578 elif isinstance(next, plcopen.inOutVariable): |
582 elif isinstance(next, plcopen.fbdObjects_inOutVariable): |
579 return self.GetNetworkType(next.connectionPointIn.getConnections(), body) |
583 return self.GetNetworkType(next.connectionPointIn.getconnections(), body) |
580 else: |
584 else: |
581 return None |
585 return None |
582 return "FBD" |
586 return "FBD" |
583 |
587 |
584 def ExtractDivergenceInput(self, divergence, pou): |
588 def ExtractDivergenceInput(self, divergence, pou): |
585 connectionPointIn = divergence.getConnectionPointIn() |
589 connectionPointIn = divergence.getconnectionPointIn() |
586 if connectionPointIn: |
590 if connectionPointIn: |
587 connections = connectionPointIn.getConnections() |
591 connections = connectionPointIn.getconnections() |
588 if len(connections) == 1: |
592 if len(connections) == 1: |
589 instanceLocalId = connections[0].getRefLocalId() |
593 instanceLocalId = connections[0].getrefLocalId() |
590 return pou.body.getContentInstance(instanceLocalId) |
594 return pou.body.getcontentInstance(instanceLocalId) |
591 return None |
595 return None |
592 |
596 |
593 def ExtractConvergenceInputs(self, convergence, pou): |
597 def ExtractConvergenceInputs(self, convergence, pou): |
594 instances = [] |
598 instances = [] |
595 for connectionPointIn in convergence.getConnectionPointIn(): |
599 for connectionPointIn in convergence.getconnectionPointIn(): |
596 connections = connectionPointIn.getConnections() |
600 connections = connectionPointIn.getconnections() |
597 if len(connections) == 1: |
601 if len(connections) == 1: |
598 instanceLocalId = connections[0].getRefLocalId() |
602 instanceLocalId = connections[0].getrefLocalId() |
599 instances.append(pou.body.getContentInstance(instanceLocalId)) |
603 instances.append(pou.body.getcontentInstance(instanceLocalId)) |
600 return instances |
604 return instances |
601 |
605 |
602 def GenerateSFCStep(self, step, pou): |
606 def GenerateSFCStep(self, step, pou): |
603 step_name = step.getName() |
607 step_name = step.getname() |
604 if step_name not in self.SFCNetworks["Steps"].keys(): |
608 if step_name not in self.SFCNetworks["Steps"].keys(): |
605 if step.getInitialStep(): |
609 if step.getinitialStep(): |
606 self.InitialSteps.append(step_name) |
610 self.InitialSteps.append(step_name) |
607 step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []} |
611 step_infos = {"initial" : step.getinitialStep(), "transitions" : [], "actions" : []} |
608 if step.connectionPointIn: |
612 if step.connectionPointIn: |
609 instances = [] |
613 instances = [] |
610 connections = step.connectionPointIn.getConnections() |
614 connections = step.connectionPointIn.getconnections() |
611 if len(connections) == 1: |
615 if len(connections) == 1: |
612 instanceLocalId = connections[0].getRefLocalId() |
616 instanceLocalId = connections[0].getrefLocalId() |
613 instance = pou.body.getContentInstance(instanceLocalId) |
617 instance = pou.body.getcontentInstance(instanceLocalId) |
614 if isinstance(instance, plcopen.transition): |
618 if isinstance(instance, plcopen.sfcObjects_transition): |
615 instances.append(instance) |
619 instances.append(instance) |
616 elif isinstance(instance, plcopen.selectionConvergence): |
620 elif isinstance(instance, plcopen.sfcObjects_selectionConvergence): |
617 instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
621 instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
618 elif isinstance(instance, plcopen.simultaneousDivergence): |
622 elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence): |
619 transition = self.ExtractDivergenceInput(instance, pou) |
623 transition = self.ExtractDivergenceInput(instance, pou) |
620 if transition: |
624 if transition: |
621 if isinstance(transition, plcopen.transition): |
625 if isinstance(transition, plcopen.sfcObjects_transition): |
622 instances.append(transition) |
626 instances.append(transition) |
623 elif isinstance(transition, plcopen.selectionConvergence): |
627 elif isinstance(transition, plcopen.sfcObjects_selectionConvergence): |
624 instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
628 instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
625 for instance in instances: |
629 for instance in instances: |
626 self.GenerateSFCTransition(instance, pou) |
630 self.GenerateSFCTransition(instance, pou) |
627 if instance in self.SFCNetworks["Transitions"].keys(): |
631 if instance in self.SFCNetworks["Transitions"].keys(): |
628 self.SFCNetworks["Transitions"][instance]["to"].append(step_name) |
632 self.SFCNetworks["Transitions"][instance]["to"].append(step_name) |
629 self.SFCNetworks["Steps"][step_name] = step_infos |
633 self.SFCNetworks["Steps"][step_name] = step_infos |
630 |
634 |
631 def GenerateSFCJump(self, jump, pou): |
635 def GenerateSFCJump(self, jump, pou): |
632 jump_target = jump.getTargetName() |
636 jump_target = jump.gettargetName() |
633 if jump.connectionPointIn: |
637 if jump.connectionPointIn: |
634 instances = [] |
638 instances = [] |
635 connections = jump.connectionPointIn.getConnections() |
639 connections = jump.connectionPointIn.getconnections() |
636 if len(connections) == 1: |
640 if len(connections) == 1: |
637 instanceLocalId = connections[0].getRefLocalId() |
641 instanceLocalId = connections[0].getrefLocalId() |
638 instance = pou.body.getContentInstance(instanceLocalId) |
642 instance = pou.body.getcontentInstance(instanceLocalId) |
639 if isinstance(instance, plcopen.transition): |
643 if isinstance(instance, plcopen.sfcObjects_transition): |
640 instances.append(instance) |
644 instances.append(instance) |
641 elif isinstance(instance, plcopen.selectionConvergence): |
645 elif isinstance(instance, plcopen.sfcObjects_selectionConvergence): |
642 instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
646 instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
643 elif isinstance(instance, plcopen.simultaneousDivergence): |
647 elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence): |
644 transition = self.ExtractDivergenceInput(instance, pou) |
648 transition = self.ExtractDivergenceInput(instance, pou) |
645 if transition: |
649 if transition: |
646 if isinstance(transition, plcopen.transition): |
650 if isinstance(transition, plcopen.sfcObjects_transition): |
647 instances.append(transition) |
651 instances.append(transition) |
648 elif isinstance(transition, plcopen.selectionConvergence): |
652 elif isinstance(transition, plcopen.sfcObjects_selectionConvergence): |
649 instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
653 instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
650 for instance in instances: |
654 for instance in instances: |
651 self.GenerateSFCTransition(instance, pou) |
655 self.GenerateSFCTransition(instance, pou) |
652 if instance in self.SFCNetworks["Transitions"].keys(): |
656 if instance in self.SFCNetworks["Transitions"].keys(): |
653 self.SFCNetworks["Transitions"][instance]["to"].append(jump_target) |
657 self.SFCNetworks["Transitions"][instance]["to"].append(jump_target) |
654 |
658 |
655 def GenerateSFCStepActions(self, actionBlock, pou): |
659 def GenerateSFCStepActions(self, actionBlock, pou): |
656 connections = actionBlock.connectionPointIn.getConnections() |
660 connections = actionBlock.connectionPointIn.getconnections() |
657 if len(connections) == 1: |
661 if len(connections) == 1: |
658 stepLocalId = connections[0].getRefLocalId() |
662 stepLocalId = connections[0].getrefLocalId() |
659 step = pou.body.getContentInstance(stepLocalId) |
663 step = pou.body.getcontentInstance(stepLocalId) |
660 self.GenerateSFCStep(step, pou) |
664 self.GenerateSFCStep(step, pou) |
661 step_name = step.getName() |
665 step_name = step.getname() |
662 if step_name in self.SFCNetworks["Steps"].keys(): |
666 if step_name in self.SFCNetworks["Steps"].keys(): |
663 actions = actionBlock.getActions() |
667 actions = actionBlock.getactions() |
664 for action in actions: |
668 for action in actions: |
665 action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]} |
669 action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]} |
666 if "duration" in action: |
670 if "duration" in action: |
667 action_infos["duration"] = action["duration"] |
671 action_infos["duration"] = action["duration"] |
668 if "indicator" in action: |
672 if "indicator" in action: |
675 action_infos["content"] = action_name |
679 action_infos["content"] = action_name |
676 self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
680 self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
677 |
681 |
678 def GenerateSFCAction(self, action_name, pou): |
682 def GenerateSFCAction(self, action_name, pou): |
679 if action_name not in self.SFCNetworks["Actions"].keys(): |
683 if action_name not in self.SFCNetworks["Actions"].keys(): |
680 actionContent = pou.getAction(action_name) |
684 actionContent = pou.getaction(action_name) |
681 if actionContent: |
685 if actionContent: |
682 actionType = actionContent.getBodyType() |
686 actionType = actionContent.getbodyType() |
683 actionBody = actionContent.getBody() |
687 actionBody = actionContent.getbody() |
684 if actionType in ["ST", "IL"]: |
688 if actionType in ["ST", "IL"]: |
685 self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4) |
689 self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.gettext(), 4) |
686 elif actionType == "FBD": |
690 elif actionType == "FBD": |
687 for instance in actionBody.getContentInstances(): |
691 for instance in actionBody.getcontentInstances(): |
688 if isinstance(instance, plcopen.outVariable): |
692 if isinstance(instance, plcopen.fbdObjects_outVariable): |
689 var = instance.getExpression() |
693 var = instance.getexpression() |
690 connections = instance.connectionPointIn.getConnections() |
694 connections = instance.connectionPointIn.getconnections() |
691 if connections and len(connections) == 1: |
695 if connections and len(connections) == 1: |
692 expression = self.ComputeFBDExpression(actionBody, connections[0]) |
696 expression = self.ComputeFBDExpression(actionBody, connections[0]) |
693 action_content = self.Program + " %s := %s;\n"%(var, expression) |
697 action_content = self.Program + " %s := %s;\n"%(var, expression) |
694 self.Program = "" |
698 self.Program = "" |
695 self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
699 self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
696 elif actionType == "LD": |
700 elif actionType == "LD": |
697 for instance in actionbody.getContentInstances(): |
701 for instance in actionbody.getcontentInstances(): |
698 if isinstance(instance, plcopen.coil): |
702 if isinstance(instance, plcopen.ldObjects_coil): |
699 paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody) |
703 paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), actionBody) |
700 variable = self.ExtractModifier(instance, instance.getVariable()) |
704 variable = self.ExtractModifier(instance, instance.getvariable()) |
701 expression = self.ComputeLDExpression(paths, True) |
705 expression = self.ComputeLDExpression(paths, True) |
702 action_content = self.Program + " %s := %s;\n"%(variable, expression) |
706 action_content = self.Program + " %s := %s;\n"%(variable, expression) |
703 self.Program = "" |
707 self.Program = "" |
704 self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
708 self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4) |
705 |
709 |
706 def GenerateSFCTransition(self, transition, pou): |
710 def GenerateSFCTransition(self, transition, pou): |
707 if transition not in self.SFCNetworks["Transitions"].keys(): |
711 if transition not in self.SFCNetworks["Transitions"].keys(): |
708 steps = [] |
712 steps = [] |
709 connections = transition.connectionPointIn.getConnections() |
713 connections = transition.connectionPointIn.getconnections() |
710 if len(connections) == 1: |
714 if len(connections) == 1: |
711 instanceLocalId = connections[0].getRefLocalId() |
715 instanceLocalId = connections[0].getrefLocalId() |
712 instance = pou.body.getContentInstance(instanceLocalId) |
716 instance = pou.body.getcontentInstance(instanceLocalId) |
713 if isinstance(instance, plcopen.step): |
717 if isinstance(instance, plcopen.sfcObjects_step): |
714 steps.append(instance) |
718 steps.append(instance) |
715 elif isinstance(instance, plcopen.selectionDivergence): |
719 elif isinstance(instance, plcopen.sfcObjects_selectionDivergence): |
716 step = self.ExtractDivergenceInput(instance, pou) |
720 step = self.ExtractDivergenceInput(instance, pou) |
717 if step: |
721 if step: |
718 if isinstance(step, plcopen.step): |
722 if isinstance(step, plcopen.sfcObjects_step): |
719 steps.append(step) |
723 steps.append(step) |
720 elif isinstance(step, plcopen.simultaneousConvergence): |
724 elif isinstance(step, plcopen.sfcObjects_simultaneousConvergence): |
721 steps.extend(self.ExtractConvergenceInputs(step, pou)) |
725 steps.extend(self.ExtractConvergenceInputs(step, pou)) |
722 elif isinstance(instance, plcopen.simultaneousConvergence): |
726 elif isinstance(instance, plcopen.sfcObjects_simultaneousConvergence): |
723 steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
727 steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
724 transition_infos = {"priority": transition.getPriority(), "from": [], "to" : []} |
728 transition_infos = {"priority": transition.getpriority(), "from": [], "to" : []} |
725 transitionValues = transition.getConditionContent() |
729 transitionValues = transition.getconditionContent() |
726 if transitionValues["type"] == "inline": |
730 if transitionValues["type"] == "inline": |
727 transition_infos["content"] = "\n := %s;\n"%transitionValues["value"] |
731 transition_infos["content"] = "\n := %s;\n"%transitionValues["value"] |
728 elif transitionValues["type"] == "reference": |
732 elif transitionValues["type"] == "reference": |
729 transitionContent = pou.getTransition(transitionValues["value"]) |
733 transitionContent = pou.gettransition(transitionValues["value"]) |
730 transitionType = transitionContent.getBodyType() |
734 transitionType = transitionContent.getbodyType() |
731 transitionBody = transitionContent.getBody() |
735 transitionBody = transitionContent.getbody() |
732 if transitionType == "IL": |
736 if transitionType == "IL": |
733 transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4) |
737 transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.gettext(), 4) |
734 elif transitionType == "ST": |
738 elif transitionType == "ST": |
735 transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4) |
739 transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.gettext(), 4) |
736 elif transitionType == "FBD": |
740 elif transitionType == "FBD": |
737 for instance in transitionBody.getContentInstances(): |
741 for instance in transitionBody.getcontentInstances(): |
738 if isinstance(instance, plcopen.outVariable): |
742 if isinstance(instance, plcopen.fbdObjects_outVariable): |
739 connections = instance.connectionPointIn.getConnections() |
743 connections = instance.connectionPointIn.getconnections() |
740 if connections and len(connections) == 1: |
744 if connections and len(connections) == 1: |
741 expression = self.ComputeFBDExpression(transitionBody, connections[0]) |
745 expression = self.ComputeFBDExpression(transitionBody, connections[0]) |
742 transition_infos["content"] = "\n := %s;\n"%expression |
746 transition_infos["content"] = "\n := %s;\n"%expression |
743 self.SFCComputedBlocks += self.Program |
747 self.SFCComputedBlocks += self.Program |
744 self.Program = "" |
748 self.Program = "" |
745 elif transitionType == "LD": |
749 elif transitionType == "LD": |
746 for instance in transitionBody.getContentInstances(): |
750 for instance in transitionBody.getcontentInstances(): |
747 if isinstance(instance, plcopen.coil): |
751 if isinstance(instance, plcopen.ldObjects_coil): |
748 paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody) |
752 paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), transitionBody) |
749 expression = self.ComputeLDExpression(paths, True) |
753 expression = self.ComputeLDExpression(paths, True) |
750 transition_infos["content"] = "\n := %s;\n"%expression |
754 transition_infos["content"] = "\n := %s;\n"%expression |
751 self.SFCComputedBlocks += self.Program |
755 self.SFCComputedBlocks += self.Program |
752 self.Program = "" |
756 self.Program = "" |
753 elif transitionValues["type"] == "connection": |
757 elif transitionValues["type"] == "connection": |
754 body = pou.getBody() |
758 body = pou.getbody() |
755 connections = transition.getConnections() |
759 connections = transition.getconnections() |
756 network_type = self.GetNetworkType(connections, body) |
760 network_type = self.GetNetworkType(connections, body) |
757 if network_type == None: |
761 if network_type == None: |
758 raise Exception |
762 raise Exception |
759 if len(connections) > 1 or network_type == "LD": |
763 if len(connections) > 1 or network_type == "LD": |
760 paths = self.GenerateLDPaths(connections, body) |
764 paths = self.GenerateLDPaths(connections, body) |