Esempio n. 1
0
def __get_code_info(solutions: pd.DataFrame, user: User, index: int,
                    ati_actions: List[AtiItem]) -> CodeInfo:
    date = __get_column_value(solutions, index,
                              consts.CODE_TRACKER_COLUMN.DATE)
    timestamp = __get_column_value(solutions, index,
                                   consts.CODE_TRACKER_COLUMN.TIMESTAMP)
    return CodeInfo(user, timestamp, date, ati_actions)
 def test_adding_code_info(self) -> None:
     sg = SolutionGraph(CURRENT_TASK)
     vertex = Vertex(sg)
     code_info_list_len = 100
     code_info_list = [CodeInfo(User()) for _ in range(code_info_list_len)]
     for code_info in code_info_list:
         vertex.add_code_info(code_info)
     assert vertex.code_info_list == code_info_list
Esempio n. 3
0
 def create_user_trees(
         self,
         source_code: str,
         profile: Profile,
         rate: Optional[float] = None) -> Tuple[AnonTree, ast.AST]:
     code_info = CodeInfo(User(profile))
     code = Code.from_source(source_code, rate=rate, task=self._graph.task)
     anon_tree = AnonTree(code.anon_tree, code.rate,
                          self.get_file_path(f'{TREE_TYPE.ANON.value}'),
                          code_info)
     anon_tree.find_medians()
     return anon_tree, code.canon_tree
Esempio n. 4
0
def get_solution_graph(task: TASK, to_plot_graph: bool = True, test_prefix: str = 'num_diffs') -> SolutionGraph:
    sources, goals = __get_sources_and_goals(task)
    sg = SolutionGraph(task)
    code_info = CodeInfo(User())
    for goal in goals:
        goal_code = __get_code_by_source(goal, is_goal=True)
        codes = [__get_code_by_source(s) for s in sources] + [goal_code]
        chain = [(code, code_info) for code in codes]
        sg.add_code_info_chain(chain)
    if to_plot_graph:
        path = __plot_graph(task, sg, test_prefix)
        log.info(f'Graph path for solution space for task {task.value} is {path}')
    return sg
    def test_getting_unique_users(self) -> None:
        users = [User(), User(), User()]
        users_dist = [3, 0, 20]
        code_info_list = []
        # Create code_info_list with users distribution
        for i, dist in enumerate(users_dist):
            for j in range(dist):
                code_info_list.append(CodeInfo(users[i]))
        assert len(code_info_list) == sum(users_dist)

        sg = SolutionGraph(CURRENT_TASK)
        vertex = Vertex(sg)
        for code_info in code_info_list:
            vertex.add_code_info(code_info)

        # In result set will be only two users form three because the second one has 0 code_info
        assert vertex.get_unique_users() == {users[0], users[2]}
Esempio n. 6
0
def __remove_loops(code_info_chain: List[Tuple[Code, CodeInfo]],
                   user: User) -> List[Tuple[Code, CodeInfo]]:
    # Add empty code
    # We want to remove loops, but we can have the situation: Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5
    # In the case we will get after loops removing: Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5,
    # but it is incorrect, because we expect Empty Tree -> Tree5
    # If we add empty code, we will have: Empty Tree -> Tree1 -> Tree2 -> Tree3 -> Empty Tree -> Tree5
    # and we will get after loops removing: Empty Tree -> Tree5
    code_info_chain = [(Code.from_source(
        '', TEST_RESULT.CORRECT_CODE.value), CodeInfo(user))] + code_info_chain

    current_tree_index = 0
    while current_tree_index < len(code_info_chain):
        current_anon_tree = code_info_chain[current_tree_index][0].anon_tree
        same_tree_indexes = []
        for next_tree_index in range(current_tree_index + 1,
                                     len(code_info_chain)):
            next_anon_tree = code_info_chain[next_tree_index][0].anon_tree
            if are_asts_equal(current_anon_tree, next_anon_tree):
                same_tree_indexes.append(next_tree_index)
        if len(same_tree_indexes) > 0:
            del code_info_chain[current_tree_index:max(same_tree_indexes)]
        current_tree_index += 1
    return code_info_chain
Esempio n. 7
0
from typing import List, Tuple, Callable

import pytest

from src.test.test_config import to_skip, TEST_LEVEL
from src.main.solution_space.serialized_code import Code
from src.main.util.consts import LOGGER_NAME, TEST_RESULT
from src.main.util.helper_classes.id_counter import IdCounter
from src.main.solution_space.data_classes import CodeInfo, User
from src.main.solution_space.solution_space_handler import __remove_loops


log = logging.getLogger(LOGGER_NAME)

user = User()
code_info = CodeInfo(user)

source_1 = ''
source_2 = 'a = int(input())'
source_3 = 'a = int(input())\nb = int(input())'
source_4 = 'a = int(input())\nb = int(input())\nc = int(input())'
source_5 = 'a = int(input())\nb = int(input())\nc = int(input())\nprint(a * c + " " + b * c)'

sources = [source_1, source_2, source_3, source_4, source_5]


def get_code_chain_from_sources(start_index: int = 0, end_index: int = len(sources)) -> List[Tuple[Code, CodeInfo]]:
    IdCounter.reset_all()
    return [(Code.from_source(source, TEST_RESULT.CORRECT_CODE.value), code_info)
            for source in sources[start_index: end_index]]
Esempio n. 8
0
def get_code_info_chain(sources: List[str]) -> List[Tuple[Code, CodeInfo]]:
    user = User()
    return [(Code.from_source(s,
                              TEST_RESULT.CORRECT_CODE.value), CodeInfo(user))
            for s in sources]