def generateNode(self, parent_node, depth, assigned_key): attribute = Attribute(self.attribute, self.rule[assigned_key]["attributes"]) # node = Node(parent_node, ) for i in range(len(self.rule[assigned_key]["attributes"])): attribute.assign_value(i, None) for i, enabled in enumerate(attribute.enabledAttributes): value = None if enabled: if attribute.activatedAttributes[i] == AttributeSet.content: if assigned_key == LeafKey.button.value: value = "\"" + get_random_text(10, 1) + "\"" elif assigned_key == LeafKey.title.value: value = "\"" + get_random_text(5, 0) + "\"" else: value = "\"" + get_random_text(30, 5, False) + "\"" attribute.assign_value(i, value) else: try: group_value = random.choice(list(self.rule[assigned_key]["attributes_set"]["groups"])) for j, v in enumerate(group_value): attribute.assign_value(i+j, v.value) except KeyError: value = random.choice(list(self.rule[assigned_key]["attributes_set"][attribute.activatedAttributes[i].value])).value attribute.assign_value(i, value) return Node(assigned_key, parent_node, attribute, depth)
def __init__(self, mapping_file_path, rule=1, node_tree=Node(RootKey.body.value, None, Attribute())): with open(mapping_file_path) as data_file: self.html_mapping = json.load(data_file) self.rule = getRule(rule) self.activatedAttributes = self.rule["attributes"] self.node_tree = node_tree self.node_opening_tag = Tag.node_opening.value self.node_closing_tag = Tag.node_closing.value self.attr_opening_tag = Tag.attr_opening.value self.attr_closing_tag = Tag.attr_closing.value
def dsl_to_node_tree(self, dsl_file_path) -> Node: self.node_tree = Node( RootKey.body.value, None, Attribute(self.activatedAttributes, self.rule[RootKey.body.value]["attributes"])) depth = 1 dsl = [] with open(dsl_file_path, 'r') as dsl_file: dsl = dsl_file.read().split() current_parent_node = self.node_tree current_node = self.node_tree in_attr_flag = False attr = [] for token in dsl: if token == self.node_opening_tag: current_parent_node = current_node depth += 1 elif token == self.node_closing_tag: depth -= 1 current_parent_node = current_parent_node.parent elif token == self.attr_opening_tag: in_attr_flag = True attr = [] elif token == self.attr_closing_tag: in_attr_flag = False current_node.attributes.list_to_attribut( self._reconstruct_attr_block(current_node.key, attr)) else: if in_attr_flag: attr.append(token) else: current_node = Node( token, current_parent_node, Attribute(self.activatedAttributes, self.rule[token]["attributes"]), depth) current_parent_node.add_child(current_node) return self.node_tree
def generateNode(parent_node, depth, assigned_key=None) -> Node: pool = [] key = None if assigned_key: key = assigned_key else: if parent_node.key in GENERATE_RULE['enabled_children']: pool = GENERATE_RULE['enabled_children'][parent_node.key] if MAX_DEPTH - depth == 1: for node_enum in GENERATE_RULE['disabled_layer']['last_1']: if node_enum in pool: pool.remove(node_enum) elif MAX_DEPTH - depth == 0: for node_enum in GENERATE_RULE['disabled_layer']['last']: if node_enum in pool: pool.remove(node_enum) key = random.choice(pool).value # print(parent_node.key, depth, pool) attribute = Attribute(None, None, None) if key in getEnumList(GENERATE_RULE['attributes']['none']): pass else: if key in getEnumList( GENERATE_RULE['attributes']['have_font_color_node']): color = random.choice(list(Color)).value attribute.set_font_color(color) if key in getEnumList( GENERATE_RULE['attributes']['have_bg_color_node']): color = random.choice(list(Color)).value attribute.set_bg_color(color) if key in getEnumList( GENERATE_RULE['attributes']['have_content_node']): content = get_random_text(5 if parent_node.key == NodeKey.button.value else 10) attribute.set_content("\"" + content + "\"") return Node(key, parent_node, attribute, depth)
def generateNodeTree(self, parent_node: Node, parent_depth): node = None pool = [] children_num = 1 children_group_flag = False reciprocal_layer_layer = self.max_depth - parent_depth - 1 brothers_limit = self.rule[parent_node.key]["children_brothers"] children_brother_node = None # print("-----\n", parent_node.key) # print("children len: ", len(self.rule[parent_node.key]["children"]), " / group: ", self.rule[parent_node.key]["children_group"] , " / reciprocal: ", (reciprocal_layer_layer)) if ((self.rule[parent_node.key]["children_group"] == None) and len(self.rule[parent_node.key]["children"]) == 0) or (reciprocal_layer_layer < 0): return parent_node if self.rule[parent_node.key]["children_group"]: if self.rule[parent_node.key]["children_group"]["enable"] == Operator.random: children_group_flag = random.choice([False, True]) # print("random group: ", children_group_flag) elif self.rule[parent_node.key]["children_group"]["enable"] == Operator.true: children_group_flag = True if reciprocal_layer_layer <=1: children_group_flag = True if children_group_flag: if len(self.rule[parent_node.key]["children_group"]["nodes"])> 1: i = random.randrange(len(self.rule[parent_node.key]["children_group"]["nodes"])) else: i=0 pool = self.rule[parent_node.key]["children_group"]["nodes"][i] else: pool = self.rule[parent_node.key]["children"] for node in pool: if reciprocal_layer_layer in self.rule[node.value]["disabled_reciprocal_layer"]: pool.remove(node) if children_group_flag: children_num = len(pool) elif self.rule[parent_node.key]["children_quantity"]: if self.rule[parent_node.key]["children_quantity"]["operator"] == Operator.equal: children_num = self.rule[parent_node.key]["children_quantity"]["value"] elif self.rule[parent_node.key]["children_quantity"]["operator"] == Operator.equal_more_then: children_num = random.randrange( self.rule[parent_node.key]["children_quantity"]["value"], self.max_each_layer_node_num) elif self.rule[parent_node.key]["children_quantity"]["operator"] == Operator.between: children_num = random.randrange( self.rule[parent_node.key]["children_quantity"]["value"][0], self.rule[parent_node.key]["children_quantity"]["value"][1]) if parent_node.key == NodeKey.row.value and parent_depth == 1: if self.rule[parent_node.key]["children_quantity"]["operator"] == Operator.equal_more_then: children_num = random.randrange( 1, self.max_each_layer_node_num) elif self.rule[parent_node.key]["children_quantity"]["operator"] == Operator.between: children_num = random.randrange( 1, self.rule[parent_node.key]["children_quantity"]["value"][1]) else: children_num = random.randrange(1, self.max_each_layer_node_num) for index in range(children_num): if children_group_flag: node = self.generateNode( parent_node, parent_depth+1, pool[index].value) elif brothers_limit == Operator.same: if index == 0: node = self.generateNode( parent_node, parent_depth+1, random.choice(pool).value) children_brother_node = node.key else: node = self.generateNode( parent_node, parent_depth+1, children_brother_node) else: node = self.generateNode( parent_node, parent_depth+1, random.choice(pool).value) node = self.generateNodeTree(node, parent_depth+1) parent_node.add_child(node) return parent_node
pool.remove(node_enum) key = random.choice(pool).value # print(parent_node.key, depth, pool) attribute = Attribute(None, None, None) if key in getEnumList(GENERATE_RULE['attributes']['none']): pass else: if key in getEnumList( GENERATE_RULE['attributes']['have_font_color_node']): color = random.choice(list(Color)).value attribute.set_font_color(color) if key in getEnumList( GENERATE_RULE['attributes']['have_bg_color_node']): color = random.choice(list(Color)).value attribute.set_bg_color(color) if key in getEnumList( GENERATE_RULE['attributes']['have_content_node']): content = get_random_text(5 if parent_node.key == NodeKey.button.value else 10) attribute.set_content("\"" + content + "\"") return Node(key, parent_node, attribute, depth) if __name__ == "__main__": root = Node(RootKey.body.value, None, Attribute(None, None, None)) tree = generateNodeTree(root, 0) print(tree.toDSL()) print("=================================") print(tree.to_row_col_DSL())
class Compiler: def __init__(self, mapping_file_path, rule=1, node_tree=Node(RootKey.body.value, None, Attribute())): with open(mapping_file_path) as data_file: self.html_mapping = json.load(data_file) self.rule = getRule(rule) self.activatedAttributes = self.rule["attributes"] self.node_tree = node_tree self.node_opening_tag = Tag.node_opening.value self.node_closing_tag = Tag.node_closing.value self.attr_opening_tag = Tag.attr_opening.value self.attr_closing_tag = Tag.attr_closing.value def dsl_to_node_tree(self, dsl_file_path) -> Node: self.node_tree = Node( RootKey.body.value, None, Attribute(self.activatedAttributes, self.rule[RootKey.body.value]["attributes"])) depth = 1 dsl = [] with open(dsl_file_path, 'r') as dsl_file: dsl = dsl_file.read().split() current_parent_node = self.node_tree current_node = self.node_tree in_attr_flag = False attr = [] for token in dsl: if token == self.node_opening_tag: current_parent_node = current_node depth += 1 elif token == self.node_closing_tag: depth -= 1 current_parent_node = current_parent_node.parent elif token == self.attr_opening_tag: in_attr_flag = True attr = [] elif token == self.attr_closing_tag: in_attr_flag = False current_node.attributes.list_to_attribut( self._reconstruct_attr_block(current_node.key, attr)) else: if in_attr_flag: attr.append(token) else: current_node = Node( token, current_parent_node, Attribute(self.activatedAttributes, self.rule[token]["attributes"]), depth) current_parent_node.add_child(current_node) return self.node_tree def _reconstruct_attr_block(self, node, attr) -> list: temp = [None] * len(self.activatedAttributes) try: context_idx = self.activatedAttributes.index(AttributeSet.content) except: context_idx = -1 temp_text = "" isText = False for token in attr: if (not isText) and ('\"' in token): isText = True temp_text = token elif isText and ('\"' in token): isText = False temp_text += " " + token temp[context_idx] = temp_text elif isText: temp_text += " " + token # elif token == "None": # temp.append(None) else: for attrIdx, activatedAttr in enumerate( self.activatedAttributes): try: attr_set = self.rule[node]["attributes_set"][ activatedAttr.value] if token in [attr.value for attr in attr_set]: temp[attrIdx] = token except KeyError: pass if len(temp_text) > 0 and isText: isText = False temp[context_idx] = temp_text if context_idx >= 0 and self.rule[node]["attributes"][ context_idx] and temp[context_idx] == None: if node == LeafKey.button.value: temp[context_idx] = get_random_text(10, 1) elif node == LeafKey.title.value: temp[context_idx] = get_random_text(5, 0) else: temp[context_idx] = get_random_text(30, 5, False) return temp def node_tree_to_dsl(self, output_file_path, row_col_only=False, with_context=True) -> str: dsl = self.node_tree.to_row_col_DSL( ) if row_col_only else self.node_tree.toDSL(with_context=with_context) with open(output_file_path, 'w+') as dsl_file: dsl_file.write(dsl) return dsl def node_tree_to_html(self, output_file_path, file_name): html = self.node_tree.toHTML(self.html_mapping, file_name) with open(output_file_path, 'w+') as html_file: html_file.write(html) return html
from datasetCode.data_transform.tag_for_yolo import manual_class_tag_from_file import os if __name__ == "__main__": RANDOM_GENERATOR = False SKELETON_TO_HTML_ONLY = False WEB_SCREENSHOOT = False RULE = 4 if RANDOM_GENERATOR: rule = getRule(RULE) generator = NodeTreeGenerator(rule=RULE) for i in range(500, 600): print(i) root = Node( RootKey.body.value, None, Attribute(rule["attributes"], rule[RootKey.body.value]["attributes"])) tree = generator.generateNodeTree(root, 0) compiler = Compiler(path.DATASET3_DSL_MAPPING_JSON_FILE, rule=RULE, node_tree=tree) # compiler = Compiler(path.DATASET2_DSL_MAPPING_JSON_FILE, rule=2) compiler.node_tree_to_dsl(path.DATASET3_ORIGIN_GUI + str(i) + TYPE.GUI) # compiler.node_tree_to_dsl(path.DATASET2_ROWCOL_GUI+str(i)+TYPE.GUI, True) compiler.node_tree_to_dsl(path.DATASET3_ORIGIN_NO_CONTEXT_GUI + str(i) + TYPE.GUI, with_context=False) # tree = compiler.dsl_to_node_tree(path.DATASET2_ORIGIN_GUI+str(i)+TYPE.GUI) # print(tree.show()) html = compiler.node_tree_to_html(