def derive(nodes_type, selected_C): lst = [] for node_type in nodes_type: if (node_type == Root1): node = Root1(3) elif (node_type == Root): node = Root(5) elif (node_type == N): node = N(0) # 此处存疑,或许也可以是包含selected_A中所有的A elif (node_type == A): node = A(0) elif (node_type == Sel): node = Sel(0) elif (node_type == Filter): id = random.randint(2, 10) node = Filter(id) elif (node_type == Order): id = random.randint(0, 1) node = Order(id) elif (node_type == Sup): id = random.randint(0, 1) node = Sup(id) generate(node, selected_C) lst.append(node) return lst
def _parse_root(self, sql): """ parsing the sql by the grammar R ::= Select | Select Filter | Select Order | ... | :return: [R(), states] """ use_sup, use_ord, use_fil = True, True, False if sql['sql']['limit'] == None: use_sup = False if sql['sql']['orderBy'] == []: use_ord = False elif sql['sql']['limit'] != None: use_ord = False # check the where and having if sql['sql']['where'] != [] or \ sql['sql']['having'] != []: use_fil = True if use_fil and use_sup: return [Root(0)], ['FILTER', 'SUP', 'SEL'] elif use_fil and use_ord: return [Root(1)], ['ORDER', 'FILTER', 'SEL'] elif use_sup: return [Root(2)], ['SUP', 'SEL'] elif use_fil: return [Root(3)], ['FILTER', 'SEL'] elif use_ord: return [Root(4)], ['ORDER', 'SEL'] else: return [Root(5)], ['SEL']
def generate_sketch(node): if (isinstance(node, N) or isinstance(node, Order) or isinstance(node, Sup) or isinstance(node, Filter)): return elif (isinstance(node, Root1)): child = Root(5) child.set_parent(node) node.add_children(child) generate_sketch(child) elif (isinstance(node, Root)): child = Sel(0) child.set_parent(node) node.add_children(child) generate_sketch(child) elif (isinstance(node, Sel)): child = N(0) child.set_parent(node) node.add_children(child) generate_sketch(child)
def generate(node, selected_C): if (isinstance(node, A)): idx = random.randint(0, len(selected_C) - 1) selected_C[idx].set_parent(node) node.add_children(selected_C[idx]) return elif (isinstance(node, Root1)): child = Root(5) child.set_parent(node) node.add_children(child) generate(child, selected_C) elif (isinstance(node, Root)): child = Sel(0) child.set_parent(node) node.add_children(child) generate(child, selected_C) elif (isinstance(node, Sel)): child = N(0) child.set_parent(node) node.add_children(child) generate(child, selected_C) elif (isinstance(node, N) or isinstance(node, Order) or isinstance(node, Sup) or isinstance(node, Filter)): child = A(0) child.set_parent(node) node.add_children(child) generate(child, selected_C)
for node in new_node_lst: node.parent = None node.children = [] return new_node_lst # print(new_node_lst) # print(action_p) if __name__ == '__main__': # correct_s = "Root1(3) Root(4) Sel(0) N(2) A(0) C(3) T(1) A(0) C(9) T(1) A(0) C(12) T(1) Order(0) A(0) C(12) T(1)".split() correct = [ Root1(3), Root(3), Sel(0), N(0), Filter(0), Filter(0), Filter(2), Root(3), Sel(0), N(0), Filter(2), Filter(2) ] # predicted_s = 'Root1(3) Root(4) Sel(0) N(2) A(0) C(4)'.split() predicted = [ Root1(3), Root(3),