@@ -13,7 +13,8 @@ def __init__(self, tag, attribute, nodes, bind=None, optional=False):
1313 self .optional = optional
1414
1515 def to_string (self , indent = 0 ):
16- return "%s%s%s[%s](%s%s)%s" % ("\n " if indent else "" , " " * indent , self .tag , self .attribute , "" .join (n .to_string (indent + 2 ) for n in self .nodes ), ("\n " + " " * indent ) if len (self .nodes ) else "" , (" -> %s" % self .bind ) if self .bind else "" )
16+ # return "%s%s%s[%s](%s%s)%s" % ("\n" if indent else "", " "*indent, self.tag, self.attribute, "".join(n.to_string(indent+2) for n in self.nodes), ("\n" + " "*indent) if len(self.nodes) else "", (" -> %s" % self.bind) if self.bind else "")
17+ return "<Rule %s %s>" % (self .tag , self .attribute )
1718
1819 def __repr__ (self ):
1920 return self .to_string ()
@@ -23,39 +24,40 @@ class template(object):
2324 Representation of an mvdXML template
2425 """
2526
26- def __init__ (self , concept , root , params = None , rules = None ):
27- self .concept , self .root , self .params = concept , root , params
27+ def __init__ (self , concept , root , constraints = None , rules = None ):
28+ self .concept , self .root , self .constraints = concept , root , ( constraints or [])
2829 self .rules = rules or []
2930 self .entity = str (root .attributes ['applicableEntity' ].value )
30- self .name = root .attributes ['name' ].value
31+ try :
32+ self .name = root .attributes ['name' ].value
33+ except :
34+ self .name = None
3135
32- def bind (self , params ):
33- return template (self .concept , self .root , params , self .rules )
36+ def bind (self , constraints ):
37+ return template (self .concept , self .root , constraints , self .rules )
3438
3539 def parse (self ):
36- for rules in self .root .childNodes :
37- if not isinstance (rules , Element ): continue
38-
40+ for rules in self .root .getElementsByTagName ("Rules" ):
3941 for r in rules .childNodes :
4042 if not isinstance (r , Element ): continue
4143 self .rules .append (self .parse_rule (r ))
4244
4345
4446 def traverse (self , fn , root = None , with_parents = False ):
45- def _ (n , p = root , ps = [root ]):
47+ def visit (n , p = root , ps = [root ]):
4648 if with_parents :
4749 close = fn (rule = n , parents = ps )
4850 else :
4951 close = fn (rule = n , parent = p )
5052
5153 for s in n .nodes :
52- _ (s , n , ps + [n ])
54+ visit (s , n , ps + [n ])
5355
5456 if close :
5557 close ()
5658
5759 for r in self .rules :
58- _ (r )
60+ visit (r )
5961
6062 def parse_rule (self , root ):
6163 def visit (node , prefix = "" ):
@@ -89,6 +91,8 @@ def child_has_ruleid_or_prefix(node):
8991 n = self .concept .template (ref ).root
9092 try : p = p + node .attributes ["IdPrefix" ].value
9193 except : pass
94+ elif node .tagName == "Constraint" :
95+ r = mvdxml_expression .parse (node .attributes ["Expression" ].value )
9296
9397 def _ (n ):
9498 for subnode in n .childNodes :
@@ -133,7 +137,10 @@ def template(self, id = None):
133137
134138 def rules (self ):
135139 # Get the top most TemplateRule and traverse
136- rules = self .concept_node .getElementsByTagName ("TemplateRules" )[0 ]
140+ try :
141+ rules = self .concept_node .getElementsByTagName ("TemplateRules" )[0 ]
142+ except :
143+ return []
137144
138145 def visit (rules ):
139146 def _ ():
@@ -167,15 +174,15 @@ def concepts(self):
167174 @staticmethod
168175 def parse (fn ):
169176 dom = parse (fn )
170- try :
171- root = dom .getElementsByTagName ("ConceptRoot" )[ 0 ]
172- CR = concept_root (dom , root )
173- return CR
174- except :
175- root = dom .getElementsByTagName ("ConceptTemplate" )[ 0 ]
176- t = template (None , root )
177- t .parse ()
178- return t
177+ if len ( dom . getElementsByTagName ( "ConceptRoot" )) :
178+ for root in dom .getElementsByTagName ("ConceptRoot" ):
179+ CR = concept_root (dom , root )
180+ yield CR
181+ else :
182+ for templ in dom .getElementsByTagName ("ConceptTemplate" ):
183+ t = template (None , templ )
184+ t .parse ()
185+ yield t
179186
180187
181188if __name__ == "__main__" :
@@ -202,6 +209,6 @@ def dump(rule, parents):
202209 t = c .template ()
203210 print ("RootEntity" , t .entity )
204211 t .traverse (dump , with_parents = True )
205- print (" " .join (map (str , t .params )))
212+ print (" " .join (map (str , t .constraints )))
206213
207214 print ()
0 commit comments