Ejemplo n.º 1
0
 def __init__(self, population_editor):
     super().__init__(population_editor.widget)
     self.population_editor = population_editor
     self.translator = None
     self.population_executor = PopulationExecutor()
     self.simulation_executor = SimulationExecutor()
     self.operation = None
     self.randomizer = Randomizer(Random())
Ejemplo n.º 2
0
class LoadPopulationWorker(QtCore.QThread):
    TRANSLATOR_READY_SIGNAL = 'TRANSLATOR_READY_SIGNAL'
    POPULATION_LOADED_SIGNAL = 'POPULATION_LOADED_SIGNAL'
    STARTING_SYMBOL = Symbol(1)

    def __init__(self, population_editor):
        super().__init__(population_editor.widget)
        self.population_editor = population_editor
        self.translator = None
        self.population_executor = PopulationExecutor()
        self.simulation_executor = SimulationExecutor()
        self.operation = None
        self.randomizer = Randomizer(Random())

    def run(self):
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.CHANGE_STEP_EVENT), 'Operation in progress...')
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.SET_PROGRESS_EVENT), -1)
        if self.operation is not None:
            # noinspection PyCallingNonCallable
            self.operation()
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.RESET_EVENT))

    def load_learning_set_operation(self):
        try:
            self.translator = self.population_executor.get_learned_translator(
                self.population_editor.population_path)
        except:
            dialog = QtGui.QMessageBox()
            dialog.critical(
                self.population_editor.widget, 'Invalid file',
                'File "{0}" has invalid format!'.format(self.population_editor.population_path))
        finally:
            self.emit(QtCore.SIGNAL(self.TRANSLATOR_READY_SIGNAL))

    def load_population_operation(self):
        name = os.path.basename(self.population_editor.population_path).split('.')[0]
        dir_path = os.path.dirname(self.population_editor.population_path)
        rule_population = self.simulation_executor.load_population(
            dir_path, name, starting_symbol=self.STARTING_SYMBOL)

        self.population_editor.rules.clear()

        for rule in rule_population.get_terminal_rules():
            translated = rule.human_friendly_representation(rule_population.symbol_shift(),
                                                            rule_population.starting_symbol,
                                                            rule_population.universal_symbol)
            rule_model = TerminalRuleModel()
            rule_model.parent = translated[0]
            rule_model.left_child = self.translator.symbol_to_word(rule.left_child)
            self.population_editor.rules.append(rule_model)

        for rule in rule_population.get_all_non_terminal_rules():
            translated = rule.human_friendly_representation(rule_population.symbol_shift(),
                                                            rule_population.starting_symbol,
                                                            rule_population.universal_symbol)
            rule_model = NonTerminalRuleModel()
            rule_model.parent, rule_model.left_child, rule_model.right_child = translated
            self.population_editor.rules.append(rule_model)

        self.emit(QtCore.SIGNAL(self.POPULATION_LOADED_SIGNAL))

    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)