def EG_append_edge(self, name_pair, root_graph=None): "Add edge to 'root_graph' only by node-names of the edge." nameA = to_unicode(name_pair[0].strip()) nameB = to_unicode(name_pair[1].strip()) if root_graph is None: root_graph = self ### Check unique. e = self.EG_get_edge_by_names((nameA, nameB), root_graph=root_graph) if not(e is None): raise Exception('Unique error. The edge with same names was existed in the graph.') nameA = add_double_quote(nameA) nameB = add_double_quote(nameB) if root_graph.get_node(nameA)[0].get_shape() == jawalker.BuildDictionariesFromDOT.ENTITY_SHAPE and root_graph.get_node(nameB)[0].get_shape() == jawalker.BuildDictionariesFromDOT.ATTRIBUTE_SHAPE: e = pydot.Edge(src=nameB, dst=nameA, color=jawalker.BuildDictionariesFromDOT.ATTRIBUTE_EDGE) elif root_graph.get_node(nameB)[0].get_shape() == jawalker.BuildDictionariesFromDOT.ENTITY_SHAPE and root_graph.get_node(nameA)[0].get_shape() == jawalker.BuildDictionariesFromDOT.ATTRIBUTE_SHAPE: e = pydot.Edge(src=nameA, dst=nameB, color=jawalker.BuildDictionariesFromDOT.ATTRIBUTE_EDGE) else: e = pydot.Edge(src=nameA, dst=nameB) root_graph.add_edge(e) self.__check_wildcard_existed() self.refresh_bitmap() return e
def EG_append_edge(self, name_pair, root_graph=None): "Add edge to 'root_graph' only by node-names of the edge." nameA = to_unicode(name_pair[0].strip()) nameB = to_unicode(name_pair[1].strip()) if root_graph is None: root_graph = self ### Check unique. e = self.EG_get_edge_by_names((nameA, nameB), root_graph=root_graph) if not (e is None): raise Exception("Unique error. The edge with same names was existed in the graph.") nameA = add_double_quote(nameA) nameB = add_double_quote(nameB) e = pydot.Edge(src=nameA, dst=nameB) root_graph.add_edge(e) self.__check_wildcard_existed() self.refresh_bitmap() return e
def EG_append_edge(self, name_pair, root_graph=None): "Add edge to 'root_graph' only by node-names of the edge." nameA = to_unicode(name_pair[0].strip()) nameB = to_unicode(name_pair[1].strip()) if root_graph is None: root_graph = self ### Check unique. e = self.EG_get_edge_by_names((nameA, nameB), root_graph=root_graph) if not (e is None): raise Exception( 'Unique error. The edge with same names was existed in the graph.' ) nameA = add_double_quote(nameA) nameB = add_double_quote(nameB) e = pydot.Edge(src=nameA, dst=nameB) root_graph.add_edge(e) self.__check_wildcard_existed() self.refresh_bitmap() return e
def generate_node_shape_images(): g = ExtParser.parse_file(ExtGraph.TEMPLATE_DOT) n1 = g.get_node('"n1"')[0] n1.set_label('" "') n1.get_attributes()['colorscheme'] = add_double_quote('blues3') n1.get_attributes()['color'] = add_double_quote('3') n1.get_attributes()['fillcolor'] = add_double_quote('1') n1.get_attributes()['style'] = add_double_quote('filled') for shape in AttrsDef.E_SHAPE: n1.get_attributes()['shape'] = add_double_quote(shape) g.write('resource/node_shape/%s.png'%shape, 'dot', 'png') return
def EG_append_ER_node(self, nodename, root_graph=None, type=0, color=None ): "Add node to 'root_graph' only by name." uname = to_unicode(nodename.strip()) if root_graph is None: root_graph = self # Check unique. n = self.EG_get_node_by_name(uname, root_graph=root_graph) if not (n is None): raise Exception('Unique error. The node with name "%s" was existed in the graph.' % uname) uname = add_double_quote(uname) attributes ={} if not (color is None or color <= 0): attributes["style"] = '\"filled\"' attributes["fillcolor"] = self.node_color[color] if color == 2: attributes["fontcolor"]= jawalker.BuildDictionariesFromDOT.TARGET_FONT_COLOR if type == 1: attributes["orientation"] = u"45.0" elif type == 3: attributes["peripheries"] = u"2" attributes["shape"] = self.node_shape[type] n = pydot.Node(name =uname, **attributes) root_graph.add_node(n) self.__check_wildcard_existed() self.refresh_bitmap() return n
def generate_node_shape_images(): g = ExtParser.parse_file(ExtGraph.TEMPLATE_DOT) n1 = g.get_node('"n1"')[0] n1.set_label('" "') n1.get_attributes()['colorscheme'] = add_double_quote('blues3') n1.get_attributes()['color'] = add_double_quote('3') n1.get_attributes()['fillcolor'] = add_double_quote('1') n1.get_attributes()['style'] = add_double_quote('filled') for shape in AttrsDef.E_SHAPE: n1.get_attributes()['shape'] = add_double_quote(shape) g.write('resource/node_shape/%s.png' % shape, 'dot', 'png') return
def EG_remove_edge(self, name_pair, root_graph=None): "Remove edge from root_graph by end points name of the edge." if root_graph is None: root_graph = self nameA = add_double_quote(to_unicode(name_pair[0])) nameB = add_double_quote(to_unicode(name_pair[1])) try: del root_graph.obj_dict["edges"][(nameA, nameB)] except: pass self.refresh_bitmap() return
def EG_remove_edge(self, name_pair, root_graph=None): "Remove edge from root_graph by end points name of the edge." if root_graph is None: root_graph = self nameA = add_double_quote(to_unicode(name_pair[0])) nameB = add_double_quote(to_unicode(name_pair[1])) try: del root_graph.obj_dict['edges'][(nameA, nameB)] except: pass self.refresh_bitmap() return
def onPGChanged(self, event): _, _, item = self.GetSelectedItem() if item is None: raise Exception("Sth strange happend! Can't find item to store attrs.") return p = event.GetProperty() if (not p) or (not p.IsEnabled()): ### No property in event or a group parent. return ### Get attr from property control. key = str(p.GetName()) ### Important!!! Make sure 'key' not unicode. if key.find('.') > 0: ### If child of a group. key = key.split('.')[-1] v = p.GetValue() if isinstance(v, str): v = v.strip() elif isinstance(v, bool): v = str(v).lower() else: v = str(v) ### Update attr. uv = to_unicode(v); udv = to_unicode(str(p.GetDefaultValue())) if uv == '' or uv.lower() == udv.lower(): try: del item.get_attributes()[key] except: pass # Restore the display value to default. p.SetValue(p.GetDefaultValue()) v = p.GetDefaultValue() else: item.get_attributes()[key] = add_double_quote(v) ### Change PG background if value is different from default. if uv == udv: p.SetBackgroundColour(self.m_tree.GetBackgroundColour(), -1) else: p.SetBackgroundColour('#ffffc0', -1) ### Update view. self.is_data_changed = True self.update_graph() return
def onPGChanged(self, event): _, _, item = self.GetSelectedItem() if item is None: raise Exception("Sth strange happend! Can't find item to store attrs.") return p = event.GetProperty() if (not p) or (not p.IsEnabled()): ### No property in event or a group parent. return ### Get attr from property control. key = str(p.GetName()) ### Important!!! Make sure 'key' not unicode. if key.find('.') > 0: ### If child of a group. key = key.split('.')[-1] v = p.GetValue() if isinstance(v, basestring): v = v.strip().encode('utf8') elif isinstance(v, types.BooleanType): v = str(v).lower() else: v = str(v) ### Update attr. uv = to_unicode(v); udv = to_unicode(p.GetDefaultValue()) if uv == '' or uv == udv: try: del item.get_attributes()[key] except: pass # Restore the display value to default. p.SetValue(p.GetDefaultValue()) v = p.GetDefaultValue() else: item.get_attributes()[key] = add_double_quote(v) ### Change PG background if value is different from default. if uv == udv: p.SetBackgroundColour(self.m_tree.GetBackgroundColour(), -1) else: p.SetBackgroundColour('#ffffc0', -1) ### Update view. self.is_data_changed = True self.update_graph() return
def EG_remove_subgraph(self, name, root_graph=None): "Remove subgraph from root_graph by name." if root_graph is None: root_graph = self sg_name = add_double_quote(to_unicode(name)) try: del root_graph.obj_dict['subgraphs'][sg_name] except: pass self.refresh_bitmap() return
def EG_remove_subgraph(self, name, root_graph=None): "Remove subgraph from root_graph by name." if root_graph is None: root_graph = self sg_name = add_double_quote(to_unicode(name)) try: del root_graph.obj_dict["subgraphs"][sg_name] except: pass self.refresh_bitmap() return
def EG_append_subgraph(self, graphname, root_graph=None): "Add node to 'root_graph' only by name." uname = to_unicode(graphname.strip()) if root_graph is None: root_graph = self # Check unique. n = self.EG_get_subgraph_by_name(uname) if not(n is None): raise Exception('Unique error. The subgraph with name "%s" was existed in the graph.'%uname) uname = add_double_quote(uname) sg = self.create_empty_subgraph(uname) root_graph.add_subgraph(sg) self.refresh_bitmap() return sg
def EG_append_subgraph(self, graphname, root_graph=None): "Add node to 'root_graph' only by name." uname = to_unicode(graphname.strip()) if root_graph is None: root_graph = self # Check unique. n = self.EG_get_subgraph_by_name(uname) if not (n is None): raise Exception('Unique error. The subgraph with name "%s" was existed in the graph.' % uname) uname = add_double_quote(uname) sg = self.create_empty_subgraph(uname) root_graph.add_subgraph(sg) self.refresh_bitmap() return sg
def EG_append_node(self, nodename, root_graph=None): "Add node to 'root_graph' only by name." uname = to_unicode(nodename.strip()) if root_graph is None: root_graph = self # Check unique. n = self.EG_get_node_by_name(uname, root_graph=root_graph) if not (n is None): raise Exception('Unique error. The node with name "%s" was existed in the graph.' % uname) uname = add_double_quote(uname) n = pydot.Node(uname) root_graph.add_node(n) self.__check_wildcard_existed() self.refresh_bitmap() return n
def EG_append_node(self, nodename, attr=None, root_graph=None): "Add node to 'root_graph' only by name." uname = to_unicode(nodename.strip()) if root_graph is None: root_graph = self # Check unique. n = self.EG_get_node_by_name(uname, root_graph=root_graph) if not(n is None): raise Exception('Unique error. The node with name "%s" was existed in the graph.'%uname) uname = add_double_quote(uname) n = pydot.Node(uname,attr) root_graph.add_node(n) self.__check_wildcard_existed() self.refresh_bitmap() return n
def onCopyItem(self, event): selected_id = self.m_tree.GetSelection() if not selected_id.IsOk(): return i_name, i_type, item = self.GetSelectedItem() i_id = self.m_tree.GetSelection() if not i_id.IsOk(): wx.MessageBox("Please select a item as the append location.", 'Don\'t know where to append', style=wx.OK|wx.ICON_EXCLAMATION) return _, i_type, i_graph = self.GetSelectedItem() if i_name not in ["node", "edge", "graph", "digraph"] and i_type=="node": ### Read attrs from data_graph. data_attrs = {} if not item is None: data_attrs = item.get_attributes() else: return if i_type == 'graph': root_id = i_id root_graph = i_graph else: root_id = self.m_tree.GetItemParent(i_id) root_graph = self.m_tree.GetItemData(root_id)[1] i_name += '_copy' if not i_name.startswith('node_'): i_name = 'node_' + i_name gnodes = self.data_graph.EG_get_all_node_names() counter = 1 while i_name in gnodes: if counter > 1: i_name = i_name[:-1] i_name += str(counter) counter += 1 try: i_name = i_name[5:] a_data = self.data_graph.EG_append_node(i_name, root_graph=root_graph) except: wx.MessageBox('Can\'t add item "%s", maybe the same-named item was existed.'%(i_name), 'Found duplicated node name') return ### Add item to tree. a_id = self.m_tree.AppendItem(root_id, i_name) for itemattribute in list(data_attrs.keys()): itemattrval = data_attrs[itemattribute] if itemattribute == 'label': counter = counter - 1 if counter: labelprefix = "Copied_{} content -> ".format(counter) else: labelprefix = "Copied content -> " itemattrval = labelprefix + itemattrval.replace('\"','') a_data.set(itemattribute, add_double_quote(itemattrval)) self.m_tree.SetItemData(a_id, ('node', a_data)) self.m_tree.SetItemImage(a_id, self.img_dict[('node', 'color')]) ### Select the item in tree. if not a_id is None: self.m_tree.SelectItem(a_id, True) self.is_data_changed = True self.update_graph() return
def onAppendItem(self, event): # Find root in m_tree to append. i_id = self.m_tree.GetSelection() if not i_id.IsOk(): wx.MessageBox("Please select a item as the append location.", 'Don\'t know where to append', style=wx.OK|wx.ICON_EXCLAMATION) return _, i_type, i_graph = self.GetSelectedItem() if i_type == 'graph': root_id = i_id root_graph = i_graph else: root_id = self.m_tree.GetItemParent(i_id) root_graph = self.m_tree.GetItemData(root_id)[1] dlg = DA(self) r = dlg.ShowModal() a_id = None if r == wx.ID_OK: v = dlg.getAppendValue() if v[0] == 'node': ### Add node. try: a_data = self.data_graph.EG_append_node(v[1], root_graph=root_graph) shape_data, style_data = v[2] shape_data = str(shape_data.strip()) style_data = str(style_data.strip()) except: wx.MessageBox('Can\'t add item "%s", maybe the same-named item was existed.'%(v[1]), 'Found duplicated node name') return ### Add item to tree. a_id = self.m_tree.AppendItem(root_id, v[1]) a_data.set('shape', add_double_quote(shape_data)) a_data.set('style', add_double_quote(style_data)) self.m_tree.SetItemData(a_id, ('node', a_data)) self.m_tree.SetItemImage(a_id, self.img_dict[('node', 'color')]) elif v[0] == 'edge': ### Add edge. n, n1 = v[1] try: a_data = self.data_graph.EG_append_edge((n,n1), root_graph=root_graph) shape_data, style_data = v[2] shape_data = str(shape_data.strip()) style_data = str(style_data.strip()) except: wx.MessageBox('Can\'t add edge "%s -> %s", maybe the edge was existed.'%(n,n1), 'Found duplicated edge name') return ### Add item to tree. a_id = self.m_tree.AppendItem(root_id, n+' -> '+n1) self.m_tree.SetItemData(a_id, ('edge', a_data)) self.m_tree.SetItemImage(a_id, self.img_dict[('edge', 'color')]) a_data.set('shape', add_double_quote(shape_data)) a_data.set('style', add_double_quote(style_data)) elif v[0] == 'subgraph': ### Add edge. try: a_data = self.data_graph.EG_append_subgraph(v[1], root_graph=root_graph) except: wx.MessageBox('Can\'t add item "%s", maybe the same-named subgraph was existed.'%(v[1]), 'Found duplicated subgraph name') return ### Add subgraph root to tree. a_id = self.m_tree.AppendItem(root_id, v[1]) self.m_tree.SetItemData(a_id, ('graph', a_data)) self.m_tree.SetItemImage(a_id, self.img_dict[('graph', 'color')]) ### Add wildcard nodes to subgraph. for n in ['node', 'edge']: n_id = self.m_tree.AppendItem(a_id, n) n_data = self.data_graph.EG_get_node_by_name(n, root_graph=a_data) self.m_tree.SetItemData(n_id, ('node', n_data) ) self.m_tree.SetItemImage(n_id, self.img_dict[(n, 'gray')]) self.m_tree.Expand(a_id) ### Set label. label = escape_dot_string(dlg.m_textCtrl_label.GetValue()) if label != '': a_data.set('label', add_double_quote(label)) ### Select the item in tree. if not a_id is None: self.m_tree.SelectItem(a_id, True) self.is_data_changed = True self.update_graph() dlg.Destroy() return