def calculate_number_of_rules_to_add(self, grammar: Grammar): grammar_size = len(grammar.get_non_terminal_rules()) round_to_even = lambda x: round((x - 0.5) / 2) * 2 if self.new_rules_number_percent_unit: if self.new_rules_number >= 100: return round_to_even(grammar_size) else: return round((grammar_size * self.new_rules_number) / 200) * 2 else: if self.new_rules_number >= len(grammar.get_non_terminal_rules()): return round_to_even(grammar_size) else: return round_to_even(self.new_rules_number)
def run(self, grammar: Grammar) -> None: if self.__run_counter != 0: self.save_grammar_state(grammar) if len(grammar.get_non_terminal_rules()) > 2 * int( self.settings.get_value( 'general', 'non_terminal_productions_number')): self.merge(grammar) else: if self.__run_counter % 2 == 1: self.split(grammar) else: self.merge(grammar) self.__run_counter += 1
def initialize_rules(self): self.crowding.is_non_terminal_type = MagicMock(return_value=True) self.crowding.filter_non_terminal_to_terminal_terminal_rule = MagicMock() grammar = Grammar() grammar.get_non_terminal_rules = MagicMock(return_value=self.rules) rule = self.__create_rule(["x", "y", "z"]) self.crowding.initialize_rules(rule, grammar) self.crowding.filter_non_terminal_to_terminal_terminal_rule.assert_called_once_with(self.rules) self.crowding.is_non_terminal_type = MagicMock(return_value=False) result = self.crowding.initialize_rules(rule, grammar) self.assertEqual(result, self.rules)
def select_rule(self, grammar: Grammar, selection_type: GeneticSelectionType) -> Rule: selection_method = self.selection_map[selection_type] return selection_method(grammar.get_non_terminal_rules())