PLCGenerator.py
author etisserant
Tue, 19 Aug 2008 18:06:41 +0200
changeset 233 de7ddb376150
parent 232 cbdfef9b2020
child 242 5b3e1c4569e6
permissions -rw-r--r--
Now generated global variable declaration is done in one separate block. Also "fixed" (masked?) problem when deleting blocks with invalid type
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
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    29
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    30
# Dictionary associating PLCOpen variable categories to the corresponding 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    31
# IEC 61131-3 variable categories
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
varTypeNames = {"localVars" : "VAR", "tempVars" : "VAR_TEMP", "inputVars" : "VAR_INPUT", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
                "outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
                "globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    35
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    36
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    37
# Dictionary associating PLCOpen POU categories to the corresponding 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    38
# IEC 61131-3 POU categories
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    39
pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    40
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    41
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    42
# Helper function for reindenting text
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    43
def ReIndentText(text, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    44
    compute = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    45
    lines = text.splitlines()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    46
    if len(lines) > 0:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    47
        line_num = 0
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    48
        while line_num < len(lines) and len(lines[line_num].strip()) == 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    49
            line_num += 1
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    50
        if line_num < len(lines):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    51
            spaces = 0
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    52
            while lines[line_num][spaces] == " ":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    53
                spaces += 1
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    54
            indent = ""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    55
            for i in xrange(spaces, nb_spaces):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    56
                indent += " "
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    57
            for line in lines:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    58
                if line != "":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    59
                    compute += "%s%s\n"%(indent, line)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    60
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
    61
                    compute += "\n"
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    62
    return compute
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    63
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    64
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    65
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    66
#                  Specific exception for PLC generating errors
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    67
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    68
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    69
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    70
class PLCGenException(Exception):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    71
    pass
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    72
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    73
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    74
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    75
#                           Generator of PLC program
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    76
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    77
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    78
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    79
class ProgramGenerator:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    80
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    81
    # Create a new PCL program generator
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    82
    def __init__(self, controler, project):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    83
        # Keep reference of the controler and project
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    84
        self.Controler = controler
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    85
        self.Project = project
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    86
        # Reset the internal variables used to generate PLC programs
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    87
        self.Program = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    88
        self.DatatypeComputed = {}
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    89
        self.PouComputed = {}
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    90
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    91
    # Compute value according to type given
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    92
    def ComputeValue(self, value, var_type):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    93
        base_type = self.Controler.GetBaseType(var_type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    94
        if base_type == "STRING":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    95
            return "'%s'"%value
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    96
        elif base_type == "WSTRING":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    97
            return "\"%s\""%value
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    98
        return value
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
    99
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   100
    # Generate a data type from its name
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   101
    def GenerateDataType(self, datatype_name):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   102
        # Verify that data type hasn't been generated yet
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   103
        if not self.DatatypeComputed.get(datatype_name, True):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   104
            # If not mark data type as computed
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   105
            self.DatatypeComputed[datatype_name] = True
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   106
            
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   107
            # Getting datatype model from project
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   108
            datatype = self.Project.getdataType(datatype_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   109
            tagname = self.Controler.ComputeDataTypeName(datatype.getname())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   110
            datatype_def = [("  ", ()), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   111
                            (datatype.getname(), (tagname, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   112
                            (" : ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   113
            basetype_content = datatype.baseType.getcontent()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   114
            # Data type derived directly from a string type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   115
            if basetype_content["name"] in ["string", "wstring"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   116
                datatype_def += [(basetype_content["name"].upper(), (tagname, "base"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   117
            # Data type derived directly from a user defined type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   118
            elif basetype_content["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   119
                basetype_name = basetype_content["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   120
                self.GenerateDataType(basetype_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   121
                datatype_def += [(basetype_name, (tagname, "base"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   122
            # Data type is a subrange
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   123
            elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   124
                base_type = basetype_content["value"].baseType.getcontent()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   125
                # Subrange derived directly from a user defined type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   126
                if base_type["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   127
                    basetype_name = base_type["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   128
                    self.GenerateDataType(basetype_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   129
                # Subrange derived directly from an elementary type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   130
                else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   131
                    basetype_name = base_type["name"]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   132
                min_value = basetype_content["value"].range.getlower()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   133
                max_value = basetype_content["value"].range.getupper()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   134
                datatype_def += [(basetype_name, (tagname, "base")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   135
                                 (" (", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   136
                                 ("%d"%min_value, (tagname, "lower")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   137
                                 ("..", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   138
                                 ("%d"%max_value, (tagname, "upper")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   139
                                 (")",())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   140
            # Data type is an enumerated type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   141
            elif basetype_content["name"] == "enum":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   142
                values = [[(value.getname(), (tagname, "value", i))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   143
                          for i, value in enumerate(basetype_content["value"].values.getvalue())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   144
                datatype_def += [("(", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   145
                datatype_def += JoinList([(", ", ())], values)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   146
                datatype_def += [(")", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   147
            # Data type is an array
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   148
            elif basetype_content["name"] == "array":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   149
                base_type = basetype_content["value"].baseType.getcontent()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   150
                # Array derived directly from a user defined type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   151
                if base_type["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   152
                    basetype_name = base_type["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   153
                    self.GenerateDataType(basetype_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   154
                # Array derived directly from a string type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   155
                elif base_type["name"] in ["string", "wstring"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   156
                    basetype_name = base_type["name"].upper()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   157
                # Array derived directly from an elementary type 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   158
                else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   159
                    basetype_name = base_type["name"]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   160
                dimensions = [[("%d"%dimension.getlower(), (tagname, "range", i, "lower")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   161
                               ("..", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   162
                               ("%d"%dimension.getupper(), (tagname, "range", i, "upper"))] 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   163
                              for i, dimension in enumerate(basetype_content["value"].getdimension())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   164
                datatype_def += [("ARRAY [", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   165
                datatype_def += JoinList([(",", ())], dimensions)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   166
                datatype_def += [("] OF " , ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   167
                                 (basetype_name, (tagname, "base"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   168
            # Data type derived directly from a elementary type 
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   169
            else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   170
                datatype_def += [(basetype_content["name"], (tagname, "base"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   171
            # Data type has an initial value
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   172
            if datatype.initialValue is not None:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   173
                datatype_def += [(" := ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   174
                                 (self.ComputeValue(datatype.initialValue.getvalue(), datatype_name), (tagname, "initial"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   175
            datatype_def += [(";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   176
            return datatype_def
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   177
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   178
    # Generate a POU from its name
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   179
    def GeneratePouProgram(self, pou_name):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   180
        # Verify that POU hasn't been generated yet
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   181
        if not self.PouComputed.get(pou_name, True):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   182
            # If not mark POU as computed
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   183
            self.PouComputed[pou_name] = True
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   184
            
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   185
            # Getting POU model from project
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   186
            pou = self.Project.getpou(pou_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   187
            pou_type = pou.getpouType()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   188
            # Verify that POU type exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   189
            if pou_type in pouTypeNames:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   190
                # Create a POU program generator
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   191
                pou_program = PouProgramGenerator(self, pou.getname(), pouTypeNames[pou_type])
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   192
                program = pou_program.GenerateProgram(pou)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   193
                self.Program += program
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   194
            else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   195
                raise PLCGenException, "Undefined pou type \"%s\""%pou_type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   196
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   197
    # Generate a configuration from its model
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   198
    def GenerateConfiguration(self, configuration):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   199
        tagname = self.Controler.ComputeConfigurationName(configuration.getname())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   200
        config = [("\nCONFIGURATION ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   201
                  (configuration.getname(), (tagname, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   202
                  ("\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   203
        var_number = 0
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   204
        # Generate any global variable in configuration
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   205
        for varlist in configuration.getglobalVars():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   206
            # Generate variable block with modifier
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   207
            config += [("  VAR_GLOBAL", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   208
            if varlist.getretain():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   209
                config += [(" RETAIN", (tagname, "variable", (var_number, var_number + len(varlist.getvariable())), "retain"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   210
            if varlist.getconstant():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   211
                config += [(" CONSTANT", (tagname, "variable", (var_number, var_number + len(varlist.getvariable())), "constant"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   212
            config += [("\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   213
            # Generate any variable of this block
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   214
            for var in varlist.getvariable():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   215
                vartype_content = var.gettype().getcontent()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   216
                # Variable type is a user data type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   217
                if vartype_content["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   218
                    var_type = vartype_content["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   219
                # Variable type is a string type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   220
                elif vartype_content["name"] in ["string", "wstring"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   221
                    var_type = vartype_content["name"].upper()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   222
                # Variable type is an elementary type
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   223
                else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   224
                    var_type = vartype_content["name"]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   225
                config += [("    ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   226
                           (var.getname(), (tagname, "variable", var_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   227
                           (" ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   228
                # Generate variable address if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   229
                address = var.getaddress()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   230
                if address:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   231
                    config += [("AT ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   232
                               (address, (tagname, "variable", var_number, "address")),
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   233
                               (" ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   234
                config += [(": ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   235
                           (var_type, (tagname, "variable", var_number, "type"))]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   236
                # Generate variable initial value if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   237
                initial = var.getinitialValue()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   238
                if initial:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   239
                    config += [(" := ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   240
                               (self.ComputeValue(initial.getvalue(), var_type), (tagname, "variable", var_number, "initial"))]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   241
                config += [(";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   242
                var_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   243
            config += [("  END_VAR\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   244
        # Generate any resource in the configuration
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   245
        for resource in configuration.getresource():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   246
            config += self.GenerateResource(resource, configuration.getname())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   247
        config += [("END_CONFIGURATION\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   248
        return config
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   249
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   250
    # Generate a resource from its model
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   251
    def GenerateResource(self, resource, config_name):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   252
        tagname = self.Controler.ComputeConfigurationResourceName(config_name, resource.getname())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   253
        resrce = [("\n  RESOURCE ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   254
                  (resource.getname(), (tagname, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   255
                  (" ON PLC\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   256
        var_number = 0
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   257
        # Generate any global variable in configuration
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   258
        for varlist in resource.getglobalVars():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   259
            # Generate variable block with modifier
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   260
            resrce += [("    VAR_GLOBAL", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   261
            if varlist.getretain():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   262
                resrce += [(" RETAIN", (tagname, "variable", (var_number, var_number + len(varlist.getvariable())), "retain"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   263
            if varlist.getconstant():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   264
                resrce += [(" CONSTANT", (tagname, "variable", (var_number, var_number + len(varlist.getvariable())), "constant"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   265
            resrce += "\n"
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   266
            # Generate any variable of this block
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   267
            for var in varlist.getvariable():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   268
                vartype_content = var.gettype().getcontent()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   269
                # Variable type is a user data type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   270
                if vartype_content["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   271
                    var_type = vartype_content["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   272
                # Variable type is a string type
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   273
                elif vartype_content["name"] in ["string", "wstring"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   274
                    var_type = vartype_content["name"].upper()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   275
                # Variable type is an elementary type
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   276
                else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   277
                    var_type = vartype_content["name"]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   278
                resrce += [("      ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   279
                           (var.getname(), (tagname, "variable", var_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   280
                           (" ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   281
                address = var.getaddress()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   282
                # Generate variable address if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   283
                if address:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   284
                    resrce += [("AT ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   285
                               (address, (tagname, "variable", var_number, "address")),
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   286
                               (" ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   287
                resrce += [(": ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   288
                           (var_type, (tagname, "variable", var_number, "type"))]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   289
                # Generate variable initial value if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   290
                initial = var.getinitialValue()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   291
                if initial:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   292
                    resrce += [(" := ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
   293
                               (self.ComputeValue(initial.getvalue(), var_type), (tagname, "variable", var_number, "initial"))]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   294
                resrce += [(";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   295
                var_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   296
            resrce += [("    END_VAR\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   297
        # Generate any task in the resource
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   298
        tasks = resource.gettask()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   299
        task_number = 0
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   300
        for task in tasks:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   301
            # Task declaration
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   302
            resrce += [("    TASK ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   303
                       (task.getname(), (tagname, "task", task_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   304
                       ("(", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   305
            args = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   306
            single = task.getsingle()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   307
            # Single argument if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   308
            if single:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   309
                resrce += [("SINGLE := ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   310
                           (single, (tagname, "task", task_number, "single")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   311
                           (",", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   312
            # Interval argument if exists
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   313
            interval = task.getinterval()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   314
            if interval:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   315
                resrce += [("INTERVAL := t#", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   316
                if interval.hour != 0:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   317
                    resrce += [("%dh"%interval.hour, (tagname, "task", task_number, "interval", "hour"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   318
                if interval.minute != 0:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   319
                    resrce += [("%dm"%interval.minute, (tagname, "task", task_number, "interval", "minute"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   320
                if interval.second != 0:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   321
                    resrce += [("%ds"%interval.second, (tagname, "task", task_number, "interval", "second"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   322
                if interval.microsecond != 0:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   323
                    resrce += [("%dms"%(interval.microsecond / 1000), (tagname, "task", task_number, "interval", "millisecond"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   324
                resrce += [(",", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   325
            # Priority argument
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   326
            resrce += [("PRIORITY := ", ()), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   327
                       ("%d"%task.getpriority(), (tagname, "task", task_number, "priority")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   328
                       (");\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   329
            task_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   330
        instance_number = 0
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   331
        # Generate any program assign to each task
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   332
        for task in tasks:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   333
            for instance in task.getpouInstance():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   334
                resrce += [("    PROGRAM ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   335
                           (instance.getname(), (tagname, "instance", instance_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   336
                           (" WITH ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   337
                           (task.getname(), (tagname, "instance", instance_number, "task")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   338
                           (" : ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   339
                           (instance.gettype(), (tagname, "instance", instance_number, "type")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   340
                           (";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   341
                instance_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   342
        # Generate any program assign to no task
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   343
        for instance in resource.getpouInstance():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   344
            resrce += [("    PROGRAM ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   345
                           (instance.getname(), (tagname, "instance", instance_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   346
                           (" : ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   347
                           (instance.gettype(), (tagname, "instance", instance_number, "type")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   348
                           (";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   349
            instance_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   350
        resrce += [("  END_RESOURCE\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   351
        return resrce
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   352
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   353
    # Generate the entire program for current project
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   354
    def GenerateProgram(self):        
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   355
        # Find all data types defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   356
        for datatype in self.Project.getdataTypes():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   357
            self.DatatypeComputed[datatype.getname()] = False
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   358
        # Find all data types defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   359
        for pou in self.Project.getpous():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   360
            self.PouComputed[pou.getname()] = False
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   361
        # Generate data type declaration structure if there is at least one data 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   362
        # type defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   363
        if len(self.DatatypeComputed) > 0:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   364
            self.Program += [("TYPE\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   365
            # Generate every data types defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   366
            for datatype_name in self.DatatypeComputed.keys():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   367
                self.Program += self.GenerateDataType(datatype_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   368
            self.Program += [("END_TYPE\n\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   369
        # Generate every POUs defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   370
        for pou_name in self.PouComputed.keys():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   371
            self.GeneratePouProgram(pou_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   372
        # Generate every configurations defined
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   373
        for config in self.Project.getconfigurations():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   374
            self.Program += self.GenerateConfiguration(config)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   375
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   376
    # Return generated program
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   377
    def GetGeneratedProgram(self):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   378
        return self.Program
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   379
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   380
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   381
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   382
#                           Generator of POU programs
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   383
#-------------------------------------------------------------------------------
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   384
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   385
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   386
class PouProgramGenerator:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   387
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   388
    # Create a new POU program generator
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   389
    def __init__(self, parent, name, type):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   390
        # Keep Reference to the parent generator
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   391
        self.ParentGenerator = parent
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   392
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
        self.Type = type
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   394
        self.TagName = self.ParentGenerator.Controler.ComputePouName(name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   395
        self.CurrentIndent = "  "
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   396
        self.ReturnType = None
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   397
        self.Interface = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   398
        self.InitialSteps = []
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   399
        self.ComputedBlocks = {}
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   400
        self.ComputedConnectors = {}
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   401
        self.ConnectionTypes = {}
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   402
        self.RelatedConnections = []
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   403
        self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   404
        self.SFCComputedBlocks = []
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   405
        self.ActionNumber = 0
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   406
        self.Program = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   407
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   408
    def GetBlockType(self, type):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   409
        return self.ParentGenerator.Controler.GetBlockType(type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   410
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   411
    def IndentLeft(self):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   412
        if len(self.CurrentIndent) >= 2:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   413
            self.CurrentIndent = self.CurrentIndent[:-2]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   414
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   415
    def IndentRight(self):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   416
        self.CurrentIndent += "  "
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   417
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   418
    # Generator of unique ID for inline actions
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   419
    def GetActionNumber(self):
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   420
        self.ActionNumber += 1
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   421
        return self.ActionNumber
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   422
    
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   423
    # Test if a variable has already been defined
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   424
    def IsAlreadyDefined(self, name):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   425
        for list_type, retain, constant, located, vars in self.Interface:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   426
            for var_type, var_name, var_address, var_initial in vars:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   427
                if name == var_name:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   428
                    return True
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   429
        return False
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   430
    
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   431
    # Return the type of a variable defined in interface
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   432
    def GetVariableType(self, name):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   433
        for list_type, retain, constant, located, vars in self.Interface:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   434
            for var_type, var_name, var_address, var_initial in vars:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   435
                if name == var_name:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   436
                    return var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   437
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   438
    
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   439
    # Return connectors linked by a connection to the given connector
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   440
    def GetConnectedConnector(self, connector, body):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   441
        links = connector.getconnections()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   442
        if links and len(links) == 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   443
            return self.GetLinkedConnector(links[0], body)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   444
        return None        
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   445
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   446
    def GetLinkedConnector(self, link, body):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   447
        parameter = link.getformalParameter()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   448
        instance = body.getcontentInstance(link.getrefLocalId())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   449
        if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable, plcopen.commonObjects_continuation, plcopen.ldObjects_contact, plcopen.ldObjects_coil)):
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   450
            return instance.connectionPointOut
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   451
        elif isinstance(instance, plcopen.fbdObjects_block):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   452
            outputvariables = instance.outputVariables.getvariable()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   453
            if len(outputvariables) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   454
                return outputvariables[0].connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   455
            elif parameter:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   456
                for variable in outputvariables:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   457
                    if variable.getformalParameter() == parameter:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   458
                        return variable.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   459
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   460
                point = link.getPosition()[-1]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   461
                for variable in outputvariables:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   462
                    relposition = variable.connectionPointOut.getrelPositionXY()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   463
                    blockposition = instance.getposition()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   464
                    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
   465
                        return variable.connectionPointOut
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   466
        elif isinstance(instance, plcopen.ldObjects_leftPowerRail):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   467
            outputconnections = instance.getconnectionPointOut()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   468
            if len(outputconnections) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   469
                return outputconnections[0]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   470
            else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   471
                point = link.getposition()[-1]
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   472
                for outputconnection in outputconnections:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   473
                    relposition = outputconnection.getrelPositionXY()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   474
                    powerrailposition = instance.getposition()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   475
                    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
   476
                        return outputconnection
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   477
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   478
        
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   479
    def ExtractRelatedConnections(self, connection):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   480
        for i, related in enumerate(self.RelatedConnections):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   481
            if connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   482
                return self.RelatedConnections.pop(i)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   483
        return [connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   484
    
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   485
    def ComputeInterface(self, pou):
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   486
        interface = pou.getinterface()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   487
        if interface is not None:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   488
            body = pou.getbody()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   489
            body_content = body.getcontent()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   490
            if self.Type == "FUNCTION":
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   491
                returntype_content = interface.getreturnType().getcontent()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   492
                if returntype_content["name"] == "derived":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   493
                    self.ReturnType = returntype_content["value"].getname()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   494
                elif returntype_content["name"] in ["string", "wstring"]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   495
                    self.ReturnType = returntype_content["name"].upper()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   496
                else:
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   497
                    self.ReturnType = returntype_content["name"]
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   498
            for varlist in interface.getcontent():
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   499
                variables = []
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   500
                located = []
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   501
                for var in varlist["value"].getvariable():
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   502
                    vartype_content = var.gettype().getcontent()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   503
                    if vartype_content["name"] == "derived":
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   504
                        var_type = vartype_content["value"].getname()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   505
                        blocktype = self.GetBlockType(var_type)
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   506
                        if blocktype is not None:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   507
                            self.ParentGenerator.GeneratePouProgram(var_type)
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   508
                            if body_content["name"] in ["FBD", "LD", "SFC"]:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   509
                                block = pou.getinstanceByName(var.getname())
191
d77f9b783ce8 Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents: 189
diff changeset
   510
                            else:
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   511
                                block = None
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   512
                            for variable in blocktype["initialise"](var_type, var.getname(), block):
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   513
                                if variable[2] is not None:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   514
                                    located.append(variable)
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   515
                                else:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   516
                                    variables.append(variable)
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   517
                        else:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   518
                            initial = var.getinitialValue()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   519
                            if initial:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   520
                                initial_value = initial.getvalue()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   521
                            else:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   522
                                initial_value = None
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   523
                            address = var.getaddress()
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   524
                            if address is not None:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   525
                                located.append((vartype_content["value"].getname(), var.getname(), address, initial_value))
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   526
                            else:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   527
                                variables.append((vartype_content["value"].getname(), var.getname(), None, initial_value))
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   528
                    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   529
                        initial = var.getinitialValue()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   530
                        if initial:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   531
                            initial_value = initial.getvalue()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   532
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   533
                            initial_value = None
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   534
                        address = var.getaddress()
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   535
                        if vartype_content["name"] in ["string", "wstring"]:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   536
                            if address is not None:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   537
                                located.append((vartype_content["name"].upper(), var.getname(), address, initial_value))
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   538
                            else:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   539
                                variables.append((vartype_content["name"].upper(), var.getname(), None, initial_value))
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   540
                        elif address is not None:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   541
                            located.append((vartype_content["name"], var.getname(), address, initial_value))
191
d77f9b783ce8 Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents: 189
diff changeset
   542
                        else:
194
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   543
                            variables.append((vartype_content["name"], var.getname(), None, initial_value))
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   544
                if len(variables) > 0:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   545
                    self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   546
                                varlist["value"].getconstant(), False, variables))
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   547
                if len(located) > 0:
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   548
                    self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getretain(), 
1b3f8b4f8e04 Adding support for Beremiz svgui plugin variable declaration
lbessard
parents: 191
diff changeset
   549
                                varlist["value"].getconstant(), True, located))
191
d77f9b783ce8 Bug with located variables generated by Beremiz svgui plugin fixed
lbessard
parents: 189
diff changeset
   550
        
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   551
    def ComputeConnectionTypes(self, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   552
        body = pou.getbody()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   553
        body_content = body.getcontent()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   554
        body_type = body_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   555
        if body_type in ["FBD", "LD", "SFC"]:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   556
            for instance in body.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   557
                if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   558
                    expression = instance.getexpression()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   559
                    var_type = self.GetVariableType(expression)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   560
                    if expression == pou.getname():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   561
                        returntype_content = pou.interface.getreturnType().getcontent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   562
                        if returntype_content["name"] == "derived":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   563
                            var_type = returntype_content["value"].getname()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   564
                        elif returntype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   565
                            var_type = returntype_content["name"].upper()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   566
                        else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   567
                            var_type = returntype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   568
                    elif var_type is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   569
                        var_type = expression.split("#")[0]
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   570
                    if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)):
208
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   571
                        for connection in self.ExtractRelatedConnections(instance.connectionPointOut):
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   572
                            self.ConnectionTypes[connection] = var_type
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   573
                    if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   574
                        self.ConnectionTypes[instance.connectionPointIn] = var_type
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   575
                        connected = self.GetConnectedConnector(instance.connectionPointIn, body)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   576
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   577
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   578
                                self.ConnectionTypes[connection] = var_type
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   579
                elif isinstance(instance, (plcopen.ldObjects_contact, plcopen.ldObjects_coil)):
208
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   580
                    for connection in self.ExtractRelatedConnections(instance.connectionPointOut):
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   581
                        self.ConnectionTypes[connection] = "BOOL"
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   582
                    self.ConnectionTypes[instance.connectionPointIn] = "BOOL"
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   583
                    connected = self.GetConnectedConnector(instance.connectionPointIn, body)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   584
                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   585
                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   586
                            self.ConnectionTypes[connection] = "BOOL"
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   587
                elif isinstance(instance, plcopen.ldObjects_leftPowerRail):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   588
                    for connection in instance.getconnectionPointOut():
208
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   589
                        for related in self.ExtractRelatedConnections(connection):
c70aefcadf66 Bugs with feedback path in View, Controler and Generator fixed
lbessard
parents: 207
diff changeset
   590
                            self.ConnectionTypes[related] = "BOOL"
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   591
                elif isinstance(instance, plcopen.ldObjects_rightPowerRail):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   592
                    for connection in instance.getconnectionPointIn():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   593
                        self.ConnectionTypes[connection] = "BOOL"
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   594
                        connected = self.GetConnectedConnector(connection, body)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   595
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   596
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   597
                                self.ConnectionTypes[connection] = "BOOL"
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   598
                elif isinstance(instance, plcopen.sfcObjects_transition):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   599
                    content = instance.condition.getcontent()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   600
                    if content["name"] == "connection" and len(content["value"]) == 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   601
                        connected = self.GetLinkedConnector(content["value"][0], body)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   602
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   603
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   604
                                self.ConnectionTypes[connection] = "BOOL"
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   605
                elif isinstance(instance, plcopen.fbdObjects_block):
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   606
                    block_infos = self.GetBlockType(instance.gettypeName())
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   607
                    undefined = {}
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   608
                    for variable in instance.outputVariables.getvariable():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   609
                        output_name = variable.getformalParameter()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   610
                        for oname, otype, oqualifier in block_infos["outputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   611
                            if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   612
                                if otype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   613
                                    if otype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   614
                                        undefined[otype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   615
                                    undefined[otype].append(variable.connectionPointOut)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   616
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   617
                                    for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   618
                                        self.ConnectionTypes[connection] = otype
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   619
                    for variable in instance.inputVariables.getvariable():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   620
                        input_name = variable.getformalParameter()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   621
                        for iname, itype, iqualifier in block_infos["inputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   622
                            if input_name == iname:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   623
                                connected = self.GetConnectedConnector(variable.connectionPointIn, body)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   624
                                if itype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   625
                                    if itype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   626
                                        undefined[itype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   627
                                    undefined[itype].append(variable.connectionPointIn)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   628
                                    if connected:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   629
                                        undefined[itype].append(connected)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   630
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   631
                                    self.ConnectionTypes[variable.connectionPointIn] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   632
                                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   633
                                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   634
                                            self.ConnectionTypes[connection] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   635
                    for var_type, connections in undefined.items():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   636
                        related = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   637
                        for connection in connections:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   638
                            if connection in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   639
                                var_type = self.ConnectionTypes[connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   640
                            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   641
                                related.extend(self.ExtractRelatedConnections(connection))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   642
                        if var_type.startswith("ANY") and len(related) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   643
                            self.RelatedConnections.append(related)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   644
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   645
                            for connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   646
                                self.ConnectionTypes[connection] = var_type
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   647
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   648
    def GetNetworkType(self, connections, body):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   649
        network_type = "FBD"
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   650
        for connection in connections:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   651
            localId = connection.getrefLocalId()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   652
            next = body.getcontentInstance(localId)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   653
            if isinstance(next, plcopen.ldObjects_leftPowerRail) or isinstance(next, plcopen.ldObjects_contact):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   654
                return "LD"
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   655
            elif isinstance(next, plcopen.fbdObjects_block):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   656
                 for variable in next.inputVariables.getvariable():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   657
                     result = self.GetNetworkType(variable.connectionPointIn.getconnections(), body)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   658
                     if result != "FBD":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   659
                         return result
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   660
            elif isinstance(next, plcopen.fbdObjects_inVariable):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   661
                return "FBD"
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   662
            elif isinstance(next, plcopen.fbdObjects_inOutVariable):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   663
                return self.GetNetworkType(next.connectionPointIn.getconnections(), body)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   664
            else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   665
                return None
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   666
        return "FBD"
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   667
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   668
    def ComputeProgram(self, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   669
        body = pou.getbody()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   670
        body_content = body.getcontent()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   671
        body_type = body_content["name"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   672
        if body_type in ["IL","ST"]:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   673
            self.Program = [(ReIndentText(body_content["value"].gettext(), len(self.CurrentIndent)), 
228
da7ddaf27cca Bug with indentation in textual languages while error display fixed
lbessard
parents: 227
diff changeset
   674
                            (self.TagName, "body", len(self.CurrentIndent)))]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   675
        elif body_type == "FBD":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   676
            orderedInstances = []
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   677
            otherInstances = {"outVariables" : [], "block" : [], "connector" : []}
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   678
            for instance in body.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   679
                if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable, plcopen.fbdObjects_block)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   680
                    executionOrderId = instance.getexecutionOrderId()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   681
                    if executionOrderId > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   682
                        orderedInstances.append((executionOrderId, instance))
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   683
                    elif isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   684
                        otherInstances["outVariables"].append(instance)
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   685
                    elif isinstance(instance, plcopen.fbdObjects_block):
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   686
                        otherInstances["block"].append(instance)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   687
                elif isinstance(instance, plcopen.commonObjects_connector):
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   688
                    otherInstances["connector"].append(instance)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   689
            orderedInstances.sort()
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   690
            instances = [instance for (executionOrderId, instance) in orderedInstances]
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   691
            instances.extend(otherInstances["connector"] + otherInstances["outVariables"] + otherInstances["block"])
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   692
            for instance in instances:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   693
                if isinstance(instance, (plcopen.fbdObjects_outVariable, plcopen.fbdObjects_inOutVariable)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   694
                    connections = instance.connectionPointIn.getconnections()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   695
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   696
                        expression = self.ComputeFBDExpression(body, connections[0])
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   697
                        self.Program += [(self.CurrentIndent, ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   698
                                         (instance.getexpression(), (self.TagName, "io_variable", instance.getlocalId(), "expression")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   699
                                         (" := ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   700
                        self.Program += expression
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   701
                        self.Program += [(";\n", ())]
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   702
                elif isinstance(instance, plcopen.fbdObjects_block):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   703
                    block_type = instance.gettypeName()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   704
                    self.ParentGenerator.GeneratePouProgram(block_type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   705
                    block_infos = self.GetBlockType(block_type)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   706
                    block_infos["generate"](self, instance, body, None)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   707
                elif isinstance(instance, plcopen.commonObjects_connector):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   708
                    connector = instance.getname()
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   709
                    if self.ComputedConnectors.get(connector, None):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   710
                        continue 
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   711
                    connections = instance.connectionPointIn.getconnections()
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   712
                    if connections and len(connections) == 1:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   713
                        self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   714
        elif body_type == "LD":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   715
            for instance in body.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   716
                if isinstance(instance, plcopen.ldObjects_coil):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   717
                    paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), body)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   718
                    if len(paths) > 0:
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   719
                        paths = tuple(paths)
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   720
                    else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   721
                        paths = paths[0]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   722
                    coil_info = (self.TagName, "coil", instance.getlocalId())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   723
                    variable = self.ExtractModifier(instance, [(instance.getvariable(), coil_info + ("reference",))], coil_info)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   724
                    expression = self.ComputeLDExpression(paths, True)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   725
                    self.Program += [(self.CurrentIndent, ())] + variable
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   726
                    self.Program += [(" := ", ())] + expression + [(";\n", ())]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   727
        elif body_type == "SFC":
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   728
            self.IndentRight()
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   729
            for instance in body.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   730
                if isinstance(instance, plcopen.sfcObjects_step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   731
                    self.GenerateSFCStep(instance, pou)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   732
                elif isinstance(instance, plcopen.commonObjects_actionBlock):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   733
                    self.GenerateSFCStepActions(instance, pou)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   734
                elif isinstance(instance, plcopen.sfcObjects_transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   735
                    self.GenerateSFCTransition(instance, pou)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   736
                elif isinstance(instance, plcopen.sfcObjects_jumpStep):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   737
                    self.GenerateSFCJump(instance, pou)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   738
            if len(self.InitialSteps) > 0 and len(self.SFCComputedBlocks) > 0:
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   739
                action_name = "COMPUTE_FUNCTION_BLOCKS"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   740
                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
   741
                self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   742
                self.SFCNetworks["Actions"][action_name] = (self.SFCComputedBlocks, ())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   743
                self.Program = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   744
            self.IndentLeft()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   745
            for initialstep in self.InitialSteps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   746
                self.ComputeSFCStep(initialstep)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   747
            
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   748
    def ComputeFBDExpression(self, body, link, order = False):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   749
        localid = link.getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   750
        instance = body.getcontentInstance(localid)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   751
        if isinstance(instance, (plcopen.fbdObjects_inVariable, plcopen.fbdObjects_inOutVariable)):
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   752
            return [(instance.getexpression(), (self.TagName, "io_variable", localid, "expression"))]
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   753
        elif isinstance(instance, plcopen.fbdObjects_block):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   754
            block_type = instance.gettypeName()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   755
            self.ParentGenerator.GeneratePouProgram(block_type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   756
            block_infos = self.GetBlockType(block_type)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   757
            return block_infos["generate"](self, instance, body, link, order)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   758
        elif isinstance(instance, plcopen.commonObjects_continuation):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   759
            name = instance.getname()
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   760
            computed_value = self.ComputedConnectors.get(name, None)
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   761
            if computed_value != None:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   762
                return computed_value
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   763
            for tmp_instance in body.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   764
                if isinstance(tmp_instance, plcopen.commonObjects_connector):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   765
                    if tmp_instance.getname() == name:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   766
                        connections = tmp_instance.connectionPointIn.getconnections()
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   767
                        if connections and len(connections) == 1:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   768
                            expression = self.ComputeFBDExpression(body, connections[0], order)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   769
                            self.ComputedConnectors[name] = expression
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   770
                            return expression
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
   771
            raise PLCGenException, "No connector found corresponding to \"%s\" continuation in \"%s\" POU"%(name, self.Name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   772
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   773
    def GenerateLDPaths(self, connections, body):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   774
        paths = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   775
        for connection in connections:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   776
            localId = connection.getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   777
            next = body.getcontentInstance(localId)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   778
            if isinstance(next, plcopen.ldObjects_leftPowerRail):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   779
                paths.append(None)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   780
            elif isinstance(next, plcopen.fbdObjects_block):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   781
                block_type = next.gettypeName()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   782
                self.ParentGenerator.GeneratePouProgram(block_type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   783
                block_infos = self.GetBlockType(block_type)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   784
                paths.append(str(block_infos["generate"](self, next, body, connection)))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   785
            else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   786
                contact_info = (self.TagName, "contact", next.getlocalId())
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   787
                variable = str(self.ExtractModifier(next, [(next.getvariable(), contact_info + ("reference",))], contact_info))
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   788
                result = self.GenerateLDPaths(next.connectionPointIn.getconnections(), body)
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   789
                if len(result) > 1:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   790
                    paths.append([variable, tuple(result)])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   791
                elif type(result[0]) == ListType:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   792
                    paths.append([variable] + result[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   793
                elif result[0]:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   794
                    paths.append([variable, result[0]])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   795
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   796
                    paths.append(variable)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   797
        return paths
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   798
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   799
    def ComputeLDExpression(self, paths, first = False):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   800
        if type(paths) == TupleType:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   801
            if None in paths:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   802
                return [("TRUE", ())]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   803
            else:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   804
                vars = [self.ComputeLDExpression(path) for path in paths]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   805
                if first:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   806
                    return JoinList([(" OR ", ())], vars)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   807
                else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   808
                    return [("(", ())] + JoinList([(" OR ", ())], vars) + [(")", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   809
        elif type(paths) == ListType:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   810
            vars = [self.ComputeLDExpression(path) for path in paths]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   811
            return JoinList([(" AND ", ())], vars)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   812
        else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   813
            return eval(paths)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   814
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   815
    def ExtractModifier(self, variable, expression, var_info):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   816
        if variable.getnegated():
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   817
            return [("NOT(", var_info + ("negated",))] + expression + [(")", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   818
        else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   819
            edge = variable.getedge()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   820
            if edge == "rising":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   821
                return self.AddTrigger("R_TRIG", expression, var_info + ("rising",))
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   822
            elif edge == "falling":
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   823
                return self.AddTrigger("F_TRIG", expression, var_info + ("falling",))
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   824
        return expression
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   825
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   826
    def AddTrigger(self, edge, expression, var_info):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   827
        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   828
            self.Interface.append(("VAR", False, False, False, []))
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   829
        i = 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   830
        name = "%s%d"%(edge, i)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   831
        while self.IsAlreadyDefined(name):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   832
            i += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   833
            name = "%s%d"%(edge, i)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   834
        self.Interface[-1][4].append((edge, name, None, None))
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   835
        self.Program += [(self.CurrentIndent, ()), (name, var_info), ("(CLK := ", ())] 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   836
        self.Program += expression
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   837
        self.Program += [(");\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   838
        return [("%s.Q"%name, var_info)]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   839
    
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   840
    def ExtractDivergenceInput(self, divergence, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   841
        connectionPointIn = divergence.getconnectionPointIn()
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   842
        if connectionPointIn:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   843
            connections = connectionPointIn.getconnections()
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   844
            if connections is not None and len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   845
                instanceLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   846
                return pou.body.getcontentInstance(instanceLocalId)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   847
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   848
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   849
    def ExtractConvergenceInputs(self, convergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   850
        instances = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   851
        for connectionPointIn in convergence.getconnectionPointIn():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   852
            connections = connectionPointIn.getconnections()
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   853
            if len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   854
                instanceLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   855
                instances.append(pou.body.getcontentInstance(instanceLocalId))
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   856
        return instances
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   857
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   858
    def GenerateSFCStep(self, step, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   859
        step_name = step.getname()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   860
        if step_name not in self.SFCNetworks["Steps"].keys():
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   861
            if step.getinitialStep():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   862
                self.InitialSteps.append(step_name)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   863
            step_infos = {"id" : step.getlocalId(), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   864
                          "initial" : step.getinitialStep(), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   865
                          "transitions" : [], 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   866
                          "actions" : []}
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   867
            if step.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   868
                instances = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   869
                connections = step.connectionPointIn.getconnections()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   870
                if len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   871
                    instanceLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   872
                    instance = pou.body.getcontentInstance(instanceLocalId)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   873
                    if isinstance(instance, plcopen.sfcObjects_transition):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   874
                        instances.append(instance)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   875
                    elif isinstance(instance, plcopen.sfcObjects_selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   876
                        instances.extend(self.ExtractConvergenceInputs(instance, pou))
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   877
                    elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   878
                        transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   879
                        if transition:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   880
                            if isinstance(transition, plcopen.sfcObjects_transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   881
                                instances.append(transition)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   882
                            elif isinstance(transition, plcopen.sfcObjects_selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   883
                                instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   884
                for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   885
                    self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   886
                    if instance in self.SFCNetworks["Transitions"].keys():
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   887
                        target_info = (self.TagName, "transition", instance.getlocalId(), "to", step_infos["id"])
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   888
                        self.SFCNetworks["Transitions"][instance]["to"].append([(step_name, target_info)])
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   889
            self.SFCNetworks["Steps"][step_name] = step_infos
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   890
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   891
    def GenerateSFCJump(self, jump, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   892
        jump_target = jump.gettargetName()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   893
        if jump.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   894
            instances = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   895
            connections = jump.connectionPointIn.getconnections()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   896
            if len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   897
                instanceLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   898
                instance = pou.body.getcontentInstance(instanceLocalId)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   899
                if isinstance(instance, plcopen.sfcObjects_transition):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   900
                    instances.append(instance)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   901
                elif isinstance(instance, plcopen.sfcObjects_selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   902
                    instances.extend(self.ExtractConvergenceInputs(instance, pou))
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   903
                elif isinstance(instance, plcopen.sfcObjects_simultaneousDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   904
                    transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   905
                    if transition:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   906
                        if isinstance(transition, plcopen.sfcObjects_transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   907
                            instances.append(transition)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   908
                        elif isinstance(transition, plcopen.sfcObjects_selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   909
                            instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   910
            for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   911
                self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   912
                if instance in self.SFCNetworks["Transitions"].keys():
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   913
                    target_info = (self.TagName, "jump", jump.getlocalId(), "target")
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   914
                    self.SFCNetworks["Transitions"][instance]["to"].append([(jump_target, target_info)])
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   915
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   916
    def GenerateSFCStepActions(self, actionBlock, pou):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   917
        connections = actionBlock.connectionPointIn.getconnections()
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   918
        if connections is not None and len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   919
            stepLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   920
            step = pou.body.getcontentInstance(stepLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   921
            self.GenerateSFCStep(step, pou)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   922
            step_name = step.getname()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   923
            if step_name in self.SFCNetworks["Steps"].keys():
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   924
                actions = actionBlock.getactions()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   925
                for i, action in enumerate(actions):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   926
                    action_infos = {"id" : actionBlock.getlocalId(), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   927
                                    "qualifier" : action["qualifier"], 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   928
                                    "content" : action["value"],
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   929
                                    "num" : i}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   930
                    if "duration" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   931
                        action_infos["duration"] = action["duration"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   932
                    if "indicator" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   933
                        action_infos["indicator"] = action["indicator"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   934
                    if action["type"] == "reference":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   935
                        self.GenerateSFCAction(action["value"], pou)
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   936
                    else:
168
fb500cc79164 Adding support for structure variable list generation module in matiec
lbessard
parents: 151
diff changeset
   937
                        action_name = "%s_INLINE%d"%(step_name.upper(), self.GetActionNumber())
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   938
                        self.SFCNetworks["Actions"][action_name] = ([(self.CurrentIndent, ()), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   939
                            (action["value"], (self.TagName, "action_block", action_infos["id"], "action", i, "inline")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   940
                            ("\n", ())], ())
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   941
                        action_infos["content"] = action_name
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   942
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   943
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   944
    def GenerateSFCAction(self, action_name, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   945
        if action_name not in self.SFCNetworks["Actions"].keys():
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   946
            actionContent = pou.getaction(action_name)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   947
            if actionContent:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   948
                previous_tagname = self.TagName
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   949
                self.TagName = self.ParentGenerator.Controler.ComputePouActionName(self.Name, action_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   950
                self.ComputeProgram(actionContent)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   951
                self.SFCNetworks["Actions"][action_name] = (self.Program, (self.TagName, "name"))
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   952
                self.Program = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   953
                self.TagName = previous_tagname
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   954
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   955
    def GenerateSFCTransition(self, transition, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   956
        if transition not in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   957
            steps = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   958
            connections = transition.connectionPointIn.getconnections()
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
   959
            if connections is not None and len(connections) == 1:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   960
                instanceLocalId = connections[0].getrefLocalId()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   961
                instance = pou.body.getcontentInstance(instanceLocalId)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   962
                if isinstance(instance, plcopen.sfcObjects_step):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   963
                    steps.append(instance)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   964
                elif isinstance(instance, plcopen.sfcObjects_selectionDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   965
                    step = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   966
                    if step:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   967
                        if isinstance(step, plcopen.sfcObjects_step):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   968
                            steps.append(step)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   969
                        elif isinstance(step, plcopen.sfcObjects_simultaneousConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   970
                            steps.extend(self.ExtractConvergenceInputs(step, pou))
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   971
                elif isinstance(instance, plcopen.sfcObjects_simultaneousConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   972
                    steps.extend(self.ExtractConvergenceInputs(instance, pou))
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   973
            transition_infos = {"id" : transition.getlocalId(), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   974
                                "priority": transition.getpriority(), 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   975
                                "from": [], 
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   976
                                "to" : []}
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   977
            transitionValues = transition.getconditionContent()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   978
            if transitionValues["type"] == "inline":
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   979
                transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   980
                                               (transitionValues["value"], (self.TagName, "transition", transition.getlocalId(), "inline")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   981
                                               (";\n", ())]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   982
            elif transitionValues["type"] == "reference":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   983
                transitionContent = pou.gettransition(transitionValues["value"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   984
                transitionType = transitionContent.getbodyType()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   985
                transitionBody = transitionContent.getbody()
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   986
                previous_tagname = self.TagName
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   987
                self.TagName = self.ParentGenerator.Controler.ComputePouTransitionName(self.Name, transitionValues["value"])
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   988
                if transitionType == "IL":
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   989
                    transition_infos["content"] = [(":\n", ()),
228
da7ddaf27cca Bug with indentation in textual languages while error display fixed
lbessard
parents: 227
diff changeset
   990
                                                   (ReIndentText(transitionBody.gettext(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   991
                elif transitionType == "ST":
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
   992
                    transition_infos["content"] = [("\n", ()),
228
da7ddaf27cca Bug with indentation in textual languages while error display fixed
lbessard
parents: 227
diff changeset
   993
                                                   (ReIndentText(transitionBody.gettext(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   994
                elif transitionType == "FBD":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   995
                    for instance in transitionBody.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   996
                        if isinstance(instance, plcopen.fbdObjects_outVariable):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
   997
                            connections = instance.connectionPointIn.getconnections()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   998
                            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
   999
                                expression = self.ComputeFBDExpression(transitionBody, connections[0])
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1000
                                transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ())] + expression + [(";\n", ())]
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
  1001
                                self.SFCComputedBlocks += self.Program
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1002
                                self.Program = []
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1003
                elif transitionType == "LD":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1004
                    for instance in transitionBody.getcontentInstances():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1005
                        if isinstance(instance, plcopen.ldObjects_coil):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1006
                            paths = self.GenerateLDPaths(instance.connectionPointIn.getconnections(), transitionBody)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1007
                            expression = self.ComputeLDExpression(paths, True)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1008
                            transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ())] + expression + [(";\n", ())]
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
  1009
                            self.SFCComputedBlocks += self.Program
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1010
                            self.Program = []
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1011
                self.TagName = previous_tagname
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1012
            elif transitionValues["type"] == "connection":
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1013
                body = pou.getbody()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1014
                connections = transition.getconnections()
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1015
                network_type = self.GetNetworkType(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1016
                if network_type == None:
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1017
                    raise PLCGenException, "Type of network connected to transition impossible to define in \"%s\" POU"%self.Name
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1018
                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
  1019
                    paths = self.GenerateLDPaths(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1020
                    expression = self.ComputeLDExpression(paths, True)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1021
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
  1022
                    expression = self.ComputeFBDExpression(body, connections[0])
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1023
                transition_infos["content"] = [("\n%s:= "%self.CurrentIndent, ())] + expression + [(";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1024
                self.SFCComputedBlocks += self.Program
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1025
                self.Program = []
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
  1026
            for step in steps:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
  1027
                self.GenerateSFCStep(step, pou)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 141
diff changeset
  1028
                step_name = step.getname()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1029
                if step_name in self.SFCNetworks["Steps"].keys():
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1030
                    transition_infos["from"].append([(step_name, (self.TagName, "transition", transition.getlocalId(), "from", step.getlocalId()))])
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1031
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1032
            self.SFCNetworks["Transitions"][transition] = transition_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1033
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1034
    def ComputeSFCStep(self, step_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1035
        if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1036
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1037
            self.Program += [(self.CurrentIndent, ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1038
            if step_infos["initial"]:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1039
                self.Program += [("INITIAL_", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1040
            self.Program += [("STEP ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1041
                             (step_name, (self.TagName, "step", step_infos["id"], "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1042
                             (":\n", ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1043
            actions = []
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1044
            self.IndentRight()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1045
            for action_infos in step_infos["actions"]:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1046
                if action_infos.get("id", None) is not None:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1047
                    action_info = (self.TagName, "action_block", action_infos["id"], "action", action_infos["num"])
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1048
                else:
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1049
                    action_info = ()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1050
                actions.append(action_infos["content"])
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1051
                self.Program += [(self.CurrentIndent, ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1052
                                 (action_infos["content"], action_info + ("reference",)),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1053
                                 ("(", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1054
                                 (action_infos["qualifier"], action_info + ("qualifier",))]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1055
                if "duration" in action_infos:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1056
                    self.Program += [(", ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1057
                                     (action_infos["duration"], action_info + ("duration",))]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1058
                if "indicator" in action_infos:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1059
                    self.Program += [(", ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1060
                                     (action_infos["indicator"], action_info + ("indicator",))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1061
                self.Program += [(");\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1062
            self.IndentLeft()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1063
            self.Program += [("%sEND_STEP\n\n"%self.CurrentIndent, ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1064
            for action in actions:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1065
                self.ComputeSFCAction(action)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1066
            for transition in step_infos["transitions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1067
                self.ComputeSFCTransition(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1068
                
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1069
    def ComputeSFCAction(self, action_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1070
        if action_name in self.SFCNetworks["Actions"].keys():
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1071
            action_content, action_info = self.SFCNetworks["Actions"].pop(action_name)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1072
            self.Program += [("%sACTION "%self.CurrentIndent, ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1073
                             (action_name, action_info),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1074
                             (" :\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1075
            self.Program += action_content
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1076
            self.Program += [("%sEND_ACTION\n\n"%self.CurrentIndent, ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1077
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1078
    def ComputeSFCTransition(self, transition):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1079
        if transition in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1080
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1081
            self.Program += [("%sTRANSITION"%self.CurrentIndent, ())]
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
  1082
            if transition_infos["priority"] != None:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1083
                self.Program += [(" (PRIORITY := ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1084
                                 ("%d"%transition_infos["priority"], (self.TagName, "transition", transition_infos["id"], "priority")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1085
                                 (")", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1086
            self.Program += [(" FROM ", ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1087
            if len(transition_infos["from"]) > 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1088
                self.Program += [("(", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1089
                self.Program += JoinList([(", ", ())], transition_infos["from"])
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1090
                self.Program += [(")", ())]
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
  1091
            elif len(transition_infos["from"]) == 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1092
                self.Program += transition_infos["from"][0]
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
  1093
            else:
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1094
                raise PLCGenException, "Transition with content \"%s\" not connected to a previous step in \"%s\" POU"%(transition_infos["content"], self.Name)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1095
            self.Program += [(" TO ", ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1096
            if len(transition_infos["to"]) > 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1097
                self.Program += [("(", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1098
                self.Program += JoinList([(", ", ())], transition_infos["to"])
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1099
                self.Program += [(")", ())]
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
  1100
            elif len(transition_infos["to"]) == 1:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1101
                self.Program += transition_infos["to"][0]
201
d5b778dab4b0 Block evaluation order fixed
lbessard
parents: 194
diff changeset
  1102
            else:
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1103
                raise PLCGenException, "Transition with content \"%s\" not connected to a next step in \"%s\" POU"%(transition_infos["content"], self.Name)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1104
            self.Program += transition_infos["content"]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1105
            self.Program += [("%sEND_TRANSITION\n\n"%self.CurrentIndent, ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1106
            for [(step_name, step_infos)] in transition_infos["to"]:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1107
                self.ComputeSFCStep(step_name)
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1108
    
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1109
    def GenerateProgram(self, pou):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1110
        self.ComputeInterface(pou)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1111
        self.ComputeConnectionTypes(pou)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1112
        self.ComputeProgram(pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1113
        
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1114
        program = [("%s "%self.Type, ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1115
                   (self.Name, (self.TagName, "name"))]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1116
        if self.ReturnType:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1117
            program += [(" : ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1118
                        (self.ReturnType, (self.TagName, "return"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1119
        program += [("\n", ())]
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1120
        if len(self.Interface) == 0:
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1121
            raise PLCGenException, "No variable defined in \"%s\" POU"%self.Name
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1122
        if len(self.Program) == 0 :
214
a88b377f75cb Adding tests for detecting POUs with no variables or/and no body
lbessard
parents: 208
diff changeset
  1123
            raise PLCGenException, "No body defined in \"%s\" POU"%self.Name
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1124
        var_number = 0
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
  1125
        for list_type, retain, constant, located, variables in self.Interface:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1126
            program += [("  %s"%list_type, ())]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1127
            if retain:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1128
                program += [(" RETAIN", (self.TagName, "variable", (var_number, var_number + len(variables)), "retain"))]
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
  1129
            if constant:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1130
                program += [(" CONSTANT", (self.TagName, "variable", (var_number, var_number + len(variables)), "constant"))]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1131
            program += [("\n", ())]
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
  1132
            for var_type, var_name, var_address, var_initial in variables:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1133
                program += [("    ", ())]
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
  1134
                if var_name:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1135
                    program += [(var_name, (self.TagName, "variable", var_number, "name")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1136
                                (" ", ())]
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
  1137
                if var_address != None:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1138
                    program += [("AT ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1139
                                (var_address, (self.TagName, "variable", var_number, "address")),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1140
                                (" ", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1141
                program += [(": ", ()),
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1142
                            (var_type, (self.TagName, "variable", var_number, "type"))]
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
  1143
                if var_initial != None:
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1144
                    program += [(" := ", ()),
232
cbdfef9b2020 Fixed typos.
etisserant
parents: 228
diff changeset
  1145
                                (self.ParentGenerator.ComputeValue(var_initial, var_type), (self.TagName, "variable", var_number, "initial"))]
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1146
                program += [(";\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1147
                var_number += 1
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1148
            program += [("  END_VAR\n", ())]
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1149
        program += [("\n", ())]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1150
        program += self.Program
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1151
        program += [("END_%s\n\n"%self.Type, ())]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1152
        return program
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
  1153
227
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1154
def GenerateCurrentProgram(controler, project):
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1155
    generator = ProgramGenerator(controler, project)
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1156
    generator.GenerateProgram()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1157
    return generator.GetGeneratedProgram()
c6fee379d446 Adding informations to program chunks to allow display of compiling errors from matiec
lbessard
parents: 215
diff changeset
  1158