Esempio n. 1
0
 def __restore_previous_state(self, grammar: Grammar) -> None:
     to_remove = set(grammar.get_rules())
     for rule in to_remove:
         grammar.remove_rule(rule)
         self.iteration.remove_crowding_rule(rule)
     for rule in self.__last_rules:
         grammar.add_rule(rule)
         self.iteration.add_rule(rule)
Esempio n. 2
0
 def add_new_rule(self,
                  grammar: Grammar,
                  first_symbol: Symbol,
                  second_symbol: Symbol = None) -> Rule:
     new_rule = Rule([grammar.get_start_symbol(), first_symbol])
     new_rule.origin = RuleOrigin.COVERING
     grammar.add_rule(new_rule)
     self.iteration.add_covering_rule(new_rule)
     return new_rule
Esempio n. 3
0
 def __replace_all_symbols_in_rules(self, replacement: Symbol,
                                    replaced: Symbol,
                                    grammar: Grammar) -> None:
     rules: Set[Rule] = grammar.get_rules()
     rules_copy = set(rules)
     for rule in rules_copy:
         if self.__rule_contains_replaced_symbol(rule, replaced):
             new_rule = self.__replace_symbols_in_rule(
                 replacement, replaced, rule)
             grammar.remove_rule(rule)
             grammar.add_rule(new_rule)
Esempio n. 4
0
 def add_rules(self, grammar: Grammar, new_rules: List[Rule]) -> None:
     for rule in new_rules:
         grammar.add_rule(rule)
Esempio n. 5
0
 def add_rule(self, grammar: Grammar, rule: Rule) -> bool:
     grammar.add_rule(rule)
     return True
Esempio n. 6
0
    def split(self, grammar: Grammar) -> None:
        replaced: Symbol = self.__find_nonterminal(grammar)
        replacement: Tuple[Symbol,
                           Symbol] = self.__find_unused_symbols_pair(grammar)
        print("[split] replaced = {} replacement = {}".format(
            replaced, replacement))
        if replacement is not None:
            rules: Set[Rule] = grammar.get_rules()
            rules_copy = set(rules)
            for rule in rules_copy:
                if rule.left == replaced:
                    first_rule = self.__create_rule(replacement[0],
                                                    rule.right1, rule.right2)
                    second_rule = self.__create_rule(replacement[1],
                                                     rule.right1, rule.right2)
                    grammar.remove_rule(rule)
                    grammar.add_rule(first_rule)
                    grammar.add_rule(second_rule)
                    self.iteration.remove_crowding_rule(rule)
                    self.iteration.add_rule(first_rule)
                    self.iteration.add_rule(second_rule)

            rules: Set[Rule] = grammar.get_rules()
            rules_copy = set(rules)
            for rule in rules_copy:
                if rule.right1 == replaced:
                    grammar.remove_rule(rule)
                    first_rule = self.__create_rule(rule.left, replacement[0],
                                                    rule.right2)
                    second_rule = self.__create_rule(rule.left, replacement[1],
                                                     rule.right2)
                    grammar.add_rule(first_rule)
                    grammar.add_rule(second_rule)
                    self.iteration.remove_crowding_rule(rule)
                    self.iteration.add_rule(first_rule)
                    self.iteration.add_rule(second_rule)

            rules: Set[Rule] = grammar.get_rules()
            rules_copy = set(rules)
            for rule in rules_copy:
                if rule.right2 == replaced:
                    first_rule = self.__create_rule(rule.left, rule.right1,
                                                    replacement[0])
                    second_rule = self.__create_rule(rule.left, rule.right1,
                                                     replacement[1])
                    grammar.remove_rule(rule)
                    grammar.add_rule(first_rule)
                    grammar.add_rule(second_rule)
                    self.iteration.remove_crowding_rule(rule)
                    self.iteration.add_rule(first_rule)
                    self.iteration.add_rule(second_rule)
Esempio n. 7
0
 def add_new_rule(self, grammar: Grammar, first_symbol: Symbol, second_symbol: Symbol = None) -> Rule:
     new_rule = Rule([RandomUtils.get_random_nonterminal_symbol_from(grammar), first_symbol])
     new_rule.origin = RuleOrigin.COVERING
     grammar.add_rule(new_rule)
     self.iteration.add_covering_rule(new_rule)
     return new_rule