[Hejes-devel] [852] egybekulon2_iface: added Rule.__str__()

hejes-devel at nytud.hu hejes-devel at nytud.hu
Thu Jul 18 12:59:27 CEST 2013


Revision: 852
Author:   mihaltz
Date:     2013-07-18 12:59:27 +0200 (Thu, 18 Jul 2013)
Log Message:
-----------
egybekulon2_iface: added Rule.__str__()

Modified Paths:
--------------
    trunk/web2py/applications/helyesiras_webdev/modules/egybekulon2_iface.py

Modified: trunk/web2py/applications/helyesiras_webdev/modules/egybekulon2_iface.py
===================================================================
--- trunk/web2py/applications/helyesiras_webdev/modules/egybekulon2_iface.py	2013-07-17 13:55:20 UTC (rev 851)
+++ trunk/web2py/applications/helyesiras_webdev/modules/egybekulon2_iface.py	2013-07-18 10:59:27 UTC (rev 852)
@@ -8,7 +8,7 @@
 
 @author: MM
 """
-from egybekulon2_def import DISPLAY_NODE_ATTRIBUTES
+from egybekulon2_def import DISPLAY_NODE_ATTRIBUTES, GRM_LHS_ATTRIBUTE_NAMES, GRM_RHS_ATTRIBUTE_NAMES
 
 class Node:
   """ Input/output class for class Ruler (parser).
@@ -41,6 +41,7 @@
     """ Returns a Node object: the parent of this Node in a tree.
 	    or returns None if this node is the root node.
 	"""
+    pass
 
   def __str__(self):
     return self.toStr()
@@ -72,9 +73,35 @@
     self.lhs_ops = [] # attribute-value operators in each node in left-hand side of rule: [{attribute_name: operator, ...}, ...]. Note: len(lhs_ops) == len(lhs) always (each map in lhs_ops corresponds to a tuple in lhs) and lhs_ops[i].keys() == lhs[i][1].keys() for each i=1..len(ops) (the list of attribute names in both lhs and and lhs_ops must be identical in each node in rule). Valid operators are listed in GRM_ATTRIBUTE_OPERATORS and GRM_NUMERIC_ATTRIBUTE_OPERATORS in egybekulon2_def.py.
     self.rhs = None # right-hand side of rule: a (category name, attribute-value map) pair, e.g. ('N', {'sep': ['']})
     self.exs = [] # rule examples: [([inp1, ...], outp), ...]
-  
+
+  def __str__(self):
+    """Return string representation of rule as in grammar file (utf8)"""
+    ret = 'id: {0}\n'.format(self.id)
+    lhs = []
+    for (node, valmap), opmap in zip(self.lhs, self.lhs_ops):
+      avm = []
+      for attr in [x for x in GRM_LHS_ATTRIBUTE_NAMES if x in valmap.keys()]:
+        if len(valmap[attr]) > 1:
+          avm.append('{0}{1}[{2}]'.format(attr, opmap[attr], ', '.join(['"{0}"'.format(x) for x in valmap[attr]])))
+        else:
+          avm.append('{0}{1}"{2}"'.format(attr, opmap[attr], valmap[attr][0]))
+      lhs.append('{0}({1})'.format(node, ', '.join(avm)))
+    avm = []
+    for attr in [x for x in GRM_RHS_ATTRIBUTE_NAMES if x in self.rhs[1].keys()]:
+      if len(self.rhs[1][attr]) > 1:
+        avm.append('{0}=[{1}]'.format(attr, ', '.join(['"{0}"'.format(x) for x in self.rhs[1][attr]])))
+      else:
+        avm.append('{0}="{1}"'.format(attr, self.rhs[1][attr][0]))
+    ret += 'rule: {0} == {1}({2})\n'.format(' + '.join(lhs), self.rhs[0], ', '.join(avm))
+    ret += 'comment: {0}\n'.format(self.comment)
+    ret += 'refs: {0}\n'.format(', '.join(self.refs))
+    ret += 'kill: {0}\n'.format(', '.join(self.kills))
+    ret += 'ex: {0}'.format(', '.join(["{0} = {1}".format(' + '.join(x[0]), x[1]) for x in self.exs]))
+    return ret
+
   def toStr(self):
-    """Returns string representation of Rule."""
+    """OBSOLETE, use __str__() instead.
+       Returns string representation of Rule."""
     ret  = '{id: ' + self.id + ', '
     ret += 'lhs: ' + str(self.lhs) + ', '
     ret += 'lhs_ops: ' + str(self.lhs_ops) + ', '
@@ -116,6 +143,7 @@
   r.lhs = [('A', {'sem': ['Color3']}), ('N', {'sem': ['Color1']})]
   r.rhs = ('N', {'sep': ['']})
   print(r.toStr())
+  print(str(r))
   
 if __name__ == '__main__':
   _test()




More information about the Hejes-devel mailing list