Esempio n. 1
0
def _find_EMO_groups(method_subtree: AST) -> List[ExtractionOpportunityGroup]:
    statements_semantic = extract_method_statements_semantic(method_subtree)
    extraction_opportunities = create_extraction_opportunities(
        statements_semantic)
    filtered_extraction_opportunities = filter_extraction_opportunities(
        extraction_opportunities, statements_semantic, method_subtree)
    extraction_opportunities_groups = rank_extraction_opportunities(
        statements_semantic, filtered_extraction_opportunities)

    return extraction_opportunities_groups
Esempio n. 2
0
 def test_semantic_extraction(self):
     method_ast = self._get_method_ast()
     semantic = extract_method_statements_semantic(method_ast)
     for comparison_index, (statement, actual_semantic, expected_semantic) in enumerate(
         zip_longest(semantic.keys(), semantic.values(), self._method_semantic)
     ):
         self.assertEqual(
             actual_semantic,
             expected_semantic,
             f"Failed on {statement.node_type} statement on line {statement.line}. "
             f"Comparison index is {comparison_index}.",
         )
Esempio n. 3
0
    def test_extraction_opportunities_creation(self):
        method_ast = self._get_method_ast()
        semantic = extract_method_statements_semantic(method_ast)
        extraction_opportunities = create_extraction_opportunities(semantic)

        for opportunity_index, (actual_opportunity, expected_opportunity) in enumerate(
            zip_longest(extraction_opportunities, self._expected_extraction_opportunities)
        ):
            actual_opportunity_statement_types = [statement.node_type for statement in actual_opportunity]
            self.assertEqual(
                actual_opportunity_statement_types,
                expected_opportunity,
                f"Failed on {opportunity_index}th opportunity comparison",
            )
    def _opportunity_test_helper(
        self,
        method_name: str,
        extraction_opportunity_statements_lines: List[int],
        is_opportunity_correct: bool,
    ):
        method_ast = get_method_ast("SemanticFilterTest.java", "Test",
                                    method_name)
        statements_semantic = extract_method_statements_semantic(method_ast)
        extraction_opportunity, block_statement_graph = create_extraction_opportunity(
            method_ast, extraction_opportunity_statements_lines)

        self.assertEqual(
            semantic_filter(extraction_opportunity, statements_semantic,
                            block_statement_graph),
            is_opportunity_correct,
        )
Esempio n. 5
0
    def test_extraction_opportunities_ranking(self):
        method_ast = self._get_method_ast()
        semantic = extract_method_statements_semantic(method_ast)
        extraction_opportunities = create_extraction_opportunities(semantic)
        filtered_extraction_opportunities = filter_extraction_opportunities(
            extraction_opportunities, semantic, method_ast
        )

        ranked_extraction_opportunities_groups = rank_extraction_opportunities(
            semantic, filtered_extraction_opportunities
        )

        benefits = [group.benefit for group in ranked_extraction_opportunities_groups]
        self.assertEqual(benefits, [24, 23, 21, 19, 19, 3])

        group_sizes = [len(list(group.opportunities)) for group in ranked_extraction_opportunities_groups]
        self.assertEqual(group_sizes, [1, 1, 1, 1, 1, 1])
Esempio n. 6
0
 def _test_helper(self, method_name: str, expected_statements_semantics: List[StatementSemantic]):
     method_ast = get_method_ast("SemanticExtractionTest.java", "SimpleMethods", method_name)
     method_semantic = extract_method_statements_semantic(method_ast)
     for (
         comparison_index,
         (statement, actual_statement_semantic, expected_statement_semantic),
     ) in enumerate(
         zip_longest(
             method_semantic.keys(),
             method_semantic.values(),
             expected_statements_semantics,
         )
     ):
         self.assertEqual(
             actual_statement_semantic,
             expected_statement_semantic,
             f"{comparison_index}th comparison failed for {statement.node_type} on line {statement.line}.",
         )
Esempio n. 7
0
 def test_all_opportunities_creation(self):
     ast = get_method_ast(
         "./alternatives/all_opportunities/TestAllOpportunitiesCreation.java",
         "Test", "test")
     statements_semantic = extract_method_statements_semantic(ast)
     actual_extraction_opportunities = create_extraction_opportunities(
         statements_semantic)
     for opportunity_index, (
             actual_opportunity,
             expected_opportunity_node_types) in enumerate(
                 zip_longest(actual_extraction_opportunities,
                             self._expected_opportunities_nodes_types)):
         actual_opportunity_node_types = list(
             map(lambda node: node.node_type, actual_opportunity))
         self.assertEqual(
             actual_opportunity_node_types, expected_opportunity_node_types,
             f"Failed comparing {opportunity_index}th opportunity "
             f"starting at line {actual_opportunity[0].line} and "
             f"ending on line {actual_opportunity[-1].line}.")
Esempio n. 8
0
def find_emos(filename: str, class_name: str, method_name: str,
              method_decl_line: int) -> List[Range]:
    ast_method = get_method_ast(filename, class_name, method_name,
                                method_decl_line)
    statements_semantic = extract_method_statements_semantic(ast_method)
    possible_extraction_opportunities = create_extraction_opportunities(
        statements_semantic)
    filtered_extraction_opportunities = filter_extraction_opportunities(
        possible_extraction_opportunities, statements_semantic, ast_method)

    source_code = get_source_code(filename)
    results = []
    for index, extraction_opportunity in enumerate(
            filtered_extraction_opportunities):
        first_statement = extraction_opportunity[0]
        last_statement = extraction_opportunity[-1]
        range = complement_range(
            source_code,
            Range(first_statement.line - 1, last_statement.line - 1))
        results.append(range)

    return results