diff --git a/rpg/rule.py b/rpg/rule.py index 15af66b2b98e61f1dd57a238afff992cc7a910da..8e16b2953be6a524651e71e1d73c4274ce997256 100644 --- a/rpg/rule.py +++ b/rpg/rule.py @@ -582,25 +582,40 @@ def create_rules(all_rules): exceptions.append(dict_rule) # No exception else: - # Parse all after cleaving site first - for i in reversed(sorted(dict_rule_no_main.keys())): - # Find the deepest rule: left if exist, otherwise, right + # Find the deepest rule: left if exist, otherwise, right + if len(dict_rule_no_main.keys()) > 0: min_key = min(dict_rule_no_main.keys()) # Deep on left max_key = max(dict_rule_no_main.keys()) # Deep on right - if min_key < 0: # Something left, change it (leftest) - if i == min_key: - cleav = not cleav + # Something left + if min_key < 0: + # Parse all after cleaving site first in reverse + for i in reversed(sorted(dict_rule_no_main.keys())): + # Deeper rule, change it (leftest) + if i == min_key: + cleav = not cleav + # Create the rule + this_rule = Rule(i, dict_rule_no_main[i], cleav, -1) + # Add this rule to upper-rule + if not previous_rule.equ(this_rule) and\ + not previous_rule.contains_any_level(this_rule): + previous_rule.rules.append(this_rule) + # Current rule is the new down-rule + previous_rule = this_rule + else: # Nothing left, change deepest rule at right - if i == max_key: - cleav = not cleav - # Create the rule - this_rule = Rule(i, dict_rule_no_main[i], cleav, -1) - # Add this rule to upper-rule - if not previous_rule.equ(this_rule) and\ - not previous_rule.contains_any_level(this_rule): - previous_rule.rules.append(this_rule) - # Current rule is the new down-rule - previous_rule = this_rule + # Parse all after cleaving site first + for i in sorted(dict_rule_no_main.keys()): + # Deeper rule, change it (rightest) + if i == max_key: + cleav = not cleav + # Create the rule + this_rule = Rule(i, dict_rule_no_main[i], cleav, -1) + # Add this rule to upper-rule + if not previous_rule.equ(this_rule) and\ + not previous_rule.contains_any_level(this_rule): + previous_rule.rules.append(this_rule) + # Current rule is the new down-rule + previous_rule = this_rule # Create the corresponding rule add_rule(correct_rules, cleaving_zone)