--- a/plcopen/structures.py Wed Apr 18 16:39:28 2007 +0200
+++ b/plcopen/structures.py Tue Jun 05 14:32:01 2007 +0200
@@ -1,6 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+import string, os, sys
+
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard.
#
@@ -44,197 +46,7 @@
- The default modifier which can be "none", "negated", "rising" or "falling"
"""
-BlockTypes = [{"name" : "Numerical functions", "list":
- [{"name" : "ADD", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")],
- "outputs" : [("OUT","ANY_NUM","none")],
- "comment" : "Addition,\nresult := I1 + I2 + ..."},
- {"name" : "MUL", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")],
- "outputs" : [("OUT","ANY_NUM","none")],
- "comment" : "Multiplication,\nresult := I1 * I2 * ..."},
- {"name" : "SUB", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")],
- "outputs" : [("OUT","ANY_NUM","none")],
- "comment" : "Substration,\nresult := I1 - I2"},
- {"name" : "DIV", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")],
- "outputs" : [("OUT","ANY_NUM","none")],
- "comment" : "Division,\nresult := I1 / I2\n(with integer division, any fractional remainder is truncated)"},
- {"name" : "MOD", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","ANY_INT","none"),("IN2","ANY_INT","none")],
- "outputs" : [("OUT","ANY_INT","none")],
- "comment" : "Modulus,\nresult := I1 MOD I2\n(only valid with integer values)"},
- {"name" : "EXPT", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","ANY_REAL","none"),("IN2","ANY_INT","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Exponential,\nresult := I1 ^ I2\n(can only take integer exponents)"},
- {"name" : "ABS", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_NUM","none")],
- "outputs" : [("OUT","ANY_NUM","none")],
- "comment" : "Absolute value (negative values become positive)"},
- {"name" : "SQRT", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Square root"},
- {"name" : "LOG", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Logarithm"},
- {"name" : "LN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Natural logarithm"},
- {"name" : "EXP", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Natural exponential"},
- {"name" : "SIN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Sine of input as radians"},
- {"name" : "COS", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Cosine of input as radians"},
- {"name" : "TAN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Tangent of input as radians"},
- {"name" : "ASIN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Principal arc-sine, result in radians"},
- {"name" : "ACOS", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Principal arc-cosine, result in radians"},
- {"name" : "ATAN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_REAL","none")],
- "outputs" : [("OUT","ANY_REAL","none")],
- "comment" : "Principal arc-tangent, result in radians"}
- ]},
- {"name" : "Boolean and bit functions", "list":
- [{"name" : "AND", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Result := I1 & I2 & ..."},
- {"name" : "OR", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Result := I1 OR I2 OR ..."},
- {"name" : "XOR", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY_BIT","none"),("IN2","ANY_BIT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Result := I1 XOR I2 XOR ..."},
- {"name" : "NOT", "type" : "function", "extensible" : False,
- "inputs" : [("IN","BIT","none")],
- "outputs" : [("OUT","BIT","none")],
- "comment" : "Result := NOT I1"},
- {"name" : "SHL", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Shift bit-string n bit positions left, zero fill on the right."},
- {"name" : "SHR", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")],
- "outputs" : [("OUT","ANY_BIT")],
- "comment" : "Shift bit-string right n bit positions, zero fill on the left."},
- {"name" : "ROR", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Shift bit-string right, rotate by n bit positions."},
- {"name" : "ROL", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY_BIT","none"),("N","ANY_INT","none")],
- "outputs" : [("OUT","ANY_BIT","none")],
- "comment" : "Shift bit-string left, rotate by n bit positions."}
- ]},
- {"name" : "Selection functions", "list":
- [{"name" : "SEL", "type" : "function", "extensible" : False,
- "inputs" : [("G","BOOL","none"),("IN0","ANY","none"),("IN1","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Selection\nIf G = TRUE then\nResult := IN1 else\nResult := IN2"},
- {"name" : "MAX", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Maximum\nResult := maximum value of all inputs"},
- {"name" : "MIN", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("","ANY","none")],
- "comment" : "Minimum\nResult := minimum value of all inputs"},
- {"name" : "LIMIT", "type" : "function", "extensible" : False,
- "inputs" : [("MN","ANY","none"),("IN","ANY","none"),("MX","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Limit\nResult is the value of IN limited between a minimum value of MN and a maximum value of MX."},
- {"name" : "MUX", "type" : "function", "extensible" : True,
- "inputs" : [("K","ANY_INT","none"),("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Multiplexer\nResult is the value of the input selected by the value of K."}
- ]},
- {"name" : "Comparison functions", "list":
- [{"name" : "GT", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Greater than\nResult := IN1 > IN2"},
- {"name" : "GE", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Greater than or equal\nResult := IN1 >= IN2"},
- {"name" : "EQ", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Equality\nResult := IN1 = IN2"},
- {"name" : "LE", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Lesser than or equal\nResult := IN1 <= IN2"},
- {"name" : "LT", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Lesser than\nResult := IN1 < IN2"},
- {"name" : "NE", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","ANY","none"),("IN2","ANY","none")],
- "outputs" : [("OUT","ANY","none")],
- "comment" : "Not equal\nResult := IN1 <> IN2"}
- ]},
- {"name" : "Character string functions", "list":
- [{"name" : "LEFT", "type" : "function", "extensible" : False,
- "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Extract left string\nResult is the string formed from L characters from the leftmost character of string IN."},
- {"name" : "RIGHT", "type" : "function", "extensible" : False,
- "inputs" : [("IN","STRING","none"),("L","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Extract right string\nResult is the string formed from L characters from the rightmost part of string IN."},
- {"name" : "MID", "type" : "function", "extensible" : False,
- "inputs" : [("IN","ANY","none"),("L","ANY_INT","none"),("P","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Extract mid string\nResult is a string extracted from the input string IN starting at character position P, and L characters long."},
- {"name" : "CONCAT", "type" : "function", "extensible" : True,
- "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Concatenate strings\nResult is a string formed by joining the input strings together. This is an extensible function that can take two or more input strings."},
- {"name" : "INSERT", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("P","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Insert string\nThe result is formed by the string IN2 being inserted into string IN1, P character positions from the start of IN1."},
- {"name" : "DELETE", "type" : "function", "extensible" : False,
- "inputs" : [("IN","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Delete string\nThe result is formed by a string of characters L in length, being deleted from the input string IN, starting from character position P."},
- {"name" : "REPLACE", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","STRING","none"),("IN2","STRING","none"),("L","ANY_INT","none"),("P","ANY_INT","none")],
- "outputs" : [("OUT","STRING","none")],
- "comment" : "Replace string\nThe result is formed by replacing L characters in string IN1, starting at position P, with character string in IN2."},
- {"name" : "LEN", "type" : "function", "extensible" : False,
- "inputs" : [("IN","STRING","none")],
- "outputs" : [("OUT","INT","none")],
- "comment" : "Length\nResult is length of the input string."},
- {"name" : "FIND", "type" : "function", "extensible" : False,
- "inputs" : [("IN1","STRING","none"),("IN2","STRING","none")],
- "outputs" : [("OUT","INT","none")],
- "comment" : "Find string\nResult is the position where string IN2 is first found in string IN1.\nIf string IN2 is not found in IN1, the result is 0."},
- ]},
- {"name" : "Standard function blocks", "list":
+BlockTypes = [{"name" : "Standard function blocks", "list":
[{"name" : "SR", "type" : "functionBlock", "extensible" : False,
"inputs" : [("S1","BOOL","none"),("R","BOOL","none")],
"outputs" : [("Q1","BOOL","none")],
@@ -377,7 +189,124 @@
test = TypeHierarchy[test]
return False
-
+def GetSubTypes(reference):
+ return [ typename for typename in TypeHierarchy.iterkeys() if typename[:3] != "ANY" and IsOfType(typename, reference)]
+
+
+
+
+
+def csv_file_to_table(file):
+ return [ map(string.strip,line.split(';')) for line in file.xreadlines()]
+
+def find_section(section_name, table):
+ fields = [None]
+ while(fields[0] != section_name):
+ fields = table.pop(0)
+ return fields[1:]
+
+
+def get_standard_funtions_input_variables(table):
+ variables = find_section("Standard_functions_variables_types", table)
+ standard_funtions_input_variables = {}
+ fields = [True,True]
+ while(fields[1]):
+ fields = table.pop(0)
+ variable_from_csv = dict([(champ, val) for champ, val in zip(variables, fields[1:])])
+ standard_funtions_input_variables[variable_from_csv['name']] = variable_from_csv['type']
+ return standard_funtions_input_variables
+
+#"(ANY_NUM, ANY_NUM)" ---> [("IN1","ANY_NUM","none"),("IN2","ANY_NUM","none")]
+def csv_input_translate(str_decl, variables, base):
+ decl = str_decl.replace('(','').replace(')','').replace(' ','').split(',')
+ param_types = []
+ param_names = []
+ modifiers = []
+ if len(decl)>1 : suffix = str(base)
+ else: suffix = ''
+
+ for param_type in decl:
+ predifined_variable_param_type = variables.get(param_type,None)
+ if predifined_variable_param_type :
+ param_types.append(predifined_variable_param_type)
+ param_names.append(param_type)
+ else:
+ param_types.append(param_type)
+ param_names.append("IN"+suffix)
+ base+=1
+ suffix = str(base)
+
+ modifiers = ["none"]*len(param_types)
+ return zip(param_names,param_types,modifiers)
+
+def decl_function(dico_from_table, variables):
+ Function_decl = { "type" : "function" }
+ for field, val in dico_from_table:
+ translate = {
+ "baseinputnumber" : lambda x:int(x),
+ "extensible" : lambda x: {"yes":True, "no":False}[x],
+ "inputs" : lambda x:csv_input_translate(x,variables,Function_decl.get("baseinputnumber",1)),
+ "outputs":lambda x:[("OUT",x,"none")]}
+ Function_decl[field] = translate.get(field,lambda x:x)(val)
+ #Function_decl.pop("baseinputnumber")
+ Function_decl.pop("overloaded")
+ return Function_decl
+
+
+def get_standard_funtions(table):
+
+ variables = get_standard_funtions_input_variables(table)
+
+ fonctions = find_section("Standard_functions_type",table)
+
+ Standard_Functions_Decl = []
+ Current_section = None
+ for fields in table:
+ if fields[1]:
+ # If function section name given
+ if fields[0] :
+ if Current_section:
+ Standard_Functions_Decl.append(Current_section)
+ Current_section = { "name" : fields[0], "list" : [] }
+ Function_decl_list = []
+
+ dico_from_table = zip(fonctions, fields[1:])
+ Function_decl = decl_function(dico_from_table,variables)
+
+ if Function_decl["name"].startswith("*"):
+ input_types = GetSubTypes(Function_decl["inputs"][0][1])
+ else:
+ input_types = [None]
+ if Function_decl["name"].endswith("**"):
+ output_types = GetSubTypes(Function_decl["outputs"][0][1])
+ else:
+ output_types = [None]
+
+ funcdecl = Function_decl["name"]
+ for intype in input_types:
+ if intype != None:
+ decl_tpl = Function_decl["inputs"][0]
+ Function_decl["inputs"] = [decl_tpl[:1] + (intype,) + decl_tpl[2:]] + Function_decl["inputs"][1:]
+ funcdeclin = funcdecl.replace("*_", intype + '_')
+ Function_decl["name"] = funcdeclin
+
+ for outype in output_types:
+ if outype != None:
+ decl_tpl = Function_decl["outputs"][0]
+ Function_decl["outputs"] = [decl_tpl[:1] + (outype,) + decl_tpl[2:]] + Function_decl["outputs"][1:]
+ funcdeclout = funcdeclin.replace("_**", '_' + outype)
+ Function_decl["name"] = funcdeclout
+ Current_section["list"].append(Function_decl.copy())
+
+ return Standard_Functions_Decl
+
+
+to_append=get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv"))))
+import pprint
+pp = pprint.PrettyPrinter(indent=4)
+pp.pprint(to_append)
+BlockTypes.extend(to_append)
+pp.pprint(BlockTypes)
#-------------------------------------------------------------------------------
# Test identifier
#-------------------------------------------------------------------------------