PLCGenerator.py
author lbessard
Thu, 06 Dec 2007 18:05:29 +0100
changeset 125 394d9f168258
parent 108 9aa1fdfb7cb2
child 128 d16a8df4d322
permissions -rw-r--r--
Adding support for execution order in PLCGenerator
Adding support for derived data types (struct not supported yet)
Fixed refresh bug with windows on Viewers
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
#
58
39cd981ff242 Changing file headers
lbessard
parents: 47
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
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
5
f8652b073e84 GPL->LGPL
etisserant
parents: 4
diff changeset
    12
#modify it under the terms of the GNU General Public
0
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
58
39cd981ff242 Changing file headers
lbessard
parents: 47
diff changeset
    19
#General Public License for more details.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 4
diff changeset
    21
#You should have received a copy of the GNU General Public
0
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
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    33
pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    34
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    35
currentProject = None
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    36
currentProgram = ""
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    37
datatypeComputed = {}
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    38
pouComputed = {}
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    39
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    40
def ReIndentText(text, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    41
    compute = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    42
    lines = text.splitlines()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    43
    if len(lines) > 0:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    44
        spaces = 0
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    45
        while lines[0][spaces] == " ":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    46
            spaces += 1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    47
        indent = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    48
        for i in xrange(spaces, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    49
            indent += " "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    50
        for line in lines:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    51
            if line != "":
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    52
                compute += "%s%s\n"%(indent, line)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    53
            else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    54
                compute += "\n"
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    55
    return compute
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    56
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    57
def GenerateDataType(datatype_name):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    58
    if not datatypeComputed.get(datatype_name, True):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    59
        datatypeComputed[datatype_name] = True
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    60
        global currentProject, currentProgram
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    61
        datatype = currentProject.getDataType(datatype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    62
        datatype_def = "  %s :"%datatype.getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    63
        basetype_content = datatype.baseType.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    64
        if basetype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    65
            datatype_def += " %s"%basetype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    66
        elif basetype_content["name"] == "derived":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    67
            basetype_name = basetype_content["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    68
            GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    69
            datatype_def += " %s"%basetype_name
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    70
        elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    71
            base_type = basetype_content["value"].baseType.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    72
            if base_type["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    73
                basetype_name = base_type["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    74
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    75
                basetype_name = base_type["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    76
                GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    77
            min_value = basetype_content["value"].range.getLower()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    78
            max_value = basetype_content["value"].range.getUpper()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    79
            datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    80
        elif basetype_content["name"] == "enum":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    81
            values = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    82
            for value in basetype_content["value"].values.getValue():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    83
                values.append(value.getName())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    84
            datatype_def += " (%s)"%", ".join(values)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    85
        elif basetype_content["name"] == "array":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    86
            base_type = basetype_content["value"].baseType.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    87
            if base_type["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    88
                basetype_name = base_type["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    89
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    90
                basetype_name = base_type["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    91
                GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    92
            dimensions = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    93
            for dimension in basetype_content["value"].getDimension():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    94
                dimensions.append("0..%d"%(dimension.getUpper() - 1))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    95
            datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    96
        if datatype.initialValue is not None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    97
            datatype_def += " := %s"%str(datatype.initialValue.getValue())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    98
        currentProgram += "%s;\n"%datatype_def
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    99
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   100
def GeneratePouProgram(pou_name):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   101
    if not pouComputed.get(pou_name, True):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   102
        pouComputed[pou_name] = True
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   103
        global currentProject, currentProgram
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   104
        pou = currentProject.getPou(pou_name)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   105
        pou_type = pou.getPouType().getValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   106
        if pou_type in pouTypeNames:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   107
            pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   108
        else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   109
            raise ValueError, "Undefined pou type"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   110
        pou_program.GenerateInterface(pou.getInterface())
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   111
        pou_program.GenerateConnectionTypes(pou)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   112
        #print pou.getName(), pou_program.ConnectionTypes, pou_program.RelatedConnections
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   113
        pou_program.GenerateProgram(pou)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   114
        currentProgram += pou_program.GenerateSTProgram()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   115
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   116
def GenerateConfiguration(configuration):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   117
    config = "\nCONFIGURATION %s\n"%configuration.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   118
    for varlist in configuration.getGlobalVars():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   119
        config += "  VAR_GLOBAL"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   120
        if varlist.getRetain():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   121
            config += " RETAIN"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   122
        if varlist.getConstant():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   123
            config += " CONSTANT"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   124
        config += "\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   125
        for var in varlist.getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   126
            vartype_content = var.getType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   127
            if vartype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   128
                var_type = vartype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   129
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   130
                var_type = vartype_content["value"].getName()
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   131
            config += "    %s "%var.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   132
            address = var.getAddress()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   133
            if address:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   134
                config += "AT %s "%address
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   135
            config += ": %s"%var_type
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   136
            initial = var.getInitialValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   137
            if initial:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   138
                value = str(initial.getValue())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   139
                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   140
                if var_type == "STRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   141
                    config += " := '%s'"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   142
                elif var_type == "WSTRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   143
                    config += " := \"%s\""%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   144
                else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   145
                    config += " := %s"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   146
            config += ";\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   147
        config += "  END_VAR\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   148
    for resource in configuration.getResource():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   149
        config += GenerateResource(resource)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   150
    config += "END_CONFIGURATION\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   151
    return config
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   152
    
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   153
def GenerateResource(resource):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   154
    resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   155
    for varlist in resource.getGlobalVars():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   156
        resrce += "    VAR_GLOBAL"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   157
        if varlist.getRetain():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   158
            resrce += " RETAIN"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   159
        if varlist.getConstant():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   160
            resrce += " CONSTANT"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   161
        resrce += "\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   162
        for var in varlist.getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   163
            vartype_content = var.getType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   164
            if vartype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   165
                var_type = vartype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   166
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   167
                var_type = vartype_content["value"].getName()
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   168
            resrce += "      %s "%var.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   169
            address = var.getAddress()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   170
            if address:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   171
                resrce += "AT %s "%address
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   172
            resrce += ": %s"%var_type
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   173
            initial = var.getInitialValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   174
            if initial:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   175
                value = str(initial.getValue())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   176
                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   177
                if var_type == "STRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   178
                    resrce += " := '%s'"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   179
                elif var_type == "WSTRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   180
                    resrce += " := \"%s\""%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   181
                else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   182
                    resrce += " := %s"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   183
            resrce += ";\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   184
        resrce += "    END_VAR\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   185
    tasks = resource.getTask()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   186
    for task in tasks:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   187
        resrce += "    TASK %s("%task.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   188
        args = []
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   189
        single = task.getSingle()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   190
        if single:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   191
            args.append("SINGLE := %s"%single)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   192
        interval = task.getInterval()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   193
        if interval:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   194
            text = "t#"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   195
            if interval.hour != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   196
                text += "%dh"%interval.hour
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   197
            if interval.minute != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   198
                text += "%dm"%interval.minute
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   199
            if interval.second != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   200
                text += "%ds"%interval.second
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   201
            if interval.microsecond != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   202
                text += "%dms"%(interval.microsecond / 1000)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   203
            args.append("INTERVAL := %s"%text)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   204
        args.append("PRIORITY := %s"%str(task.priority.getValue()))
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   205
        resrce += ",".join(args) + ");\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   206
    for task in tasks:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   207
        for instance in task.getPouInstance():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   208
            resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   209
    for instance in resource.getPouInstance():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   210
        resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   211
    resrce += "  END_RESOURCE\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   212
    return resrce
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   213
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
Module implementing methods for generating PLC programs in ST or IL
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   216
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   217
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   218
class PouProgram:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   219
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   220
    def __init__(self, name, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   221
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   222
        self.Type = type
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   223
        self.ReturnType = None
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   224
        self.Interface = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   225
        self.InitialSteps = []
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   226
        self.ComputedBlocks = {}
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   227
        self.ComputedConnectors = {}
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   228
        self.ConnectionTypes = {}
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   229
        self.RelatedConnections = []
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   230
        self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   231
        self.SFCComputedBlocks = ""
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   232
        self.ActionNumber = 0
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   233
        self.Program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   234
    
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   235
    def GetActionNumber(self):
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   236
        self.ActionNumber += 1
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   237
        return self.ActionNumber
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   238
    
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   239
    def IsAlreadyDefined(self, name):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   240
        for list_type, retain, constant, located, vars in self.Interface:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   241
            for var_type, var_name, var_address, var_initial in vars:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   242
                if name == var_name:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   243
                    return True
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   244
        return False
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   245
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   246
    def GetVariableType(self, name):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   247
        for list_type, retain, constant, located, vars in self.Interface:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   248
            for var_type, var_name, var_address, var_initial in vars:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   249
                if name == var_name:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   250
                    return var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   251
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   252
    
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   253
    def GetConnectedConnection(self, connection, body):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   254
        links = connection.getConnections()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   255
        if links and len(links) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   256
            return self.GetLinkedConnection(links[0], body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   257
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   258
        
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   259
    def GetLinkedConnection(self, link, body):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   260
        parameter = link.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   261
        instance = body.getContentInstance(link.getRefLocalId())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   262
        if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable, plcopen.continuation, plcopen.contact, plcopen.coil)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   263
            return instance.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   264
        elif isinstance(instance, plcopen.block):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   265
            outputvariables = instance.outputVariables.getVariable()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   266
            if len(outputvariables) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   267
                return outputvariables[0].connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   268
            elif parameter:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   269
                for variable in outputvariables:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   270
                    if variable.getFormalParameter() == parameter:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   271
                        return variable.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   272
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   273
                point = link.getPosition()[-1]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   274
                for variable in outputvariables:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   275
                    relposition = variable.connectionPointOut.getRelPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   276
                    blockposition = instance.getPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   277
                    if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   278
                        return variable.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   279
        elif isinstance(instance, plcopen.leftPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   280
            outputconnections = instance.getConnectionPointOut()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   281
            if len(outputconnections) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   282
                return outputconnections[0]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   283
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   284
                point = link.getPosition()[-1]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   285
                for outputconnection in outputconnections:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   286
                    relposition = outputconnection.getRelPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   287
                    powerrailposition = instance.getPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   288
                    if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   289
                        return outputconnection
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   290
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   291
        
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   292
    def ExtractRelatedConnections(self, connection):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   293
        for i, related in enumerate(self.RelatedConnections):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   294
            if connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   295
                return self.RelatedConnections.pop(i)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   296
        return [connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   297
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   298
    def GenerateInterface(self, interface):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   299
        if self.Type == "FUNCTION":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   300
            returntype_content = interface.getReturnType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   301
            if returntype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   302
                self.ReturnType = returntype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   303
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   304
                self.ReturnType = returntype_content["value"].getName()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
        for varlist in interface.getContent():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   306
            variables = []
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   307
            located = False
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   308
            for var in varlist["value"].getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   309
                vartype_content = var.getType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   310
                if vartype_content["value"] is None:
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   311
                    initial = var.getInitialValue()
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   312
                    if initial:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   313
                        initial_value = initial.getValue()
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   314
                    else:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   315
                        initial_value = None
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   316
                    address = var.getAddress()
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   317
                    if address:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   318
                        located = True
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   319
                    variables.append((vartype_content["name"], var.getName(), address, initial_value))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   320
                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   321
                    var_type = vartype_content["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   322
                    GeneratePouProgram(var_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   323
                    blocktype = GetBlockType(var_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   324
                    if blocktype is not None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   325
                        variables.extend(blocktype["initialise"](var_type, var.getName()))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   326
                        located = False
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   327
                    else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   328
                        initial = var.getInitialValue()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   329
                        if initial:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   330
                            initial_value = initial.getValue()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   331
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   332
                            initial_value = None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   333
                        address = var.getAddress()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   334
                        if address:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   335
                            located = True
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   336
                        variables.append((vartype_content["value"].getName(), var.getName(), address, initial_value))
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   337
            if len(variables) > 0:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   338
                self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   339
                            varlist["value"].getConstant(), located, variables))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   340
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   341
    def GenerateConnectionTypes(self, pou):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   342
        body = pou.getBody()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   343
        body_content = body.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   344
        body_type = body_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   345
        if body_type in ["FBD", "LD", "SFC"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   346
            for instance in body.getContentInstances():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   347
                if isinstance(instance, (plcopen.inVariable, plcopen.outVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   348
                    expression = instance.getExpression()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   349
                    var_type = self.GetVariableType(expression)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   350
                    if expression == pou.getName():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   351
                        returntype_content = pou.interface.getReturnType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   352
                        if returntype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   353
                            var_type = returntype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   354
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   355
                            var_type = returntype_content["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   356
                    elif var_type is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   357
                        var_type = expression.split("#")[0]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   358
                    if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   359
                        self.ConnectionTypes[instance.connectionPointOut] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   360
                    if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   361
                        self.ConnectionTypes[instance.connectionPointIn] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   362
                        connected = self.GetConnectedConnection(instance.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   363
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   364
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   365
                                self.ConnectionTypes[connection] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   366
                elif isinstance(instance, (plcopen.contact, plcopen.coil)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   367
                    self.ConnectionTypes[instance.connectionPointOut] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   368
                    self.ConnectionTypes[instance.connectionPointIn] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   369
                    connected = self.GetConnectedConnection(instance.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   370
                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   371
                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   372
                            self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   373
                elif isinstance(instance, plcopen.leftPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   374
                    for connection in instance.getConnectionPointOut():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   375
                        self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   376
                elif isinstance(instance, plcopen.rightPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   377
                    for connection in instance.getConnectionPointIn():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   378
                        self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   379
                        connected = self.GetConnectedConnection(connection, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   380
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   381
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   382
                                self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   383
                elif isinstance(instance, plcopen.transition):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   384
                    content = instance.condition.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   385
                    if content["name"] == "connection" and len(content["value"]) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   386
                        connected = self.GetLinkedConnection(content["value"][0], body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   387
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   388
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   389
                                self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   390
                elif isinstance(instance, plcopen.block):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   391
                    block_infos = GetBlockType(instance.getTypeName())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   392
                    undefined = {}
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   393
                    for variable in instance.outputVariables.getVariable():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   394
                        output_name = variable.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   395
                        for oname, otype, oqualifier in block_infos["outputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   396
                            if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   397
                                if otype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   398
                                    if otype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   399
                                        undefined[otype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   400
                                    undefined[otype].append(variable.connectionPointOut)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   401
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   402
                                    for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   403
                                        self.ConnectionTypes[connection] = otype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   404
                    for variable in instance.inputVariables.getVariable():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   405
                        input_name = variable.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   406
                        for iname, itype, iqualifier in block_infos["inputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   407
                            if input_name == iname:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   408
                                connected = self.GetConnectedConnection(variable.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   409
                                if itype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   410
                                    if itype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   411
                                        undefined[itype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   412
                                    undefined[itype].append(variable.connectionPointIn)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   413
                                    if connected:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   414
                                        undefined[itype].append(connected)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   415
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   416
                                    self.ConnectionTypes[variable.connectionPointIn] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   417
                                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   418
                                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   419
                                            self.ConnectionTypes[connection] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   420
                    for var_type, connections in undefined.items():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   421
                        related = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   422
                        for connection in connections:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   423
                            if connection in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   424
                                var_type = self.ConnectionTypes[connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   425
                            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   426
                                related.extend(self.ExtractRelatedConnections(connection))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   427
                        if var_type.startswith("ANY") and len(related) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   428
                            self.RelatedConnections.append(related)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   429
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   430
                            for connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   431
                                self.ConnectionTypes[connection] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   432
                                    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   433
    def GenerateProgram(self, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   434
        body = pou.getBody()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   435
        body_content = body.getContent()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   436
        body_type = body_content["name"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   437
        if body_type in ["IL","ST"]:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   438
            self.Program = ReIndentText(body_content["value"].getText(), 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   439
        elif body_type == "FBD":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   440
            orderedInstances = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   441
            otherInstances = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   442
            for instance in body.getContentInstances():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   443
                if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable, plcopen.block)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   444
                    executionOrderId = instance.getExecutionOrderId()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   445
                    if executionOrderId > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   446
                        orderedInstances.append((executionOrderId, instance))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   447
                    else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   448
                        otherInstances.append(instance)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   449
                elif isinstance(instance, plcopen.connector):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   450
                    otherInstances.append(instance)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   451
            orderedInstances.sort()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   452
            instances = [instance for (executionOrderId, instance) in orderedInstances] + otherInstances
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   453
            for instance in instances:
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   454
                if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
                    var = instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   456
                    connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   457
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
                        expression = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   459
                        self.Program += "  %s := %s;\n"%(var, expression)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   460
                elif isinstance(instance, plcopen.block):
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   461
                    block_type = instance.getTypeName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   462
                    self.GeneratePouProgram(block_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   463
                    block_infos = GetBlockType(block_type)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   464
                    block_infos["generate"](self, instance, body, None)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   465
                elif isinstance(instance, plcopen.connector):
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   466
                    connector = instance.getName()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   467
                    if self.ComputedConnectors.get(connector, None):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   468
                        continue 
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   469
                    connections = instance.connectionPointIn.getConnections()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   470
                    if connections and len(connections) == 1:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   471
                        self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   472
        elif body_type == "LD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   473
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   474
                if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   475
                    paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   476
                    if len(paths) > 0:
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   477
                        paths = tuple(paths)
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   478
                    else:
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   479
                        paths = paths[0] 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   480
                    variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   481
                    expression = self.ComputeLDExpression(paths, True)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   482
                    self.Program += "  %s := %s;\n"%(variable, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   483
        elif body_type == "SFC":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   484
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   485
                if isinstance(instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   486
                    self.GenerateSFCStep(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   487
                elif isinstance(instance, plcopen.actionBlock):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   488
                    self.GenerateSFCStepActions(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   489
                elif isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   490
                    self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   491
                elif isinstance(instance, plcopen.jumpStep):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   492
                    self.GenerateSFCJump(instance, pou)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   493
            if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "":
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   494
                action_name = "COMPUTE_FUNCTION_BLOCKS"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   495
                action_infos = {"qualifier" : "S", "content" : action_name}
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   496
                self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   497
                self.SFCNetworks["Actions"][action_name] = ReIndentText(self.SFCComputedBlocks, 4)
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   498
                self.Program = ""
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   499
            for initialstep in self.InitialSteps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   500
                self.ComputeSFCStep(initialstep)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   501
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   502
    def ComputeFBDExpression(self, body, link, order = False):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   503
        localid = link.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
        instance = body.getContentInstance(localid)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   505
        if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   506
            return instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   507
        elif isinstance(instance, plcopen.block):
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   508
            block_type = instance.getTypeName()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   509
            self.GeneratePouProgram(block_type)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   510
            block_infos = GetBlockType(block_type)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   511
            return block_infos["generate"](self, instance, body, link, order)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   512
        elif isinstance(instance, plcopen.continuation):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   513
            name = instance.getName()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   514
            computed_value = self.ComputedConnectors.get(name, None)
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   515
            if computed_value != None:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   516
                return computed_value
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   517
            for tmp_instance in body.getContentInstances():
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   518
                if isinstance(tmp_instance, plcopen.connector):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   519
                    if tmp_instance.getName() == name:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   520
                        connections = tmp_instance.connectionPointIn.getConnections()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   521
                        if connections and len(connections) == 1:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   522
                            expression = self.ComputeFBDExpression(body, connections[0], order)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   523
                            self.ComputedConnectors[name] = expression
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   524
                            return expression
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   525
            raise ValueError, "No connector found"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   526
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   527
    def GenerateLDPaths(self, connections, body):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   528
        paths = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   529
        for connection in connections:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   530
            localId = connection.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   531
            next = body.getContentInstance(localId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   532
            if isinstance(next, plcopen.leftPowerRail):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   533
                paths.append(None)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   534
            elif isinstance(next, plcopen.block):
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   535
                block_type = next.getTypeName()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   536
                self.GeneratePouProgram(block_type)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   537
                block_infos = GetBlockType(block_type)
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   538
                paths.append(block_infos["generate"](self, next, body, connection))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   539
            else:
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   540
                variable = self.ExtractModifier(next, next.getVariable())
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   541
                result = self.GenerateLDPaths(next.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   542
                if len(result) > 1:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   543
                    paths.append([variable, tuple(result)])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   544
                elif type(result[0]) == ListType:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   545
                    paths.append([variable] + result[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   546
                elif result[0]:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   547
                    paths.append([variable, result[0]])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   548
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   549
                    paths.append(variable)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   550
        return paths
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   551
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   552
    def GetNetworkType(self, connections, body):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   553
        network_type = "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   554
        for connection in connections:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   555
            localId = connection.getRefLocalId()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   556
            next = body.getContentInstance(localId)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   557
            if isinstance(next, plcopen.leftPowerRail) or isinstance(next, plcopen.contact):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   558
                return "LD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   559
            elif isinstance(next, plcopen.block):
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   560
                 for variable in next.inputVariables.getVariable():
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   561
                     result = self.GetNetworkType(variable.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   562
                     if result != "FBD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   563
                         return result
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   564
            elif isinstance(next, plcopen.inVariable):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   565
                return "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   566
            elif isinstance(next, plcopen.inOutVariable):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   567
                return self.GetNetworkType(next.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   568
            else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   569
                return None
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   570
        return "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   571
    
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   572
    def ExtractDivergenceInput(self, divergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   573
        connectionPointIn = divergence.getConnectionPointIn()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   574
        if connectionPointIn:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   575
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   576
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   577
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   578
                return pou.body.getContentInstance(instanceLocalId)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   579
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   580
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   581
    def ExtractConvergenceInputs(self, convergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   582
        instances = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   583
        for connectionPointIn in convergence.getConnectionPointIn():
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   584
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   585
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   586
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   587
                instances.append(pou.body.getContentInstance(instanceLocalId))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   588
        return instances
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   589
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   590
    def GenerateSFCStep(self, step, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   591
        step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   592
        if step_name not in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   593
            if step.getInitialStep():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   594
                self.InitialSteps.append(step_name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   595
            step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   596
            if step.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   597
                instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   598
                connections = step.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   599
                if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   600
                    instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   601
                    instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   602
                    if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
                        instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   604
                    elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   605
                        instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   606
                    elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   607
                        transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   608
                        if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   609
                            if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   610
                                instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   611
                            elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   612
                                instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   613
                for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   614
                    self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   615
                    if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   616
                        self.SFCNetworks["Transitions"][instance]["to"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   617
            self.SFCNetworks["Steps"][step_name] = step_infos
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   618
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   619
    def GenerateSFCJump(self, jump, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   620
        jump_target = jump.getTargetName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   621
        if jump.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   622
            instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   623
            connections = jump.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   624
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   625
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   626
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   627
                if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   628
                    instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   629
                elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   630
                    instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   631
                elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   632
                    transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   633
                    if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   634
                        if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   635
                            instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   636
                        elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   637
                            instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   638
            for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   639
                self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   640
                if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   641
                    self.SFCNetworks["Transitions"][instance]["to"].append(jump_target)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   642
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   643
    def GenerateSFCStepActions(self, actionBlock, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   644
        connections = actionBlock.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   645
        if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   646
            stepLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   647
            step = pou.body.getContentInstance(stepLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   648
            self.GenerateSFCStep(step, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   649
            step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   650
            if step_name in self.SFCNetworks["Steps"].keys():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   651
                actions = actionBlock.getActions()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   652
                for action in actions:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   653
                    action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   654
                    if "duration" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   655
                        action_infos["duration"] = action["duration"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   656
                    if "indicator" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   657
                        action_infos["indicator"] = action["indicator"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   658
                    if action["type"] == "reference":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   659
                        self.GenerateSFCAction(action["value"], pou)
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   660
                    else:
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   661
                        action_name = "INLINE%d"%self.GetActionNumber()
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   662
                        self.SFCNetworks["Actions"][action_name] = "    %s\n"%action["value"]
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   663
                        action_infos["content"] = action_name
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   664
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   665
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   666
    def GenerateSFCAction(self, action_name, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   667
        if action_name not in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   668
            actionContent = pou.getAction(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   669
            if actionContent:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   670
                actionType = actionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   671
                actionBody = actionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   672
                if actionType in ["ST", "IL"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   673
                    self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   674
                elif actionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   675
                    for instance in actionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   676
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   677
                            var = instance.getExpression()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   678
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   679
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   680
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   681
                                action_content = self.Program + "  %s := %s;\n"%(var, expression)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   682
                                self.Program = ""
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   683
                                self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   684
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   685
                    for instance in actionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   686
                        if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   687
                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   688
                            variable = self.ExtractModifier(instance, instance.getVariable())
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   689
                            expression = self.ComputeLDExpression(paths, True)
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   690
                            action_content = self.Program + "  %s := %s;\n"%(variable, expression)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   691
                            self.Program = ""
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   692
                            self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   693
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   694
    def GenerateSFCTransition(self, transition, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   695
        if transition not in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   696
            steps = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   697
            connections = transition.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   698
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   699
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   700
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   701
                if isinstance(instance, plcopen.step):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   702
                    steps.append(instance)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   703
                elif isinstance(instance, plcopen.selectionDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   704
                    step = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   705
                    if step:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   706
                        if isinstance(step, plcopen.step):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   707
                            steps.append(step)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   708
                        elif isinstance(step, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   709
                            steps.extend(self.ExtractConvergenceInputs(step, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   710
                elif isinstance(instance, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   711
                    steps.extend(self.ExtractConvergenceInputs(instance, pou))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   712
            transition_infos = {"priority": transition.getPriority(), "from": [], "to" : []}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   713
            transitionValues = transition.getConditionContent()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   714
            if transitionValues["type"] == "inline":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   715
                transition_infos["content"] = "\n    := %s;\n"%transitionValues["value"]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   716
            elif transitionValues["type"] == "reference":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   717
                transitionContent = pou.getTransition(transitionValues["value"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   718
                transitionType = transitionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   719
                transitionBody = transitionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   720
                if transitionType == "IL":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   721
                    transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   722
                elif transitionType == "ST":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   723
                    transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4)
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   724
                elif transitionType == "FBD":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   725
                    for instance in transitionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   726
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   727
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   728
                            if connections and len(connections) == 1:
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   729
                                expression = self.ComputeFBDExpression(transitionBody, connections[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   730
                                transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   731
                                self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   732
                                self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   733
                elif transitionType == "LD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   734
                    for instance in transitionBody.getContentInstances():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   735
                        if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   736
                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   737
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   738
                            transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   739
                            self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   740
                            self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   741
            elif transitionValues["type"] == "connection":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   742
                body = pou.getBody()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   743
                connections = transition.getConnections()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   744
                network_type = self.GetNetworkType(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   745
                if network_type == None:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   746
                    raise Exception
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   747
                if len(connections) > 1 or network_type == "LD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   748
                    paths = self.GenerateLDPaths(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   749
                    expression = self.ComputeLDExpression(paths, True)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   750
                    transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   751
                    self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   752
                    self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   753
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   754
                    expression = self.ComputeFBDExpression(body, connections[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   755
                    transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   756
                    self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   757
                    self.Program = ""
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   758
            for step in steps:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   759
                self.GenerateSFCStep(step, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   760
                step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   761
                if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   762
                    transition_infos["from"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   763
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   764
            self.SFCNetworks["Transitions"][transition] = transition_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   765
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   766
    def ComputeSFCStep(self, step_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   767
        if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   768
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   769
            if step_infos["initial"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   770
                self.Program += "  INITIAL_STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   771
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   772
                self.Program += "  STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   773
            actions = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   774
            for action_infos in step_infos["actions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   775
                actions.append(action_infos["content"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   776
                self.Program += "    %(content)s(%(qualifier)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   777
                if "duration" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   778
                    self.Program += ", %(duration)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   779
                if "indicator" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   780
                    self.Program += ", %(indicator)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   781
                self.Program += ");\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   782
            self.Program += "  END_STEP\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   783
            for action in actions:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   784
                self.ComputeSFCAction(action)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   785
            for transition in step_infos["transitions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   786
                self.ComputeSFCTransition(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   787
                
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   788
    def ComputeSFCAction(self, action_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   789
        if action_name in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   790
            action_content = self.SFCNetworks["Actions"].pop(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   791
            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
   792
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   793
    def ComputeSFCTransition(self, transition):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   794
        if transition in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   795
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   796
            self.Program += "  TRANSITION"
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   797
            if transition_infos["priority"] != None:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   798
                self.Program += " (PRIORITY := %d)"%transition_infos["priority"]
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   799
            self.Program += " FROM "
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   800
            if len(transition_infos["from"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   801
                self.Program += "(%s)"%", ".join(transition_infos["from"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   802
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   803
                self.Program += "%s"%transition_infos["from"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   804
            self.Program += " TO "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   805
            if len(transition_infos["to"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   806
                self.Program += "(%s)"%", ".join(transition_infos["to"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   807
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   808
                self.Program += "%s"%transition_infos["to"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   809
            self.Program += transition_infos["content"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   810
            self.Program += "  END_TRANSITION\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   811
            for step_name in transition_infos["to"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   812
                self.ComputeSFCStep(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   813
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   814
    def ComputeLDExpression(self, paths, first = False):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   815
        if type(paths) == TupleType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   816
            if None in paths:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   817
                return "TRUE"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   818
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   819
                var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   820
                if first:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   821
                    return " OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   822
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   823
                    return "(%s)"%" OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   824
        elif type(paths) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   825
            var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   826
            return " AND ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   827
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   828
            return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   829
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   830
    def ExtractModifier(self, variable, text):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   831
        if variable.getNegated():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   832
            return "NOT(%s)"%text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   833
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   834
            edge = variable.getEdge()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   835
            if edge:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   836
                if edge.getValue() == "rising":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   837
                    return self.AddTrigger("R_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   838
                elif edge.getValue() == "falling":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   839
                    return self.AddTrigger("F_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   840
        return text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   841
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   842
    def AddTrigger(self, edge, text):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   843
        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   844
            self.Interface.append(("VAR", False, False, False, []))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   845
        i = 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   846
        name = "%s%d"%(edge, i)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   847
        while self.IsAlreadyDefined(name):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   848
            i += 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   849
            name = "%s%d"%(edge, i)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   850
        self.Interface[-1][4].append((edge, name, None, None))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   851
        self.Program += "  %s(CLK := %s);\n"%(name, text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   852
        return "%s.Q"%name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   853
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   854
    def GenerateSTProgram(self):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   855
        if self.ReturnType:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   856
            program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   857
        else:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   858
            program = "%s %s\n"%(self.Type, self.Name)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   859
        for list_type, retain, constant, located, variables in self.Interface:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   860
            program += "  %s"%list_type
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   861
            if retain:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   862
                program += " RETAIN"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   863
            if constant:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   864
                program += " CONSTANT"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   865
            program += "\n"
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   866
            for var_type, var_name, var_address, var_initial in variables:
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   867
                program += "    "
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   868
                if var_name:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   869
                    program += "%s "%var_name
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   870
                if var_address != None:
31
d833bf7567b1 Bug on variable location fixed
lbessard
parents: 30
diff changeset
   871
                    program += "AT %s "%var_address
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   872
                program += ": %s"%var_type
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
   873
                if var_initial != None:
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   874
                    value = {"TRUE":"0","FALSE":"1"}.get(str(var_initial).upper(), str(var_initial))
37
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   875
                    if var_type == "STRING":
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   876
                        program += " := '%s'"%value
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   877
                    elif var_type == "WSTRING":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   878
                        program += " := \"%s\""%value
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   879
                    else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   880
                        program += " := %s"%value
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   881
                program += ";\n"
4
2de7fd952fdd Adding File Dialog for choosing path to generated program
lbessard
parents: 2
diff changeset
   882
            program += "  END_VAR\n"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   883
        program += "\n"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   884
        program += self.Program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   885
        program += "END_%s\n\n"%self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   886
        return program
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   887
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   888
    def GeneratePouProgram(self, pou_name):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   889
        GeneratePouProgram(pou_name)
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   890
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   891
def GenerateCurrentProgram(project):
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   892
    global currentProject, currentProgram
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   893
    currentProject = project
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   894
    currentProgram = ""
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   895
    for datatype in project.getDataTypes():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   896
        datatypeComputed[datatype.getName()] = False
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   897
    for pou in project.getPous():
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   898
        pouComputed[pou.getName()] = False
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   899
    if len(datatypeComputed) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   900
        currentProgram += "TYPE\n"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   901
        for datatype_name in datatypeComputed.keys():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   902
            GenerateDataType(datatype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   903
        currentProgram += "END_TYPE\n\n"
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   904
    for pou_name in pouComputed.keys():
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   905
        GeneratePouProgram(pou_name)
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   906
    for config in project.getConfigurations():
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   907
        currentProgram += GenerateConfiguration(config)
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   908
    return currentProgram
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   909