author | Paul Beltyukov |
Thu, 18 Aug 2016 14:35:03 +0500 | |
changeset 1517 | 30692ab8b5c7 |
parent 1390 | 0f4d32a033e0 |
child 1571 | 486f94a8032c |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
1390 | 25 |
import string, re |
1313
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
26 |
from plcopen import LoadProject |
1320
bb04c41cbee9
Used OrderedDict for keeping of standard function blocks library defined
Laurent Bessard
parents:
1313
diff
changeset
|
27 |
from collections import OrderedDict |
1390 | 28 |
from definitions import * |
29 |
||
30 |
TypeHierarchy = dict(TypeHierarchy_list) |
|
31 |
||
32 |
""" |
|
33 |
returns true if the given data type is the same that "reference" meta-type or one of its types. |
|
34 |
""" |
|
35 |
def IsOfType(type, reference): |
|
36 |
if reference is None: |
|
37 |
return True |
|
38 |
elif type == reference: |
|
39 |
return True |
|
40 |
else: |
|
41 |
parent_type = TypeHierarchy[type] |
|
42 |
if parent_type is not None: |
|
43 |
return IsOfType(parent_type, reference) |
|
44 |
return False |
|
45 |
||
46 |
""" |
|
47 |
returns list of all types that correspont to the ANY* meta type |
|
48 |
""" |
|
49 |
def GetSubTypes(type): |
|
50 |
return [typename for typename, parenttype in TypeHierarchy.items() if not typename.startswith("ANY") and IsOfType(typename, type)] |
|
51 |
||
52 |
DataTypeRange = dict(DataTypeRange_list) |
|
53 |
||
54 |
""" |
|
55 |
Ordered list of common Function Blocks defined in the IEC 61131-3 |
|
814 | 56 |
Each block have this attributes: |
57 |
- "name" : The block name |
|
58 |
- "type" : The block type. It can be "function", "functionBlock" or "program" |
|
59 |
- "extensible" : Boolean that define if the block is extensible |
|
60 |
- "inputs" : List of the block inputs |
|
61 |
- "outputs" : List of the block outputs |
|
62 |
- "comment" : Comment that will be displayed in the block popup |
|
63 |
- "generate" : Method that generator will call for generating ST block code |
|
64 |
Inputs and outputs are a tuple of characteristics that are in order: |
|
65 |
- The name |
|
66 |
- The data type |
|
67 |
- The default modifier which can be "none", "negated", "rising" or "falling" |
|
68 |
""" |
|
69 |
||
1390 | 70 |
StdBlckLibs = {libname : LoadProject(tc6fname)[0] |
71 |
for libname, tc6fname in StdTC6Libs} |
|
72 |
StdBlckLst = [{"name" : libname, "list": |
|
73 |
[GetBlockInfos(pous) for pous in lib.getpous()]} |
|
74 |
for libname, lib in StdBlckLibs.iteritems()] |
|
814 | 75 |
|
76 |
#------------------------------------------------------------------------------- |
|
77 |
# Test identifier |
|
78 |
#------------------------------------------------------------------------------- |
|
79 |
||
965 | 80 |
IDENTIFIER_MODEL = re.compile( |
81 |
"(?:%(letter)s|_(?:%(letter)s|%(digit)s))(?:_?(?:%(letter)s|%(digit)s))*$" % |
|
82 |
{"letter": "[a-zA-Z]", "digit": "[0-9]"}) |
|
814 | 83 |
|
84 |
# Test if identifier is valid |
|
85 |
def TestIdentifier(identifier): |
|
965 | 86 |
return IDENTIFIER_MODEL.match(identifier) is not None |
814 | 87 |
|
88 |
#------------------------------------------------------------------------------- |
|
89 |
# Standard functions list generation |
|
90 |
#------------------------------------------------------------------------------- |
|
91 |
||
92 |
||
93 |
""" |
|
94 |
take a .csv file and translate it it a "csv_table" |
|
95 |
""" |
|
96 |
def csv_file_to_table(file): |
|
97 |
return [ map(string.strip,line.split(';')) for line in file.xreadlines()] |
|
98 |
||
99 |
""" |
|
100 |
seek into the csv table to a section ( section_name match 1st field ) |
|
101 |
return the matching row without first field |
|
102 |
""" |
|
103 |
def find_section(section_name, table): |
|
104 |
fields = [None] |
|
105 |
while(fields[0] != section_name): |
|
106 |
fields = table.pop(0) |
|
107 |
return fields[1:] |
|
108 |
||
109 |
""" |
|
110 |
extract the standard functions standard parameter names and types... |
|
111 |
return a { ParameterName: Type, ...} |
|
112 |
""" |
|
113 |
def get_standard_funtions_input_variables(table): |
|
114 |
variables = find_section("Standard_functions_variables_types", table) |
|
115 |
standard_funtions_input_variables = {} |
|
116 |
fields = [True,True] |
|
117 |
while(fields[1]): |
|
118 |
fields = table.pop(0) |
|
119 |
variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:]) if champ!='']) |
|
120 |
standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type'] |
|
121 |
return standard_funtions_input_variables |
|
122 |
||
123 |
""" |
|
124 |
translate .csv file input declaration into PLCOpenEditor interessting values |
|
125 |
in : "(ANY_NUM, ANY_NUM)" and { ParameterName: Type, ...} |
|
126 |
return [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")] |
|
127 |
""" |
|
128 |
def csv_input_translate(str_decl, variables, base): |
|
129 |
decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',') |
|
130 |
params = [] |
|
131 |
||
132 |
len_of_not_predifined_variable = len([True for param_type in decl if param_type not in variables]) |
|
133 |
||
134 |
for param_type in decl: |
|
135 |
if param_type in variables.keys(): |
|
136 |
param_name = param_type |
|
137 |
param_type = variables[param_type] |
|
138 |
elif len_of_not_predifined_variable > 1: |
|
139 |
param_name = "IN%d"%base |
|
140 |
base += 1 |
|
141 |
else: |
|
142 |
param_name = "IN" |
|
143 |
params.append((param_name, param_type, "none")) |
|
144 |
return params |
|
145 |
||
146 |
||
147 |
""" |
|
148 |
Returns this kind of declaration for all standard functions |
|
149 |
||
150 |
[{"name" : "Numerical", 'list': [ { |
|
151 |
'baseinputnumber': 1, |
|
152 |
'comment': 'Addition', |
|
153 |
'extensible': True, |
|
154 |
'inputs': [ ('IN1', 'ANY_NUM', 'none'), |
|
155 |
('IN2', 'ANY_NUM', 'none')], |
|
156 |
'name': 'ADD', |
|
157 |
'outputs': [('OUT', 'ANY_NUM', 'none')], |
|
158 |
'type': 'function'}, ...... ] },.....] |
|
159 |
""" |
|
160 |
def get_standard_funtions(table): |
|
161 |
||
162 |
variables = get_standard_funtions_input_variables(table) |
|
163 |
||
164 |
fonctions = find_section("Standard_functions_type",table) |
|
165 |
||
166 |
Standard_Functions_Decl = [] |
|
167 |
Current_section = None |
|
168 |
||
169 |
translate = { |
|
170 |
"extensible" : lambda x: {"yes":True, "no":False}[x], |
|
171 |
"inputs" : lambda x:csv_input_translate(x,variables,baseinputnumber), |
|
172 |
"outputs":lambda x:[("OUT",x,"none")]} |
|
173 |
||
174 |
for fields in table: |
|
175 |
if fields[1]: |
|
176 |
# If function section name given |
|
177 |
if fields[0]: |
|
178 |
words = fields[0].split('"') |
|
179 |
if len(words) > 1: |
|
180 |
section_name = words[1] |
|
181 |
else: |
|
182 |
section_name = fields[0] |
|
183 |
Current_section = {"name" : section_name, "list" : []} |
|
184 |
Standard_Functions_Decl.append(Current_section) |
|
185 |
Function_decl_list = [] |
|
186 |
if Current_section: |
|
187 |
Function_decl = dict([(champ, val) for champ, val in zip(fonctions, fields[1:]) if champ]) |
|
188 |
baseinputnumber = int(Function_decl.get("baseinputnumber",1)) |
|
189 |
Function_decl["baseinputnumber"] = baseinputnumber |
|
190 |
for param, value in Function_decl.iteritems(): |
|
191 |
if param in translate: |
|
192 |
Function_decl[param] = translate[param](value) |
|
193 |
Function_decl["type"] = "function" |
|
194 |
||
195 |
if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : |
|
196 |
input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) |
|
197 |
output_types = GetSubTypes(Function_decl["outputs"][0][1]) |
|
198 |
else: |
|
199 |
input_ovrloading_types = [None] |
|
200 |
output_types = [None] |
|
201 |
||
202 |
funcdeclname_orig = Function_decl["name"] |
|
203 |
funcdeclname = Function_decl["name"].strip('*_') |
|
204 |
fdc = Function_decl["inputs"][:] |
|
205 |
for intype in input_ovrloading_types: |
|
206 |
if intype != None: |
|
207 |
Function_decl["inputs"] = [] |
|
208 |
for decl_tpl in fdc: |
|
209 |
if IsOfType(intype, decl_tpl[1]): |
|
210 |
Function_decl["inputs"] += [(decl_tpl[0], intype, decl_tpl[2])] |
|
211 |
else: |
|
212 |
Function_decl["inputs"] += [(decl_tpl)] |
|
213 |
||
214 |
if funcdeclname_orig.startswith('*'): |
|
215 |
funcdeclin = intype + '_' + funcdeclname |
|
216 |
else: |
|
217 |
funcdeclin = funcdeclname |
|
218 |
else: |
|
219 |
funcdeclin = funcdeclname |
|
220 |
||
221 |
for outype in output_types: |
|
222 |
if outype != None: |
|
223 |
decl_tpl = Function_decl["outputs"][0] |
|
224 |
Function_decl["outputs"] = [ (decl_tpl[0] , outype, decl_tpl[2])] |
|
225 |
if funcdeclname_orig.endswith('*'): |
|
226 |
funcdeclout = funcdeclin + '_' + outype |
|
227 |
else: |
|
228 |
funcdeclout = funcdeclin |
|
229 |
else: |
|
230 |
funcdeclout = funcdeclin |
|
231 |
Function_decl["name"] = funcdeclout |
|
232 |
||
1390 | 233 |
# apply filter given in "filter" column |
234 |
filter_name = Function_decl["filter"] |
|
235 |
store = True |
|
236 |
for (InTypes, OutTypes) in ANY_TO_ANY_FILTERS.get(filter_name,[]): |
|
237 |
outs = reduce(lambda a,b: a or b, |
|
238 |
map(lambda testtype : IsOfType( |
|
239 |
Function_decl["outputs"][0][1], |
|
240 |
testtype), OutTypes)) |
|
241 |
inps = reduce(lambda a,b: a or b, |
|
242 |
map(lambda testtype : IsOfType( |
|
243 |
Function_decl["inputs"][0][1], |
|
244 |
testtype), InTypes)) |
|
245 |
if inps and outs and Function_decl["outputs"][0][1] != Function_decl["inputs"][0][1]: |
|
246 |
store = True |
|
247 |
break |
|
248 |
else: |
|
249 |
store = False |
|
250 |
if store : |
|
814 | 251 |
# create the copy of decl dict to be appended to section |
252 |
Function_decl_copy = Function_decl.copy() |
|
253 |
Current_section["list"].append(Function_decl_copy) |
|
254 |
else: |
|
255 |
raise "First function must be in a category" |
|
256 |
||
257 |
return Standard_Functions_Decl |
|
258 |
||
1390 | 259 |
StdBlckLst.extend(get_standard_funtions(csv_file_to_table(open(StdFuncsCSV)))) |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
260 |
|
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
261 |
# Dictionary to speedup block type fetching by name |
1320
bb04c41cbee9
Used OrderedDict for keeping of standard function blocks library defined
Laurent Bessard
parents:
1313
diff
changeset
|
262 |
StdBlckDct = OrderedDict() |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
263 |
|
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
264 |
for section in StdBlckLst: |
814 | 265 |
for desc in section["list"]: |
266 |
words = desc["comment"].split('"') |
|
267 |
if len(words) > 1: |
|
268 |
desc["comment"] = words[1] |
|
1313
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
269 |
desc["usage"] = ("\n (%s) => (%s)" % |
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
270 |
(", ".join(["%s:%s" % (input[1], input[0]) |
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
271 |
for input in desc["inputs"]]), |
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
272 |
", ".join(["%s:%s" % (output[1], output[0]) |
85c167bfff93
Replaced standard function blocks library definition from dictionary to plcopen xml files
Laurent Bessard
parents:
1310
diff
changeset
|
273 |
for output in desc["outputs"]]))) |
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
274 |
BlkLst = StdBlckDct.setdefault(desc["name"],[]) |
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
275 |
BlkLst.append((section["name"], desc)) |
814 | 276 |
|
277 |
#------------------------------------------------------------------------------- |
|
278 |
# Languages Keywords |
|
279 |
#------------------------------------------------------------------------------- |
|
280 |
||
281 |
# Keywords for Pou Declaration |
|
282 |
POU_BLOCK_START_KEYWORDS = ["FUNCTION", "FUNCTION_BLOCK", "PROGRAM"] |
|
283 |
POU_BLOCK_END_KEYWORDS = ["END_FUNCTION", "END_FUNCTION_BLOCK", "END_PROGRAM"] |
|
284 |
POU_KEYWORDS = ["EN", "ENO", "F_EDGE", "R_EDGE"] + POU_BLOCK_START_KEYWORDS + POU_BLOCK_END_KEYWORDS |
|
1283
f3cfe1ff917e
More optimization attemps in type handling
Edouard Tisserant
parents:
1239
diff
changeset
|
285 |
for category in StdBlckLst: |
814 | 286 |
for block in category["list"]: |
287 |
if block["name"] not in POU_KEYWORDS: |
|
288 |
POU_KEYWORDS.append(block["name"]) |
|
289 |
||
290 |
||
291 |
# Keywords for Type Declaration |
|
292 |
TYPE_BLOCK_START_KEYWORDS = ["TYPE", "STRUCT"] |
|
293 |
TYPE_BLOCK_END_KEYWORDS = ["END_TYPE", "END_STRUCT"] |
|
294 |
TYPE_KEYWORDS = ["ARRAY", "OF", "T", "D", "TIME_OF_DAY", "DATE_AND_TIME"] + TYPE_BLOCK_START_KEYWORDS + TYPE_BLOCK_END_KEYWORDS |
|
295 |
TYPE_KEYWORDS.extend([keyword for keyword in TypeHierarchy.keys() if keyword not in TYPE_KEYWORDS]) |
|
296 |
||
297 |
||
298 |
# Keywords for Variable Declaration |
|
299 |
VAR_BLOCK_START_KEYWORDS = ["VAR", "VAR_INPUT", "VAR_OUTPUT", "VAR_IN_OUT", "VAR_TEMP", "VAR_EXTERNAL"] |
|
300 |
VAR_BLOCK_END_KEYWORDS = ["END_VAR"] |
|
301 |
VAR_KEYWORDS = ["AT", "CONSTANT", "RETAIN", "NON_RETAIN"] + VAR_BLOCK_START_KEYWORDS + VAR_BLOCK_END_KEYWORDS |
|
302 |
||
303 |
||
304 |
# Keywords for Configuration Declaration |
|
305 |
CONFIG_BLOCK_START_KEYWORDS = ["CONFIGURATION", "RESOURCE", "VAR_ACCESS", "VAR_CONFIG", "VAR_GLOBAL"] |
|
306 |
CONFIG_BLOCK_END_KEYWORDS = ["END_CONFIGURATION", "END_RESOURCE", "END_VAR"] |
|
307 |
CONFIG_KEYWORDS = ["ON", "PROGRAM", "WITH", "READ_ONLY", "READ_WRITE", "TASK"] + CONFIG_BLOCK_START_KEYWORDS + CONFIG_BLOCK_END_KEYWORDS |
|
308 |
||
309 |
# Keywords for Structured Function Chart |
|
310 |
SFC_BLOCK_START_KEYWORDS = ["ACTION", "INITIAL_STEP", "STEP", "TRANSITION"] |
|
311 |
SFC_BLOCK_END_KEYWORDS = ["END_ACTION", "END_STEP", "END_TRANSITION"] |
|
949 | 312 |
SFC_KEYWORDS = ["FROM", "TO"] + SFC_BLOCK_START_KEYWORDS + SFC_BLOCK_END_KEYWORDS |
814 | 313 |
|
314 |
||
315 |
# Keywords for Instruction List |
|
316 |
IL_KEYWORDS = ["TRUE", "FALSE", "LD", "LDN", "ST", "STN", "S", "R", "AND", "ANDN", "OR", "ORN", |
|
317 |
"XOR", "XORN", "NOT", "ADD", "SUB", "MUL", "DIV", "MOD", "GT", "GE", "EQ", "NE", |
|
318 |
"LE", "LT", "JMP", "JMPC", "JMPCN", "CAL", "CALC", "CALCN", "RET", "RETC", "RETCN"] |
|
319 |
||
320 |
||
321 |
# Keywords for Structured Text |
|
322 |
ST_BLOCK_START_KEYWORDS = ["IF", "ELSIF", "ELSE", "CASE", "FOR", "WHILE", "REPEAT"] |
|
323 |
ST_BLOCK_END_KEYWORDS = ["END_IF", "END_CASE", "END_FOR", "END_WHILE", "END_REPEAT"] |
|
324 |
ST_KEYWORDS = ["TRUE", "FALSE", "THEN", "OF", "TO", "BY", "DO", "DO", "UNTIL", "EXIT", |
|
325 |
"RETURN", "NOT", "MOD", "AND", "XOR", "OR"] + ST_BLOCK_START_KEYWORDS + ST_BLOCK_END_KEYWORDS |
|
326 |
||
327 |
# All the keywords of IEC |
|
328 |
IEC_BLOCK_START_KEYWORDS = [] |
|
329 |
IEC_BLOCK_END_KEYWORDS = [] |
|
330 |
IEC_KEYWORDS = ["E", "TRUE", "FALSE"] |
|
331 |
for all_keywords, keywords_list in [(IEC_BLOCK_START_KEYWORDS, [POU_BLOCK_START_KEYWORDS, TYPE_BLOCK_START_KEYWORDS, |
|
332 |
VAR_BLOCK_START_KEYWORDS, CONFIG_BLOCK_START_KEYWORDS, |
|
333 |
SFC_BLOCK_START_KEYWORDS, ST_BLOCK_START_KEYWORDS]), |
|
334 |
(IEC_BLOCK_END_KEYWORDS, [POU_BLOCK_END_KEYWORDS, TYPE_BLOCK_END_KEYWORDS, |
|
335 |
VAR_BLOCK_END_KEYWORDS, CONFIG_BLOCK_END_KEYWORDS, |
|
336 |
SFC_BLOCK_END_KEYWORDS, ST_BLOCK_END_KEYWORDS]), |
|
337 |
(IEC_KEYWORDS, [POU_KEYWORDS, TYPE_KEYWORDS, VAR_KEYWORDS, CONFIG_KEYWORDS, |
|
338 |
SFC_KEYWORDS, IL_KEYWORDS, ST_KEYWORDS])]: |
|
339 |
for keywords in keywords_list: |
|
340 |
all_keywords.extend([keyword for keyword in keywords if keyword not in all_keywords]) |
|
341 |
||
1390 | 342 |