127 #------------------------------------------------------------------------------- |
127 #------------------------------------------------------------------------------- |
128 # Helpers functions for translating list of arguments |
128 # Helpers functions for translating list of arguments |
129 # from xslt to valid arguments |
129 # from xslt to valid arguments |
130 #------------------------------------------------------------------------------- |
130 #------------------------------------------------------------------------------- |
131 |
131 |
|
132 _StringValue = lambda x: x |
132 _BoolValue = lambda x: x in ["true", "0"] |
133 _BoolValue = lambda x: x in ["true", "0"] |
133 |
134 |
134 def _translate_args(translations, args): |
135 def _translate_args(translations, args): |
135 return [translate(arg[0]) if len(arg) > 0 else None |
136 return [translate(arg[0]) if len(arg) > 0 else None |
136 for translate, arg in |
137 for translate, arg in |
168 def GetTree(self): |
169 def GetTree(self): |
169 return (self.TreeStack.pop(-1), self.Dimensions) |
170 return (self.TreeStack.pop(-1), self.Dimensions) |
170 |
171 |
171 def AddDimension(self, context, *args): |
172 def AddDimension(self, context, *args): |
172 self.Dimensions.append(tuple( |
173 self.Dimensions.append(tuple( |
173 _translate_args([str] * 2, args))) |
174 _translate_args([_StringValue] * 2, args))) |
174 |
175 |
175 def AddTree(self, context, *args): |
176 def AddTree(self, context, *args): |
176 self.TreeStack.append([]) |
177 self.TreeStack.append([]) |
177 self.Dimensions = [] |
178 self.Dimensions = [] |
178 |
179 |
180 var = (args[0][0], self.Type, self.GetTree()) |
181 var = (args[0][0], self.Type, self.GetTree()) |
181 self.TreeStack[-1].append(var) |
182 self.TreeStack[-1].append(var) |
182 |
183 |
183 def AddVariable(self, context, *args): |
184 def AddVariable(self, context, *args): |
184 self.Variables.append(_VariableInfos(*(_translate_args( |
185 self.Variables.append(_VariableInfos(*(_translate_args( |
185 [str] * 5 + [_BoolValue] + [str], args) + |
186 [_StringValue] * 5 + [_BoolValue] + [_StringValue], args) + |
186 [self.GetType(), self.GetTree()]))) |
187 [self.GetType(), self.GetTree()]))) |
187 |
188 |
188 #------------------------------------------------------------------------------- |
189 #------------------------------------------------------------------------------- |
189 # Helpers object for generating pou variable instance list |
190 # Helpers object for generating pou variable instance list |
190 #------------------------------------------------------------------------------- |
191 #------------------------------------------------------------------------------- |
226 return self.Root |
227 return self.Root |
227 |
228 |
228 def SetRoot(self, context, *args): |
229 def SetRoot(self, context, *args): |
229 self.Root = _VariablesTreeItemInfos( |
230 self.Root = _VariablesTreeItemInfos( |
230 *([''] + _translate_args( |
231 *([''] + _translate_args( |
231 [class_extraction, str] + [_BoolValue] * 2, |
232 [class_extraction, _StringValue] + [_BoolValue] * 2, |
232 args) + [[]])) |
233 args) + [[]])) |
233 |
234 |
234 def AddVariable(self, context, *args): |
235 def AddVariable(self, context, *args): |
235 if self.Root is not None: |
236 if self.Root is not None: |
236 self.Root.variables.append(_VariablesTreeItemInfos( |
237 self.Root.variables.append(_VariablesTreeItemInfos( |
237 *(_translate_args( |
238 *(_translate_args( |
238 [str, class_extraction, str] + [_BoolValue] * 2, |
239 [_StringValue, class_extraction, _StringValue] + |
239 args) + [[]]))) |
240 [_BoolValue] * 2, args) + [[]]))) |
240 |
241 |
241 #------------------------------------------------------------------------------- |
242 #------------------------------------------------------------------------------- |
242 # Helpers object for generating instances path list |
243 # Helpers object for generating instances path list |
243 #------------------------------------------------------------------------------- |
244 #------------------------------------------------------------------------------- |
244 |
245 |
288 ["type", "id", "x", "y", "width", "height", "specific_values", "inputs", "outputs"]) |
289 ["type", "id", "x", "y", "width", "height", "specific_values", "inputs", "outputs"]) |
289 |
290 |
290 _BlockSpecificValues = ( |
291 _BlockSpecificValues = ( |
291 namedtuple("BlockSpecificValues", |
292 namedtuple("BlockSpecificValues", |
292 ["name", "execution_order"]), |
293 ["name", "execution_order"]), |
293 [str, int]) |
294 [_StringValue, int]) |
294 _VariableSpecificValues = ( |
295 _VariableSpecificValues = ( |
295 namedtuple("VariableSpecificValues", |
296 namedtuple("VariableSpecificValues", |
296 ["name", "value_type", "execution_order"]), |
297 ["name", "value_type", "execution_order"]), |
297 [str, str, int]) |
298 [_StringValue, _StringValue, int]) |
298 _ConnectionSpecificValues = ( |
299 _ConnectionSpecificValues = ( |
299 namedtuple("ConnectionSpecificValues", ["name"]), |
300 namedtuple("ConnectionSpecificValues", ["name"]), |
300 [str]) |
301 [_StringValue]) |
301 |
302 |
302 _PowerRailSpecificValues = ( |
303 _PowerRailSpecificValues = ( |
303 namedtuple("PowerRailSpecificValues", ["connectors"]), |
304 namedtuple("PowerRailSpecificValues", ["connectors"]), |
304 [int]) |
305 [int]) |
305 |
306 |
306 _LDElementSpecificValues = ( |
307 _LDElementSpecificValues = ( |
307 namedtuple("LDElementSpecificValues", |
308 namedtuple("LDElementSpecificValues", |
308 ["name", "negated", "edge", "storage", "execution_order"]), |
309 ["name", "negated", "edge", "storage", "execution_order"]), |
309 [str, _BoolValue, str, str, int]) |
310 [_StringValue, _BoolValue, _StringValue, _StringValue, int]) |
310 |
311 |
311 _DivergenceSpecificValues = ( |
312 _DivergenceSpecificValues = ( |
312 namedtuple("DivergenceSpecificValues", ["connectors"]), |
313 namedtuple("DivergenceSpecificValues", ["connectors"]), |
313 [int]) |
314 [int]) |
314 |
315 |
315 _SpecificValuesTuples = { |
316 _SpecificValuesTuples = { |
316 "comment": ( |
317 "comment": ( |
317 namedtuple("CommentSpecificValues", ["content"]), |
318 namedtuple("CommentSpecificValues", ["content"]), |
318 [str]), |
319 [_StringValue]), |
319 "input": _VariableSpecificValues, |
320 "input": _VariableSpecificValues, |
320 "output": _VariableSpecificValues, |
321 "output": _VariableSpecificValues, |
321 "inout": _VariableSpecificValues, |
322 "inout": _VariableSpecificValues, |
322 "connector": _ConnectionSpecificValues, |
323 "connector": _ConnectionSpecificValues, |
323 "continuation": _ConnectionSpecificValues, |
324 "continuation": _ConnectionSpecificValues, |
325 "rightPowerRail": _PowerRailSpecificValues, |
326 "rightPowerRail": _PowerRailSpecificValues, |
326 "contact": _LDElementSpecificValues, |
327 "contact": _LDElementSpecificValues, |
327 "coil": _LDElementSpecificValues, |
328 "coil": _LDElementSpecificValues, |
328 "step": ( |
329 "step": ( |
329 namedtuple("StepSpecificValues", ["name", "initial", "action"]), |
330 namedtuple("StepSpecificValues", ["name", "initial", "action"]), |
330 [str, _BoolValue, lambda x: x]), |
331 [_StringValue, _BoolValue, lambda x: x]), |
331 "transition": ( |
332 "transition": ( |
332 namedtuple("TransitionSpecificValues", |
333 namedtuple("TransitionSpecificValues", |
333 ["priority", "condition_type", "condition", "connection"]), |
334 ["priority", "condition_type", "condition", "connection"]), |
334 [int, str, str, lambda x: x]), |
335 [int, _StringValue, _StringValue, lambda x: x]), |
335 "selectionDivergence": _DivergenceSpecificValues, |
336 "selectionDivergence": _DivergenceSpecificValues, |
336 "selectionConvergence": _DivergenceSpecificValues, |
337 "selectionConvergence": _DivergenceSpecificValues, |
337 "simultaneousDivergence": _DivergenceSpecificValues, |
338 "simultaneousDivergence": _DivergenceSpecificValues, |
338 "simultaneousConvergence": _DivergenceSpecificValues, |
339 "simultaneousConvergence": _DivergenceSpecificValues, |
339 "jump": ( |
340 "jump": ( |
340 namedtuple("JumpSpecificValues", ["target"]), |
341 namedtuple("JumpSpecificValues", ["target"]), |
341 [str]), |
342 [_StringValue]), |
342 "actionBlock": ( |
343 "actionBlock": ( |
343 namedtuple("ActionBlockSpecificValues", ["actions"]), |
344 namedtuple("ActionBlockSpecificValues", ["actions"]), |
344 [lambda x: x]), |
345 [lambda x: x]), |
345 } |
346 } |
346 |
347 |
385 specific_values = specific_values_tuple(*_translate_args( |
386 specific_values = specific_values_tuple(*_translate_args( |
386 specific_values_translation, self.SpecificValues)) |
387 specific_values_translation, self.SpecificValues)) |
387 self.SpecificValues = None |
388 self.SpecificValues = None |
388 |
389 |
389 self.CurrentInstance = _BlockInstanceInfos( |
390 self.CurrentInstance = _BlockInstanceInfos( |
390 *(_translate_args([str, int] + [float] * 4, args) + |
391 *(_translate_args([_StringValue, int] + [float] * 4, args) + |
391 [specific_values, [], []])) |
392 [specific_values, [], []])) |
392 |
393 |
393 self.BlockInstances[self.CurrentInstance.id] = self.CurrentInstance |
394 self.BlockInstances[self.CurrentInstance.id] = self.CurrentInstance |
394 |
395 |
395 def AddInstanceConnection(self, context, *args): |
396 def AddInstanceConnection(self, context, *args): |
396 connection_args = _translate_args( |
397 connection_args = _translate_args( |
397 [str, str, _BoolValue, str, float, float], args) |
398 [_StringValue] * 2 + [_BoolValue, _StringValue] + [float] * 2, args) |
398 |
399 |
399 self.CurrentConnection = _InstanceConnectionInfos( |
400 self.CurrentConnection = _InstanceConnectionInfos( |
400 *(connection_args[1:4] + [ |
401 *(connection_args[1:4] + [ |
401 _Point(*connection_args[4:6]), []])) |
402 _Point(*connection_args[4:6]), []])) |
402 |
403 |
408 else: |
409 else: |
409 self.SpecificValues.append([self.CurrentConnection]) |
410 self.SpecificValues.append([self.CurrentConnection]) |
410 |
411 |
411 def AddConnectionLink(self, context, *args): |
412 def AddConnectionLink(self, context, *args): |
412 self.CurrentLink = _ConnectionLinkInfos( |
413 self.CurrentLink = _ConnectionLinkInfos( |
413 *(_translate_args([int, str], args) + [[]])) |
414 *(_translate_args([int, _StringValue], args) + [[]])) |
414 self.CurrentConnection.links.append(self.CurrentLink) |
415 self.CurrentConnection.links.append(self.CurrentLink) |
415 |
416 |
416 def AddLinkPoint(self, context, *args): |
417 def AddLinkPoint(self, context, *args): |
417 self.CurrentLink.points.append(_Point( |
418 self.CurrentLink.points.append(_Point( |
418 *_translate_args([float] * 2, args))) |
419 *_translate_args([float] * 2, args))) |
419 |
420 |
420 def AddAction(self, context, *args): |
421 def AddAction(self, context, *args): |
421 if len(self.SpecificValues) == 0: |
422 if len(self.SpecificValues) == 0: |
422 self.SpecificValues.append([[]]) |
423 self.SpecificValues.append([[]]) |
423 translated_args = _translate_args([str] * 5, args) |
424 translated_args = _translate_args([_StringValue] * 5, args) |
424 self.SpecificValues[0][0].append(_ActionInfos(*translated_args)) |
425 self.SpecificValues[0][0].append(_ActionInfos(*translated_args)) |
425 |
426 |
426 pou_block_instances_xslt = etree.parse( |
427 pou_block_instances_xslt = etree.parse( |
427 os.path.join(ScriptDirectory, "plcopen", "pou_block_instances.xslt")) |
428 os.path.join(ScriptDirectory, "plcopen", "pou_block_instances.xslt")) |
428 |
429 |