Ejemplo n.º 1
0
    def save_population_operation(self):
        population = RulePopulation(self.STARTING_SYMBOL)
        for rule_model in self.population_editor.rules:
            if rule_model.is_terminal:
                parent, terminal_word = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )
                terminal_symbol = self.translator.word_to_symbol(terminal_word)

                population.add_rule(Rule(parent, terminal_symbol), self.randomizer)
            else:
                parent, left, right = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )

                population.add_rule(Rule(parent, left, right), self.randomizer)

        name = os.path.basename(self.population_editor.population_path).split('.')[0]
        path = os.path.dirname(self.population_editor.population_path)
        self.simulation_executor.save_population_data(population, path, name)
Ejemplo n.º 2
0
    def create_rules(self, rules):
        rule_population = RulePopulation(Symbol('S'), universal_symbol=Symbol('U'))
        for rule in rules:
            rule_population.add_rule(rule, self.randomizer)

        return rule_population
class TestRulePopulation(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sut = RulePopulation('S')

        self.rules = [
            Rule('A', 'B', 'C'),
            Rule('D', 'B', 'C'),
            Rule('A', 'J', 'C'),
            Rule('A', 'B', 'J')
        ]

        self.randomizer_mock = create_autospec(Randomizer)

    def add_rules(self):
        for rule in self.rules:
            self.sut.add_rule(rule, self.randomizer_mock)

        assert_that(self.sut.get_all_non_terminal_rules(), contains_inanyorder(*self.rules))

    def test_adding_rule_should_result_in_storing_it(self):
        # Given:
        self.add_rules()

        # When/Then:
        assert_that(self.sut.get_rules_by_right(('B', 'C')),
                    only_contains(self.rules[0], self.rules[1]))
        assert_that(self.sut.get_rules_by_right(('B', 'B')), is_(empty()))
        assert_that(self.sut.get_rules_by_right(('A', 'B')), is_(empty()))
        assert_that(calling(self.sut.get_rules_by_right).with_args(('A',)),
                    raises(RulePopulationAccessViolationError))
        assert_that(calling(self.sut.get_rules_by_right).with_args(('A', 'B', 'J')),
                    raises(RulePopulationAccessViolationError))

    def test_should_be_able_to_add_terminal_rule(self):
        rule_a = TerminalRule(Symbol('A'), Symbol('a'))
        rule_b = TerminalRule(Symbol('B'), Symbol('a'))
        self.sut.add_rule(rule_a, self.randomizer_mock)
        self.sut.add_rule(rule_b, self.randomizer_mock)

        assert_that(self.sut._rules_by_right, is_(empty()))
        assert_that(self.sut.get_terminal_rules(Symbol('a')), only_contains(rule_a, rule_b))
        assert_that(self.sut.get_terminal_rules(Symbol('b')), is_(empty()))
        assert_that(self.sut.get_terminal_rules(), only_contains(rule_a, rule_b))

    def test_universal_symbol_should_work_properly(self):
        # Given:
        rule_a = TerminalRule(self.sut.universal_symbol, Symbol('a'))
        rule_b = TerminalRule(self.sut.universal_symbol, Symbol('b'))

        # When:
        self.sut.add_rule(rule_a, self.randomizer_mock)
        self.sut.add_rule(rule_b, self.randomizer_mock)

        # Then:
        assert_that(self.sut._rules_by_right, is_(empty()))
        assert_that(self.sut.get_terminal_rules(Symbol('a')), only_contains(rule_a))
        assert_that(self.sut.get_terminal_rules(Symbol('b')), only_contains(rule_b))
        assert_that(self.sut.get_terminal_rules(), only_contains(rule_a, rule_b))

    def test_should_be_able_to_obtain_random_population(self):
        # Given:
        self.add_rules()

        self.randomizer_mock.sample.return_value = [Rule('A', 'J', 'C'), Rule('D', 'B', 'C')]

        # When:
        rules = self.sut.get_random_rules(self.randomizer_mock, False, 2)

        # Then:
        assert_that(rules, only_contains(Rule('D', 'B', 'C'), Rule('A', 'J', 'C')))
        assert_that(self.randomizer_mock.sample.called)

    def test_should_be_able_to_remove_a_rule(self):
        # Given:
        self.add_rules()
        assert_that(
            self.sut.get_rules_by_right((self.rules[0].left_child, self.rules[0].right_child)),
            only_contains(self.rules[0], self.rules[1])
        )

        # When:
        self.sut.remove_rule(self.rules[0])

        # Then:
        assert_that(
            self.sut.get_rules_by_right((self.rules[0].left_child, self.rules[0].right_child)),
            only_contains(self.rules[1])
        )

    def test_should_be_able_to_obtain_random_population_matching_filter(self):
        # Given:
        self.add_rules()

        filter = lambda x: x.right_child == 'C'

        self.randomizer_mock.sample.return_value = [Rule('A', 'J', 'C'), Rule('D', 'B', 'C')]

        # When:
        rules = self.sut.get_random_rules_matching_filter(self.randomizer_mock, False, 2, filter)

        # Then:
        assert_that(rules, only_contains(Rule('D', 'B', 'C'), Rule('A', 'J', 'C')))
        assert_that(self.randomizer_mock.sample.call_count, is_(equal_to(1)))

    def test_should_know_if_rule_already_exists(self):
        # Given:
        self.add_rules()
        not_added_rule = Rule('J', 'J', 'J')

        # When:
        for rule in self.rules:
            assert_that(self.sut.has_rule(rule))
        assert_that(not_(self.sut.has_rule(not_added_rule)))