Esempio n. 1
0
def test_load_population():
    reset_project()

    initializer_ = SmellInitialization(
        config.UDB_PATH,
        population_size=config.POPULATION_SIZE,
        lower_band=config.LOWER_BAND,
        upper_band=config.UPPER_BAND
    )

    initializer_.load_population(config.PROJECT_PATH + '_population.json')
Esempio n. 2
0
def test_generate_population():
    reset_project()

    initializer_ = SmellInitialization(
        config.UDB_PATH,
        population_size=config.POPULATION_SIZE,
        lower_band=config.LOWER_BAND,
        upper_band=config.UPPER_BAND
    )
    population_ = initializer_.generate_population()
    print(population_)
Esempio n. 3
0
    def generate_population(self):
        """

        Generate a biased initial population consists of first-time validated refactorings

        Return:

            list: list of refactoring sequences (list of refactoring operations)

        """

        config.logger.debug(f'Generating a biased initial population ...')
        for _ in range(0, self.population_size):
            individual = []
            individual_size = random.randint(self.lower_band, self.upper_band)
            for j in range(individual_size):
                main, params, name = self.select_random()
                logger.debug(f'Refactoring name: {name}')
                logger.debug(f'Refactoring params: {params}')
                is_correct_refactoring = main(**params)
                while is_correct_refactoring is False:
                    reset_project()
                    main, params, name = self.select_random()
                    logger.debug(f'Refactoring name: {name}')
                    logger.debug(f'Refactoring params: {params}')
                    is_correct_refactoring = main(**params)

                ####
                # update_understand_database(self.udb_path)
                # quit()
                ####

                individual.append((main, params, name))
                logger.debug(f'Append a refactoring "{name}" to "{j}th" gene of the individual {_}.')
                reset_project()
                logger.debug('-' * 100)

            self.population.append(individual)
            logger.debug(f'Append individual {_} to population, s')

        logger.debug('=' * 100)
        initial_pop_path = f'{config.PROJECT_LOG_DIR}initial_population_{config.global_execution_start_time}.json'
        self.dump_population(path=initial_pop_path)
        config.logger.debug(f'Generating a biased initial population was finished.')
        return self.population
Esempio n. 4
0
    def execute_from_json_log(self, input_file_path=None, reset=True):
        if input_file_path is None:
            input_file_path = glob.glob(
                os.path.join(self.log_directory,
                             'best_refactoring_sequences*.json'))[0]

        # log_project_info(reset_=True, )

        population = []
        with open(input_file_path, 'r', encoding='utf-8') as fp:
            population_trimmed = json.load(fp)

        for chromosome in population_trimmed:
            chromosome_new = []
            for gene_ in chromosome:
                refactoring_params = gene_[1]
                if refactoring_params.get('udb_path'):
                    refactoring_params['udb_path'] = config.UDB_PATH
                if refactoring_params.get('project_dir'):
                    refactoring_params['project_dir'] = config.PROJECT_PATH
                if refactoring_params.get('file_path'):
                    refactoring_params['file_path'] = refactoring_params[
                        'file_path'].replace(self.file_path_base_dir_old,
                                             config.PROJECT_ROOT_DIR)
                if refactoring_params.get('class_path'):
                    refactoring_params['class_path'] = refactoring_params[
                        'class_path'].replace(self.file_path_base_dir_old,
                                              config.PROJECT_ROOT_DIR)
                chromosome_new.append((REFACTORING_MAIN_MAP[gene_[0]],
                                       refactoring_params, gene_[0]))
            population.append(chromosome_new)

        # print(population)
        # quit()
        applicability_map = {
            'Project': [],
            'Sequence': [],
            'Applied refactorings': [],
            'Rejected refactorings': []
        }
        for k, refactoring_sequence in enumerate(population):
            true_refactorings_count = 0
            false_refactorings_count = 0
            reset_project()

            # Apply sequence X to system S
            for refactoring_operation in refactoring_sequence:
                res = refactoring_operation[0](**refactoring_operation[1])
                update_understand_database(config.UDB_PATH)
                if res:
                    true_refactorings_count += 1
                else:
                    false_refactorings_count += 1
                config.logger.info(
                    f"Executed {refactoring_operation[2]} with status {res}")

            applicability_map['Project'].append(config.PROJECT_NAME)
            applicability_map['Sequence'].append(k)
            applicability_map['Applied refactorings'].append(
                true_refactorings_count)
            applicability_map['Rejected refactorings'].append(
                false_refactorings_count)

            continue

            # Dump refactored project
            dump_path = os.path.join(
                config.PROJECT_ROOT_DIR,
                f'{config.PROJECT_NAME}_refactored_with_algorithm{config.PROBLEM}_rand',
                f'dump{k}')

            if not os.path.exists(config.PROJECT_ROOT_DIR):
                os.mkdir(dump_path)

            shutil.copytree(config.PROJECT_PATH, dump_path)

            # Compute quality metrics
            log_project_info(
                reset_=False,
                quality_attributes_path=os.path.join(
                    config.PROJECT_LOG_DIR,
                    'best_refactoring_sequences_objectives_extended.csv'),
                generation='-1',  # config.MAX_ITERATIONS,
                testability_verbose=True,
                testability_log_path=os.path.join(
                    config.PROJECT_LOG_DIR,
                    f'classes_testability2_for_problem_{config.PROBLEM}_best_sequence_{k}.csv'
                ))

        # Log applied and rejected refactorings
        df = pd.DataFrame(data=applicability_map)
        df.to_csv(os.path.join(config.PROJECT_LOG_DIR,
                               'applicability_map.csv'),
                  index=False)

        if reset:
            reset_project()
Esempio n. 5
0
            res = REFACTORING_MAIN_MAP[refactoring_name](**params)
            print(f"Executed {refactoring_name} with status {res}")
            update_understand_database(config.UDB_PATH)
            print('-' * 100)


def execute_refactoring_sequence():
    directory_name_ = os.path.dirname(__file__)
    refactoring_sequence_input_file = os.path.join(directory_name_,
                                                   r'utils/input.txt')
    # reset_project()
    # execute_from_txt_log(input_file_path=refactoring_sequence_input_file)


def measure_ad_hoc_project_quality():
    # create_understand_database(project_dir=config.PROJECT_PATH, db_dir=config.UDB_ROOT_DIR)
    log_project_info()


if __name__ == '__main__':
    # measure_ad_hoc_project_quality()
    # quit()
    reset_project()
    # quit()
    # execute_refactoring_sequence()
    eval_ = RefactoringSequenceEvaluation(log_directory=config.PROJECT_LOG_DIR)
    eval_.execute_from_json_log(reset=False)
    # eval_.evaluate_sequences()

    # eval_.analysis_refactoring_sequences()
Esempio n. 6
0
def log_project_info(reset_=True, design_metrics_path=None, quality_attributes_path=None,
                     generation=0, testability_verbose=True, testability_log_path=None):
    """

    Logging project metrics and information

    """

    if reset_:
        reset_project()
    if quality_attributes_path is None:
        quality_attributes_path = os.path.join(config.PROJECT_LOG_DIR, 'quality_attrs_initial_values.csv')
    if design_metrics_path is None:
        design_metrics_path = os.path.join(config.PROJECT_LOG_DIR, 'design_metrics.csv')

    design_quality_attribute = DesignQualityAttributes(config.UDB_PATH)
    avg_, sum_ = design_quality_attribute.average_sum
    predicted_testability = testability_main(
        config.UDB_PATH,
        initial_value=config.CURRENT_METRICS.get("TEST", 1.0),
        verbose=testability_verbose,
        log_path=testability_log_path
    )
    mdg_modularity = modularity_main(
        config.UDB_PATH,
        initial_value=config.CURRENT_METRICS.get("MODULE", 1.0)
    )

    design_metrics = {
        "DSC": [design_quality_attribute.DSC],
        "NOH": [design_quality_attribute.NOH],
        "ANA": [design_quality_attribute.ANA],
        "MOA": [design_quality_attribute.MOA],
        "DAM": [design_quality_attribute.DAM],
        "CAMC": [design_quality_attribute.CAMC],
        "CIS": [design_quality_attribute.CIS],
        "NOM": [design_quality_attribute.NOM],
        "DCC": [design_quality_attribute.DCC],
        "MFA": [design_quality_attribute.MFA],
        "NOP": [design_quality_attribute.NOP]
    }

    quality_objectives = {
        "generation": [generation],
        "reusability": [design_quality_attribute.reusability],
        "understandability": [design_quality_attribute.understandability],
        "flexibility": [design_quality_attribute.flexibility],
        "functionality": [design_quality_attribute.functionality],
        "effectiveness": [design_quality_attribute.effectiveness],
        "extendability": [design_quality_attribute.extendability],
        "testability": [predicted_testability],
        "modularity": [mdg_modularity],
    }

    logger.info('QMOOD design metrics (N):')
    logger.info(design_metrics)

    logger.info('Objectives:')
    logger.info(quality_objectives)

    logger.info('QMOOD quality attributes sum:')
    logger.info(sum_)
    logger.info('QMOOD quality attributes mean:')
    logger.info(avg_)

    df_quality_attributes = pd.DataFrame(data=quality_objectives)
    if os.path.exists(quality_attributes_path):
        df = pd.read_csv(quality_attributes_path, index_col=False)
        df_result = pd.concat([df, df_quality_attributes], ignore_index=True)
        df_result.to_csv(quality_attributes_path, index=False)
    else:
        df_quality_attributes.to_csv(quality_attributes_path, index=False)

    df_design_metrics = pd.DataFrame(data=design_metrics)
    if os.path.exists(design_metrics_path):
        df = pd.read_csv(design_metrics_path, index_col=False)
        df_results = pd.concat([df, df_design_metrics], ignore_index=True)
        # df = df.append(df_design_metrics, ignore_index=True)
        df_results.to_csv(design_metrics_path, index=False)
    else:
        df_design_metrics.to_csv(design_metrics_path, index=False)