author | Laurent Bessard |
Mon, 09 Sep 2013 23:36:51 +0200 | |
changeset 1309 | 85ce56758900 |
parent 1298 | f034fb2b1aab |
child 1310 | 3d7fa2257b24 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
1297 | 25 |
from plcopen import PLCOpenParser |
814 | 26 |
from plcopen.structures import * |
27 |
from types import * |
|
28 |
import re |
|
29 |
||
30 |
# Dictionary associating PLCOpen variable categories to the corresponding |
|
31 |
# IEC 61131-3 variable categories |
|
32 |
varTypeNames = {"localVars" : "VAR", "tempVars" : "VAR_TEMP", "inputVars" : "VAR_INPUT", |
|
33 |
"outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL", |
|
34 |
"globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"} |
|
35 |
||
36 |
||
37 |
# Dictionary associating PLCOpen POU categories to the corresponding |
|
38 |
# IEC 61131-3 POU categories |
|
39 |
pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"} |
|
40 |
||
41 |
||
42 |
errorVarTypes = { |
|
43 |
"VAR_INPUT": "var_input", |
|
44 |
"VAR_OUTPUT": "var_output", |
|
45 |
"VAR_INOUT": "var_inout", |
|
46 |
} |
|
47 |
||
48 |
# Helper function for reindenting text |
|
49 |
def ReIndentText(text, nb_spaces): |
|
50 |
compute = "" |
|
51 |
lines = text.splitlines() |
|
52 |
if len(lines) > 0: |
|
53 |
line_num = 0 |
|
54 |
while line_num < len(lines) and len(lines[line_num].strip()) == 0: |
|
55 |
line_num += 1 |
|
56 |
if line_num < len(lines): |
|
57 |
spaces = 0 |
|
58 |
while lines[line_num][spaces] == " ": |
|
59 |
spaces += 1 |
|
60 |
indent = "" |
|
61 |
for i in xrange(spaces, nb_spaces): |
|
62 |
indent += " " |
|
63 |
for line in lines: |
|
64 |
if line != "": |
|
65 |
compute += "%s%s\n"%(indent, line) |
|
66 |
else: |
|
67 |
compute += "\n" |
|
68 |
return compute |
|
69 |
||
70 |
def SortInstances(a, b): |
|
71 |
ax, ay = int(a.getx()), int(a.gety()) |
|
72 |
bx, by = int(b.getx()), int(b.gety()) |
|
73 |
if abs(ay - by) < 10: |
|
74 |
return cmp(ax, bx) |
|
75 |
else: |
|
76 |
return cmp(ay, by) |
|
77 |
||
78 |
#------------------------------------------------------------------------------- |
|
79 |
# Specific exception for PLC generating errors |
|
80 |
#------------------------------------------------------------------------------- |
|
81 |
||
82 |
||
83 |
class PLCGenException(Exception): |
|
84 |
pass |
|
85 |
||
86 |
||
87 |
#------------------------------------------------------------------------------- |
|
88 |
# Generator of PLC program |
|
89 |
#------------------------------------------------------------------------------- |
|
90 |
||
91 |
||
92 |
class ProgramGenerator: |
|
93 |
||
94 |
# Create a new PCL program generator |
|
95 |
def __init__(self, controler, project, errors, warnings): |
|
96 |
# Keep reference of the controler and project |
|
97 |
self.Controler = controler |
|
98 |
self.Project = project |
|
99 |
# Reset the internal variables used to generate PLC programs |
|
100 |
self.Program = [] |
|
101 |
self.DatatypeComputed = {} |
|
102 |
self.PouComputed = {} |
|
103 |
self.Errors = errors |
|
104 |
self.Warnings = warnings |
|
105 |
||
106 |
# Compute value according to type given |
|
107 |
def ComputeValue(self, value, var_type): |
|
108 |
base_type = self.Controler.GetBaseType(var_type) |
|
1032
c4989e53f9c3
Fix bug defining string initial value using quotes
Laurent Bessard
parents:
893
diff
changeset
|
109 |
if base_type == "STRING" and not value.startswith("'") and not value.endswith("'"): |
814 | 110 |
return "'%s'"%value |
1032
c4989e53f9c3
Fix bug defining string initial value using quotes
Laurent Bessard
parents:
893
diff
changeset
|
111 |
elif base_type == "WSTRING" and not value.startswith('"') and not value.endswith('"'): |
814 | 112 |
return "\"%s\""%value |
113 |
return value |
|
114 |
||
115 |
# Generate a data type from its name |
|
116 |
def GenerateDataType(self, datatype_name): |
|
117 |
# Verify that data type hasn't been generated yet |
|
118 |
if not self.DatatypeComputed.get(datatype_name, True): |
|
119 |
# If not mark data type as computed |
|
120 |
self.DatatypeComputed[datatype_name] = True |
|
121 |
||
122 |
# Getting datatype model from project |
|
123 |
datatype = self.Project.getdataType(datatype_name) |
|
124 |
tagname = self.Controler.ComputeDataTypeName(datatype.getname()) |
|
125 |
datatype_def = [(" ", ()), |
|
126 |
(datatype.getname(), (tagname, "name")), |
|
127 |
(" : ", ())] |
|
128 |
basetype_content = datatype.baseType.getcontent() |
|
1297 | 129 |
basetype_content_type = basetype_content.getLocalTag() |
814 | 130 |
# Data type derived directly from a user defined type |
1297 | 131 |
if basetype_content_type == "derived": |
132 |
basetype_name = basetype_content.getname() |
|
814 | 133 |
self.GenerateDataType(basetype_name) |
134 |
datatype_def += [(basetype_name, (tagname, "base"))] |
|
135 |
# Data type is a subrange |
|
1297 | 136 |
elif basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]: |
137 |
base_type = basetype_content.baseType.getcontent() |
|
138 |
base_type_type = base_type.getLocalTag() |
|
814 | 139 |
# Subrange derived directly from a user defined type |
1297 | 140 |
if base_type_type == "derived": |
141 |
basetype_name = base_type_type.getname() |
|
814 | 142 |
self.GenerateDataType(basetype_name) |
143 |
# Subrange derived directly from an elementary type |
|
144 |
else: |
|
1297 | 145 |
basetype_name = base_type_type |
146 |
min_value = basetype_content.range.getlower() |
|
147 |
max_value = basetype_content.range.getupper() |
|
814 | 148 |
datatype_def += [(basetype_name, (tagname, "base")), |
149 |
(" (", ()), |
|
150 |
("%s"%min_value, (tagname, "lower")), |
|
151 |
("..", ()), |
|
152 |
("%s"%max_value, (tagname, "upper")), |
|
153 |
(")",())] |
|
154 |
# Data type is an enumerated type |
|
1297 | 155 |
elif basetype_content_type == "enum": |
814 | 156 |
values = [[(value.getname(), (tagname, "value", i))] |
1297 | 157 |
for i, value in enumerate( |
158 |
basetype_content.xpath("ppx:values/ppx:value", |
|
159 |
namespaces=PLCOpenParser.NSMAP))] |
|
814 | 160 |
datatype_def += [("(", ())] |
161 |
datatype_def += JoinList([(", ", ())], values) |
|
162 |
datatype_def += [(")", ())] |
|
163 |
# Data type is an array |
|
1297 | 164 |
elif basetype_content_type == "array": |
165 |
base_type = basetype_content.baseType.getcontent() |
|
166 |
base_type_type = base_type.getLocalTag() |
|
814 | 167 |
# Array derived directly from a user defined type |
1297 | 168 |
if base_type_type == "derived": |
169 |
basetype_name = base_type.getname() |
|
814 | 170 |
self.GenerateDataType(basetype_name) |
171 |
# Array derived directly from an elementary type |
|
172 |
else: |
|
1297 | 173 |
basetype_name = base_type_type.upper() |
814 | 174 |
dimensions = [[("%s"%dimension.getlower(), (tagname, "range", i, "lower")), |
175 |
("..", ()), |
|
176 |
("%s"%dimension.getupper(), (tagname, "range", i, "upper"))] |
|
1297 | 177 |
for i, dimension in enumerate(basetype_content.getdimension())] |
814 | 178 |
datatype_def += [("ARRAY [", ())] |
179 |
datatype_def += JoinList([(",", ())], dimensions) |
|
180 |
datatype_def += [("] OF " , ()), |
|
181 |
(basetype_name, (tagname, "base"))] |
|
182 |
# Data type is a structure |
|
1297 | 183 |
elif basetype_content_type == "struct": |
814 | 184 |
elements = [] |
1297 | 185 |
for i, element in enumerate(basetype_content.getvariable()): |
814 | 186 |
element_type = element.type.getcontent() |
1297 | 187 |
element_type_type = element_type.getLocalTag() |
814 | 188 |
# Structure element derived directly from a user defined type |
1297 | 189 |
if element_type_type == "derived": |
190 |
elementtype_name = element_type.getname() |
|
814 | 191 |
self.GenerateDataType(elementtype_name) |
1297 | 192 |
elif element_type_type == "array": |
193 |
base_type = element_type.baseType.getcontent() |
|
194 |
base_type_type = base_type.getLocalTag() |
|
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
195 |
# Array derived directly from a user defined type |
1297 | 196 |
if base_type_type == "derived": |
197 |
basetype_name = base_type.getname() |
|
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
198 |
self.GenerateDataType(basetype_name) |
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
199 |
# Array derived directly from an elementary type |
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
200 |
else: |
1297 | 201 |
basetype_name = base_type_type.upper() |
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
202 |
dimensions = ["%s..%s" % (dimension.getlower(), dimension.getupper()) |
1297 | 203 |
for dimension in element_type.getdimension()] |
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset
|
204 |
elementtype_name = "ARRAY [%s] OF %s" % (",".join(dimensions), basetype_name) |
814 | 205 |
# Structure element derived directly from an elementary type |
206 |
else: |
|
1297 | 207 |
elementtype_name = element_type_type.upper() |
814 | 208 |
element_text = [("\n ", ()), |
209 |
(element.getname(), (tagname, "struct", i, "name")), |
|
210 |
(" : ", ()), |
|
211 |
(elementtype_name, (tagname, "struct", i, "type"))] |
|
212 |
if element.initialValue is not None: |
|
213 |
element_text.extend([(" := ", ()), |
|
214 |
(self.ComputeValue(element.initialValue.getvalue(), elementtype_name), (tagname, "struct", i, "initial value"))]) |
|
215 |
element_text.append((";", ())) |
|
216 |
elements.append(element_text) |
|
217 |
datatype_def += [("STRUCT", ())] |
|
218 |
datatype_def += JoinList([("", ())], elements) |
|
219 |
datatype_def += [("\n END_STRUCT", ())] |
|
220 |
# Data type derived directly from a elementary type |
|
221 |
else: |
|
1297 | 222 |
datatype_def += [(basetype_content_type.upper(), (tagname, "base"))] |
814 | 223 |
# Data type has an initial value |
224 |
if datatype.initialValue is not None: |
|
225 |
datatype_def += [(" := ", ()), |
|
226 |
(self.ComputeValue(datatype.initialValue.getvalue(), datatype_name), (tagname, "initial value"))] |
|
227 |
datatype_def += [(";\n", ())] |
|
228 |
self.Program += datatype_def |
|
229 |
||
230 |
# Generate a POU from its name |
|
231 |
def GeneratePouProgram(self, pou_name): |
|
232 |
# Verify that POU hasn't been generated yet |
|
233 |
if not self.PouComputed.get(pou_name, True): |
|
234 |
# If not mark POU as computed |
|
235 |
self.PouComputed[pou_name] = True |
|
236 |
||
237 |
# Getting POU model from project |
|
238 |
pou = self.Project.getpou(pou_name) |
|
239 |
pou_type = pou.getpouType() |
|
240 |
# Verify that POU type exists |
|
241 |
if pouTypeNames.has_key(pou_type): |
|
242 |
# Create a POU program generator |
|
243 |
pou_program = PouProgramGenerator(self, pou.getname(), pouTypeNames[pou_type], self.Errors, self.Warnings) |
|
244 |
program = pou_program.GenerateProgram(pou) |
|
245 |
self.Program += program |
|
246 |
else: |
|
247 |
raise PLCGenException, _("Undefined pou type \"%s\"")%pou_type |
|
248 |
||
249 |
# Generate a POU defined and used in text |
|
250 |
def GeneratePouProgramInText(self, text): |
|
251 |
for pou_name in self.PouComputed.keys(): |
|
252 |
model = re.compile("(?:^|[^0-9^A-Z])%s(?:$|[^0-9^A-Z])"%pou_name.upper()) |
|
253 |
if model.search(text) is not None: |
|
254 |
self.GeneratePouProgram(pou_name) |
|
255 |
||
256 |
# Generate a configuration from its model |
|
257 |
def GenerateConfiguration(self, configuration): |
|
258 |
tagname = self.Controler.ComputeConfigurationName(configuration.getname()) |
|
259 |
config = [("\nCONFIGURATION ", ()), |
|
260 |
(configuration.getname(), (tagname, "name")), |
|
261 |
("\n", ())] |
|
262 |
var_number = 0 |
|
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
263 |
|
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
264 |
varlists = [(varlist, varlist.getvariable()[:]) for varlist in configuration.getglobalVars()] |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
265 |
|
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
266 |
extra_variables = self.Controler.GetConfigurationExtraVariables() |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
267 |
if len(extra_variables) > 0: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
268 |
if len(varlists) == 0: |
1297 | 269 |
varlists = [(PLCOpenParser.CreateElement("globalVars", "interface"), [])] |
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
270 |
varlists[-1][1].extend(extra_variables) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
271 |
|
814 | 272 |
# Generate any global variable in configuration |
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
273 |
for varlist, varlist_variables in varlists: |
814 | 274 |
variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local") |
275 |
# Generate variable block with modifier |
|
276 |
config += [(" VAR_GLOBAL", ())] |
|
277 |
if varlist.getconstant(): |
|
278 |
config += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))] |
|
279 |
elif varlist.getretain(): |
|
280 |
config += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))] |
|
281 |
elif varlist.getnonretain(): |
|
282 |
config += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))] |
|
283 |
config += [("\n", ())] |
|
284 |
# Generate any variable of this block |
|
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
285 |
for var in varlist_variables: |
814 | 286 |
vartype_content = var.gettype().getcontent() |
1297 | 287 |
if vartype_content.getLocalTag() == "derived": |
288 |
var_type = vartype_content.getname() |
|
814 | 289 |
self.GenerateDataType(var_type) |
290 |
else: |
|
291 |
var_type = var.gettypeAsText() |
|
292 |
||
293 |
config += [(" ", ()), |
|
294 |
(var.getname(), (tagname, variable_type, var_number, "name")), |
|
295 |
(" ", ())] |
|
296 |
# Generate variable address if exists |
|
297 |
address = var.getaddress() |
|
298 |
if address: |
|
299 |
config += [("AT ", ()), |
|
300 |
(address, (tagname, variable_type, var_number, "location")), |
|
301 |
(" ", ())] |
|
302 |
config += [(": ", ()), |
|
303 |
(var.gettypeAsText(), (tagname, variable_type, var_number, "type"))] |
|
304 |
# Generate variable initial value if exists |
|
305 |
initial = var.getinitialValue() |
|
306 |
if initial: |
|
307 |
config += [(" := ", ()), |
|
308 |
(self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))] |
|
309 |
config += [(";\n", ())] |
|
310 |
var_number += 1 |
|
311 |
config += [(" END_VAR\n", ())] |
|
312 |
# Generate any resource in the configuration |
|
313 |
for resource in configuration.getresource(): |
|
314 |
config += self.GenerateResource(resource, configuration.getname()) |
|
315 |
config += [("END_CONFIGURATION\n", ())] |
|
316 |
return config |
|
317 |
||
318 |
# Generate a resource from its model |
|
319 |
def GenerateResource(self, resource, config_name): |
|
320 |
tagname = self.Controler.ComputeConfigurationResourceName(config_name, resource.getname()) |
|
321 |
resrce = [("\n RESOURCE ", ()), |
|
322 |
(resource.getname(), (tagname, "name")), |
|
323 |
(" ON PLC\n", ())] |
|
324 |
var_number = 0 |
|
325 |
# Generate any global variable in configuration |
|
326 |
for varlist in resource.getglobalVars(): |
|
327 |
variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local") |
|
328 |
# Generate variable block with modifier |
|
329 |
resrce += [(" VAR_GLOBAL", ())] |
|
330 |
if varlist.getconstant(): |
|
331 |
resrce += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))] |
|
332 |
elif varlist.getretain(): |
|
333 |
resrce += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))] |
|
334 |
elif varlist.getnonretain(): |
|
335 |
resrce += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))] |
|
336 |
resrce += [("\n", ())] |
|
337 |
# Generate any variable of this block |
|
338 |
for var in varlist.getvariable(): |
|
339 |
vartype_content = var.gettype().getcontent() |
|
1297 | 340 |
if vartype_content.getLocalTag() == "derived": |
341 |
var_type = vartype_content.getname() |
|
814 | 342 |
self.GenerateDataType(var_type) |
343 |
else: |
|
344 |
var_type = var.gettypeAsText() |
|
345 |
||
346 |
resrce += [(" ", ()), |
|
347 |
(var.getname(), (tagname, variable_type, var_number, "name")), |
|
348 |
(" ", ())] |
|
349 |
address = var.getaddress() |
|
350 |
# Generate variable address if exists |
|
351 |
if address: |
|
352 |
resrce += [("AT ", ()), |
|
353 |
(address, (tagname, variable_type, var_number, "location")), |
|
354 |
(" ", ())] |
|
355 |
resrce += [(": ", ()), |
|
356 |
(var.gettypeAsText(), (tagname, variable_type, var_number, "type"))] |
|
357 |
# Generate variable initial value if exists |
|
358 |
initial = var.getinitialValue() |
|
359 |
if initial: |
|
360 |
resrce += [(" := ", ()), |
|
361 |
(self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))] |
|
362 |
resrce += [(";\n", ())] |
|
363 |
var_number += 1 |
|
364 |
resrce += [(" END_VAR\n", ())] |
|
365 |
# Generate any task in the resource |
|
366 |
tasks = resource.gettask() |
|
367 |
task_number = 0 |
|
368 |
for task in tasks: |
|
369 |
# Task declaration |
|
370 |
resrce += [(" TASK ", ()), |
|
371 |
(task.getname(), (tagname, "task", task_number, "name")), |
|
372 |
("(", ())] |
|
373 |
args = [] |
|
374 |
single = task.getsingle() |
|
375 |
# Single argument if exists |
|
376 |
if single: |
|
377 |
resrce += [("SINGLE := ", ()), |
|
378 |
(single, (tagname, "task", task_number, "single")), |
|
379 |
(",", ())] |
|
380 |
# Interval argument if exists |
|
381 |
interval = task.getinterval() |
|
382 |
if interval: |
|
383 |
resrce += [("INTERVAL := ", ()), |
|
384 |
(interval, (tagname, "task", task_number, "interval")), |
|
385 |
(",", ())] |
|
386 |
## resrce += [("INTERVAL := t#", ())] |
|
387 |
## if interval.hour != 0: |
|
388 |
## resrce += [("%dh"%interval.hour, (tagname, "task", task_number, "interval", "hour"))] |
|
389 |
## if interval.minute != 0: |
|
390 |
## resrce += [("%dm"%interval.minute, (tagname, "task", task_number, "interval", "minute"))] |
|
391 |
## if interval.second != 0: |
|
392 |
## resrce += [("%ds"%interval.second, (tagname, "task", task_number, "interval", "second"))] |
|
393 |
## if interval.microsecond != 0: |
|
394 |
## resrce += [("%dms"%(interval.microsecond / 1000), (tagname, "task", task_number, "interval", "millisecond"))] |
|
395 |
## resrce += [(",", ())] |
|
396 |
# Priority argument |
|
397 |
resrce += [("PRIORITY := ", ()), |
|
398 |
("%d"%task.getpriority(), (tagname, "task", task_number, "priority")), |
|
399 |
(");\n", ())] |
|
400 |
task_number += 1 |
|
401 |
instance_number = 0 |
|
402 |
# Generate any program assign to each task |
|
403 |
for task in tasks: |
|
404 |
for instance in task.getpouInstance(): |
|
405 |
resrce += [(" PROGRAM ", ()), |
|
406 |
(instance.getname(), (tagname, "instance", instance_number, "name")), |
|
407 |
(" WITH ", ()), |
|
408 |
(task.getname(), (tagname, "instance", instance_number, "task")), |
|
409 |
(" : ", ()), |
|
410 |
(instance.gettypeName(), (tagname, "instance", instance_number, "type")), |
|
411 |
(";\n", ())] |
|
412 |
instance_number += 1 |
|
413 |
# Generate any program assign to no task |
|
414 |
for instance in resource.getpouInstance(): |
|
415 |
resrce += [(" PROGRAM ", ()), |
|
416 |
(instance.getname(), (tagname, "instance", instance_number, "name")), |
|
417 |
(" : ", ()), |
|
418 |
(instance.gettypeName(), (tagname, "instance", instance_number, "type")), |
|
419 |
(";\n", ())] |
|
420 |
instance_number += 1 |
|
421 |
resrce += [(" END_RESOURCE\n", ())] |
|
422 |
return resrce |
|
423 |
||
424 |
# Generate the entire program for current project |
|
425 |
def GenerateProgram(self): |
|
426 |
# Find all data types defined |
|
427 |
for datatype in self.Project.getdataTypes(): |
|
428 |
self.DatatypeComputed[datatype.getname()] = False |
|
429 |
# Find all data types defined |
|
430 |
for pou in self.Project.getpous(): |
|
431 |
self.PouComputed[pou.getname()] = False |
|
432 |
# Generate data type declaration structure if there is at least one data |
|
433 |
# type defined |
|
434 |
if len(self.DatatypeComputed) > 0: |
|
435 |
self.Program += [("TYPE\n", ())] |
|
436 |
# Generate every data types defined |
|
437 |
for datatype_name in self.DatatypeComputed.keys(): |
|
438 |
self.GenerateDataType(datatype_name) |
|
439 |
self.Program += [("END_TYPE\n\n", ())] |
|
440 |
# Generate every POUs defined |
|
441 |
for pou_name in self.PouComputed.keys(): |
|
442 |
self.GeneratePouProgram(pou_name) |
|
443 |
# Generate every configurations defined |
|
444 |
for config in self.Project.getconfigurations(): |
|
445 |
self.Program += self.GenerateConfiguration(config) |
|
446 |
||
447 |
# Return generated program |
|
448 |
def GetGeneratedProgram(self): |
|
449 |
return self.Program |
|
450 |
||
451 |
||
452 |
#------------------------------------------------------------------------------- |
|
453 |
# Generator of POU programs |
|
454 |
#------------------------------------------------------------------------------- |
|
455 |
||
1297 | 456 |
[ConnectorClass, ContinuationClass, ActionBlockClass] = [ |
457 |
PLCOpenParser.GetElementClass(instance_name, "commonObjects") |
|
458 |
for instance_name in ["connector", "continuation", "actionBlock"]] |
|
459 |
[InVariableClass, InOutVariableClass, OutVariableClass, BlockClass] = [ |
|
460 |
PLCOpenParser.GetElementClass(instance_name, "fbdObjects") |
|
461 |
for instance_name in ["inVariable", "inOutVariable", "outVariable", "block"]] |
|
462 |
[ContactClass, CoilClass, LeftPowerRailClass, RightPowerRailClass] = [ |
|
463 |
PLCOpenParser.GetElementClass(instance_name, "ldObjects") |
|
464 |
for instance_name in ["contact", "coil", "leftPowerRail", "rightPowerRail"]] |
|
465 |
[StepClass, TransitionClass, JumpStepClass, |
|
466 |
SelectionConvergenceClass, SelectionDivergenceClass, |
|
467 |
SimultaneousConvergenceClass, SimultaneousDivergenceClass] = [ |
|
468 |
PLCOpenParser.GetElementClass(instance_name, "sfcObjects") |
|
469 |
for instance_name in ["step", "transition", "jumpStep", |
|
470 |
"selectionConvergence", "selectionDivergence", |
|
471 |
"simultaneousConvergence", "simultaneousDivergence"]] |
|
472 |
TransitionObjClass = PLCOpenParser.GetElementClass("transition", "transitions") |
|
473 |
ActionObjClass = PLCOpenParser.GetElementClass("action", "actions") |
|
814 | 474 |
|
475 |
class PouProgramGenerator: |
|
476 |
||
477 |
# Create a new POU program generator |
|
478 |
def __init__(self, parent, name, type, errors, warnings): |
|
479 |
# Keep Reference to the parent generator |
|
480 |
self.ParentGenerator = parent |
|
481 |
self.Name = name |
|
482 |
self.Type = type |
|
483 |
self.TagName = self.ParentGenerator.Controler.ComputePouName(name) |
|
484 |
self.CurrentIndent = " " |
|
485 |
self.ReturnType = None |
|
486 |
self.Interface = [] |
|
487 |
self.InitialSteps = [] |
|
488 |
self.ComputedBlocks = {} |
|
489 |
self.ComputedConnectors = {} |
|
490 |
self.ConnectionTypes = {} |
|
491 |
self.RelatedConnections = [] |
|
492 |
self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}} |
|
493 |
self.SFCComputedBlocks = [] |
|
494 |
self.ActionNumber = 0 |
|
495 |
self.Program = [] |
|
496 |
self.Errors = errors |
|
497 |
self.Warnings = warnings |
|
498 |
||
499 |
def GetBlockType(self, type, inputs=None): |
|
500 |
return self.ParentGenerator.Controler.GetBlockType(type, inputs) |
|
501 |
||
502 |
def IndentLeft(self): |
|
503 |
if len(self.CurrentIndent) >= 2: |
|
504 |
self.CurrentIndent = self.CurrentIndent[:-2] |
|
505 |
||
506 |
def IndentRight(self): |
|
507 |
self.CurrentIndent += " " |
|
508 |
||
509 |
# Generator of unique ID for inline actions |
|
510 |
def GetActionNumber(self): |
|
511 |
self.ActionNumber += 1 |
|
512 |
return self.ActionNumber |
|
513 |
||
514 |
# Test if a variable has already been defined |
|
515 |
def IsAlreadyDefined(self, name): |
|
516 |
for list_type, option, located, vars in self.Interface: |
|
517 |
for var_type, var_name, var_address, var_initial in vars: |
|
518 |
if name == var_name: |
|
519 |
return True |
|
520 |
return False |
|
521 |
||
522 |
# Return the type of a variable defined in interface |
|
523 |
def GetVariableType(self, name): |
|
524 |
parts = name.split('.') |
|
525 |
current_type = None |
|
526 |
if len(parts) > 0: |
|
527 |
name = parts.pop(0) |
|
528 |
for list_type, option, located, vars in self.Interface: |
|
529 |
for var_type, var_name, var_address, var_initial in vars: |
|
530 |
if name == var_name: |
|
531 |
current_type = var_type |
|
532 |
break |
|
533 |
while current_type is not None and len(parts) > 0: |
|
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
534 |
blocktype = self.ParentGenerator.Controler.GetBlockType(current_type) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
535 |
if blocktype is not None: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
536 |
name = parts.pop(0) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
537 |
current_type = None |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
538 |
for var_name, var_type, var_modifier in blocktype["inputs"] + blocktype["outputs"]: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
539 |
if var_name == name: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
540 |
current_type = var_type |
814 | 541 |
break |
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
542 |
else: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
543 |
tagname = self.ParentGenerator.Controler.ComputeDataTypeName(current_type) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
544 |
infos = self.ParentGenerator.Controler.GetDataTypeInfos(tagname) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
545 |
if infos is not None and infos["type"] == "Structure": |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
546 |
name = parts.pop(0) |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
547 |
current_type = None |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
548 |
for element in infos["elements"]: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
549 |
if element["Name"] == name: |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
550 |
current_type = element["Type"] |
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset
|
551 |
break |
814 | 552 |
return current_type |
553 |
||
554 |
# Return connectors linked by a connection to the given connector |
|
555 |
def GetConnectedConnector(self, connector, body): |
|
556 |
links = connector.getconnections() |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
557 |
if links is not None and len(links) == 1: |
814 | 558 |
return self.GetLinkedConnector(links[0], body) |
559 |
return None |
|
560 |
||
561 |
def GetLinkedConnector(self, link, body): |
|
562 |
parameter = link.getformalParameter() |
|
563 |
instance = body.getcontentInstance(link.getrefLocalId()) |
|
1297 | 564 |
if isinstance(instance, (InVariableClass, InOutVariableClass, |
565 |
ContinuationClass, ContactClass, CoilClass)): |
|
814 | 566 |
return instance.connectionPointOut |
1297 | 567 |
elif isinstance(instance, BlockClass): |
814 | 568 |
outputvariables = instance.outputVariables.getvariable() |
569 |
if len(outputvariables) == 1: |
|
570 |
return outputvariables[0].connectionPointOut |
|
571 |
elif parameter: |
|
572 |
for variable in outputvariables: |
|
573 |
if variable.getformalParameter() == parameter: |
|
574 |
return variable.connectionPointOut |
|
575 |
else: |
|
576 |
point = link.getposition()[-1] |
|
577 |
for variable in outputvariables: |
|
578 |
relposition = variable.connectionPointOut.getrelPositionXY() |
|
579 |
blockposition = instance.getposition() |
|
580 |
if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]: |
|
581 |
return variable.connectionPointOut |
|
1297 | 582 |
elif isinstance(instance, LeftPowerRailClass): |
814 | 583 |
outputconnections = instance.getconnectionPointOut() |
584 |
if len(outputconnections) == 1: |
|
585 |
return outputconnections[0] |
|
586 |
else: |
|
587 |
point = link.getposition()[-1] |
|
588 |
for outputconnection in outputconnections: |
|
589 |
relposition = outputconnection.getrelPositionXY() |
|
590 |
powerrailposition = instance.getposition() |
|
591 |
if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]: |
|
592 |
return outputconnection |
|
593 |
return None |
|
594 |
||
595 |
def ExtractRelatedConnections(self, connection): |
|
596 |
for i, related in enumerate(self.RelatedConnections): |
|
597 |
if connection in related: |
|
598 |
return self.RelatedConnections.pop(i) |
|
599 |
return [connection] |
|
600 |
||
601 |
def ComputeInterface(self, pou): |
|
602 |
interface = pou.getinterface() |
|
603 |
if interface is not None: |
|
604 |
body = pou.getbody() |
|
605 |
if isinstance(body, ListType): |
|
606 |
body = body[0] |
|
607 |
body_content = body.getcontent() |
|
1297 | 608 |
body_type = body_content.getLocalTag() |
814 | 609 |
if self.Type == "FUNCTION": |
610 |
returntype_content = interface.getreturnType().getcontent() |
|
1297 | 611 |
returntype_content_type = returntype_content.getLocalTag() |
612 |
if returntype_content_type == "derived": |
|
613 |
self.ReturnType = returntype_content.getname() |
|
614 |
else: |
|
615 |
self.ReturnType = returntype_content_type.upper() |
|
814 | 616 |
for varlist in interface.getcontent(): |
617 |
variables = [] |
|
618 |
located = [] |
|
1297 | 619 |
varlist_type = varlist.getLocalTag() |
620 |
for var in varlist.getvariable(): |
|
814 | 621 |
vartype_content = var.gettype().getcontent() |
1297 | 622 |
if vartype_content.getLocalTag() == "derived": |
623 |
var_type = vartype_content.getname() |
|
814 | 624 |
blocktype = self.GetBlockType(var_type) |
625 |
if blocktype is not None: |
|
626 |
self.ParentGenerator.GeneratePouProgram(var_type) |
|
1297 | 627 |
if body_type in ["FBD", "LD", "SFC"]: |
814 | 628 |
block = pou.getinstanceByName(var.getname()) |
629 |
else: |
|
630 |
block = None |
|
631 |
for variable in blocktype["initialise"](var_type, var.getname(), block): |
|
632 |
if variable[2] is not None: |
|
633 |
located.append(variable) |
|
634 |
else: |
|
635 |
variables.append(variable) |
|
636 |
else: |
|
637 |
self.ParentGenerator.GenerateDataType(var_type) |
|
638 |
initial = var.getinitialValue() |
|
639 |
if initial: |
|
640 |
initial_value = initial.getvalue() |
|
641 |
else: |
|
642 |
initial_value = None |
|
643 |
address = var.getaddress() |
|
644 |
if address is not None: |
|
1297 | 645 |
located.append((vartype_content.getname(), var.getname(), address, initial_value)) |
814 | 646 |
else: |
1297 | 647 |
variables.append((vartype_content.getname(), var.getname(), None, initial_value)) |
814 | 648 |
else: |
649 |
var_type = var.gettypeAsText() |
|
650 |
initial = var.getinitialValue() |
|
651 |
if initial: |
|
652 |
initial_value = initial.getvalue() |
|
653 |
else: |
|
654 |
initial_value = None |
|
655 |
address = var.getaddress() |
|
656 |
if address is not None: |
|
657 |
located.append((var_type, var.getname(), address, initial_value)) |
|
658 |
else: |
|
659 |
variables.append((var_type, var.getname(), None, initial_value)) |
|
1297 | 660 |
if varlist.getconstant(): |
814 | 661 |
option = "CONSTANT" |
1297 | 662 |
elif varlist.getretain(): |
814 | 663 |
option = "RETAIN" |
1297 | 664 |
elif varlist.getnonretain(): |
814 | 665 |
option = "NON_RETAIN" |
666 |
else: |
|
667 |
option = None |
|
668 |
if len(variables) > 0: |
|
1297 | 669 |
self.Interface.append((varTypeNames[varlist_type], option, False, variables)) |
814 | 670 |
if len(located) > 0: |
1297 | 671 |
self.Interface.append((varTypeNames[varlist_type], option, True, located)) |
1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
672 |
|
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
673 |
LITERAL_TYPES = { |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
674 |
"T": "TIME", |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
675 |
"D": "DATE", |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
676 |
"TOD": "TIME_OF_DAY", |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
677 |
"DT": "DATE_AND_TIME", |
1183
a01618805821
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1181
diff
changeset
|
678 |
"2": None, |
a01618805821
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1181
diff
changeset
|
679 |
"8": None, |
a01618805821
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1181
diff
changeset
|
680 |
"16": None, |
1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
681 |
} |
814 | 682 |
def ComputeConnectionTypes(self, pou): |
683 |
body = pou.getbody() |
|
684 |
if isinstance(body, ListType): |
|
685 |
body = body[0] |
|
686 |
body_content = body.getcontent() |
|
1297 | 687 |
body_type = body_content.getLocalTag() |
814 | 688 |
if body_type in ["FBD", "LD", "SFC"]: |
689 |
undefined_blocks = [] |
|
690 |
for instance in body.getcontentInstances(): |
|
1297 | 691 |
if isinstance(instance, (InVariableClass, OutVariableClass, |
692 |
InOutVariableClass)): |
|
693 |
expression = instance.getexpression().text |
|
814 | 694 |
var_type = self.GetVariableType(expression) |
1297 | 695 |
if (isinstance(pou, TransitionObjClass) |
696 |
and expression == pou.getname()): |
|
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
697 |
var_type = "BOOL" |
1297 | 698 |
elif (not isinstance(pou, (TransitionObjClass, ActionObjClass)) and |
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
699 |
pou.getpouType() == "function" and expression == pou.getname()): |
814 | 700 |
returntype_content = pou.interface.getreturnType().getcontent() |
1297 | 701 |
returntype_content_type = returntype_content.getLocalTag() |
702 |
if returntype_content_type == "derived": |
|
703 |
var_type = returntype_content.getname() |
|
814 | 704 |
else: |
1297 | 705 |
var_type = returntype_content_type.upper() |
814 | 706 |
elif var_type is None: |
707 |
parts = expression.split("#") |
|
708 |
if len(parts) > 1: |
|
1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
709 |
literal_prefix = parts[0].upper() |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
710 |
var_type = self.LITERAL_TYPES.get(literal_prefix, |
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset
|
711 |
literal_prefix) |
814 | 712 |
elif expression.startswith("'"): |
713 |
var_type = "STRING" |
|
714 |
elif expression.startswith('"'): |
|
715 |
var_type = "WSTRING" |
|
716 |
if var_type is not None: |
|
1297 | 717 |
if isinstance(instance, (InVariableClass, InOutVariableClass)): |
814 | 718 |
for connection in self.ExtractRelatedConnections(instance.connectionPointOut): |
719 |
self.ConnectionTypes[connection] = var_type |
|
1297 | 720 |
if isinstance(instance, (OutVariableClass, InOutVariableClass)): |
814 | 721 |
self.ConnectionTypes[instance.connectionPointIn] = var_type |
722 |
connected = self.GetConnectedConnector(instance.connectionPointIn, body) |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
723 |
if connected is not None and not self.ConnectionTypes.has_key(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
724 |
for related in self.ExtractRelatedConnections(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
725 |
self.ConnectionTypes[related] = var_type |
1297 | 726 |
elif isinstance(instance, (ContactClass, CoilClass)): |
814 | 727 |
for connection in self.ExtractRelatedConnections(instance.connectionPointOut): |
728 |
self.ConnectionTypes[connection] = "BOOL" |
|
729 |
self.ConnectionTypes[instance.connectionPointIn] = "BOOL" |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
730 |
for link in instance.connectionPointIn.getconnections(): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
731 |
connected = self.GetLinkedConnector(link, body) |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
732 |
if connected is not None and not self.ConnectionTypes.has_key(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
733 |
for related in self.ExtractRelatedConnections(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
734 |
self.ConnectionTypes[related] = "BOOL" |
1297 | 735 |
elif isinstance(instance, LeftPowerRailClass): |
814 | 736 |
for connection in instance.getconnectionPointOut(): |
737 |
for related in self.ExtractRelatedConnections(connection): |
|
738 |
self.ConnectionTypes[related] = "BOOL" |
|
1297 | 739 |
elif isinstance(instance, RightPowerRailClass): |
814 | 740 |
for connection in instance.getconnectionPointIn(): |
741 |
self.ConnectionTypes[connection] = "BOOL" |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
742 |
for link in connection.getconnections(): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
743 |
connected = self.GetLinkedConnector(link, body) |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
744 |
if connected is not None and not self.ConnectionTypes.has_key(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
745 |
for related in self.ExtractRelatedConnections(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
746 |
self.ConnectionTypes[related] = "BOOL" |
1297 | 747 |
elif isinstance(instance, TransitionClass): |
748 |
content = instance.getconditionContent() |
|
749 |
if content["type"] == "connection": |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
750 |
self.ConnectionTypes[content["value"]] = "BOOL" |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
751 |
for link in content["value"].getconnections(): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
752 |
connected = self.GetLinkedConnector(link, body) |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
753 |
if connected is not None and not self.ConnectionTypes.has_key(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
754 |
for related in self.ExtractRelatedConnections(connected): |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
755 |
self.ConnectionTypes[related] = "BOOL" |
1297 | 756 |
elif isinstance(instance, ContinuationClass): |
814 | 757 |
name = instance.getname() |
758 |
connector = None |
|
759 |
var_type = "ANY" |
|
760 |
for element in body.getcontentInstances(): |
|
1297 | 761 |
if isinstance(element, ConnectorClass) and element.getname() == name: |
814 | 762 |
if connector is not None: |
763 |
raise PLCGenException, _("More than one connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name) |
|
764 |
connector = element |
|
765 |
if connector is not None: |
|
766 |
undefined = [instance.connectionPointOut, connector.connectionPointIn] |
|
767 |
connected = self.GetConnectedConnector(connector.connectionPointIn, body) |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
768 |
if connected is not None: |
814 | 769 |
undefined.append(connected) |
770 |
related = [] |
|
771 |
for connection in undefined: |
|
772 |
if self.ConnectionTypes.has_key(connection): |
|
773 |
var_type = self.ConnectionTypes[connection] |
|
774 |
else: |
|
775 |
related.extend(self.ExtractRelatedConnections(connection)) |
|
776 |
if var_type.startswith("ANY") and len(related) > 0: |
|
777 |
self.RelatedConnections.append(related) |
|
778 |
else: |
|
779 |
for connection in related: |
|
780 |
self.ConnectionTypes[connection] = var_type |
|
781 |
else: |
|
782 |
raise PLCGenException, _("No connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name) |
|
1297 | 783 |
elif isinstance(instance, BlockClass): |
814 | 784 |
block_infos = self.GetBlockType(instance.gettypeName(), "undefined") |
785 |
if block_infos is not None: |
|
786 |
self.ComputeBlockInputTypes(instance, block_infos, body) |
|
787 |
else: |
|
788 |
for variable in instance.inputVariables.getvariable(): |
|
789 |
connected = self.GetConnectedConnector(variable.connectionPointIn, body) |
|
790 |
if connected is not None: |
|
791 |
var_type = self.ConnectionTypes.get(connected, None) |
|
792 |
if var_type is not None: |
|
793 |
self.ConnectionTypes[variable.connectionPointIn] = var_type |
|
794 |
else: |
|
795 |
related = self.ExtractRelatedConnections(connected) |
|
796 |
related.append(variable.connectionPointIn) |
|
797 |
self.RelatedConnections.append(related) |
|
798 |
undefined_blocks.append(instance) |
|
799 |
for instance in undefined_blocks: |
|
800 |
block_infos = self.GetBlockType(instance.gettypeName(), tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) |
|
801 |
if block_infos is not None: |
|
802 |
self.ComputeBlockInputTypes(instance, block_infos, body) |
|
803 |
else: |
|
804 |
raise PLCGenException, _("No informations found for \"%s\" block")%(instance.gettypeName()) |
|
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
805 |
if body_type == "SFC": |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
806 |
previous_tagname = self.TagName |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
807 |
for action in pou.getactionList(): |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
808 |
self.TagName = self.ParentGenerator.Controler.ComputePouActionName(self.Name, action.getname()) |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
809 |
self.ComputeConnectionTypes(action) |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
810 |
for transition in pou.gettransitionList(): |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
811 |
self.TagName = self.ParentGenerator.Controler.ComputePouTransitionName(self.Name, transition.getname()) |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
812 |
self.ComputeConnectionTypes(transition) |
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset
|
813 |
self.TagName = previous_tagname |
814 | 814 |
|
815 |
def ComputeBlockInputTypes(self, instance, block_infos, body): |
|
816 |
undefined = {} |
|
817 |
for variable in instance.outputVariables.getvariable(): |
|
818 |
output_name = variable.getformalParameter() |
|
819 |
if output_name == "ENO": |
|
820 |
for connection in self.ExtractRelatedConnections(variable.connectionPointOut): |
|
821 |
self.ConnectionTypes[connection] = "BOOL" |
|
822 |
else: |
|
823 |
for oname, otype, oqualifier in block_infos["outputs"]: |
|
824 |
if output_name == oname: |
|
825 |
if otype.startswith("ANY"): |
|
826 |
if not undefined.has_key(otype): |
|
827 |
undefined[otype] = [] |
|
828 |
undefined[otype].append(variable.connectionPointOut) |
|
829 |
elif not self.ConnectionTypes.has_key(variable.connectionPointOut): |
|
830 |
for connection in self.ExtractRelatedConnections(variable.connectionPointOut): |
|
831 |
self.ConnectionTypes[connection] = otype |
|
832 |
for variable in instance.inputVariables.getvariable(): |
|
833 |
input_name = variable.getformalParameter() |
|
834 |
if input_name == "EN": |
|
835 |
for connection in self.ExtractRelatedConnections(variable.connectionPointIn): |
|
836 |
self.ConnectionTypes[connection] = "BOOL" |
|
837 |
else: |
|
838 |
for iname, itype, iqualifier in block_infos["inputs"]: |
|
839 |
if input_name == iname: |
|
840 |
connected = self.GetConnectedConnector(variable.connectionPointIn, body) |
|
841 |
if itype.startswith("ANY"): |
|
842 |
if not undefined.has_key(itype): |
|
843 |
undefined[itype] = [] |
|
844 |
undefined[itype].append(variable.connectionPointIn) |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
845 |
if connected is not None: |
814 | 846 |
undefined[itype].append(connected) |
847 |
else: |
|
848 |
self.ConnectionTypes[variable.connectionPointIn] = itype |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
849 |
if connected is not None and not self.ConnectionTypes.has_key(connected): |
814 | 850 |
for connection in self.ExtractRelatedConnections(connected): |
851 |
self.ConnectionTypes[connection] = itype |
|
852 |
for var_type, connections in undefined.items(): |
|
853 |
related = [] |
|
854 |
for connection in connections: |
|
854
c10f2092c43a
Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents:
822
diff
changeset
|
855 |
connection_type = self.ConnectionTypes.get(connection) |
c10f2092c43a
Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents:
822
diff
changeset
|
856 |
if connection_type and not connection_type.startswith("ANY"): |
c10f2092c43a
Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents:
822
diff
changeset
|
857 |
var_type = connection_type |
814 | 858 |
else: |
859 |
related.extend(self.ExtractRelatedConnections(connection)) |
|
860 |
if var_type.startswith("ANY") and len(related) > 0: |
|
861 |
self.RelatedConnections.append(related) |
|
862 |
else: |
|
863 |
for connection in related: |
|
864 |
self.ConnectionTypes[connection] = var_type |
|
865 |
||
866 |
def ComputeProgram(self, pou): |
|
867 |
body = pou.getbody() |
|
868 |
if isinstance(body, ListType): |
|
869 |
body = body[0] |
|
870 |
body_content = body.getcontent() |
|
1297 | 871 |
body_type = body_content.getLocalTag() |
814 | 872 |
if body_type in ["IL","ST"]: |
1297 | 873 |
text = body_content.getanyText() |
814 | 874 |
self.ParentGenerator.GeneratePouProgramInText(text.upper()) |
875 |
self.Program = [(ReIndentText(text, len(self.CurrentIndent)), |
|
876 |
(self.TagName, "body", len(self.CurrentIndent)))] |
|
877 |
elif body_type == "SFC": |
|
878 |
self.IndentRight() |
|
879 |
for instance in body.getcontentInstances(): |
|
1297 | 880 |
if isinstance(instance, StepClass): |
814 | 881 |
self.GenerateSFCStep(instance, pou) |
1297 | 882 |
elif isinstance(instance, ActionBlockClass): |
814 | 883 |
self.GenerateSFCStepActions(instance, pou) |
1297 | 884 |
elif isinstance(instance, TransitionClass): |
814 | 885 |
self.GenerateSFCTransition(instance, pou) |
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
886 |
elif isinstance(instance, JumpStepClass): |
814 | 887 |
self.GenerateSFCJump(instance, pou) |
888 |
if len(self.InitialSteps) > 0 and len(self.SFCComputedBlocks) > 0: |
|
889 |
action_name = "COMPUTE_FUNCTION_BLOCKS" |
|
890 |
action_infos = {"qualifier" : "S", "content" : action_name} |
|
891 |
self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) |
|
892 |
self.SFCNetworks["Actions"][action_name] = (self.SFCComputedBlocks, ()) |
|
893 |
self.Program = [] |
|
894 |
self.IndentLeft() |
|
895 |
for initialstep in self.InitialSteps: |
|
896 |
self.ComputeSFCStep(initialstep) |
|
897 |
else: |
|
898 |
otherInstances = {"outVariables&coils" : [], "blocks" : [], "connectors" : []} |
|
899 |
orderedInstances = [] |
|
900 |
for instance in body.getcontentInstances(): |
|
1297 | 901 |
if isinstance(instance, (OutVariableClass, InOutVariableClass, BlockClass)): |
814 | 902 |
executionOrderId = instance.getexecutionOrderId() |
903 |
if executionOrderId > 0: |
|
904 |
orderedInstances.append((executionOrderId, instance)) |
|
1297 | 905 |
elif isinstance(instance, (OutVariableClass, InOutVariableClass)): |
814 | 906 |
otherInstances["outVariables&coils"].append(instance) |
1297 | 907 |
elif isinstance(instance, BlockClass): |
814 | 908 |
otherInstances["blocks"].append(instance) |
1297 | 909 |
elif isinstance(instance, ConnectorClass): |
814 | 910 |
otherInstances["connectors"].append(instance) |
1297 | 911 |
elif isinstance(instance, CoilClass): |
814 | 912 |
otherInstances["outVariables&coils"].append(instance) |
913 |
orderedInstances.sort() |
|
914 |
otherInstances["outVariables&coils"].sort(SortInstances) |
|
915 |
otherInstances["blocks"].sort(SortInstances) |
|
916 |
instances = [instance for (executionOrderId, instance) in orderedInstances] |
|
1048
b450202605ab
Fixed bug in program elements computation order in PLCGenerator
Laurent Bessard
parents:
1032
diff
changeset
|
917 |
instances.extend(otherInstances["outVariables&coils"] + otherInstances["blocks"] + otherInstances["connectors"]) |
814 | 918 |
for instance in instances: |
1297 | 919 |
if isinstance(instance, (OutVariableClass, InOutVariableClass)): |
814 | 920 |
connections = instance.connectionPointIn.getconnections() |
921 |
if connections is not None: |
|
922 |
expression = self.ComputeExpression(body, connections) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
923 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
924 |
self.Program += [(self.CurrentIndent, ()), |
1297 | 925 |
(instance.getexpression().text, (self.TagName, "io_variable", instance.getlocalId(), "expression")), |
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
926 |
(" := ", ())] |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
927 |
self.Program += expression |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
928 |
self.Program += [(";\n", ())] |
1297 | 929 |
elif isinstance(instance, BlockClass): |
814 | 930 |
block_type = instance.gettypeName() |
931 |
self.ParentGenerator.GeneratePouProgram(block_type) |
|
932 |
block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) |
|
933 |
if block_infos is None: |
|
934 |
block_infos = self.GetBlockType(block_type) |
|
935 |
if block_infos is None: |
|
936 |
raise PLCGenException, _("Undefined block type \"%s\" in \"%s\" POU")%(block_type, self.Name) |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
937 |
try: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
938 |
block_infos["generate"](self, instance, block_infos, body, None) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
939 |
except ValueError, e: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
940 |
raise PLCGenException, e.message |
1297 | 941 |
elif isinstance(instance, ConnectorClass): |
814 | 942 |
connector = instance.getname() |
943 |
if self.ComputedConnectors.get(connector, None): |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
944 |
continue |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
945 |
expression = self.ComputeExpression(body, instance.connectionPointIn.getconnections()) |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
946 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
947 |
self.ComputedConnectors[connector] = expression |
1297 | 948 |
elif isinstance(instance, CoilClass): |
814 | 949 |
connections = instance.connectionPointIn.getconnections() |
950 |
if connections is not None: |
|
951 |
coil_info = (self.TagName, "coil", instance.getlocalId()) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
952 |
expression = self.ComputeExpression(body, connections) |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
953 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
954 |
expression = self.ExtractModifier(instance, expression, coil_info) |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
955 |
self.Program += [(self.CurrentIndent, ())] |
1297 | 956 |
self.Program += [(instance.getvariable().text, coil_info + ("reference",))] |
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
957 |
self.Program += [(" := ", ())] + expression + [(";\n", ())] |
814 | 958 |
|
959 |
def FactorizePaths(self, paths): |
|
960 |
same_paths = {} |
|
961 |
uncomputed_index = range(len(paths)) |
|
962 |
factorized_paths = [] |
|
963 |
for num, path in enumerate(paths): |
|
964 |
if type(path) == ListType: |
|
965 |
if len(path) > 1: |
|
966 |
str_path = str(path[-1:]) |
|
967 |
same_paths.setdefault(str_path, []) |
|
968 |
same_paths[str_path].append((path[:-1], num)) |
|
969 |
else: |
|
970 |
factorized_paths.append(path) |
|
971 |
uncomputed_index.remove(num) |
|
972 |
for same_path, elements in same_paths.items(): |
|
973 |
if len(elements) > 1: |
|
974 |
elements_paths = self.FactorizePaths([path for path, num in elements]) |
|
975 |
if len(elements_paths) > 1: |
|
976 |
factorized_paths.append([tuple(elements_paths)] + eval(same_path)) |
|
977 |
else: |
|
978 |
factorized_paths.append(elements_paths + eval(same_path)) |
|
979 |
for path, num in elements: |
|
980 |
uncomputed_index.remove(num) |
|
981 |
for num in uncomputed_index: |
|
982 |
factorized_paths.append(paths[num]) |
|
983 |
factorized_paths.sort() |
|
984 |
return factorized_paths |
|
985 |
||
986 |
def GeneratePaths(self, connections, body, order = False, to_inout = False): |
|
987 |
paths = [] |
|
988 |
for connection in connections: |
|
989 |
localId = connection.getrefLocalId() |
|
990 |
next = body.getcontentInstance(localId) |
|
1297 | 991 |
if isinstance(next, LeftPowerRailClass): |
814 | 992 |
paths.append(None) |
1297 | 993 |
elif isinstance(next, (InVariableClass, InOutVariableClass)): |
994 |
paths.append(str([(next.getexpression().text, (self.TagName, "io_variable", localId, "expression"))])) |
|
995 |
elif isinstance(next, BlockClass): |
|
814 | 996 |
block_type = next.gettypeName() |
997 |
self.ParentGenerator.GeneratePouProgram(block_type) |
|
998 |
block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in next.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) |
|
999 |
if block_infos is None: |
|
1000 |
block_infos = self.GetBlockType(block_type) |
|
1001 |
if block_infos is None: |
|
1002 |
raise PLCGenException, _("Undefined block type \"%s\" in \"%s\" POU")%(block_type, self.Name) |
|
1134
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
1003 |
try: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
1004 |
paths.append(str(block_infos["generate"](self, next, block_infos, body, connection, order, to_inout))) |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
1005 |
except ValueError, e: |
1c7a4ad86aa1
Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents:
1048
diff
changeset
|
1006 |
raise PLCGenException, e.message |
1297 | 1007 |
elif isinstance(next, ContinuationClass): |
814 | 1008 |
name = next.getname() |
1009 |
computed_value = self.ComputedConnectors.get(name, None) |
|
1010 |
if computed_value != None: |
|
1011 |
paths.append(str(computed_value)) |
|
1012 |
else: |
|
1013 |
connector = None |
|
1014 |
for instance in body.getcontentInstances(): |
|
1297 | 1015 |
if isinstance(instance, ConnectorClass) and instance.getname() == name: |
814 | 1016 |
if connector is not None: |
1017 |
raise PLCGenException, _("More than one connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name) |
|
1018 |
connector = instance |
|
1019 |
if connector is not None: |
|
1020 |
connections = connector.connectionPointIn.getconnections() |
|
1021 |
if connections is not None: |
|
1022 |
expression = self.ComputeExpression(body, connections, order) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1023 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1024 |
self.ComputedConnectors[name] = expression |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1025 |
paths.append(str(expression)) |
814 | 1026 |
else: |
1027 |
raise PLCGenException, _("No connector found corresponding to \"%s\" continuation in \"%s\" POU")%(name, self.Name) |
|
1297 | 1028 |
elif isinstance(next, ContactClass): |
814 | 1029 |
contact_info = (self.TagName, "contact", next.getlocalId()) |
1297 | 1030 |
variable = str(self.ExtractModifier(next, [(next.getvariable().text, contact_info + ("reference",))], contact_info)) |
814 | 1031 |
result = self.GeneratePaths(next.connectionPointIn.getconnections(), body, order) |
1032 |
if len(result) > 1: |
|
1033 |
factorized_paths = self.FactorizePaths(result) |
|
1034 |
if len(factorized_paths) > 1: |
|
1035 |
paths.append([variable, tuple(factorized_paths)]) |
|
1036 |
else: |
|
1037 |
paths.append([variable] + factorized_paths) |
|
1038 |
elif type(result[0]) == ListType: |
|
1039 |
paths.append([variable] + result[0]) |
|
1040 |
elif result[0] is not None: |
|
1041 |
paths.append([variable, result[0]]) |
|
1042 |
else: |
|
1043 |
paths.append(variable) |
|
1297 | 1044 |
elif isinstance(next, CoilClass): |
814 | 1045 |
paths.append(str(self.GeneratePaths(next.connectionPointIn.getconnections(), body, order))) |
1046 |
return paths |
|
1047 |
||
1048 |
def ComputePaths(self, paths, first = False): |
|
1049 |
if type(paths) == TupleType: |
|
1050 |
if None in paths: |
|
1051 |
return [("TRUE", ())] |
|
1052 |
else: |
|
1053 |
vars = [self.ComputePaths(path) for path in paths] |
|
1054 |
if first: |
|
1055 |
return JoinList([(" OR ", ())], vars) |
|
1056 |
else: |
|
1057 |
return [("(", ())] + JoinList([(" OR ", ())], vars) + [(")", ())] |
|
1058 |
elif type(paths) == ListType: |
|
1059 |
vars = [self.ComputePaths(path) for path in paths] |
|
1060 |
return JoinList([(" AND ", ())], vars) |
|
1061 |
elif paths is None: |
|
1062 |
return [("TRUE", ())] |
|
1063 |
else: |
|
1064 |
return eval(paths) |
|
1065 |
||
1066 |
def ComputeExpression(self, body, connections, order = False, to_inout = False): |
|
1067 |
paths = self.GeneratePaths(connections, body, order, to_inout) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1068 |
if len(paths) == 0: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1069 |
return None |
814 | 1070 |
if len(paths) > 1: |
1071 |
factorized_paths = self.FactorizePaths(paths) |
|
1072 |
if len(factorized_paths) > 1: |
|
1073 |
paths = tuple(factorized_paths) |
|
1074 |
else: |
|
1075 |
paths = factorized_paths[0] |
|
1076 |
else: |
|
1077 |
paths = paths[0] |
|
1078 |
return self.ComputePaths(paths, True) |
|
1079 |
||
1080 |
def ExtractModifier(self, variable, expression, var_info): |
|
1081 |
if variable.getnegated(): |
|
1082 |
return [("NOT(", var_info + ("negated",))] + expression + [(")", ())] |
|
1083 |
else: |
|
1084 |
storage = variable.getstorage() |
|
1085 |
if storage in ["set", "reset"]: |
|
1086 |
self.Program += [(self.CurrentIndent + "IF ", var_info + (storage,))] + expression |
|
1087 |
self.Program += [(" THEN\n ", ())] |
|
1088 |
if storage == "set": |
|
1089 |
return [("TRUE; (*set*)\n" + self.CurrentIndent + "END_IF", ())] |
|
1090 |
else: |
|
1091 |
return [("FALSE; (*reset*)\n" + self.CurrentIndent + "END_IF", ())] |
|
1092 |
edge = variable.getedge() |
|
1093 |
if edge == "rising": |
|
1094 |
return self.AddTrigger("R_TRIG", expression, var_info + ("rising",)) |
|
1095 |
elif edge == "falling": |
|
1096 |
return self.AddTrigger("F_TRIG", expression, var_info + ("falling",)) |
|
1097 |
return expression |
|
1098 |
||
1099 |
def AddTrigger(self, edge, expression, var_info): |
|
1100 |
if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] is not None or self.Interface[-1][2]: |
|
1101 |
self.Interface.append(("VAR", None, False, [])) |
|
1102 |
i = 1 |
|
1103 |
name = "%s%d"%(edge, i) |
|
1104 |
while self.IsAlreadyDefined(name): |
|
1105 |
i += 1 |
|
1106 |
name = "%s%d"%(edge, i) |
|
1107 |
self.Interface[-1][3].append((edge, name, None, None)) |
|
1108 |
self.Program += [(self.CurrentIndent, ()), (name, var_info), ("(CLK := ", ())] |
|
1109 |
self.Program += expression |
|
1110 |
self.Program += [(");\n", ())] |
|
1111 |
return [("%s.Q"%name, var_info)] |
|
1112 |
||
1113 |
def ExtractDivergenceInput(self, divergence, pou): |
|
1114 |
connectionPointIn = divergence.getconnectionPointIn() |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1115 |
if connectionPointIn is not None: |
814 | 1116 |
connections = connectionPointIn.getconnections() |
1117 |
if connections is not None and len(connections) == 1: |
|
1118 |
instanceLocalId = connections[0].getrefLocalId() |
|
1119 |
body = pou.getbody() |
|
1120 |
if isinstance(body, ListType): |
|
1121 |
body = body[0] |
|
1122 |
return body.getcontentInstance(instanceLocalId) |
|
1123 |
return None |
|
1124 |
||
1125 |
def ExtractConvergenceInputs(self, convergence, pou): |
|
1126 |
instances = [] |
|
1127 |
for connectionPointIn in convergence.getconnectionPointIn(): |
|
1128 |
connections = connectionPointIn.getconnections() |
|
1129 |
if connections is not None and len(connections) == 1: |
|
1130 |
instanceLocalId = connections[0].getrefLocalId() |
|
1131 |
body = pou.getbody() |
|
1132 |
if isinstance(body, ListType): |
|
1133 |
body = body[0] |
|
1134 |
instances.append(body.getcontentInstance(instanceLocalId)) |
|
1135 |
return instances |
|
1136 |
||
1137 |
def GenerateSFCStep(self, step, pou): |
|
1138 |
step_name = step.getname() |
|
1139 |
if step_name not in self.SFCNetworks["Steps"].keys(): |
|
1140 |
if step.getinitialStep(): |
|
1141 |
self.InitialSteps.append(step_name) |
|
1142 |
step_infos = {"id" : step.getlocalId(), |
|
1143 |
"initial" : step.getinitialStep(), |
|
1144 |
"transitions" : [], |
|
1145 |
"actions" : []} |
|
889
ac18acb6917f
Fix bug when using feedback loop in SFC program instead of jump
Laurent Bessard
parents:
883
diff
changeset
|
1146 |
self.SFCNetworks["Steps"][step_name] = step_infos |
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1147 |
if step.connectionPointIn is not None: |
814 | 1148 |
instances = [] |
1149 |
connections = step.connectionPointIn.getconnections() |
|
1150 |
if connections is not None and len(connections) == 1: |
|
1151 |
instanceLocalId = connections[0].getrefLocalId() |
|
1152 |
body = pou.getbody() |
|
1153 |
if isinstance(body, ListType): |
|
1154 |
body = body[0] |
|
1155 |
instance = body.getcontentInstance(instanceLocalId) |
|
1297 | 1156 |
if isinstance(instance, TransitionClass): |
814 | 1157 |
instances.append(instance) |
1297 | 1158 |
elif isinstance(instance, SelectionConvergenceClass): |
814 | 1159 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
1297 | 1160 |
elif isinstance(instance, SimultaneousDivergenceClass): |
814 | 1161 |
transition = self.ExtractDivergenceInput(instance, pou) |
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1162 |
if transition is not None: |
1297 | 1163 |
if isinstance(transition, TransitionClass): |
814 | 1164 |
instances.append(transition) |
1297 | 1165 |
elif isinstance(transition, SelectionConvergenceClass): |
814 | 1166 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
1167 |
for instance in instances: |
|
1168 |
self.GenerateSFCTransition(instance, pou) |
|
1169 |
if instance in self.SFCNetworks["Transitions"].keys(): |
|
1170 |
target_info = (self.TagName, "transition", instance.getlocalId(), "to", step_infos["id"]) |
|
1171 |
self.SFCNetworks["Transitions"][instance]["to"].append([(step_name, target_info)]) |
|
1172 |
||
1173 |
def GenerateSFCJump(self, jump, pou): |
|
1174 |
jump_target = jump.gettargetName() |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1175 |
if jump.connectionPointIn is not None: |
814 | 1176 |
instances = [] |
1177 |
connections = jump.connectionPointIn.getconnections() |
|
1178 |
if connections is not None and len(connections) == 1: |
|
1179 |
instanceLocalId = connections[0].getrefLocalId() |
|
1180 |
body = pou.getbody() |
|
1181 |
if isinstance(body, ListType): |
|
1182 |
body = body[0] |
|
1183 |
instance = body.getcontentInstance(instanceLocalId) |
|
1297 | 1184 |
if isinstance(instance, TransitionClass): |
814 | 1185 |
instances.append(instance) |
1297 | 1186 |
elif isinstance(instance, SelectionConvergenceClass): |
814 | 1187 |
instances.extend(self.ExtractConvergenceInputs(instance, pou)) |
1297 | 1188 |
elif isinstance(instance, SimultaneousDivergenceClass): |
814 | 1189 |
transition = self.ExtractDivergenceInput(instance, pou) |
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1190 |
if transition is not None: |
1297 | 1191 |
if isinstance(transition, TransitionClass): |
814 | 1192 |
instances.append(transition) |
1297 | 1193 |
elif isinstance(transition, SelectionConvergenceClass): |
814 | 1194 |
instances.extend(self.ExtractConvergenceInputs(transition, pou)) |
1195 |
for instance in instances: |
|
1196 |
self.GenerateSFCTransition(instance, pou) |
|
1197 |
if instance in self.SFCNetworks["Transitions"].keys(): |
|
1198 |
target_info = (self.TagName, "jump", jump.getlocalId(), "target") |
|
1199 |
self.SFCNetworks["Transitions"][instance]["to"].append([(jump_target, target_info)]) |
|
1200 |
||
1201 |
def GenerateSFCStepActions(self, actionBlock, pou): |
|
1202 |
connections = actionBlock.connectionPointIn.getconnections() |
|
1203 |
if connections is not None and len(connections) == 1: |
|
1204 |
stepLocalId = connections[0].getrefLocalId() |
|
1205 |
body = pou.getbody() |
|
1206 |
if isinstance(body, ListType): |
|
1207 |
body = body[0] |
|
1208 |
step = body.getcontentInstance(stepLocalId) |
|
1209 |
self.GenerateSFCStep(step, pou) |
|
1210 |
step_name = step.getname() |
|
1211 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
1212 |
actions = actionBlock.getactions() |
|
1213 |
for i, action in enumerate(actions): |
|
1214 |
action_infos = {"id" : actionBlock.getlocalId(), |
|
1215 |
"qualifier" : action["qualifier"], |
|
1216 |
"content" : action["value"], |
|
1217 |
"num" : i} |
|
1218 |
if "duration" in action: |
|
1219 |
action_infos["duration"] = action["duration"] |
|
1220 |
if "indicator" in action: |
|
1221 |
action_infos["indicator"] = action["indicator"] |
|
1222 |
if action["type"] == "reference": |
|
1223 |
self.GenerateSFCAction(action["value"], pou) |
|
1224 |
else: |
|
1225 |
action_name = "%s_INLINE%d"%(step_name.upper(), self.GetActionNumber()) |
|
1226 |
self.SFCNetworks["Actions"][action_name] = ([(self.CurrentIndent, ()), |
|
1227 |
(action["value"], (self.TagName, "action_block", action_infos["id"], "action", i, "inline")), |
|
1228 |
("\n", ())], ()) |
|
1229 |
action_infos["content"] = action_name |
|
1230 |
self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos) |
|
1231 |
||
1232 |
def GenerateSFCAction(self, action_name, pou): |
|
1233 |
if action_name not in self.SFCNetworks["Actions"].keys(): |
|
1234 |
actionContent = pou.getaction(action_name) |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1235 |
if actionContent is not None: |
814 | 1236 |
previous_tagname = self.TagName |
1237 |
self.TagName = self.ParentGenerator.Controler.ComputePouActionName(self.Name, action_name) |
|
1238 |
self.ComputeProgram(actionContent) |
|
1239 |
self.SFCNetworks["Actions"][action_name] = (self.Program, (self.TagName, "name")) |
|
1240 |
self.Program = [] |
|
1241 |
self.TagName = previous_tagname |
|
1242 |
||
1243 |
def GenerateSFCTransition(self, transition, pou): |
|
1244 |
if transition not in self.SFCNetworks["Transitions"].keys(): |
|
1245 |
steps = [] |
|
1246 |
connections = transition.connectionPointIn.getconnections() |
|
1247 |
if connections is not None and len(connections) == 1: |
|
1248 |
instanceLocalId = connections[0].getrefLocalId() |
|
1249 |
body = pou.getbody() |
|
1250 |
if isinstance(body, ListType): |
|
1251 |
body = body[0] |
|
1252 |
instance = body.getcontentInstance(instanceLocalId) |
|
1297 | 1253 |
if isinstance(instance, StepClass): |
814 | 1254 |
steps.append(instance) |
1297 | 1255 |
elif isinstance(instance, SelectionDivergenceClass): |
814 | 1256 |
step = self.ExtractDivergenceInput(instance, pou) |
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1257 |
if step is not None: |
1297 | 1258 |
if isinstance(step, StepClass): |
814 | 1259 |
steps.append(step) |
1297 | 1260 |
elif isinstance(step, SimultaneousConvergenceClass): |
814 | 1261 |
steps.extend(self.ExtractConvergenceInputs(step, pou)) |
1297 | 1262 |
elif isinstance(instance, SimultaneousConvergenceClass): |
814 | 1263 |
steps.extend(self.ExtractConvergenceInputs(instance, pou)) |
1264 |
transition_infos = {"id" : transition.getlocalId(), |
|
1265 |
"priority": transition.getpriority(), |
|
1266 |
"from": [], |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1267 |
"to" : [], |
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1268 |
"content": []} |
889
ac18acb6917f
Fix bug when using feedback loop in SFC program instead of jump
Laurent Bessard
parents:
883
diff
changeset
|
1269 |
self.SFCNetworks["Transitions"][transition] = transition_infos |
814 | 1270 |
transitionValues = transition.getconditionContent() |
1271 |
if transitionValues["type"] == "inline": |
|
1272 |
transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ()), |
|
1273 |
(transitionValues["value"], (self.TagName, "transition", transition.getlocalId(), "inline")), |
|
1274 |
(";\n", ())] |
|
1275 |
elif transitionValues["type"] == "reference": |
|
1276 |
transitionContent = pou.gettransition(transitionValues["value"]) |
|
1277 |
transitionType = transitionContent.getbodyType() |
|
1278 |
transitionBody = transitionContent.getbody() |
|
1279 |
previous_tagname = self.TagName |
|
1280 |
self.TagName = self.ParentGenerator.Controler.ComputePouTransitionName(self.Name, transitionValues["value"]) |
|
1281 |
if transitionType == "IL": |
|
1282 |
transition_infos["content"] = [(":\n", ()), |
|
1297 | 1283 |
(ReIndentText(transitionBody.getanyText(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))] |
814 | 1284 |
elif transitionType == "ST": |
1285 |
transition_infos["content"] = [("\n", ()), |
|
1297 | 1286 |
(ReIndentText(transitionBody.getanyText(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))] |
814 | 1287 |
else: |
1288 |
for instance in transitionBody.getcontentInstances(): |
|
1297 | 1289 |
if isinstance(instance, OutVariableClass) and instance.getexpression().text == transitionValues["value"]\ |
1290 |
or isinstance(instance, CoilClass) and instance.getvariable().text == transitionValues["value"]: |
|
814 | 1291 |
connections = instance.connectionPointIn.getconnections() |
1292 |
if connections is not None: |
|
1293 |
expression = self.ComputeExpression(transitionBody, connections) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1294 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1295 |
transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ())] + expression + [(";\n", ())] |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1296 |
self.SFCComputedBlocks += self.Program |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1297 |
self.Program = [] |
814 | 1298 |
if not transition_infos.has_key("content"): |
1299 |
raise PLCGenException, _("Transition \"%s\" body must contain an output variable or coil referring to its name") % transitionValues["value"] |
|
1300 |
self.TagName = previous_tagname |
|
1301 |
elif transitionValues["type"] == "connection": |
|
1302 |
body = pou.getbody() |
|
1303 |
if isinstance(body, ListType): |
|
1304 |
body = body[0] |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1305 |
connections = transitionValues["value"].getconnections() |
814 | 1306 |
if connections is not None: |
1307 |
expression = self.ComputeExpression(body, connections) |
|
1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1308 |
if expression is not None: |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1309 |
transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ())] + expression + [(";\n", ())] |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1310 |
self.SFCComputedBlocks += self.Program |
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset
|
1311 |
self.Program = [] |
814 | 1312 |
for step in steps: |
1313 |
self.GenerateSFCStep(step, pou) |
|
1314 |
step_name = step.getname() |
|
1315 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
1316 |
transition_infos["from"].append([(step_name, (self.TagName, "transition", transition.getlocalId(), "from", step.getlocalId()))]) |
|
1317 |
self.SFCNetworks["Steps"][step_name]["transitions"].append(transition) |
|
1318 |
||
1319 |
def ComputeSFCStep(self, step_name): |
|
1320 |
if step_name in self.SFCNetworks["Steps"].keys(): |
|
1321 |
step_infos = self.SFCNetworks["Steps"].pop(step_name) |
|
1322 |
self.Program += [(self.CurrentIndent, ())] |
|
1323 |
if step_infos["initial"]: |
|
1324 |
self.Program += [("INITIAL_", ())] |
|
1325 |
self.Program += [("STEP ", ()), |
|
1326 |
(step_name, (self.TagName, "step", step_infos["id"], "name")), |
|
1327 |
(":\n", ())] |
|
1328 |
actions = [] |
|
1329 |
self.IndentRight() |
|
1330 |
for action_infos in step_infos["actions"]: |
|
1331 |
if action_infos.get("id", None) is not None: |
|
1332 |
action_info = (self.TagName, "action_block", action_infos["id"], "action", action_infos["num"]) |
|
1333 |
else: |
|
1334 |
action_info = () |
|
1335 |
actions.append(action_infos["content"]) |
|
1336 |
self.Program += [(self.CurrentIndent, ()), |
|
1337 |
(action_infos["content"], action_info + ("reference",)), |
|
1338 |
("(", ()), |
|
1339 |
(action_infos["qualifier"], action_info + ("qualifier",))] |
|
1340 |
if "duration" in action_infos: |
|
1341 |
self.Program += [(", ", ()), |
|
1342 |
(action_infos["duration"], action_info + ("duration",))] |
|
1343 |
if "indicator" in action_infos: |
|
1344 |
self.Program += [(", ", ()), |
|
1345 |
(action_infos["indicator"], action_info + ("indicator",))] |
|
1346 |
self.Program += [(");\n", ())] |
|
1347 |
self.IndentLeft() |
|
1348 |
self.Program += [("%sEND_STEP\n\n"%self.CurrentIndent, ())] |
|
1349 |
for action in actions: |
|
1350 |
self.ComputeSFCAction(action) |
|
1351 |
for transition in step_infos["transitions"]: |
|
1352 |
self.ComputeSFCTransition(transition) |
|
1353 |
||
1354 |
def ComputeSFCAction(self, action_name): |
|
1355 |
if action_name in self.SFCNetworks["Actions"].keys(): |
|
1356 |
action_content, action_info = self.SFCNetworks["Actions"].pop(action_name) |
|
1357 |
self.Program += [("%sACTION "%self.CurrentIndent, ()), |
|
1358 |
(action_name, action_info), |
|
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset
|
1359 |
(":\n", ())] |
814 | 1360 |
self.Program += action_content |
1361 |
self.Program += [("%sEND_ACTION\n\n"%self.CurrentIndent, ())] |
|
1362 |
||
1363 |
def ComputeSFCTransition(self, transition): |
|
1364 |
if transition in self.SFCNetworks["Transitions"].keys(): |
|
1365 |
transition_infos = self.SFCNetworks["Transitions"].pop(transition) |
|
1366 |
self.Program += [("%sTRANSITION"%self.CurrentIndent, ())] |
|
1367 |
if transition_infos["priority"] != None: |
|
1368 |
self.Program += [(" (PRIORITY := ", ()), |
|
1369 |
("%d"%transition_infos["priority"], (self.TagName, "transition", transition_infos["id"], "priority")), |
|
1370 |
(")", ())] |
|
1371 |
self.Program += [(" FROM ", ())] |
|
1372 |
if len(transition_infos["from"]) > 1: |
|
1373 |
self.Program += [("(", ())] |
|
1374 |
self.Program += JoinList([(", ", ())], transition_infos["from"]) |
|
1375 |
self.Program += [(")", ())] |
|
1376 |
elif len(transition_infos["from"]) == 1: |
|
1377 |
self.Program += transition_infos["from"][0] |
|
1378 |
else: |
|
1379 |
raise PLCGenException, _("Transition with content \"%s\" not connected to a previous step in \"%s\" POU")%(transition_infos["content"], self.Name) |
|
1380 |
self.Program += [(" TO ", ())] |
|
1381 |
if len(transition_infos["to"]) > 1: |
|
1382 |
self.Program += [("(", ())] |
|
1383 |
self.Program += JoinList([(", ", ())], transition_infos["to"]) |
|
1384 |
self.Program += [(")", ())] |
|
1385 |
elif len(transition_infos["to"]) == 1: |
|
1386 |
self.Program += transition_infos["to"][0] |
|
1387 |
else: |
|
1388 |
raise PLCGenException, _("Transition with content \"%s\" not connected to a next step in \"%s\" POU")%(transition_infos["content"], self.Name) |
|
1389 |
self.Program += transition_infos["content"] |
|
1390 |
self.Program += [("%sEND_TRANSITION\n\n"%self.CurrentIndent, ())] |
|
1391 |
for [(step_name, step_infos)] in transition_infos["to"]: |
|
1392 |
self.ComputeSFCStep(step_name) |
|
1393 |
||
1394 |
def GenerateProgram(self, pou): |
|
1395 |
self.ComputeInterface(pou) |
|
1396 |
self.ComputeConnectionTypes(pou) |
|
1397 |
self.ComputeProgram(pou) |
|
1398 |
||
1399 |
program = [("%s "%self.Type, ()), |
|
1400 |
(self.Name, (self.TagName, "name"))] |
|
1401 |
if self.ReturnType: |
|
1402 |
program += [(" : ", ()), |
|
1403 |
(self.ReturnType, (self.TagName, "return"))] |
|
1404 |
program += [("\n", ())] |
|
1405 |
if len(self.Interface) == 0: |
|
1406 |
raise PLCGenException, _("No variable defined in \"%s\" POU")%self.Name |
|
1407 |
if len(self.Program) == 0 : |
|
1408 |
raise PLCGenException, _("No body defined in \"%s\" POU")%self.Name |
|
1409 |
var_number = 0 |
|
1410 |
for list_type, option, located, variables in self.Interface: |
|
1411 |
variable_type = errorVarTypes.get(list_type, "var_local") |
|
1412 |
program += [(" %s"%list_type, ())] |
|
1413 |
if option is not None: |
|
1414 |
program += [(" %s"%option, (self.TagName, variable_type, (var_number, var_number + len(variables)), option.lower()))] |
|
1415 |
program += [("\n", ())] |
|
1416 |
for var_type, var_name, var_address, var_initial in variables: |
|
1417 |
program += [(" ", ())] |
|
1418 |
if var_name: |
|
1419 |
program += [(var_name, (self.TagName, variable_type, var_number, "name")), |
|
1420 |
(" ", ())] |
|
1421 |
if var_address != None: |
|
1422 |
program += [("AT ", ()), |
|
1423 |
(var_address, (self.TagName, variable_type, var_number, "location")), |
|
1424 |
(" ", ())] |
|
1425 |
program += [(": ", ()), |
|
1426 |
(var_type, (self.TagName, variable_type, var_number, "type"))] |
|
1427 |
if var_initial != None: |
|
1428 |
program += [(" := ", ()), |
|
1429 |
(self.ParentGenerator.ComputeValue(var_initial, var_type), (self.TagName, variable_type, var_number, "initial value"))] |
|
1430 |
program += [(";\n", ())] |
|
1431 |
var_number += 1 |
|
1432 |
program += [(" END_VAR\n", ())] |
|
1433 |
program += [("\n", ())] |
|
1434 |
program += self.Program |
|
1435 |
program += [("END_%s\n\n"%self.Type, ())] |
|
1436 |
return program |
|
1437 |
||
1438 |
def GenerateCurrentProgram(controler, project, errors, warnings): |
|
1439 |
generator = ProgramGenerator(controler, project, errors, warnings) |
|
1440 |
generator.GenerateProgram() |
|
1441 |
return generator.GetGeneratedProgram() |
|
1442 |