44 def generate_block(generator, block, block_infos, body, link, order=False, to_inout=False): |
44 def generate_block(generator, block, block_infos, body, link, order=False, to_inout=False): |
45 body_type = body.getcontent()["name"] |
45 body_type = body.getcontent()["name"] |
46 name = block.getinstanceName() |
46 name = block.getinstanceName() |
47 type = block.gettypeName() |
47 type = block.gettypeName() |
48 executionOrderId = block.getexecutionOrderId() |
48 executionOrderId = block.getexecutionOrderId() |
|
49 input_variables = block.inputVariables.getvariable() |
|
50 output_variables = block.outputVariables.getvariable() |
49 inout_variables = {} |
51 inout_variables = {} |
50 for input_variable in block.inputVariables.getvariable(): |
52 for input_variable in input_variables: |
51 for output_variable in block.outputVariables.getvariable(): |
53 for output_variable in output_variables: |
52 if input_variable.getformalParameter() == output_variable.getformalParameter(): |
54 if input_variable.getformalParameter() == output_variable.getformalParameter(): |
53 inout_variables[input_variable.getformalParameter()] = "" |
55 inout_variables[input_variable.getformalParameter()] = "" |
|
56 input_names = [input[0] for input in block_infos["inputs"]] |
|
57 output_names = [output[0] for output in block_infos["outputs"]] |
54 if block_infos["type"] == "function": |
58 if block_infos["type"] == "function": |
55 output_variables = block.outputVariables.getvariable() |
|
56 if not generator.ComputedBlocks.get(block, False) and not order: |
59 if not generator.ComputedBlocks.get(block, False) and not order: |
57 generator.ComputedBlocks[block] = True |
60 generator.ComputedBlocks[block] = True |
58 vars = [] |
61 connected_vars = [] |
|
62 input_connected = dict([("EN", None)] + |
|
63 [(input_name, None) for input_name in input_names]) |
|
64 for variable in input_variables: |
|
65 parameter = variable.getformalParameter() |
|
66 if input_connected.has_key(parameter): |
|
67 input_connected[parameter] = variable |
|
68 if input_connected["EN"] is None: |
|
69 input_connected.pop("EN") |
|
70 input_parameters = input_names |
|
71 else: |
|
72 input_parameters = ["EN"] + input_names |
59 one_input_connected = False |
73 one_input_connected = False |
60 for i, variable in enumerate(block.inputVariables.getvariable()): |
74 all_input_connected = True |
61 input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
75 for i, parameter in enumerate(input_parameters): |
62 connections = variable.connectionPointIn.getconnections() |
76 variable = input_connected.get(parameter) |
63 if connections is not None: |
77 if variable is not None: |
64 parameter = variable.getformalParameter() |
78 input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
65 if parameter != "EN": |
79 connections = variable.connectionPointIn.getconnections() |
66 one_input_connected = True |
80 if connections is not None: |
67 if inout_variables.has_key(parameter): |
81 if parameter != "EN": |
68 value = generator.ComputeExpression(body, connections, executionOrderId > 0, True) |
82 one_input_connected = True |
69 inout_variables[parameter] = value |
83 if inout_variables.has_key(parameter): |
|
84 value = generator.ComputeExpression(body, connections, executionOrderId > 0, True) |
|
85 inout_variables[parameter] = value |
|
86 else: |
|
87 value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
|
88 connected_vars.append(([(parameter, input_info), (" := ", ())], |
|
89 generator.ExtractModifier(variable, value, input_info))) |
70 else: |
90 else: |
71 value = generator.ComputeExpression(body, connections, executionOrderId > 0) |
91 all_input_connected = False |
72 if len(output_variables) > 1: |
92 else: |
73 vars.append([(parameter, input_info), |
93 all_input_connected = False |
74 (" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
94 if len(output_variables) > 1 or not all_input_connected: |
75 else: |
95 vars = [name + value for name, value in connected_vars] |
76 vars.append(generator.ExtractModifier(variable, value, input_info)) |
96 else: |
|
97 vars = [value for name, value in connected_vars] |
77 if one_input_connected: |
98 if one_input_connected: |
78 for i, variable in enumerate(output_variables): |
99 for i, variable in enumerate(output_variables): |
79 parameter = variable.getformalParameter() |
100 parameter = variable.getformalParameter() |
80 if not inout_variables.has_key(parameter): |
101 if not inout_variables.has_key(parameter) and parameter in output_names + ["ENO"]: |
81 if variable.getformalParameter() == "": |
102 if variable.getformalParameter() == "": |
82 variable_name = "%s%d"%(type, block.getlocalId()) |
103 variable_name = "%s%d"%(type, block.getlocalId()) |
83 else: |
104 else: |
84 variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
105 variable_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
85 if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
106 if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
101 ("(", ())] |
122 ("(", ())] |
102 generator.Program += JoinList([(", ", ())], vars) |
123 generator.Program += JoinList([(", ", ())], vars) |
103 generator.Program += [(");\n", ())] |
124 generator.Program += [(");\n", ())] |
104 else: |
125 else: |
105 generator.Warnings.append(_("\"%s\" function cancelled in \"%s\" POU: No input connected")%(type, generator.TagName.split("::")[-1])) |
126 generator.Warnings.append(_("\"%s\" function cancelled in \"%s\" POU: No input connected")%(type, generator.TagName.split("::")[-1])) |
106 if link: |
|
107 connectionPoint = link.getposition()[-1] |
|
108 else: |
|
109 connectionPoint = None |
|
110 for i, variable in enumerate(output_variables): |
|
111 blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
|
112 if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety(): |
|
113 output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
|
114 parameter = variable.getformalParameter() |
|
115 if inout_variables.has_key(parameter): |
|
116 output_value = inout_variables[parameter] |
|
117 else: |
|
118 if parameter == "": |
|
119 output_name = "%s%d"%(type, block.getlocalId()) |
|
120 else: |
|
121 output_name = "%s%d_%s"%(type, block.getlocalId(), parameter) |
|
122 output_value = [(output_name, output_info)] |
|
123 return generator.ExtractModifier(variable, output_value, output_info) |
|
124 elif block_infos["type"] == "functionBlock": |
127 elif block_infos["type"] == "functionBlock": |
125 if not generator.ComputedBlocks.get(block, False) and not order: |
128 if not generator.ComputedBlocks.get(block, False) and not order: |
126 generator.ComputedBlocks[block] = True |
129 generator.ComputedBlocks[block] = True |
127 vars = [] |
130 vars = [] |
128 for i, variable in enumerate(block.inputVariables.getvariable()): |
131 offset_idx = 0 |
129 input_info = (generator.TagName, "block", block.getlocalId(), "input", i) |
132 for variable in input_variables: |
130 connections = variable.connectionPointIn.getconnections() |
133 parameter = variable.getformalParameter() |
131 if connections is not None: |
134 if parameter in input_names or parameter == "EN": |
132 parameter = variable.getformalParameter() |
135 if parameter == "EN": |
133 value = generator.ComputeExpression(body, connections, executionOrderId > 0, inout_variables.has_key(parameter)) |
136 input_idx = 0 |
134 vars.append([(parameter, input_info), |
137 offset_idx = 1 |
135 (" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
138 else: |
|
139 input_idx = offset_idx + input_names.index(parameter) |
|
140 input_info = (generator.TagName, "block", block.getlocalId(), "input", input_idx) |
|
141 connections = variable.connectionPointIn.getconnections() |
|
142 if connections is not None: |
|
143 value = generator.ComputeExpression(body, connections, executionOrderId > 0, inout_variables.has_key(parameter)) |
|
144 vars.append([(parameter, input_info), |
|
145 (" := ", ())] + generator.ExtractModifier(variable, value, input_info)) |
136 generator.Program += [(generator.CurrentIndent, ()), |
146 generator.Program += [(generator.CurrentIndent, ()), |
137 (name, (generator.TagName, "block", block.getlocalId(), "name")), |
147 (name, (generator.TagName, "block", block.getlocalId(), "name")), |
138 ("(", ())] |
148 ("(", ())] |
139 generator.Program += JoinList([(", ", ())], vars) |
149 generator.Program += JoinList([(", ", ())], vars) |
140 generator.Program += [(");\n", ())] |
150 generator.Program += [(");\n", ())] |
141 if link: |
151 |
142 connectionPoint = link.getposition()[-1] |
152 if link: |
|
153 connectionPoint = link.getposition()[-1] |
|
154 output_parameter = link.getformalParameter() |
|
155 else: |
|
156 connectionPoint = None |
|
157 output_parameter = None |
|
158 |
|
159 output_variable = None |
|
160 output_idx = 0 |
|
161 if output_parameter is not None: |
|
162 if output_parameter in output_names or output_parameter == "ENO": |
|
163 for variable in output_variables: |
|
164 if variable.getformalParameter() == output_parameter: |
|
165 output_variable = variable |
|
166 if output_parameter != "ENO": |
|
167 output_idx = output_names.index(output_parameter) |
|
168 else: |
|
169 for i, variable in enumerate(output_variables): |
|
170 blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
|
171 if (not connectionPoint or |
|
172 block.getx() + blockPointx == connectionPoint.getx() and |
|
173 block.gety() + blockPointy == connectionPoint.gety()): |
|
174 output_variable = variable |
|
175 output_parameter = variable.getformalParameter() |
|
176 output_idx = i |
|
177 |
|
178 if output_variable is not None: |
|
179 if block_infos["type"] == "function": |
|
180 output_info = (generator.TagName, "block", block.getlocalId(), "output", output_idx) |
|
181 if inout_variables.has_key(output_parameter): |
|
182 output_value = inout_variables[output_parameter] |
|
183 else: |
|
184 if output_parameter == "": |
|
185 output_name = "%s%d"%(type, block.getlocalId()) |
|
186 else: |
|
187 output_name = "%s%d_%s"%(type, block.getlocalId(), output_parameter) |
|
188 output_value = [(output_name, output_info)] |
|
189 return generator.ExtractModifier(output_variable, output_value, output_info) |
|
190 |
|
191 if block_infos["type"] == "functionBlock": |
|
192 output_info = (generator.TagName, "block", block.getlocalId(), "output", output_idx) |
|
193 output_name = generator.ExtractModifier(output_variable, [("%s.%s"%(name, output_parameter), output_info)], output_info) |
|
194 if to_inout: |
|
195 variable_name = "%s_%s"%(name, output_parameter) |
|
196 if not generator.IsAlreadyDefined(variable_name): |
|
197 if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
|
198 generator.Interface.append(("VAR", None, False, [])) |
|
199 if variable.connectionPointOut in generator.ConnectionTypes: |
|
200 generator.Interface[-1][3].append( |
|
201 (generator.ConnectionTypes[output_variable.connectionPointOut], variable_name, None, None)) |
|
202 else: |
|
203 generator.Interface[-1][3].append(("ANY", variable_name, None, None)) |
|
204 generator.Program += [(generator.CurrentIndent, ()), |
|
205 ("%s := "%variable_name, ())] |
|
206 generator.Program += output_name |
|
207 generator.Program += [(";\n", ())] |
|
208 return [(variable_name, ())] |
|
209 return output_name |
|
210 if link is not None: |
|
211 if output_parameter is None: |
|
212 output_parameter = "" |
|
213 if name: |
|
214 blockname = "%s(%s)" % (name, type) |
143 else: |
215 else: |
144 connectionPoint = None |
216 blockname = type |
145 for i, variable in enumerate(block.outputVariables.getvariable()): |
217 raise ValueError, _("No output %s variable found in block %s in POU %s. Connection must be broken") % \ |
146 blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() |
218 (output_parameter, blockname, generator.Name) |
147 if not connectionPoint or block.getx() + blockPointx == connectionPoint.getx() and block.gety() + blockPointy == connectionPoint.gety(): |
|
148 output_info = (generator.TagName, "block", block.getlocalId(), "output", i) |
|
149 output_name = generator.ExtractModifier(variable, [("%s.%s"%(name, variable.getformalParameter()), output_info)], output_info) |
|
150 if to_inout: |
|
151 variable_name = "%s_%s"%(name, variable.getformalParameter()) |
|
152 if not generator.IsAlreadyDefined(variable_name): |
|
153 if generator.Interface[-1][0] != "VAR" or generator.Interface[-1][1] is not None or generator.Interface[-1][2]: |
|
154 generator.Interface.append(("VAR", None, False, [])) |
|
155 if variable.connectionPointOut in generator.ConnectionTypes: |
|
156 generator.Interface[-1][3].append((generator.ConnectionTypes[variable.connectionPointOut], variable_name, None, None)) |
|
157 else: |
|
158 generator.Interface[-1][3].append(("ANY", variable_name, None, None)) |
|
159 generator.Program += [(generator.CurrentIndent, ()), |
|
160 ("%s := "%variable_name, ())] |
|
161 generator.Program += output_name |
|
162 generator.Program += [(";\n", ())] |
|
163 return [(variable_name, ())] |
|
164 return output_name |
|
165 if link is not None: |
|
166 raise ValueError, _("No output variable found") |
|
167 |
219 |
168 def initialise_block(type, name, block = None): |
220 def initialise_block(type, name, block = None): |
169 return [(type, name, None, None)] |
221 return [(type, name, None, None)] |
170 |
222 |
171 #------------------------------------------------------------------------------- |
223 #------------------------------------------------------------------------------- |