def handle_alert(iodefv2): graph = gvgen.GvGen() alert_g = graph.newItem("IODEFV2 Alert") value = iodefv2.Get("alert.classification.text") if value: act = graph.newItem("alert.classification.text", None, distinct=1) actc = graph.newItem(value, act, distinct=1) graph.newLink(alert_g, actc) value = iodefv2.Get("alert.assessment.impact.description") if value: aaid = graph.newItem("alert.assessment.impact.description", None, distinct=1) aaidc = graph.newItem(value, aaid, distinct=1) graph.newLink(alert_g, aaidc) value = iodefv2.Get("alert.assessment.impact.completion") if value: aaic = graph.newItem("alert.assessment.impact.completion", None, distinct=1) aaicc = graph.newItem(value, aaic, distinct=1) graph.newLink(alert_g, aaicc) graph.dot()
def object_to_GvGen(obj_file): """ Generate a DOT file with GvGen. http://www.picviz.com/sections/opensource/gvgen.html """ dic_obj = open(obj_file, "r") if options.verbose: pass # print "Loading dictionary..." dic_ip = pickle.load(dic_obj) if options.verbose: pass # print "Creating GvGen object..." graph = gvgen.GvGen() graph.styleDefaultAppend("color", "lightblue") graph.styleDefaultAppend("style", "filled") source = graph.newItem("Source") destination = graph.newItem("Destination") dic_ip_dst = {} for ip_src in dic_ip: ipsrc = graph.newItem(ip_src, source) for ip_dst in dic_ip[ip_src]: if ip_dst not in dic_ip_dst: ipdst = graph.newItem(ip_dst, destination) dic_ip_dst[ip_dst] = ipdst else: ipdst = dic_ip_dst[ip_dst] link = graph.newLink(ipsrc, ipdst) graph.propertyAppend(link, "color", "#158510") graph.propertyAppend(link, "label", str(dic_ip[ip_src][ip_dst])) graph.dot()
def serialize_workflow_spec(self, wf_spec): nodes = set() linked = set() graph = gvgen.GvGen() parent = graph.newItem("Workflow") # these built in shapes are available: # http://www.graphviz.org/doc/info/shapes.html graph.styleAppend("Cancel", "shape", "oval") graph.styleAppend("CancelTask", "shape", "oval") graph.styleAppend("Choose", "shape", "diamond") graph.styleAppend("ExclusiveChoice", "shape", "diamond") graph.styleAppend("Execute", "shape", "rect") graph.styleAppend("Gate", "shape", "trapezium") graph.styleAppend("Join", "shape", "invtriangle") graph.styleAppend("Merge", "shape", "invtriangle") graph.styleAppend("MultiChoice", "shape", "diamond") graph.styleAppend("MultiInstance", "shape", "box") graph.styleAppend("ReleaseMutex", "shape", "diamond") graph.styleAppend("Simple", "shape", "rect") graph.styleAppend("StartTask", "shape", "oval") graph.styleAppend("SubWorkflow", "shape", "invhouse") graph.styleAppend("ThreadMerge", "shape", "invtriangle") graph.styleAppend("ThreadSplit", "shape", "triangle") graph.styleAppend("ThreadStart", "shape", "oval") graph.styleAppend("Transform", "shape", "rect") graph.styleAppend("Trigger", "shape", "oval") # build graph with all the nodes first def recurisvelyAddNodes(task_spec): if task_spec in nodes: return task_spec.gv = graph.newItem(task_spec.name, parent) # add a default style for this class so that if we don't have one # when we apply it doesn't break the GvGen library graph.styleAppend(task_spec.__class__.__name__, "ignore", "this") graph.styleApply(task_spec.__class__.__name__, task_spec.gv) nodes.add(task_spec) sub_specs = ([task_spec.spec.start] if hasattr(task_spec, 'spec') else []) + task_spec.outputs for t in sub_specs: recurisvelyAddNodes(t) # then link all the nodes together def recursive_linking(task_spec): if task_spec in linked: return linked.add(task_spec) sub_specs = ([task_spec.spec.start] if hasattr(task_spec, 'spec') else []) + task_spec.outputs for i, t in enumerate(sub_specs): graph.newLink(task_spec.gv, t.gv) recursive_linking(t) recurisvelyAddNodes(wf_spec.start) recursive_linking(wf_spec.start) return (graph.dot() if graph.dot() else '')
def getGraph(self): graph = gvgen.GvGen() if self.root == None: print('No root node, could not get graph') return self.root.addToGraph(graph) return graph
def generateGraph(self, keep=[], nodes_attr=[], LR=False, file_name="graph"): """ Generates the max-tree graph. You can provide an array containing attributes to be displayed in the graph representation. """ n_nodes = self.node_array.shape[1] h = self.node_array[2, :] if keep == []: keep = np.ones(n_nodes, dtype=bool) if nodes_attr == []: nodes_attr = self.node_array[3, :] # Default attribute is area G = gvgen.GvGen() G.styleAppend("remove", "style", "dashed") G.styleAppend("keep", "color", "red") G.styleAppend("keep", "style", "filled") items = {} for i in range(n_nodes): if keep[i]: items[i] = G.newItem('%d: %d [%d]' % (i, h[i], nodes_attr[i])) G.styleApply("keep", items[i]) else: items[i] = G.newItem('%d: %d [%d]' % (i, h[i], nodes_attr[i])) G.styleApply("remove", items[i]) pindex = self.node_array[0, i] if pindex != i: G.newLink(items[i], items[pindex]) fd = StringIO() G.dot(fd) dottext = fd.getvalue() if LR: dottext = dottext.replace("TB", "RL") dottext = dottext.replace("{", "{rankdir=LR") text_file = open(file_name + ".dot", "w") text_file.write(dottext) text_file.close() try: os.system("/usr/bin/dot -Tpng %s.dot > %s.png" % (file_name, file_name)) # os.remove(file_name + ".dot") except: print( "Unable to save graph image. The method will return just the GraphViz code" ) # os.remove(file_name + ".dot") return dottext return
def generateGraph(par, img): """ This method generates the graphviz code to draw the the pixel oriented max-tree. Input: - par, 1d-array, int32. Parent array. - img, 2d-array, uint8. Image. Output: - graphviz_tree, str. String containing the graphviz code that will be used to draw the pixel oriented max-tree. """ f = img.ravel() G = gvgen.GvGen() G.styleAppend("levroot", "color", "red") G.styleAppend("levroot", "style", "filled") G.styleAppend("levroot", "fontcolor", "white") l = [] items = {} for i, p in enumerate(f): if i != par[i]: l.append(i) items[i] = G.newItem('%d: %f' % (i, f[i])) if par[i] == i or f[i] != f[par[i]]: G.styleApply("levroot", items[i]) for i in l: p = i while (p != par[p]): if not p in items.keys(): items[p] = G.newItem('%d:%f' % (p, f[p])) if par[p] == p or f[p] != f[par[p]]: G.styleApply("levroot", items[p]) if not par[p] in items.keys(): q = par[p] items[q] = G.newItem('%d:%f' % (q, f[q])) if par[q] == q or f[q] != f[par[q]]: G.styleApply("levroot", items[q]) G.newLink(items[p], items[par[p]]) p = par[p] G.newLink(items[p], items[par[p]]) fd = StringIO.StringIO() G.dot(fd) dottext = fd.getvalue() return dottext
def __init__(self, path, **kwargs): import gvgen super().__init__(path, **kwargs) self.nodes = {} self.links = [] self.G = gvgen.GvGen() self.G.styleDefaultAppend("fontsize", "10") self.G.styleDefaultAppend("style", "filled") self.G.styleDefaultAppend("fillcolor", "#009EDB") self.G.styleDefaultAppend("fontcolor", "#FFFFFF") self.G.styleDefaultAppend("height", "0.75") self.G.styleDefaultAppend("width", "0.75") self.G.styleDefaultAppend("widshapeth", "circle")
import LibIodefEasy except Exception, e: print "Import failed: ", e print "Try 'cd ./.libs && ln -s libiodef_python.so _LibIodefEasy.so'" sys.exit(1) client = LibIodefEasy.Client("LibIodefGvGen") client.Init() client.PoolInit("localhost", 1) sourceslist = {} targetslist = {} alertslist = {} graph = gvgen.GvGen() def sighandler(sig, frame): graph.dot() sys.exit(0) signal.signal(signal.SIGTERM, sighandler) signal.signal(signal.SIGINT, sighandler) def handle_alert(iodef): source = iodef.Get("alert.source(0).node.address(0).address") if not sourceslist.has_key(source):
def generateCCPathGraph(self, start, end=0, s=(100, 100), parent_scale=True, composite_nodes=True, LR=False, file_name="graph"): """ Generates the graph of a max-tree path. """ G = gvgen.GvGen() parents = self.node_array[0, :] h = self.node_array[2, :] nlevels = h - h[parents] G.styleAppend("node", "fontcolor", "transparent") G.styleAppend("node", "shape", "rectangle") items = {} pos = start items[pos] = G.newItem("") j = 1000000 while True: path = 'node___' + str(pos) + '.png' if parent_scale and pos != end: xpmin, xpmax = self.node_array[6, parents[pos]], self.node_array[ 7, parents[pos]] ypmin, ypmax = self.node_array[9, parents[pos]], self.node_array[ 10, parents[pos]] else: xpmin, xpmax = self.node_array[6, pos], self.node_array[7, pos] ypmin, ypmax = self.node_array[9, pos], self.node_array[10, pos] node_image = (self.recConnectedComponent(pos)[xpmin:xpmax + 1, ypmin:ypmax + 1]).astype('uint8') * 255 node_image = cv2.resize(node_image, (s[1], s[0])) if parent_scale and pos != end: bool_image = np.zeros(s, dtype=bool) indexes = np.nonzero(node_image) if indexes[0].size: ymin, ymax = indexes[0].min(), indexes[0].max() xmin, xmax = indexes[1].min(), indexes[1].max() bool_image[ymin:ymax + 1, xmin] = 1 bool_image[ymin:ymax + 1, xmax] = 1 bool_image[ymin, xmin:xmax + 1] = 1 bool_image[ymax, xmin:xmax + 1] = 1 node_image = np.array([node_image, node_image, node_image]) node_image[0][bool_image] = 255 node_image[1][bool_image] = 0 node_image[2][bool_image] = 0 if node_image.ndim == 3: cv2.imwrite(path, node_image.transpose(1, 2, 0)) else: cv2.imwrite(path, cv2.cvtColor(node_image, cv2.COLOR_GRAY2RGB)) #items[pos] = G.newItem("") G.propertyAppend(items[pos], "image", path) G.styleApply("node", items[pos]) if nlevels[pos] > 1 and composite_nodes: items[j] = G.newItem("") G.propertyAppend(items[j], "image", path) G.styleApply("node", items[j]) G.newLink(items[pos], items[j]) j += 1 for i in range(nlevels[pos] - 2): items[j] = G.newItem("") G.propertyAppend(items[j], "image", path) G.styleApply("node", items[j]) G.newLink(items[j - 1], items[j]) j += 1 if pos == end: break pindex = parents[pos] items[pindex] = G.newItem("") if nlevels[pos] > 1 and composite_nodes: G.newLink(items[j - 1], items[pindex]) else: G.newLink(items[pos], items[pindex]) pos = pindex fd = StringIO() G.dot(fd) dottext = fd.getvalue() dottext = dottext.replace("TB", "RL") dottext = dottext.replace("{", "{rankdir=LR") text_file = open(file_name + ".dot", "w") text_file.write(dottext) text_file.close() try: os.system("/usr/bin/dot -Tpng %s.dot > %s.png" % (file_name, file_name)) os.remove(file_name + ".dot") files = os.listdir('.') for f in files: if f.startswith("node___"): os.remove(f) except: print( "Unable to save graph image. The method will return just the GraphViz code. Lots of temporary files were generated in your current folder." ) os.remove(file_name + ".dot") return dottext return
def generateCCGraph(self, s=(100, 100), parent_scale=True, LR=False, file_name="graph"): """ Generates the max-tree graph. Showing the connected components of each node. """ n_nodes = self.node_array.shape[1] G = gvgen.GvGen() parents = self.node_array[0, :] G.styleAppend("node", "fontcolor", "transparent") G.styleAppend("node", "shape", "rectangle") items = {} items[0] = G.newItem(str(0)) G.styleApply("node", items[0]) for i in range(1, n_nodes): items[i] = G.newItem("") path = 'node___' + str(i) + '.png' if parent_scale: xpmin = self.node_array[6, parents[i]] xpmax = self.node_array[7, parents[i]] ypmin = self.node_array[9, parents[i]] ypmax = self.node_array[10, parents[i]] else: xpmin, xpmax = self.node_array[6, i], self.node_array[7, i] ypmin, ypmax = self.node_array[9, i], self.node_array[10, i] node_image = (self.recConnectedComponent(i)[xpmin:xpmax + 1, ypmin:ypmax + 1]).astype('uint8') * 255 node_image = cv2.resize(node_image, (s[1], s[0])) if parent_scale: bool_image = np.zeros(s, dtype=bool) indexes = np.nonzero(node_image) if indexes[0].size: ymin, ymax = indexes[0].min(), indexes[0].max() xmin, xmax = indexes[1].min(), indexes[1].max() bool_image[ymin:ymax + 1, xmin] = 1 bool_image[ymin:ymax + 1, xmax] = 1 bool_image[ymin, xmin:xmax + 1] = 1 bool_image[ymax, xmin:xmax + 1] = 1 node_image = np.array([node_image, node_image, node_image]) node_image[0][bool_image] = 255 node_image[1][bool_image] = 0 node_image[2][bool_image] = 0 if node_image.ndim == 3: cv2.imwrite(path, node_image.transpose(1, 2, 0)) else: cv2.imwrite(path, cv2.cvtColor(node_image, cv2.COLOR_GRAY2RGB)) G.propertyAppend(items[i], "image", path) G.styleApply("node", items[i]) pindex = parents[i] if pindex != i: G.newLink(items[i], items[pindex]) fd = StringIO() G.dot(fd) dottext = fd.getvalue() if LR: dottext = dottext.replace("TB", "RL") dottext = dottext.replace("{", "{rankdir=LR") text_file = open(file_name + ".dot", "w") text_file.write(dottext) text_file.close() try: os.system("/usr/bin/dot -Tpng %s.dot > %s.png" % (file_name, file_name)) os.remove(file_name + ".dot") files = os.listdir('.') for f in files: if f.startswith("node___"): os.remove(f) except: print( "Unable to save graph image. The method will return just the GraphViz code. Lots of temporary files were generated in your current folder." ) os.remove(file_name + ".dot") return dottext return
#!/usr/bin/python import gvgen # Creates the new graph instance graph = gvgen.GvGen("Legend") # Creates two items labeled "Foo" and "Bar" a = graph.newItem("foo") b = graph.newItem("bar") # Links from "foo" to "bar" graph.newLink(a,b) graph.styleAppend("foostyle","color","red") graph.styleAppend("foostyle","shape","rectangle") graph.styleApply("foostyle", a) graph.styleAppend("barstyle","color","blue") graph.styleAppend("barstyle","style","filled") graph.styleApply("barstyle", b) graph.legendAppend("foostyle", "Foo item") graph.legendAppend("barstyle", "This is the bar item") # Outputs the graphviz code graph.dot()
#!/usr/bin/python # # Example of complex # Graph representing a network # import gvgen # Creates the new graph instance graph = gvgen.GvGen( None, "overlap=\"scale\";\nlabelfloat=\"true\";\nsplines=\"true\";") # We define different styles graph.styleAppend("router", "shapefile", "router.png") graph.styleAppend("router", "color", "white") graph.styleAppend("router", "label", "") # Creates items insidenet = graph.newItem("Inside network") internet = graph.newItem("Internet") win1 = graph.newItem("Windows", insidenet) win2 = graph.newItem("Windows", insidenet) linux = graph.newItem("Linux", insidenet) hurd = graph.newItem("GNU/Hurd", insidenet) sun = graph.newItem("Sun", internet) router = graph.newItem("Router") # Time to apply styles and set some properties graph.styleApply("router", router) graph.propertyAppend(win1, "shapefile", "wingdows.png") graph.propertyAppend(win2, "shapefile", "wingdows.png")