def get_p_star(): HOC_1 = ASTNode('run', None, [ASTNode('move'), ASTNode('turn_left'), ASTNode('move'), ASTNode('turn_right'), ASTNode('move')]) hoc_1_json = HOC_1.to_json() print("HOC_1:", hoc_1_json) return HOC_1
def get_p_star(): Karel_9 = ASTNode('run', None, [ ASTNode('repeat', '8', [ ASTNode('ifelse', 'bool_no_marker', [ ASTNode('do', 'bool_no_marker', [ASTNode('put_marker')]), ASTNode('else', 'bool_no_marker', [ASTNode('pick_marker')]) ]), ASTNode('move') ]) ]) karel_9_json = Karel_9.to_json() print("Karel_9:", karel_9_json) return Karel_9
def is_ast_repeat_valid(ast: ASTNode): root_children = ast.children() repeat_children = [] for ele in root_children: if ele._type == "repeat": repeat_children.extend(ele.children()) repeat_children_types = [ele._type for ele in repeat_children] if len(repeat_children_types) == 0: print("No elements inside repeat") return False if len(repeat_children_types) > 1: flag = all(ele == "move" for ele in repeat_children_types) if flag: return False children_types = [ele._type for ele in root_children] id_of_repeat = children_types.index('repeat') block_1 = children_types[:id_of_repeat] block_2 = children_types[id_of_repeat + 1:] if repeat_children_types == block_1[-len(repeat_children_types ):]: #end ele return False if repeat_children_types == block_2[:len(repeat_children_types )]: #first ele return False return True
def get_p_star(): Karel_10 = ASTNode('run', None, [ ASTNode('put_marker'), ASTNode('while', 'bool_path_ahead', [ ASTNode('move'), ASTNode('turn_left'), ASTNode('move'), ASTNode('turn_right'), ASTNode('put_marker') ]) ]) karel_10_json = Karel_10.to_json() print("Karel_10:", karel_10_json) return Karel_10
def get_p_star(): HOC_6 = ASTNode('run', None, [ ASTNode('repeat_until_goal', 'bool_goal', [ ASTNode('ifelse', 'bool_path_ahead', [ ASTNode('do', 'bool_path_ahead', [ASTNode('move')]), ASTNode('else', 'bool_path_ahead', [ASTNode('turn_left')]) ]) ]) ]) hoc_6_json = HOC_6.to_json() print("HOC_6:", hoc_6_json) return HOC_6
def get_p_star(): HOC_3 = ASTNode('run', None, [ ASTNode('repeat', '4', [ASTNode('move')]), ASTNode('turn_left'), ASTNode('repeat', '5', [ASTNode('move')]) ]) hoc_3_json = HOC_3.to_json() print("HOC_3:", hoc_3_json) return HOC_3
def is_ast_repeat_valid(ast: ASTNode): root_children = ast.children() repeat_indices = [ i for i, x in enumerate(root_children) if x._type == "repeat" ] if len(repeat_indices) == 0: print("No repeat encountered") return True children_types = [ele._type for ele in root_children] #### Assumption: Code has only 2 repeat constructs repeat_1_children = [ ele._type for ele in root_children[repeat_indices[0]].children() ] repeat_2_children = [ ele._type for ele in root_children[repeat_indices[1]].children() ] if len(repeat_1_children) == 0: print("No elements inside repeat") return False if len(repeat_2_children) == 0: print("No elements inside repeat") return False if len(repeat_1_children) > 1: flag = all(ele == "move" for ele in repeat_1_children) if flag: return False if len(repeat_2_children) > 1: flag = all(ele == "move" for ele in repeat_2_children) if flag: return False block_1 = children_types[:repeat_indices[0]] block_2 = children_types[repeat_indices[0] + 1:repeat_indices[1]] block_3 = children_types[repeat_indices[1] + 1:] # print(block_1, block_2, block_3) # print(repeat_1_children, repeat_2_children) if repeat_1_children == block_1[-len(repeat_1_children):]: #end ele return False if repeat_1_children == block_2[:len(repeat_1_children)]: #first ele return False if repeat_2_children == block_2[-len(repeat_2_children):]: #end ele return False if repeat_2_children == block_3[:len(repeat_2_children)]: #first ele return False return True
def get_p_star(): Karel_8 = ASTNode('run', None, [ ASTNode('repeat', '4', [ASTNode('put_marker'), ASTNode('move'), ASTNode('turn_left')])] ) karel_8_json = Karel_8.to_json() print("Karel_8:", karel_8_json) return Karel_8
def get_p_star(): HOC_5 = ASTNode('run', None, [ ASTNode('repeat_until_goal', 'bool_goal', [ ASTNode('move'), ASTNode('if', 'bool_path_left', [ASTNode('do', 'bool_path_left', [ASTNode('turn_left')])]) ]) ]) hoc_5_json = HOC_5.to_json() print("HOC_5:", hoc_5_json) return HOC_5
def get_p_star(): HOC_4 = ASTNode('run', None, [ ASTNode('repeat_until_goal', 'bool_goal', [ ASTNode('move'), ASTNode('turn_left'), ASTNode('move'), ASTNode('turn_right') ]) ]) hoc_4_json = HOC_4.to_json() print("HOC_4:", hoc_4_json) return HOC_4
def get_p_star(): HOC_2 = ASTNode( 'run', None, [ASTNode('turn_right'), ASTNode('repeat', '5', [ASTNode('move')])]) hoc_2_json = HOC_2.to_json() print("HOC_2:", hoc_2_json) return HOC_2
def generate_ast_nodes_from_assignments(assignments: list): all_ast_progs = [] for a in assignments: ast = ASTNode('run', None, [ ASTNode(a[0]), ASTNode(a[1]), ASTNode(a[2]), ASTNode(a[3]), ASTNode(a[4]), ASTNode('repeat', a[5], [ ASTNode(a[6]), ASTNode(a[7]), ASTNode(a[8]), ASTNode(a[9]), ASTNode(a[10]) ]), ASTNode(a[11]), ASTNode(a[12]), ASTNode(a[13]), ASTNode(a[14]), ASTNode(a[15]) ]) ast = remove_null_nodes(ast) ##### remove the asts with turn actions in the end if not is_last_child_turn(ast): ######## Remove the asts with blocks matching the block inside repeat if is_ast_repeat_valid(ast): all_ast_progs.append(ast) #################################################### return all_ast_progs
def generate_ast_nodes_from_assignments(assignments: list): all_ast_progs = [] for a in assignments: ast = ASTNode('run', None, [ ASTNode(a[12]), ASTNode(a[13]), ASTNode(a[14]), ASTNode(a[15]), ASTNode(a[16]), ASTNode('repeat_until_goal', 'bool_goal', [ ASTNode(a[1]), ASTNode(a[2]), ASTNode(a[3]), ASTNode(a[4]), ASTNode(a[5]), ASTNode('if', a[6], [ ASTNode('do', a[6], [ ASTNode(a[7]), ASTNode(a[8]), ASTNode(a[9]), ASTNode(a[10]), ASTNode(a[11]) ]) ]) ]) ]) ast = remove_null_nodes(ast) all_ast_progs.append(ast) return all_ast_progs
def generate_ast_nodes_from_assignments(assignments: list): all_ast_progs = [] for a in assignments: ast = ASTNode('run', None, [ ASTNode(a[0]), ASTNode(a[1]), ASTNode(a[2]), ASTNode(a[3]), ASTNode(a[4]), ASTNode('while', a[5], [ ASTNode(a[6]), ASTNode(a[7]), ASTNode(a[8]), ASTNode(a[9]), ASTNode(a[10]), ASTNode(a[11]), ASTNode(a[12]), ASTNode(a[13]), ASTNode(a[14]), ASTNode(a[15]), ASTNode(a[16]), ASTNode(a[17]), ASTNode(a[18]), ]), ASTNode(a[19]), ASTNode(a[20]), ASTNode(a[21]), ASTNode(a[22]), ASTNode(a[23]), ]) ast = remove_null_nodes(ast) all_ast_progs.append(ast) return all_ast_progs
def generate_ast_nodes_from_assignments(assignments: list): all_ast_progs = [] for a in assignments: ast = ASTNode('run', None, [ ASTNode(a[10]), ASTNode(a[11]), ASTNode(a[12]), ASTNode(a[13]), ASTNode(a[14]), ASTNode('repeat', a[0], [ ASTNode(a[1]), ASTNode(a[2]), ASTNode(a[3]), ASTNode(a[4]), ASTNode(a[5]), ASTNode(a[6]), ASTNode(a[7]), ASTNode(a[8]), ASTNode(a[9]), ]), ASTNode(a[15]), ASTNode(a[16]), ASTNode(a[17]), ASTNode(a[18]), ASTNode(a[19]), ]) # remove the nodes which are phi' ast = remove_null_nodes(ast) ####### Allow asts with blocks of code NOT matching inner block of repeat if is_ast_repeat_valid(ast): all_ast_progs.append(ast) return all_ast_progs
def generate_ast_nodes_from_assignments(assignments: list): all_ast_progs = [] for a in assignments: ast = ASTNode('run', None, [ ASTNode(a[0]), ASTNode(a[1]), ASTNode(a[2]), ASTNode(a[3]), ASTNode(a[4]), ASTNode(a[5]), ASTNode(a[6]), ASTNode(a[7]), ASTNode(a[8]), ASTNode(a[9]), ASTNode(a[10]), ASTNode(a[11]), ASTNode(a[12]) ]) # remove the nodes which are phi' ast = remove_null_nodes(ast) ##### remove the asts with turn actions in the end if not is_last_child_turn(ast): all_ast_progs.append(ast) #################################################### return all_ast_progs