PLCGenerator.py
changeset 2450 5024c19ca8f0
parent 2447 1c04a50dc7ff
child 2456 7373e3048167
equal deleted inserted replaced
2449:b0560adec4b7 2450:5024c19ca8f0
    22 # along with this program; if not, write to the Free Software
    22 # along with this program; if not, write to the Free Software
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    24 
    24 
    25 
    25 
    26 from __future__ import absolute_import
    26 from __future__ import absolute_import
    27 from types import *
       
    28 import re
    27 import re
    29 from six.moves import xrange
    28 from six.moves import xrange
    30 
    29 
    31 from plcopen import PLCOpenParser
    30 from plcopen import PLCOpenParser
    32 from plcopen.structures import *
    31 from plcopen.structures import *
   708         "16":   None,
   707         "16":   None,
   709     }
   708     }
   710 
   709 
   711     def ComputeConnectionTypes(self, pou):
   710     def ComputeConnectionTypes(self, pou):
   712         body = pou.getbody()
   711         body = pou.getbody()
   713         if isinstance(body, ListType):
   712         if isinstance(body, list):
   714             body = body[0]
   713             body = body[0]
   715         body_content = body.getcontent()
   714         body_content = body.getcontent()
   716         body_type = body_content.getLocalTag()
   715         body_type = body_content.getLocalTag()
   717         if body_type in ["FBD", "LD", "SFC"]:
   716         if body_type in ["FBD", "LD", "SFC"]:
   718             undefined_blocks = []
   717             undefined_blocks = []
   941                     return var_name
   940                     return var_name
   942         return None
   941         return None
   943 
   942 
   944     def ComputeProgram(self, pou):
   943     def ComputeProgram(self, pou):
   945         body = pou.getbody()
   944         body = pou.getbody()
   946         if isinstance(body, ListType):
   945         if isinstance(body, list):
   947             body = body[0]
   946             body = body[0]
   948         body_content = body.getcontent()
   947         body_content = body.getcontent()
   949         body_type = body_content.getLocalTag()
   948         body_type = body_content.getLocalTag()
   950         if body_type in ["IL", "ST"]:
   949         if body_type in ["IL", "ST"]:
   951             text = body_content.getanyText()
   950             text = body_content.getanyText()
  1049     def FactorizePaths(self, paths):
  1048     def FactorizePaths(self, paths):
  1050         same_paths = {}
  1049         same_paths = {}
  1051         uncomputed_index = range(len(paths))
  1050         uncomputed_index = range(len(paths))
  1052         factorized_paths = []
  1051         factorized_paths = []
  1053         for num, path in enumerate(paths):
  1052         for num, path in enumerate(paths):
  1054             if isinstance(path, ListType):
  1053             if isinstance(path, list):
  1055                 if len(path) > 1:
  1054                 if len(path) > 1:
  1056                     str_path = str(path[-1:])
  1055                     str_path = str(path[-1:])
  1057                     same_paths.setdefault(str_path, [])
  1056                     same_paths.setdefault(str_path, [])
  1058                     same_paths[str_path].append((path[:-1], num))
  1057                     same_paths[str_path].append((path[:-1], num))
  1059             else:
  1058             else:
  1339                     factorized_paths = self.FactorizePaths(result)
  1338                     factorized_paths = self.FactorizePaths(result)
  1340                     if len(factorized_paths) > 1:
  1339                     if len(factorized_paths) > 1:
  1341                         paths.append([variable, tuple(factorized_paths)])
  1340                         paths.append([variable, tuple(factorized_paths)])
  1342                     else:
  1341                     else:
  1343                         paths.append([variable] + factorized_paths)
  1342                         paths.append([variable] + factorized_paths)
  1344                 elif isinstance(result[0], ListType):
  1343                 elif isinstance(result[0], list):
  1345                     paths.append([variable] + result[0])
  1344                     paths.append([variable] + result[0])
  1346                 elif result[0] is not None:
  1345                 elif result[0] is not None:
  1347                     paths.append([variable, result[0]])
  1346                     paths.append([variable, result[0]])
  1348                 else:
  1347                 else:
  1349                     paths.append(variable)
  1348                     paths.append(variable)
  1350             elif isinstance(next, CoilClass):
  1349             elif isinstance(next, CoilClass):
  1351                 paths.append(self.GeneratePaths(next.connectionPointIn.getconnections(), body, order))
  1350                 paths.append(self.GeneratePaths(next.connectionPointIn.getconnections(), body, order))
  1352         return paths
  1351         return paths
  1353 
  1352 
  1354     def ComputePaths(self, paths, first=False):
  1353     def ComputePaths(self, paths, first=False):
  1355         if isinstance(paths, TupleType):
  1354         if isinstance(paths, tuple):
  1356             if None in paths:
  1355             if None in paths:
  1357                 return [("TRUE", ())]
  1356                 return [("TRUE", ())]
  1358             else:
  1357             else:
  1359                 vars = [self.ComputePaths(path) for path in paths]
  1358                 vars = [self.ComputePaths(path) for path in paths]
  1360                 if first:
  1359                 if first:
  1361                     return JoinList([(" OR ", ())], vars)
  1360                     return JoinList([(" OR ", ())], vars)
  1362                 else:
  1361                 else:
  1363                     return [("(", ())] + JoinList([(" OR ", ())], vars) + [(")", ())]
  1362                     return [("(", ())] + JoinList([(" OR ", ())], vars) + [(")", ())]
  1364         elif isinstance(paths, ListType):
  1363         elif isinstance(paths, list):
  1365             vars = [self.ComputePaths(path) for path in paths]
  1364             vars = [self.ComputePaths(path) for path in paths]
  1366             return JoinList([(" AND ", ())], vars)
  1365             return JoinList([(" AND ", ())], vars)
  1367         elif paths is None:
  1366         elif paths is None:
  1368             return [("TRUE", ())]
  1367             return [("TRUE", ())]
  1369         else:
  1368         else:
  1421         if connectionPointIn is not None:
  1420         if connectionPointIn is not None:
  1422             connections = connectionPointIn.getconnections()
  1421             connections = connectionPointIn.getconnections()
  1423             if connections is not None and len(connections) == 1:
  1422             if connections is not None and len(connections) == 1:
  1424                 instanceLocalId = connections[0].getrefLocalId()
  1423                 instanceLocalId = connections[0].getrefLocalId()
  1425                 body = pou.getbody()
  1424                 body = pou.getbody()
  1426                 if isinstance(body, ListType):
  1425                 if isinstance(body, list):
  1427                     body = body[0]
  1426                     body = body[0]
  1428                 return body.getcontentInstance(instanceLocalId)
  1427                 return body.getcontentInstance(instanceLocalId)
  1429         return None
  1428         return None
  1430 
  1429 
  1431     def ExtractConvergenceInputs(self, convergence, pou):
  1430     def ExtractConvergenceInputs(self, convergence, pou):
  1433         for connectionPointIn in convergence.getconnectionPointIn():
  1432         for connectionPointIn in convergence.getconnectionPointIn():
  1434             connections = connectionPointIn.getconnections()
  1433             connections = connectionPointIn.getconnections()
  1435             if connections is not None and len(connections) == 1:
  1434             if connections is not None and len(connections) == 1:
  1436                 instanceLocalId = connections[0].getrefLocalId()
  1435                 instanceLocalId = connections[0].getrefLocalId()
  1437                 body = pou.getbody()
  1436                 body = pou.getbody()
  1438                 if isinstance(body, ListType):
  1437                 if isinstance(body, list):
  1439                     body = body[0]
  1438                     body = body[0]
  1440                 instances.append(body.getcontentInstance(instanceLocalId))
  1439                 instances.append(body.getcontentInstance(instanceLocalId))
  1441         return instances
  1440         return instances
  1442 
  1441 
  1443     def GenerateSFCStep(self, step, pou):
  1442     def GenerateSFCStep(self, step, pou):
  1454                 instances = []
  1453                 instances = []
  1455                 connections = step.connectionPointIn.getconnections()
  1454                 connections = step.connectionPointIn.getconnections()
  1456                 if connections is not None and len(connections) == 1:
  1455                 if connections is not None and len(connections) == 1:
  1457                     instanceLocalId = connections[0].getrefLocalId()
  1456                     instanceLocalId = connections[0].getrefLocalId()
  1458                     body = pou.getbody()
  1457                     body = pou.getbody()
  1459                     if isinstance(body, ListType):
  1458                     if isinstance(body, list):
  1460                         body = body[0]
  1459                         body = body[0]
  1461                     instance = body.getcontentInstance(instanceLocalId)
  1460                     instance = body.getcontentInstance(instanceLocalId)
  1462                     if isinstance(instance, TransitionClass):
  1461                     if isinstance(instance, TransitionClass):
  1463                         instances.append(instance)
  1462                         instances.append(instance)
  1464                     elif isinstance(instance, SelectionConvergenceClass):
  1463                     elif isinstance(instance, SelectionConvergenceClass):
  1488             instances = []
  1487             instances = []
  1489             connections = jump.connectionPointIn.getconnections()
  1488             connections = jump.connectionPointIn.getconnections()
  1490             if connections is not None and len(connections) == 1:
  1489             if connections is not None and len(connections) == 1:
  1491                 instanceLocalId = connections[0].getrefLocalId()
  1490                 instanceLocalId = connections[0].getrefLocalId()
  1492                 body = pou.getbody()
  1491                 body = pou.getbody()
  1493                 if isinstance(body, ListType):
  1492                 if isinstance(body, list):
  1494                     body = body[0]
  1493                     body = body[0]
  1495                 instance = body.getcontentInstance(instanceLocalId)
  1494                 instance = body.getcontentInstance(instanceLocalId)
  1496                 if isinstance(instance, TransitionClass):
  1495                 if isinstance(instance, TransitionClass):
  1497                     instances.append(instance)
  1496                     instances.append(instance)
  1498                 elif isinstance(instance, SelectionConvergenceClass):
  1497                 elif isinstance(instance, SelectionConvergenceClass):
  1513     def GenerateSFCStepActions(self, actionBlock, pou):
  1512     def GenerateSFCStepActions(self, actionBlock, pou):
  1514         connections = actionBlock.connectionPointIn.getconnections()
  1513         connections = actionBlock.connectionPointIn.getconnections()
  1515         if connections is not None and len(connections) == 1:
  1514         if connections is not None and len(connections) == 1:
  1516             stepLocalId = connections[0].getrefLocalId()
  1515             stepLocalId = connections[0].getrefLocalId()
  1517             body = pou.getbody()
  1516             body = pou.getbody()
  1518             if isinstance(body, ListType):
  1517             if isinstance(body, list):
  1519                 body = body[0]
  1518                 body = body[0]
  1520             step = body.getcontentInstance(stepLocalId)
  1519             step = body.getcontentInstance(stepLocalId)
  1521             self.GenerateSFCStep(step, pou)
  1520             self.GenerateSFCStep(step, pou)
  1522             step_name = step.getname()
  1521             step_name = step.getname()
  1523             if step_name in self.SFCNetworks["Steps"].keys():
  1522             if step_name in self.SFCNetworks["Steps"].keys():
  1560             steps = []
  1559             steps = []
  1561             connections = transition.connectionPointIn.getconnections()
  1560             connections = transition.connectionPointIn.getconnections()
  1562             if connections is not None and len(connections) == 1:
  1561             if connections is not None and len(connections) == 1:
  1563                 instanceLocalId = connections[0].getrefLocalId()
  1562                 instanceLocalId = connections[0].getrefLocalId()
  1564                 body = pou.getbody()
  1563                 body = pou.getbody()
  1565                 if isinstance(body, ListType):
  1564                 if isinstance(body, list):
  1566                     body = body[0]
  1565                     body = body[0]
  1567                 instance = body.getcontentInstance(instanceLocalId)
  1566                 instance = body.getcontentInstance(instanceLocalId)
  1568                 if isinstance(instance, StepClass):
  1567                 if isinstance(instance, StepClass):
  1569                     steps.append(instance)
  1568                     steps.append(instance)
  1570                 elif isinstance(instance, SelectionDivergenceClass):
  1569                 elif isinstance(instance, SelectionDivergenceClass):
  1615                             _("Transition \"%s\" body must contain an output variable or coil referring to its name")
  1614                             _("Transition \"%s\" body must contain an output variable or coil referring to its name")
  1616                             % transitionValues["value"])
  1615                             % transitionValues["value"])
  1617                 self.TagName = previous_tagname
  1616                 self.TagName = previous_tagname
  1618             elif transitionValues["type"] == "connection":
  1617             elif transitionValues["type"] == "connection":
  1619                 body = pou.getbody()
  1618                 body = pou.getbody()
  1620                 if isinstance(body, ListType):
  1619                 if isinstance(body, list):
  1621                     body = body[0]
  1620                     body = body[0]
  1622                 connections = transitionValues["value"].getconnections()
  1621                 connections = transitionValues["value"].getconnections()
  1623                 if connections is not None:
  1622                 if connections is not None:
  1624                     expression = self.ComputeExpression(body, connections)
  1623                     expression = self.ComputeExpression(body, connections)
  1625                     if expression is not None:
  1624                     if expression is not None: