PLCGenerator.py
author lbessard
Thu, 01 Feb 2007 18:09:34 +0100
changeset 1 e9d01d824086
parent 0 b622defdfd98
child 2 93bc4c2cf376
permissions -rw-r--r--
SFC textual programs generation completed
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     5
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     7
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU Lesser General Public
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    19
#Lesser General Public License for more details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU Lesser General Public
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
from plcopen import plcopen
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
from plcopen.structures import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
from types import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    28
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
varTypeNames = {"localVars" : "VAR", "tempVars" : "VAR_TEMP", "inputVars" : "VAR_INPUT", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
                "outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
                "globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    32
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    33
def ReIndentText(text, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    34
    compute = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    35
    lines = text.splitlines()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    36
    if len(lines) > 0:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    37
        spaces = 0
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    38
        while lines[0][spaces] == " ":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    39
            spaces += 1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    40
        indent = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    41
        for i in xrange(spaces, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    42
            indent += " "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    43
        for line in lines:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    44
            compute += "%s%s\n"%(indent, line)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    45
    return compute
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    46
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    47
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    48
Module implementing methods for generating PLC programs in ST or IL
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    49
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    50
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
class PouProgram:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
    def __init__(self, name, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
        self.Type = type
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    56
        self.ReturnType = None
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    57
        self.Interface = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    58
        self.InitialSteps = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    59
        self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
        self.Program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    61
    
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    62
    def IsAlreadyDefined(self, name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    63
        for list_type, retain, constant, vars in self.Interface:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    64
            for var_type, var_name in vars:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    65
                if name == var_name:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    66
                    return True
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    67
        return False
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    68
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    69
    def GenerateInterface(self, interface):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    70
        if self.Type == "FUNCTION":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    71
            self.ReturnType = interface.getReturnType().getValue()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    72
        for varlist in interface.getContent():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    73
            variables = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    74
            for var in varlist["value"].getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    75
                type = var.getType().getValue()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    76
                variables.append((type, var.getName()))
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    77
            self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    78
                            varlist["value"].getConstant(), variables))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    79
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    80
    def GenerateProgram(self, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    81
        body = pou.getBody()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
        body_content = body.getContent()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    83
        body_type = body_content["name"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    84
        if body_type in ["IL","ST"]:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    85
            self.Program = ReIndentText(body_content["value"].getText(), 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
        elif body_type == "FBD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
                if isinstance(instance, plcopen.outVariable):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
                    var = instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
                    connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    91
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
                        expression = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
                        self.Program += "  %s := %s;\n"%(var, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    94
        elif body_type == "LD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
                if isinstance(instance, plcopen.coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
                    paths = self.GenerateLDPaths(instance, body)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    98
                    variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    99
                    expression = self.ComputeLDExpression(paths, True)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
                    self.Program += "  %s := %s;\n"%(variable, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
        elif body_type == "SFC":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
                if isinstance(instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   104
                    self.GenerateSFCStep(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
                elif isinstance(instance, plcopen.actionBlock):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   106
                    self.GenerateSFCStepActions(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
                elif isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   108
                    self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
                elif isinstance(instance, plcopen.jumpStep):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
                    self.GenerateSFCJump(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   111
            for initialstep in self.InitialSteps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   112
                self.ComputeSFCStep(initialstep)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
    def ComputeFBDExpression(self, body, link):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
        localid = link.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
        instance = body.getContentInstance(localid)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
        if isinstance(instance, plcopen.inVariable):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
            return instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   119
        elif isinstance(instance, plcopen.block):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   120
            name = instance.getInstanceName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   121
            type = instance.getTypeName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   122
            block_infos = GetBlockType(type)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
            if block_infos["type"] == "function":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   124
                vars = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   125
                for variable in instance.inputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
                    connections = variable.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   127
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
                        value = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   129
                        vars.append(self.ExtractModifier(variable, value))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
                variable = instance.outputVariables.getVariable()[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   131
                return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars)))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   132
            elif block_infos["type"] == "functionBlock":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   133
                if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   134
                    self.Interface.append(("VAR", False, False, []))
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   135
                if not self.IsAlreadyDefined(name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   136
                    self.Interface[-1][3].append((type, name))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
                    vars = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
                    for variable in instance.inputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
                        connections = variable.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
                        if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
                            parameter = variable.getFormalParameter()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
                            value = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
                            vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value)))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
                    self.Program += "  %s(%s);\n"%(name, ", ".join(vars))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
                connectionPoint = link.getPosition()[-1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
                for variable in instance.outputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
                    blockPointx, blockPointy = variable.connectionPointOut.getRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
                    if instance.getX() + blockPointx == connectionPoint.getX() and instance.getY() + blockPointy == connectionPoint.getY():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
                        return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter()))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
                raise ValueError, "No output variable found"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
    def GenerateLDPaths(self, instance, body):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
        paths = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   154
        variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   155
        connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   156
        for connection in connections:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   157
            localId = connection.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
            next = body.getContentInstance(localId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   159
            if isinstance(next, plcopen.leftPowerRail):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
                paths.append(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   162
                paths.append(self.GenerateLDPaths(next, body))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
        if isinstance(instance, plcopen.coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
            if len(paths) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
                return tuple(paths)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
                return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   169
            if len(paths) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
                return [variable, tuple(paths)]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
            elif type(paths[0]) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
                return [variable] + paths[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
            elif paths[0]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
                return [variable, paths[0]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
                return variable
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   178
    def GenerateSFCStep(self, step, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
        step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   180
        if step_name not in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   181
            if step.getInitialStep():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   182
                self.InitialSteps.append(step_name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
            step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
            if step.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
                instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
                connections = step.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
                if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
                    instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
                    instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
                    if isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   191
                        self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
                        instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
                    elif isinstance(instance, plcopen.selectionConvergence):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
                        for connectionPointIn in instance.getConnectionPointIn():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
                            divergence_connections = connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   196
                            if len(divergence_connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   197
                                transitionLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   198
                                transition = pou.body.getContentInstance(transitionLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   199
                                self.GenerateSFCTransition(transition, pou)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   200
                                instances.append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   201
                    elif isinstance(instance, plcopen.simultaneousDivergence):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   202
                        connectionPointIn = instance.getConnectionPointIn()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   203
                        if connectionPointIn:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   204
                            divergence_connections = connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   205
                            if len(divergence_connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   206
                                transitionLocalId = connections[0].getRefLocalId()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   207
                                transition = pou.body.getContentInstance(transitionLocalId)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   208
                                self.GenerateSFCTransition(transition, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   209
                                instances.append(transition)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   210
                for instance in instances:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   211
                    if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   212
                        self.SFCNetworks["Transitions"][instance]["to"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   213
            self.SFCNetworks["Steps"][step_name] = step_infos
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   214
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   215
    def GenerateSFCJump(self, jump, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   216
        jump_target = jump.getTargetName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   217
        if jump.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   218
            instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   219
            connections = jump.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   220
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   221
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   222
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
                if isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   224
                    self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
                    instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
                elif isinstance(instance, plcopen.selectionConvergence):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   227
                    for connectionPointIn in instance.getConnectionPointIn():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   228
                        divergence_connections = connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   229
                        if len(divergence_connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   230
                            transitionLocalId = divergence_connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   231
                            transition = pou.body.getContentInstance(transitionLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   232
                            self.GenerateSFCTransition(transition, pou)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   233
                            instances.append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   234
                elif isinstance(instance, plcopen.simultaneousDivergence):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   235
                    connectionPointIn = instance.getConnectionPointIn()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   236
                    if connectionPointIn:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   237
                        divergence_connections = connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   238
                        if len(divergence_connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   239
                            transitionLocalId = connections[0].getRefLocalId()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   240
                            transition = pou.body.getContentInstance(transitionLocalId)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   241
                            self.GenerateSFCTransition(transition, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   242
                            instances.append(transition)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   243
            for instance in instances:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   244
                if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   245
                    self.SFCNetworks["Transitions"][instance]["to"].append(jump_target)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   246
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   247
    def GenerateSFCStepActions(self, actionBlock, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
        connections = actionBlock.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
        if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
            stepLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   251
            step = pou.body.getContentInstance(stepLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   252
            self.GenerateSFCStep(step, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   253
            step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   254
            if step_name in self.SFCNetworks["Steps"].keys():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   255
                actions = actionBlock.getActions()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   256
                for action in actions:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   257
                    action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   258
                    if "duration" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   259
                        action_infos["duration"] = action["duration"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   260
                    if "indicator" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   261
                        action_infos["indicator"] = action["indicator"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   262
                    if action["type"] == "reference":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   263
                        self.GenerateSFCAction(action["value"], pou)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   264
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   265
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   266
    def GenerateSFCAction(self, action_name, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   267
        if action_name not in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   268
            actionContent = pou.getAction(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   269
            if actionContent:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   270
                actionType = actionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   271
                actionBody = actionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   272
                if actionType in ["ST", "IL"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   273
                    self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   274
                elif actionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   275
                    for instance in actionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   276
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   277
                            var = instance.getExpression()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   278
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   279
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   280
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   281
                                self.SFCNetworks["Actions"][action_name] = self.Program + "  %s := %s;\n"%(var, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   282
                                self.Program = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   283
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   284
                    for instance in actionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   285
                        if isinstance(instance, plcopen.coil):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   286
                            paths = self.GenerateLDPaths(instance, actionBody)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   287
                            variable = self.ExtractModifier(instance, instance.getVariable())
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   288
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   289
                            self.SFCNetworks["Actions"][action_name] = self.Program + "  %s := %s;\n"%(variable, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   290
                            self.Program = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   291
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   292
    def GenerateSFCTransition(self, transition, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   293
        if transition not in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   294
            steps = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   295
            connections = transition.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   296
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   297
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   298
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   299
                if isinstance(instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   300
                    self.GenerateSFCStep(instance, pou)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   301
                    steps.append(instance.getName())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   302
                elif isinstance(instance, plcopen.selectionDivergence):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   303
                    divergence_connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   304
                    if len(divergence_connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
                        stepLocalId = divergence_connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   306
                        divergence_instance = pou.body.getContentInstance(stepLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   307
                        if isinstance(divergence_instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   308
                            self.GenerateSFCStep(divergence_instance, pou)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   309
                            steps.append(divergence_instance.getName())
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   310
            transition_infos = {"from": [], "to" : []}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   311
            transitionValues = transition.getConditionContent()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   312
            if transitionValues["type"] == "inline":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   313
                transition_infos["content"] = "\n    := %s;\n"%transitionValues["value"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   314
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   315
                transitionContent = pou.getTransition(transitionValues["value"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   316
                transitionType = transitionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   317
                transitionBody = transitionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   318
                if transitionType == "IL":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   319
                    transition_infos["content"] = ":\n%s\n"%ReIndentText(transitionBody.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   320
                elif transitionType == "ST":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   321
                    transition_infos["content"] = "\n%s\n"%ReIndentText(transitionBody.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   322
                elif conditionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   323
                    for instance in transitionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   324
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   325
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   326
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   327
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   328
                                transition_infos["content"] = "\n    := %s;\n"%(var, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   329
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   330
                    for instance in transitionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   331
                        if isinstance(instance, plcopen.coil):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   332
                            paths = self.GenerateLDPaths(instance, conditionBody)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   333
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   334
                            transition_infos["content"] = "\n    := %s;\n"%expression
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   335
            for step_name in steps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   336
                if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   337
                    transition_infos["from"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   338
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   339
            self.SFCNetworks["Transitions"][transition] = transition_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   340
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   341
    def ComputeSFCStep(self, step_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   342
        if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   343
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   344
            if step_infos["initial"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   345
                self.Program += "  INITIAL_STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   346
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   347
                self.Program += "  STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   348
            actions = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   349
            for action_infos in step_infos["actions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   350
                actions.append(action_infos["content"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   351
                self.Program += "    %(content)s(%(qualifier)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   352
                if "duration" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   353
                    self.Program += ", %(duration)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   354
                if "indicator" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   355
                    self.Program += ", %(indicator)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   356
                self.Program += ");\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   357
            self.Program += "  END_STEP\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   358
            for action in actions:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   359
                self.ComputeSFCAction(action)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   360
            for transition in step_infos["transitions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   361
                self.ComputeSFCTransition(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   362
                
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   363
    def ComputeSFCAction(self, action_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   364
        if action_name in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   365
            action_content = self.SFCNetworks["Actions"].pop(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   366
            self.Program += "  ACTION %s:\n%s  END_ACTION\n\n"%(action_name, action_content)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   367
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   368
    def ComputeSFCTransition(self, transition):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   369
        if transition in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   370
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   371
            self.Program += "  TRANSITION FROM "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   372
            if len(transition_infos["from"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   373
                self.Program += "(%s)"%", ".join(transition_infos["from"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   374
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   375
                self.Program += "%s"%transition_infos["from"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   376
            self.Program += " TO "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   377
            if len(transition_infos["to"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   378
                self.Program += "(%s)"%", ".join(transition_infos["to"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   379
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   380
                self.Program += "%s"%transition_infos["to"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   381
            self.Program += transition_infos["content"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   382
            self.Program += "  END_TRANSITION\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   383
            for step_name in transition_infos["to"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   384
                self.ComputeSFCStep(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   385
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   386
    def ComputeLDExpression(self, paths, first = False):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   387
        if type(paths) == TupleType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   388
            if None in paths:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   389
                return "TRUE"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   390
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   391
                var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   392
                if first:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
                    return " OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   394
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   395
                    return "(%s)"%" OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   396
        elif type(paths) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   397
            var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   398
            return " AND ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   399
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   400
            return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   401
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   402
    def ExtractModifier(self, variable, text):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   403
        if variable.getNegated():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   404
            return "NOT(%s)"%text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   405
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   406
            edge = variable.getEdge()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   407
            if edge:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   408
                if edge.getValue() == "rising":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   409
                    return self.AddTrigger("R_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   410
                elif edge.getValue() == "falling":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   411
                    return self.AddTrigger("F_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   412
        return text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   413
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   414
    def AddTrigger(self, edge, text):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   415
        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   416
            self.Interface.append(("VAR", False, False, []))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   417
        i = 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   418
        name = "%s%d"%(edge, i)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   419
        while self.IsAlreadyDefined(name):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   420
            i += 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   421
            name = "%s%d"%(edge, i)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   422
        self.Interface[-1][3].append((edge, name))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   423
        self.Program += "  %s(CLK := %s);\n"%(name, text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   424
        return "%s.Q"%name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   425
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   426
    def GenerateSTProgram(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   427
        program = ""
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   428
        if self.ReturnType:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   429
            program += "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   430
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   431
            program += "%s %s\n"%(self.Type, self.Name)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   432
        for list_type, retain, constant, variables in self.Interface:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   433
            program += "  %s"%list_type
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   434
            if retain:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   435
                program += " RETAIN"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   436
            if constant:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   437
                program += " CONSTANT"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   438
            program += "\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   439
            for var_type, var_name in variables:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   440
                program += "    %s : %s;\n"%(var_name, var_type)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   441
            program += "  END_%s\n"%list_type
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   442
        program += "\n"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   443
        program += self.Program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   444
        program += "END_%s\n\n"%self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   445
        return program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   446
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
def GenerateCurrentProgram(project):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   448
    program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
    for pou in project.getPous():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   450
        pou_type = pou.getPouType().getValue()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   451
        if pou_type == "function":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   452
            pou_program = PouProgram(pou.getName(), "FUNCTION")
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   453
        elif pou_type == "functionBlock":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
            pou_program = PouProgram(pou.getName(), "FUNCTION_BLOCK")
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
        elif pou_type == "program":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   456
            pou_program = PouProgram(pou.getName(), "PROGRAM")
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   457
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
            raise ValueError, "Undefined pou type"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   459
        pou_program.GenerateInterface(pou.getInterface())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   460
        pou_program.GenerateProgram(pou)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   461
        program += pou_program.GenerateSTProgram()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   462
    return program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   463