コード例 #1
1
def gen_dot():
    d = Dot()
    nodes = dict()
    login = read_ssv_file("vpopmail.login")
    db = MySQLdb.connect(host="localhost", user=login[0], passwd=login[2], db=login[1])
    c = db.cursor()
    c.execute("SELECT alias, valias_line FROM valias WHERE domain=%s", (MAILDOMAIN,))
    for alias, target in c.fetchall():
        assert target[0] == "&"
        target = target[1:]
        alias += "@" + MAILDOMAIN
        if not alias in nodes:
            nodes[alias] = Node(alias)
            d.add_node(nodes[alias])
        if not target in nodes:
            nodes[target] = Node(target)
            d.add_node(nodes[target])
        d.add_edge(Edge(nodes[alias], nodes[target]))
    for list in Utils.list_names():
        if list == "plukdenacht2008":
            continue
        source = list + "@" + LISTDOMAIN
        if not source in nodes:
            nodes[source] = Node(source)
            d.add_node(nodes[source])
        m = MailList.MailList(list, lock=False)
        for member in m.members:
            if not member in nodes:
                nodes[member] = Node(member)
                d.add_node(nodes[member])
            d.add_edge(Edge(nodes[source], nodes[member]))
    d.write("the.dot")
コード例 #2
0
    def handle(self, filename=None, **options):
        try:
            from pydot import Dot, Edge, Node
        except ImportError:
            raise CommandError("need pydot python module ( apt-get install python-pydot )")

        graph = Dot()

        for status, description in STATUS_CHOICES:
            graph.add_node(Node(
                'status-%s' % status,
                label='"%s (%s)"' %
                    (description.encode('utf-8'), status))
            )

        from sadiki.core.workflow import workflow
        for transition_index in workflow.available_transitions():
            transition = workflow.get_transition_by_index(transition_index)
            graph.add_edge(Edge(
                'status-%s'% transition.src,
                'status-%s' % transition.dst,
                label='"%s (%s)"' % (transition.comment.encode('utf-8'), transition.index),
                style='solid' if transition.required_permissions else 'dashed',
            ))

        if filename:
            graph.write_png(filename)
        else:
            print graph.to_string()
コード例 #3
0
ファイル: BoxModel.py プロジェクト: lpouillo/boxmodel
 def plot_state(self, boxes, deltas, name = '', outdir = None):
     """ Make a graph of a given state """
     graph = Dot(graph_type='digraph', fontname="Verdana", size="10, 5", fixedsize= True)
     i_box = 0
     for box in boxes:            
         textcolor = 'white' if sum( [ self.color_chars.index(col) for col in self.plots_conf[box]['color'].split('#')[1] ] ) < 35 else 'black' 
         node_box = Node(box, style="filled", label = '<<font POINT-SIZE="10" color="'+textcolor+'">'+box+'<br/> '+
                         "%.7f" % round(deltas[i_box], 7)+'</font>>',
             fillcolor = self.plots_conf[box]['color'], shape = self.plots_conf[box]['shape'])
         i_box += 1
         graph.add_node(node_box)
         
     for box_from, boxes_to in self.Flux.iteritems():
         for box_to, flux in boxes_to.iteritems():
             if flux !=0:
                 if flux > 0:
                     edge = Edge(box_from, box_to,  label = '<<font POINT-SIZE="10">'+str(flux)+'</font>>')
                 elif flux < 0:
                     edge = Edge(box_to, box_from,  label = '<<font POINT-SIZE="10">'+str(flux)+'</font>>')                
                 graph.add_edge(edge)
     
     if outdir is None:
         outdir = self.result_dir
         
     outfile = outdir+'/state'+name+'.png'
     graph.write_png(outfile)
     logger.info('State has been saved to '+set_style(outfile, 'emph'))
コード例 #4
0
ファイル: graphviz.py プロジェクト: mediatum/mediatum
def create_example_graph():
    g = Dot(graph_name="workflow: example",
            labelloc="t", label="workflow: example", fontsize=18, fontcolor="blue")
    g.set_node_defaults(shape="box", fontsize=12)
    g.set_edge_defaults(fontsize=13, labeldistance=3)
    n1 = Node(name="Start")
    g.add_node(n1)
    n2 = Node(name="StepTrueEnd", color="#04B45F")
    g.add_node(n2)
    n3 = Node(name="StepFalse")
    g.add_node(n3)
    n4 = Node(name="StepFalse2")
    g.add_node(n4)
    n5 = Node(name="StepFalse3End", color="#04B45F")
    g.add_node(n5)
    e1 = true_edge(n1, n2)
    g.add_edge(e1)
    e2 = false_edge(n1, n3)
    g.add_edge(e2)
    e3 = true_edge(n3, n4)
    g.add_edge(e3)
    e_back = false_edge(n4, n1, label="back if false")
    g.add_edge(e_back)
    e4 = true_edge(n4, n5)
    g.add_edge(e4)
    return g
コード例 #5
0
ファイル: graphviz.py プロジェクト: USGCRP/gcis-prov
def get_session_svg(viz_data):
    """Take session visualization data and return svg."""
    
    graph = Dot('graphname', graph_type='digraph')
    
    #loop create all nodes and store by id
    node_dict = {}
    for i, node_data in enumerate(viz_data['nodes']):
        id = node_data['id']
        node_dict[id] = str(i)
        graph.add_node(Node(str(i)))
        
    #add edges by links
    for link_data in viz_data['links']:
        snode = node_dict[viz_data['nodes'][link_data['source']]['id']]
        tnode = node_dict[viz_data['nodes'][link_data['target']]['id']]
        graph.add_edge(Edge(snode, tnode))
    
    #get svg of graph
    file = NamedTemporaryFile()
    graph.write_svg(file.name)
    svg = file.read()
    file.close()
    
    #f = open('/tmp/session/session.svg', 'w')
    #f.write("%s\n" % svg)
    #f.close()

    return svg
コード例 #6
0
    def save(self, cfg, filename, print_ir=False, format='dot', options=None):
        """Save basic block graph into a file.
        """
        if options is None:
            options = {}

        try:
            dot_graph = Dot(**self.graph_format)

            # Add nodes.
            nodes = {}
            for bb in cfg.basic_blocks:
                nodes[bb.address] = self._create_node(bb, cfg.name, print_ir, options)

                dot_graph.add_node(nodes[bb.address])

            # Add edges.
            for bb_src in cfg.basic_blocks:
                for bb_dst_addr, branch_type in bb_src.branches:
                    edge = self._create_edge(nodes[bb_src.address], nodes[bb_dst_addr], branch_type)

                    dot_graph.add_edge(edge)

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)", filename, format, exc_info=True)
コード例 #7
0
    def save(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        node_format = {
            'shape' : 'Mrecord',
            'rankdir' : 'LR',
            'fontname' : 'monospace',
            'fontsize' : '9.0'
        }

        edge_format = {
            'fontname' : 'monospace',
            'fontsize' : '8.0'
        }

        edge_colors = {
            'taken' : 'green',
            'not-taken' : 'red',
            'direct' : 'blue'
        }

        try:
            # for each conneted component
            for idx, gr in enumerate(networkx.connected_component_subgraphs(self._graph.to_undirected())):
                graph = Dot(graph_type="digraph", rankdir="TB")

                # add nodes
                nodes = {}
                for bb_addr in gr.node.keys():
                    dump = self._dump_bb(self._bb_by_addr[bb_addr], print_ir)

                    # html-encode colon character
                    dump = dump.replace("!", "&#33;")
                    dump = dump.replace("#", "&#35;")
                    dump = dump.replace(":", "&#58;")
                    dump = dump.replace("{", "&#123;")
                    dump = dump.replace("}", "&#125;")

                    label = "{<f0> 0x%08x | %s}" % (bb_addr, dump)

                    nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                    graph.add_node(nodes[bb_addr])

                # add edges
                for bb_src_addr in gr.node.keys():
                    for bb_dst_addr, branch_type in self._bb_by_addr[bb_src_addr].branches:
                        graph.add_edge(Edge(nodes[bb_src_addr],
                            nodes[bb_dst_addr], label=branch_type, \
                            color=edge_colors[branch_type], **edge_format))

                graph.write("%s_%03d.%s" % (filename, idx, format), format=format)
        except Exception as err:
            import traceback
            import sys
            print("[E] Error loading BARF (%s:%d) : '%s'" %
                (__name__, sys.exc_traceback.tb_lineno, str(err)))
            print("")
            print(traceback.format_exc())
コード例 #8
0
def buildDependencyControlGraph(dependencies,sizes=[1,2],productionTarget=1.25):
    from pydot import Dot, Node, Edge, Cluster
    
    dgraph = Dot(graph_type='graph',fontname='Verdana',splines="line",maxiter="500")
    #splines="line",
    nodes = {}
    edges = {}
    
    for (controlMap,pvalue) in dependencies:
        targetNames = []
        size = len(controlMap)
        if size not in sizes:
            continue
        for (targetName,cvalue) in controlMap.items():
            #targetTag = "%s(%s)" % (targetName,cvalue)
            targetTag = targetName
            targetNames.append(targetTag)
            if size not in nodes.keys():
                nodes[size] = set()
            nodes[size].add((targetName,cvalue))            
        edgeCombos = combinations(targetNames,2)
        for value in edgeCombos:
            key = list(value)
            key.sort()
            (e1,e2) = key
            if (e1,e2) not in edges.keys() and (e2,e1) not in edges.keys():
                edge = Edge(e1,e2)
                edges[(e1,e2)] = edge
            else:
                #print "dup key %s %s" % (e1,e2)
                pass
            
    nodes = cleanNodeNames(nodes)
    
    for (key,nodeValues) in nodes.items():
        if key == 1:
            ishape = "rectangle"
        elif key == 2:
            ishape = "oval"
        elif key == 3:
            ishape = "hexagon"
        else:
            ishape = "circle"
        
        for (name,value) in nodeValues:
            icolor = "black"
            if value > 0:
                icolor = "red"
            if value < 0:
                icolor = "green"
            targetTag = "%s(%s)" % (name,value)
            targetTag = name       
            dgraph.add_node(Node(targetTag,shape=ishape,color=icolor))
            #print "Node: [%s] : size[%s] value[%s] shape[%s]" % (name,key,value,ishape)
        
    for (key,edge) in edges.items():
        dgraph.add_edge(edge)
    
    return dgraph 
コード例 #9
0
ファイル: visualize.py プロジェクト: SegFaultAX/graffiti
def visualize(graph, filename="graph.png", include_args=True, transitive=False):
    data = to_graphviz(graph, transitive)
    dot = Dot(graph_type="digraph")

    for node in data["nodes"]:
        fmt = format_edge(graph, node) if include_args else node
        dot.add_node(Node(node, label=fmt))
    for a, b in data["edges"]:
        dot.add_edge(Edge(a, b))

    dot.write_png(filename)
コード例 #10
0
def RenderSIPCollection(sipGraph, dot=None):
    try:
        from pydot import Node, Edge, Dot
    except:
        import warnings
        warnings.warn("Missing pydot library", ImportWarning)
    if not dot:
        dot = Dot(graph_type='digraph')
        dot.leftNodesLookup = {}
    nodes = {}
    for N, prop, q in sipGraph.query(
            'SELECT ?N ?prop ?q {  ?prop a magic:SipArc . ?N ?prop ?q . }',
            initNs={u'magic': MAGIC}):

        if MAGIC.BoundHeadPredicate in sipGraph.objects(
                subject=N, predicate=RDF.type):
            NCol = [N]
        else:
            NCol = Collection(sipGraph, N)

        if q not in nodes:
            newNode = Node(makeMD5Digest(q),
                           label=normalizeTerm(q, sipGraph),
                           shape='plaintext')
            nodes[q] = newNode
            dot.add_node(newNode)

        bNode = BNode()
        nodeLabel = ', '.join([normalizeTerm(term, sipGraph)
                               for term in NCol])
        edgeLabel = ', '.join([var.n3()
                               for var in Collection(sipGraph,
                                                     first(sipGraph.objects(
                                                         prop, MAGIC.bindings)))])
        markedEdgeLabel = ''
        if nodeLabel in dot.leftNodesLookup:
            bNode, leftNode, markedEdgeLabel = dot.leftNodesLookup[nodeLabel]
            # print("\t", nodeLabel, edgeLabel,
            #       markedEdgeLabel, not edgeLabel == markedEdgeLabel
        else:
            leftNode = Node(makeMD5Digest(bNode),
                            label=nodeLabel, shape='plaintext')
            dot.leftNodesLookup[nodeLabel] = (bNode, leftNode, edgeLabel)
            nodes[bNode] = leftNode
            dot.add_node(leftNode)

        if not edgeLabel == markedEdgeLabel:
            edge = Edge(leftNode,
                        nodes[q],
                        label=edgeLabel)
            dot.add_edge(edge)
    return dot
コード例 #11
0
ファイル: main.py プロジェクト: marvinHao/DepAnalysis
    def draw(self, path: str, format: str = "raw") -> None:

        node_aggr = {}
        for node in self._nodes:
            level = node.get_level()
            label = node.get_label()
            identifier = node.get_id()
            if node_aggr.get(level) is None:
                node_aggr[level] = {}
            if level != 0:
                gv_cluster = GVCluster(identifier)
                gv_cluster.set("label", label)
                node_aggr[level][identifier] = gv_cluster
            else:
                gv_node = GVNode(identifier)
                gv_node.set("label", label)
                node_aggr[level][identifier] = gv_node

        gv_dot = GVDot()
        gv_dot.set("ranksep", "1.0 equally")

        if self._lable is not None:
            gv_dot.set("label", self._lable)

        for node in self._nodes:
            level = node.get_level()
            parent = node.get_parent()
            parent_level = node.get_parent_level()
            identifier = node.get_id()
            if level != 0:
                if parent is not None:
                    node_aggr[parent_level][parent].add_subgraph(node_aggr[level][identifier])
                else:
                    gv_dot.add_subgraph(node_aggr[level][identifier])
            else:
                if parent is not None:
                    node_aggr[parent_level][parent].add_node(node_aggr[level][identifier])
                else:
                    gv_dot.add_node(node_aggr[level][identifier])

        for edge in self._edges:
            label = edge.get_label()
            gv_edge = GVEdge(edge.get_src(), edge.get_dst())
            if label is not None:
                gv_edge.set("label", edge.get_label())
            gv_dot.add_edge(gv_edge)

        gv_dot.write(path, format=format)
コード例 #12
0
def process_plan(filename, plan):
    graph = Dot()
    teams = fetch_teams()

    nodes = make_nodes(plan, teams)

    for team_node in nodes:
        graph.add_node(nodes[team_node])

    for team in plan:
        edges = graph_way(team, plan[team], nodes)
        for edge in edges:
            graph.add_edge(edge)

    graph.set_prog("circo")
    graph.write_png(filename)
コード例 #13
0
ファイル: equationgui.py プロジェクト: dfwyatt/eutactic
 def draw(self, prob, targetFile):
     # Do the graphing stuff here...
     # Root graph
     g = Dot(graph_type="digraph", nodesep=2, overlap=False)
     #g.set_edge_defaults(weight="0", minlen="10")
     # Organise by adding constraints (adds edges too)
     for constr in prob.constrs:
         # Node for constraint
         constrNode = Node(constr.name, shape="ellipse", style="filled", fillcolor = "#aaaaff")
         constrNode.set_label(constr.name + ": " + constr.getTextFormula())
         g.add_node(constrNode)
         # Associated expressions
         for expr in constr.exprs:
             self.addNodesForChildren(g, expr, constr)
     # Finally, render
     #g.write_png("problem_structure.png", prog="dot")
     g.write_png(targetFile, prog="neato")
コード例 #14
0
ファイル: Util.py プロジェクト: drewp/FuXi
def renderNetwork(network, nsMap={}):
    """
    Takes an instance of a compiled ReteNetwork and a namespace mapping (for constructing QNames
    for rule pattern terms) and returns a BGL Digraph instance representing the Rete network
    #(from which GraphViz diagrams can be generated)
    """
    # from FuXi.Rete import BuiltInAlphaNode
    # from BetaNode import LEFT_MEMORY, RIGHT_MEMORY, LEFT_UNLINKING
    dot = Dot(graph_type='digraph')
    namespace_manager = NamespaceManager(Graph())
    for prefix, uri in list(nsMap.items()):
        namespace_manager.bind(prefix, uri, override=False)

    visitedNodes = {}
    edges = []
    idx = 0
    for node in list(network.nodes.values()):
        if node not in visitedNodes:
            idx += 1
            visitedNodes[node] = generateBGLNode(
                dot, node, namespace_manager, str(idx))
            dot.add_node(visitedNodes[node])
    nodeIdxs = {}
    for node in list(network.nodes.values()):
        for mem in node.descendentMemory:
            if not mem:
                continue
            bNode = mem.successor
        for bNode in node.descendentBetaNodes:
            for idx, otherNode in enumerate([bNode.leftNode, bNode.rightNode]):
                if node == otherNode and (node, otherNode) not in edges:
                    for i in [node, bNode]:
                        if i not in visitedNodes:
                            idx += 1
                            nodeIdxs[i] = idx
                            visitedNodes[i] = generateBGLNode(
                                dot, i, namespace_manager, str(idx))
                            dot.add_node(visitedNodes[i])
                    edge = Edge(visitedNodes[node],
                                visitedNodes[bNode],
                                label=idx == 0 and 'left' or 'right')
                    dot.add_edge(edge)
                    edges.append((node, bNode))

    return dot
コード例 #15
0
ファイル: state_inference.py プロジェクト: CarlEkerot/Exjobb
def render_state_diagram(C_M, S_M, filename, depth=None):
    tot_M = C_M + S_M

    if depth:
        reachable_states = find_reachable_states(tot_M, len(tot_M) - 1, depth)
        reachable_states.add(len(tot_M) - 1)
    else:
        reachable_states = range(len(tot_M))

    graph = Dot(graph_type="digraph", rankdir="LR")
    nodes = [Node(str(i), shape="circle") for i in range(len(tot_M) + 1)]
    nodes[-2].set_shape("point")
    nodes[-1].set_shape("doublecircle")

    for i in reachable_states:
        graph.add_node(nodes[i])
    graph.add_node(nodes[-1])

    def _render_edges(M, color, end_state):
        for i in reachable_states:
            row_sum = sum(tot_M[i])
            col_sum = sum(tot_M[:, i])
            if not end_state:
                for j in reachable_states:
                    elem = M[i, j]
                    if elem > 0:
                        if col_sum > 0:
                            prob = elem / col_sum
                        else:
                            prob = elem / row_sum
                        edge = Edge(nodes[i], nodes[j], label="%.2f" % prob, color=color)
                        graph.add_edge(edge)
            else:
                if row_sum < col_sum:
                    prob = (col_sum - row_sum) / col_sum
                    edge = Edge(nodes[i], nodes[-1], label="%.2f" % prob, color=color)
                    graph.add_edge(edge)

    _render_edges(C_M, "blue", False)
    _render_edges(S_M, "red", False)
    _render_edges(None, "black", True)

    graph.write_png(filename)
コード例 #16
0
def process_plan(filename, plan, names=True, with_dist=False, with_label=False):
    graph = Dot(overlap="false", splines="true", esep=.2)
    teams = fetch_teams()

    distances = fetch_distances()

    nodes = make_nodes(plan, teams, names)

    for team_node in nodes:
        graph.add_node(nodes[team_node])

    for team in plan:
        edges = graph_way(team, plan[team], nodes, distances, with_dist, with_label)
        for edge in edges:
            graph.add_edge(edge)

    if with_dist:
        graph.write_png(filename, prog="neato")
    else:
        graph.write_png(filename, prog="dot")
コード例 #17
0
ファイル: graph.py プロジェクト: ichim-david/eea.relations
    def markBrokenRelations(self):
        """ Construct graph and return message with info about broken 
        relations for ToolGraph if any errors are found
        """
        bad_relations = []
        bad_content = [] 
        bad_rel = ""

        graph = PyGraph()
        tool = queryAdapter(self.context, IToolAccessor)
        types = tool.types(proxy=False)
        for ctype in types:
            node = queryAdapter(ctype, INode)
            graph.add_node(node())

        relations = tool.relations(proxy=False)
        for relation in relations:
            edge = queryAdapter(relation, IEdge)
            res = edge()
            if res:
                graph.add_edge(res)
                continue
            else:
                # if no result then check which relation id is missing
                from_rel = relation['from']
                to_rel = relation['to']
                pr_from = self.pt_relations.get(from_rel)
                pr_to = self.pt_relations.get(to_rel)
                if not pr_from:
                    bad_rel = from_rel
                if not pr_to:
                    bad_rel = to_rel
                if bad_rel and bad_rel not in bad_content:
                    bad_content.append(bad_rel)
                bad_relations.append(relation.Title())

        self.graph_res = graph
        if bad_relations:
            return self.brokenRelationMessage(bad_content, bad_relations)
        return ""
コード例 #18
0
ファイル: graph.py プロジェクト: collective/eea.relations
class ToolGraph(BaseGraph):
    """ Draw a graph for portal_relations
    """

    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        xtool = queryAdapter(self.context, IToolAccessor)
        typesIds = set()
        for ctype in xtool.types(proxy=False):
            typesIds.add(ctype.getId())
            node = queryAdapter(ctype, INode)
            self._graph.add_node(node())

        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            edge = queryAdapter(relation, IEdge)()
            if edge:
                self._graph.add_edge(edge)
            else:
                self._bad_relations.add(relation.Title())
                if value_from not in typesIds:
                    self._bad_content.add(value_from)
                if value_to not in typesIds:
                    self._bad_content.add(value_to)

        return self._graph
コード例 #19
0
ファイル: graph.py プロジェクト: ichim-david/eea.relations
    def markBrokenRelations(self):
        """ Construct graph and return message with info about broken 
        relations for RelationGraph if any errors are found
        """ 
        bad_relations = []
        bad_content = []
        bad_rel = ""

        graph = PyGraph()
        value_from = self.context.getField('from').getAccessor(self.context)()
        nfrom = self.pt_relations.get(value_from)
        if nfrom:
            node = queryAdapter(nfrom, INode)
            graph.add_node(node())

        value_to = self.context.getField('to').getAccessor(self.context)()
        nto = self.pt_relations.get(value_to)
        if not (value_from == value_to) and nto:
            node = queryAdapter(nto, INode)
            graph.add_node(node())

        edge = queryAdapter(self.context, IEdge)
        res = edge()
        if res:
            graph.add_edge(res)
            self.graph_res = graph
            return ""

        if not nfrom:
            bad_rel = value_from
        if not nto:
            bad_rel = value_to
        relation = self.pt_relations[self.context.getId()]
        if bad_rel and bad_rel not in bad_content:
            bad_content.append(bad_rel)
            bad_relations.append(relation.Title())
            self.graph_res = graph
            return self.brokenRelationMessage(bad_content, bad_relations)
コード例 #20
0
def main(input):
    state_obj = yaml.load(input)
    graph = Dot("states", graph_type='digraph')

    rules = {
        'require': {'color': 'blue'},
        'require_in': {'color': 'blue', 'reverse': True},
        'watch': {'color': 'red'},
        'watch_in': {'color': 'red', 'reverse': True},
    }

    for top_key, props in state_obj.iteritems():
        # Add a node for each state type embedded in this state
        # keys starting with underscores are not candidates

        if top_key == '__extend__':
            # TODO - merge these into the main states and remove them
            sys.stderr.write(
                    "Removing __extend__ states:\n{0}\n".format(str(props)))
            continue

        for top_key_type, states in props.iteritems():
            if top_key_type[:2] == '__':
                continue

            node_name = make_node_name(top_key_type, top_key)
            graph.add_node(Node(node_name))

            for edge_type, ruleset in rules.iteritems():
                for relname in find_edges(states, edge_type):
                    if 'reverse' in ruleset and ruleset['reverse']:
                        graph.add_edge(Edge(
                            node_name, relname, color=ruleset['color']))
                    else:
                        graph.add_edge(Edge(
                            relname, node_name, color=ruleset['color']))

    graph.write('/dev/stdout')
コード例 #21
0
    def save(self, cf, filename, format='dot'):
        """Save basic block graph into a file.
        """
        try:
            dot_graph = Dot(**self.graph_format)

            # add nodes
            nodes = {}
            for cfg_addr in cf._graph.node.keys():
                nodes[cfg_addr] = self._create_node(cfg_addr, cf)

                dot_graph.add_node(nodes[cfg_addr])

            # add edges
            for cfg_src_addr in cf._graph.node.keys():
                for cfg_dst_addr in cf._edges.get(cfg_src_addr, []):
                    edge = self._create_edge(nodes, cfg_src_addr, cfg_dst_addr)

                    dot_graph.add_edge(edge)

            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save call graph: %s (%s)", filename, format, exc_info=True)
コード例 #22
0
class ToolGraph(BaseGraph):
    """ Draw a graph for portal_relations
    """
    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        xtool = queryAdapter(self.context, IToolAccessor)
        typesIds = set()
        for ctype in xtool.types(proxy=False):
            typesIds.add(ctype.getId())
            node = queryAdapter(ctype, INode)
            self._graph.add_node(node())

        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            edge = queryAdapter(relation, IEdge)()
            if edge:
                self._graph.add_edge(edge)
            else:
                self._bad_relations.add(relation.Title())
                if value_from not in typesIds:
                    self._bad_content.add(value_from)
                if value_to not in typesIds:
                    self._bad_content.add(value_to)

        return self._graph
コード例 #23
0
class RelationGraph(BaseGraph):
    """ Draw a graph for Relation
    """
    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        # Valid graph edge
        self._graph = PyGraph()

        value_from = self.context.getField('from').getAccessor(self.context)()
        nfrom = self.tool.get(value_from)
        if nfrom:
            node = queryAdapter(nfrom, INode)
            self._graph.add_node(node())
        else:
            self._bad_content.add(value_from)
            self._bad_relations.add(self.context.Title())

        value_to = self.context.getField('to').getAccessor(self.context)()
        nto = self.tool.get(value_to)
        if nto:
            if value_from != value_to:
                node = queryAdapter(nto, INode)
                self._graph.add_node(node())
        else:
            self._bad_content.add(value_to)
            self._bad_relations.add(self.context.Title())

        edge = queryAdapter(self.context, IEdge)()
        if edge:
            self._graph.add_edge(edge)

        return self._graph
コード例 #24
0
    def show_diagram(self, path=None):
        """
            Creates the graph associated with this DFA
        """
        # Nodes are set of states

        graph = Dot(graph_type='digraph', rankdir='LR')
        nodes = {}
        for state in self.states:
            if state == self.initial_state:
                # color start state with green
                if state in self.final_states:
                    initial_state_node = Node(state,
                                              style='filled',
                                              peripheries=2,
                                              fillcolor='#66cc33')
                else:
                    initial_state_node = Node(state,
                                              style='filled',
                                              fillcolor='#66cc33')
                nodes[state] = initial_state_node
                graph.add_node(initial_state_node)
            else:
                if state in self.final_states:
                    state_node = Node(state, peripheries=2)
                else:
                    state_node = Node(state)
                nodes[state] = state_node
                graph.add_node(state_node)
        # adding edges
        for from_state, lookup in self.transitions.items():
            for to_label, to_state in lookup.items():
                graph.add_edge(
                    Edge(nodes[from_state], nodes[to_state], label=to_label))
        if path:
            graph.write_png(path)
        return graph
コード例 #25
0
ファイル: graph.py プロジェクト: collective/eea.relations
class RelationGraph(BaseGraph):
    """ Draw a graph for Relation
    """
    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        # Valid graph edge
        self._graph = PyGraph()

        value_from = self.context.getField('from').getAccessor(self.context)()
        nfrom = self.tool.get(value_from)
        if nfrom:
            node = queryAdapter(nfrom, INode)
            self._graph.add_node(node())
        else:
            self._bad_content.add(value_from)
            self._bad_relations.add(self.context.Title())

        value_to = self.context.getField('to').getAccessor(self.context)()
        nto = self.tool.get(value_to)
        if nto:
            if value_from != value_to:
                node = queryAdapter(nto, INode)
                self._graph.add_node(node())
        else:
            self._bad_content.add(value_to)
            self._bad_relations.add(self.context.Title())

        edge = queryAdapter(self.context, IEdge)()
        if edge:
            self._graph.add_edge(edge)

        return self._graph
コード例 #26
0
    def save(self, cfg, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        try:
            dot_graph = Dot(**self.graph_format)

            # Add nodes.
            nodes = {}
            for bb in cfg.basic_blocks:
                nodes[bb.address] = self._create_node(bb, cfg.name, print_ir)

                dot_graph.add_node(nodes[bb.address])

            # Add edges.
            for bb_src in cfg.basic_blocks:
                for bb_dst_addr, branch_type in bb_src.branches:
                    edge = self._create_edge(nodes[bb_src.address], nodes[bb_dst_addr], branch_type)

                    dot_graph.add_edge(edge)

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)", filename, format, exc_info=True)
コード例 #27
0
ファイル: graph.py プロジェクト: ichim-david/eea.relations
    def markBrokenRelations(self):
        """ Construct graph and return message with info about broken 
        relations for ContentTypeGraph if any errors are found
        """
        bad_relations = []
        bad_content = [] 
        name = self.context.getId()
        node = queryAdapter(self.context, INode)
        graph = PyGraph()
        graph.add_node(node())
        tool = queryAdapter(self.context, IToolAccessor)
        relations = tool.relations(proxy=False)
        for relation in relations:
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()
            field = relation.getField('from')
            value_to = field.getAccessor(relation)()
            if name == value_from:
                nto = self.pt_relations.get(value_to)
                if not (value_from == value_to
                    ) and nto:
                    node = queryAdapter(nto, INode)
                    graph.add_node(node())
                edge = queryAdapter(relation, IEdge)
                res = edge()
                if res:
                    graph.add_edge(res)
                else:
                    if value_to not in bad_content:
                        bad_content.append(value_to)
                    bad_relations.append(relation.Title())
                # if we don't continue when value_from == name
                # then we will get a double "node-myX" -> "node-myX"
                # graph entry when value_to is also equal to name
                continue

            if name == value_to:
                nfrom = self.pt_relations.get(value_from)
                if not (value_from == value_to
                    ) and nfrom:
                    node = queryAdapter(nfrom, INode)
                    graph.add_node(node())
                edge = queryAdapter(relation, IEdge)
                res = edge()
                if res:
                    graph.add_edge(res)
                else:
                    if value_from not in bad_content:
                        bad_content.append(value_from)
                    bad_relations.append(relation.Title())

        self.graph_res = graph
        if bad_relations:
            return self.brokenRelationMessage(bad_content, bad_relations)
        return ""
コード例 #28
0
class PydotAstWalker(object):

    def __init__(self, ast_root, course_code, valid_expr=True):
        self.course_code = course_code
        self.valid_expr = valid_expr
        self.ast = ast_root
        self.graph = Dot(graph_type='digraph')

    def _name_node(self, node):
        if node.is_course():
            # what if not leaf node?
            return node.code
        elif node.is_operator():
            name = str(node)
            for child in node.children:
                name += self._name_node(child)
            return name

    def generate_graph(self):
        course_root_node = Node(self.course_code, style="filled", colorscheme="set35", fillcolor="5")
        if not self.ast:
            if not self.valid_expr:
                return Node(self.course_code, style="filled", colorscheme="set35", fillcolor="4")
            else:
                return course_root_node
        edges = set()
        root_operator_node = self._visit_node(self.ast, edges)
        for edge in edges:
            self.graph.add_edge(edge.to_pydot_edge())
        self.graph.add_node(course_root_node)
        self.graph.add_edge(Edge(course_root_node, root_operator_node))
        return self.graph

    def _visit_node(self, ast_node, edges):
        if ast_node.is_course():
            node = Node(ast_node.code, style="filled", colorscheme="set35", fillcolor="2")
            self.graph.add_node(node)
            if not ast_node.is_leaf():
                child = self._visit_node(ast_node.child, edges)
                edge = UniqueEdge(node, child)
                edges.add(edge)
            return node
        elif ast_node.is_operator():
            name = self._name_node(ast_node)
            if isinstance(ast_node, AllOf):
                this_node = Node(name, label=str(ast_node), shape="square", style="filled", colorscheme="set35", fillcolor="3")
            else:
                this_node = Node(name, label=str(ast_node), shape="diamond", style="filled", colorscheme="set35", fillcolor="1")
            self.graph.add_node(this_node)
            child_nodes = [self._visit_node(child, edges) for child in ast_node.children]
            for child in child_nodes:
                edge = UniqueEdge(this_node, child)
                edges.add(edge)
            return this_node
コード例 #29
0
ファイル: graph.py プロジェクト: collective/eea.relations
class ContentTypeGraph(BaseGraph):
    """ Draw a graph for ContentType
    """

    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        # This node
        name = self.context.getId()
        node = queryAdapter(self.context, INode)
        self._graph.add_node(node())

        xtool = queryAdapter(self.context, IToolAccessor)
        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            if name == value_from:
                nto = self.tool.get(value_to)
                if (value_from != value_to) and nto:
                    node = queryAdapter(nto, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_to)
                    self._bad_relations.add(relation.Title())

            elif name == value_to:
                nfrom = self.tool.get(value_from)
                if (value_from != value_to) and nfrom:
                    node = queryAdapter(nfrom, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_from)
                    self._bad_relations.add(relation.Title())

        return self._graph
コード例 #30
0
class ContentTypeGraph(BaseGraph):
    """ Draw a graph for ContentType
    """
    @property
    def graph(self):
        """ Construct graph and mark broken relations if any
        """
        if self._graph is not None:
            return self._graph

        self._graph = PyGraph()

        # This node
        name = self.context.getId()
        node = queryAdapter(self.context, INode)
        self._graph.add_node(node())

        xtool = queryAdapter(self.context, IToolAccessor)
        for relation in xtool.relations(proxy=False):
            field = relation.getField('to')
            value_from = field.getAccessor(relation)()

            field = relation.getField('from')
            value_to = field.getAccessor(relation)()

            if name == value_from:
                nto = self.tool.get(value_to)
                if (value_from != value_to) and nto:
                    node = queryAdapter(nto, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_to)
                    self._bad_relations.add(relation.Title())

            elif name == value_to:
                nfrom = self.tool.get(value_from)
                if (value_from != value_to) and nfrom:
                    node = queryAdapter(nfrom, INode)
                    self._graph.add_node(node())
                edge = queryAdapter(relation, IEdge)()
                if edge:
                    self._graph.add_edge(edge)
                else:
                    self._bad_content.add(value_from)
                    self._bad_relations.add(relation.Title())

        return self._graph
コード例 #31
0
    def graph_representation(self):
        """
        Construct a dot graph representation of these results.
        :return: The dot graph
        """
        from pydot import Dot, Edge, Node

        g = Dot()

        for resource in self.resources:
            color = 'black'
            if resource in self.targets and self.consumed(resource) == 0:
                color = 'darkgreen'
            elif self.consumed(resource) == 0:
                color = 'brown'
            g.add_node(Node('i_' + resource, label='{:.3} {}'.format(float(self.produced(resource) + self.supplied(resource)), resource), color=color, style='dashed'))
            if resource in self.targets and self.consumed(resource) > 0:
                # add a second node if requested and consumed so we can see a terminal node
                g.add_node(Node('ir_' + resource, label='{:.3} {}'.format(float(self.requested(resource)), resource), color='darkgreen', style='dashed'))
                weight = self.requested(resource)
                g.add_edge(Edge('i_' + resource, 'ir_' + resource, label='{:.3}'.format(weight), weight=weight))
        for recipe, batches in self.recipes.items():
            if self.book.crafters_defined():
                crafter = self.book.get_crafter_for(recipe) or Crafter('', 1)
                label = '{:.3} {}\n{}'.format(batches[1], crafter.name, recipe)
            else:
                label = '{:.3} {}'.format(batches[1], recipe)
            g.add_node(Node('r_' + recipe, label=label, shape='box'))

            r: Recipe = self.book[recipe]
            for output in r.outputs():
                weight = r.produced(output, batches[1])
                g.add_edge(Edge('r_' + recipe, 'i_' + output, label='{:.3}'.format(weight), weight=weight))
            for input in r.inputs():
                weight = r.consumed(input, batches[1])
                g.add_edge(Edge('i_' + input, 'r_' + recipe, label='{:.3}'.format(weight), weight=weight))

        return g
コード例 #32
0
def visualize_pta(root_node, path='pta.pdf'):
    from pydot import Dot, Node, Edge
    graph = Dot('fpta', graph_type='digraph')

    graph.add_node(Node(str(root_node.prefix), label=f'{root_node.output}'))

    queue = [root_node]
    visited = set()
    visited.add(root_node.prefix)
    while queue:
        curr = queue.pop(0)
        for i, c in curr.children.items():
            if c.prefix not in visited:
                graph.add_node(Node(str(c.prefix), label=f'{c.output}'))
            graph.add_edge(Edge(str(curr.prefix), str(c.prefix), label=f'{i}'))
            if c.prefix not in visited:
                queue.append(c)
            visited.add(c.prefix)

    graph.add_node(Node('__start0', shape='none', label=''))
    graph.add_edge(Edge('__start0', str(root_node.prefix), label=''))

    graph.write(path=path, format='pdf')
コード例 #33
0
ファイル: FileHandler.py プロジェクト: haubitzer/AALpy
def save_automaton_to_file(automaton,
                           path="LearnedModel",
                           file_type='dot',
                           display_same_state_trans=True,
                           visualize=False):
    """
    The Standard of the automata strictly follows the syntax found at: https://automata.cs.ru.nl/Syntax/Overview.
    For non-deterministic and stochastic systems syntax can be found on AALpy's Wiki.

    Args:

        automaton: automaton to be saved to file

        path: file in which automaton will be saved (Default value = "LearnedModel")

        file_type: Can be ['dot', 'png', 'svg', 'pdf'] (Default value = 'dot')

        display_same_state_trans: True, should not be set to false except from the visualization method
            (Default value = True)

        visualize: visualize the automaton

    Returns:

    """
    assert file_type in file_types
    if file_type == 'dot' and not display_same_state_trans:
        print("When saving to file all transitions will be saved")
        display_same_state_trans = True
    is_dfa = isinstance(automaton, Dfa)
    is_moore = isinstance(automaton, MooreMachine)
    is_mdp = isinstance(automaton, Mdp)
    is_onsfm = isinstance(automaton, Onfsm)
    is_smm = isinstance(automaton, StochasticMealyMachine)
    is_mc = isinstance(automaton, MarkovChain)

    graph = Dot(path, graph_type='digraph')
    for state in automaton.states:
        if is_dfa and state.is_accepting:
            graph.add_node(
                Node(state.state_id,
                     label=state.state_id,
                     shape='doublecircle'))
        elif is_moore:
            graph.add_node(
                Node(state.state_id,
                     label=f'{state.state_id}|{state.output}',
                     shape='record',
                     style='rounded'))
        elif is_mdp or is_mc:
            graph.add_node(Node(state.state_id, label=f'{state.output}'))
        else:
            graph.add_node(Node(state.state_id, label=state.state_id))

    for state in automaton.states:
        if is_mc:
            for new_state, prob in state.transitions:
                graph.add_edge(
                    Edge(state.state_id,
                         new_state.state_id,
                         label=f'{round(prob, 2)}'))
            continue
        for i in state.transitions.keys():
            if isinstance(state, MealyState):
                new_state = state.transitions[i]
                if not display_same_state_trans and new_state.state_id == state.state_id:
                    continue
                graph.add_edge(
                    Edge(state.state_id,
                         new_state.state_id,
                         label=f'{i}/{state.output_fun[i]}'))
            elif is_mdp:
                # here we do not have single state, but a list of (State, probability) tuples
                new_state = state.transitions[i]
                for s in new_state:
                    if not display_same_state_trans and s[
                            0].state_id == state.state_id:
                        continue
                    graph.add_edge(
                        Edge(state.state_id,
                             s[0].state_id,
                             label=f'{i} : {round(s[1], 2)}'))
            elif is_onsfm:
                new_state = state.transitions[i]
                for s in new_state:
                    if not display_same_state_trans and state.state_id == s[
                            1].state_id:
                        continue
                    graph.add_edge(
                        Edge(state.state_id,
                             s[1].state_id,
                             label=f'{i}/{s[0]}'))
            elif is_smm:
                new_state = state.transitions[i]
                for s in new_state:
                    if not display_same_state_trans and s[
                            0].state_id == state.state_id:
                        continue
                    graph.add_edge(
                        Edge(state.state_id,
                             s[0].state_id,
                             label=f'{i}/{s[1]}:{round(s[2], 2)}'))
            else:
                new_state = state.transitions[i]
                if not display_same_state_trans and new_state.state_id == state.state_id:
                    continue
                graph.add_edge(
                    Edge(state.state_id, new_state.state_id, label=f'{i}'))

    graph.add_node(Node('__start0', shape='none', label=''))
    graph.add_edge(Edge('__start0', automaton.initial_state.state_id,
                        label=''))

    if file_type == 'string':
        return graph.to_string()
    else:
        try:
            graph.write(path=f'{path}.{file_type}',
                        format=file_type if file_type != 'dot' else 'raw')
            print(f'Model saved to {path}.{file_type}.')

            if visualize and file_type in {'pdf', 'png', 'svg'}:
                try:
                    import webbrowser
                    abs_path = os.path.abspath(f'{path}.{file_type}')
                    path = f'file:///{abs_path}'
                    webbrowser.open(path)
                except OSError:
                    traceback.print_exc()
                    print(f'Could not open the file {path}.{file_type}.',
                          file=sys.stderr)
        except OSError:
            traceback.print_exc()
            print(f'Could not write to the file {path}.{file_type}.',
                  file=sys.stderr)
コード例 #34
0
class GrapVisitor(Visitor):
    def __init__(self):
        self.graph = Dot(graph_type='digraph')

    def save(self, filename="graph.png"):
        self.graph.write_png(filename)

    def accept_BooleanValue(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_Identifier(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_NumberValue(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_List(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_Enumeration(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_UnaryExpression(self, node):
        n1 = create_node(str(node.type))
        n2 = self.accept(node.expr)
        e = Edge(n1, n2)

        self.graph.add_node(n1)
        self.graph.add_edge(e)

        return n1

    def accept_BinaryExpression(self, node):
        n1 = create_node(str(node.type))
        n2 = self.accept(node.left_expr)
        n3 = self.accept(node.right_expr)

        e1 = Edge(n1, n2)
        e2 = Edge(n1, n3)

        self.graph.add_node(n1)
        self.graph.add_edge(e1)
        self.graph.add_edge(e2)

        return n1

    def accept_ProcedureCall(self, node):
        n1 = create_node("call")
        n2 = create_node(str(node.name))
        n3 = create_node("arguments")

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))

        for arg in node.arguments:
            arg_node = self.accept(arg)
            self.graph.add_edge(Edge(n3, arg_node))

        return n1

    def accept_RepeatUntil(self, node):
        n1 = create_node("RepeatUntil")
        n2 = create_node(self.accept(node.condition))
        n3 = create_node("statements")

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)

        for st in node.statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n3, st_node))

        return n1

    def accept_While(self, node):
        n1 = create_node("While")
        n2 = create_node(self.accept(node.condition))
        n3 = create_node("statements")

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))

        for st in node.statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n3, st_node))

        return n1

    def accept_For(self, node):
        n1 = create_node("For")
        n2 = create_node("from")
        n3 = create_node("to")
        n4 = create_node("statements")
        n5 = self.accept(node.from_)
        n6 = self.accept(node.to)

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)
        self.graph.add_node(n4)

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))
        self.graph.add_edge(Edge(n1, n4))
        self.graph.add_edge(Edge(n2, n5))
        self.graph.add_edge(Edge(n3, n6))

        for st in node.statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n4, st_node))

        return n1

    def accept_If(self, node):
        n1 = create_node("If")
        n2 = create_node("condition")
        n3 = create_node("if_statements")
        n4 = create_node("else_statements")
        n5 = self.accept(node.condition)

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)
        self.graph.add_node(n4)
        self.graph.add_node(n5)

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))
        self.graph.add_edge(Edge(n1, n4))
        self.graph.add_edge(Edge(n2, n5))

        for st in node.if_statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n3, st_node))

        for st in node.else_statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n4, st_node))

        return n1

    def accept_BitExtraction(self, node):
        n1 = create_node("BitExtraction")
        n2 = create_node(str(node.identifier))

        self.graph.add_node(n1)
        self.graph.add_node(n2)

        self.graph.add_edge(Edge(n1, n2))

        for r in node.range:
            n = self.accept(r)
            self.graph.add_edge(Edge(n1, n))

        return 1

    def accept_MaskedBinary(self, node):
        n1 = create_node("MaskedBinary")
        n2 = create_node(str(node.value))

        self.graph.add_node(n1)
        self.graph.add_node(n2)

        self.graph.add_edge(Edge(n1, n2))

        return n1

    def accept_Ignore(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_IfExpression(self, node):
        n1 = create_node("IfExpression")
        n2 = create_node("condition")
        n3 = self.accept(node.condition)
        n4 = create_node("ifTrue")
        n5 = create_node("ifFalse")

        self.graph.add_node(n1)
        self.graph.add_node(n2)
        self.graph.add_node(n3)
        self.graph.add_node(n4)
        self.graph.add_node(n5)

        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n2, n3))
        self.graph.add_edge(Edge(n1, n4))
        self.graph.add_edge(Edge(n1, n5))

        node_true = self.accept(node.trueValue)
        node_false = self.accept(node.falseValue)

        self.graph.add_edge(Edge(n4, node_true))
        self.graph.add_edge(Edge(n5, node_false))

        return n1

    def accept_CaseElement(self, node):
        n1 = create_node("CaseElement")
        n2 = self.accept(node.value)
        n3 = create_node("statements")

        self.graph.add_node(n1)
        self.graph.add_node(n3)
        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))

        for st in node.statements:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n3, st_node))

        return n1

    def accept_Case(self, node):
        n1 = create_node("Case")
        n2 = self.accept(node.expr)
        n3 = create_node("cases")

        self.graph.add_node(n1)
        self.graph.add_node(n3)
        self.graph.add_edge(Edge(n1, n2))
        self.graph.add_edge(Edge(n1, n3))

        for st in node.cases:
            st_node = self.accept(st)
            self.graph.add_edge(Edge(n3, st_node))

        return n1

    def accept_Undefined(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_Unpredictable(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_See(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_ImplementationDefined(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_SubArchitectureDefined(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n

    def accept_Return(self, node):
        n = create_node(str(node))
        self.graph.add_node(n)
        return n
コード例 #35
0
class DependencyGraph:
    def __init__(self, _contract: Contract):
        """
        Takes a Contract object and constructs the DDG for it.

        *** To be completed
            Currently cannot detect indirect read.
            Indirect write can be detected.
        """
        self.contract = _contract
        self.node_dic = {}
        self.edge_dic = {}
        self.graph = Dot()

        for f in _contract.functions + _contract.constructor_as_list:
            if f.name in ['slitherConstructorVariables', 'slitherConstructorConstantVariables'] or not f.is_public_or_external:
                continue
            self.construct_node(f)

        self.construct_graph(_contract)

    @property
    def html(self):
        return svg_to_html(self.graph.create_svg().decode('utf-8'))

    def construct_graph(self, _contract):
        """
        Constructs the graph by connecting nodes with edges.
        """
        for f in _contract.functions + _contract.constructor_as_list:
            if f.name in ['slitherConstructorVariables', 'slitherConstructorConstantVariables'] or not f.is_public_or_external:
                continue

            for dependency in f.depends_on:
                written_f, sr = dependency[0], dependency[1]
                n1 = self.get_node(f.name)
                n2 = self.get_node(written_f.name)
                if self.edge_dic.get((n1, n2)):
                    e = self.edge_dic[(n1, n2)]
                    old_label = e.get_label()
                    e.set_label(f'{old_label.strip()}, {sr.name}        ')
                else:
                    self.construct_edge(n1, n2)
                    e = self.edge_dic[(n1, n2)]
                    e.set_label(f'{sr.name}        ')

    def update_graph(self):
        remaining_edges_dic = defaultdict(list)
        for f in self.contract.functions + self.contract.constructor_as_list:
            if f.name in ['slitherConstructorVariables', 'slitherConstructorConstantVariables'] or not f.is_public_or_external:
                continue

            for dependency in f.depends_on:
                written_f, sr = dependency[0], dependency[1]
                n1 = self.get_node(f.name)
                n2 = self.get_node(written_f.name)
                remaining_edges_dic[(n1, n2)].append(sr)

        existing_edges = self.edge_dic.keys()
        remaining_edges = remaining_edges_dic.keys()

        for ex_edge in existing_edges:
            if ex_edge not in remaining_edges:
                remove_edge(self.edge_dic[ex_edge])
            # no need to check if only one state variable dependency is remove
            # because if a dependency between two functions is removed
            # all of its dependency based on whatever variable will be removed.

    def construct_node(self, _function: Function):
        """
        Takes a Function object and constructs a Dot Node object for it.
        Adds the created object to the dictionary.

        Finished.
        """
        n = Node(_function.name)
        n.set_tooltip(construct_tooltip(_function))
        self.node_dic[_function.name] = n
        self.graph.add_node(n)

    def get_node(self, _name):
        return self.node_dic.get(_name)

    def construct_edge(self, _n1: Node, _n2: Node):
        """
        Takes two nodes
        n1 depends on n2
        n1 points to n2
        Constructs the edge object and adds it to the dictionary.

        Finished.
        """
        e = Edge(_n1, _n2, fontsize="8", fontcolor="#2E86C1", arrowsize="0.7")
        self.edge_dic[(_n1, _n2)] = e
        # e.set('color', 'green')
        self.graph.add_edge(e)
コード例 #36
0
def main():
    graph = Dot('Controller Dependency Graph')

    # controller node list
    graph.add_node(
        Node('amazoncloudintegration',
             label='AmazonCloudIntegration',
             shape='box'))
    graph.add_node(Node('apiserver', label='APIServer', shape='box'))
    graph.add_node(
        Node('applicationlayer', label='ApplicationLayer', shape='box'))
    graph.add_node(Node('authentication', label='Authentication', shape='box'))
    graph.add_node(
        Node(
            'clusterconnection',
            label=
            '{ClusterConnection|Standalone\nManagementCluster\nManagementClusterConnection}',
            shape='record'))
    graph.add_node(Node('compliance', label='Compliance', shape='box'))
    graph.add_node(Node('installation', label='Installation', shape='box'))
    graph.add_node(
        Node('intrusiondetection', label='IntrusionDetection', shape='box'))
    graph.add_node(Node('logcollector', label='LogCollector', shape='box'))
    graph.add_node(Node('logstorage', label='LogStorage', shape='box'))
    graph.add_node(Node('manager', label='Manager', shape='box'))
    graph.add_node(Node('monitor', label='Monitor', shape='box'))

    # The controller dependencies are deduced from controller Reconcile() function.
    # This is still a manual process at the moment.
    # [AmazonCloudIntegration] -> [Installation]
    graph.add_edge(Edge('amazoncloudintegration', 'installation'))
    # [APIServer] --> [AmazonCloudIntegration]
    # [APIServer] --> [ClusterConnection]
    # [APIServer] -> [Installation]
    graph.add_edge(
        Edge('apiserver',
             'amazoncloudintegration',
             label='amazonCRDExists',
             style='dashed'))
    graph.add_edge(
        Edge('apiserver', 'clusterconnection', label='TSEE', style='dashed'))
    graph.add_edge(Edge('apiserver', 'installation'))
    # [ApplicationLayer] -> [Installation]
    graph.add_edge(Edge('applicationlayer', 'installation'))
    # [Authentication] -> [ClusterConnection]
    # [Authentication] -> [Installation]
    graph.add_edge(Edge('authentication', 'clusterconnection', style='dashed'))
    graph.add_edge(Edge('authentication', 'installation'))
    # [ClusterConnection|ManagementCluster;ManagementClusterConnection] -> [Installation]
    graph.add_edge(Edge('clusterconnection', 'installation', style='dashed'))
    # [Compliance] -> [Authentication]
    # [Compliance] -> [ClusterConnection]
    # [Compliance] -> [Installation]
    # [Compliance] -> [LogStorage]
    graph.add_edge(Edge('compliance', 'authentication'))
    graph.add_edge(Edge('compliance', 'clusterconnection', style='dashed'))
    graph.add_edge(Edge('compliance', 'installation'))
    graph.add_edge(Edge('compliance', 'logstorage'))
    # [IntrusionDetection] -> [ClusterConnection]
    # [IntrusionDetection] -> [Installation]
    # [IntrusionDetection] -> [LogStorage]
    graph.add_edge(
        Edge('intrusiondetection', 'clusterconnection', style='dashed'))
    graph.add_edge(Edge('intrusiondetection', 'installation'))
    graph.add_edge(Edge('intrusiondetection', 'logstorage'))
    # [LogCollector] --> [ClusterConnection]
    # [LogCollector] -> [Installation]
    # [LogCollector] -> [LogStorage]
    graph.add_edge(
        Edge('logcollector',
             'clusterconnection',
             label='AdditionalStores',
             style='dashed'))
    graph.add_edge(Edge('logcollector', 'installation'))
    graph.add_edge(Edge('logcollector', 'logstorage'))
    # [LogStorage] -> [Authentication]
    # [LogStorage] -> [ClusterConnection]
    # [LogStorage] -> [Installation]
    graph.add_edge(Edge('logstorage', 'authentication'))
    graph.add_edge(Edge('logstorage', 'clusterconnection', style='dashed'))
    graph.add_edge(Edge('logstorage', 'installation'))
    # [Manager] -> [Authentication]
    # [Manager] -> [ClusterConnection]
    # [Manager] -> [Compliance]
    # [Manager] -> [Installation]
    # [Manager] -> [LogStorage]
    graph.add_edge(Edge('manager', 'authentication'))
    graph.add_edge(Edge('manager', 'clusterconnection', style='dashed'))
    graph.add_edge(Edge('manager', 'compliance'))
    graph.add_edge(Edge('manager', 'installation'))
    graph.add_edge(Edge('manager', 'logstorage'))
    # [Monitor] -> [Authentication]
    graph.add_edge(Edge('monitor', 'authentication'))
    # [Monitor] -> [Installation]
    graph.add_edge(Edge('monitor', 'installation'))

    graph.write_svg('controller-dependency-graph.svg')
コード例 #37
0
ファイル: viewers.py プロジェクト: hal2001/simpleai
    def create_graph(self, png_path):
        from pydot import Dot, Edge, Node

        graph = Dot(graph_type='digraph')

        graph_nodes = {}
        graph_edges = {}
        done = set()

        def add_node(node,
                     expanded=False,
                     chosen=False,
                     in_fringe=False,
                     in_successors=False):
            node_id = id(node)
            if node_id not in graph_nodes:
                label = node.state_representation()
                if hasattr(node, 'cost'):
                    label += '\nCost: %s' % node.cost
                if hasattr(node, 'heuristic'):
                    label += '\nHeuristic: %s' % node.heuristic
                if hasattr(node, 'value'):
                    label += '\nValue: %s' % node.value

                new_g_node = Node(node_id,
                                  label=label,
                                  style='filled',
                                  shape='circle',
                                  fillcolor='#ffffff',
                                  fontsize=self.font_size)

                graph_nodes[node_id] = new_g_node

            g_node = graph_nodes[node_id]

            if expanded or chosen:
                g_node.set_fillcolor(self.fringe_color)
            if in_fringe:
                g_node.set_color(self.fringe_color)
                g_node.set_penwidth(3)
            if in_successors:
                g_node.set_color(self.successor_color)
                g_node.set_fontcolor(self.successor_color)

            return g_node

        def add_edge_to_parent(node, is_successor=False, parent=None):
            if parent is None:
                parent = node.parent

            g_node = add_node(node, in_successors=is_successor)
            g_parent_node = add_node(parent)

            edge = Edge(g_parent_node,
                        g_node,
                        label=node.action_representation(),
                        fontsize=self.font_size)

            if is_successor:
                edge.set_color(self.successor_color)
                edge.set_labelfontcolor(self.successor_color)

            graph_edges[id(node), id(parent)] = edge

        if self.last_event == 'chosen_node':
            add_node(self.last_chosen, chosen=True)

        if self.last_event == 'expanded':
            for node, successors in zip(self.last_expandeds,
                                        self.last_successors):
                add_node(node, expanded=True)
                for successor_node in successors:
                    add_edge_to_parent(successor_node,
                                       is_successor=True,
                                       parent=node)

        for node in self.current_fringe:
            add_node(node, in_fringe=True)
            while node is not None and node not in done:
                if node.parent is not None:
                    add_edge_to_parent(node)
                else:
                    add_node(node)

                done.add(node)
                node = node.parent

        for node_id in sorted(graph_nodes.keys()):
            graph.add_node(graph_nodes[node_id])
        for node_id, parent_id in sorted(graph_edges.keys()):
            graph.add_edge(graph_edges[node_id, parent_id])

        graph.write_png(png_path)
コード例 #38
0
    def save_ex(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        fontname = 'Ubuntu Mono'
        # fontname = 'DejaVu Sans Mono'
        # fontname = 'DejaVu Sans Condensed'
        # fontname = 'DejaVu Sans Light'
        # fontname = 'Liberation Mono'
        # fontname = 'DejaVu Serif Condensed'
        # fontname = 'Ubuntu Condensed'

        graph_format = {
            'graph_type': 'digraph',
            'nodesep': 1.2,
            'rankdir': 'TB',
            'splines': 'ortho',
        }

        node_format_base = {
            'fontname': fontname,
            'fontsize': 9.0,
            'penwidth': 0.5,
            'rankdir': 'LR',
            'shape': 'plaintext',
        }

        node_format_entry = {
            'pencolor': 'orange',
        }

        node_format_exit = {
            'pencolor': 'gray',
        }

        node_format_entry_exit = {
            'pencolor': 'gray',
        }

        edge_format = {
            'arrowhead': 'vee',
            'arrowsize': 0.6,
            'fontname': fontname,
            'fontsize': 8.0,
            'penwidth': 0.5,
        }

        edge_colors = {
            'direct': 'blue',
            'not-taken': 'red',
            'taken': 'darkgreen',
        }

        try:
            dot_graph = Dot(**graph_format)

            # add nodes
            nodes = {}
            for bb_addr in self._graph.node.keys():
                # Skip jmp/jcc to sub routines.
                if not bb_addr in self._bb_by_addr:
                    continue

                bb_dump = self._dump_bb_ex(self._bb_by_addr[bb_addr], print_ir)

                if self._bb_by_addr[bb_addr].is_entry and not self._bb_by_addr[
                        bb_addr].is_exit:
                    node_format = dict(node_format_base, **node_format_entry)
                elif self._bb_by_addr[bb_addr].is_exit and not self._bb_by_addr[
                        bb_addr].is_entry:
                    node_format = dict(node_format_base, **node_format_exit)
                elif self._bb_by_addr[bb_addr].is_entry and self._bb_by_addr[
                        bb_addr].is_exit:
                    node_format = dict(node_format_base,
                                       **node_format_entry_exit)
                else:
                    node_format = dict(node_format_base)

                if self._bb_by_addr[bb_addr].is_entry:
                    bb_label = "{} @ {:x}".format(self._name, bb_addr)
                else:
                    bb_label = "loc_{:x}".format(bb_addr)

                label = '<'
                label += '<table border="1.0" cellborder="0" cellspacing="1" cellpadding="0" valign="middle">'
                label += '  <tr><td align="center" cellpadding="1" port="enter"></td></tr>'
                label += '  <tr><td align="left" cellspacing="1">{label}</td></tr>'
                label += '  {assembly}'
                label += '  <tr><td align="center" cellpadding="1" port="exit" ></td></tr>'
                label += '</table>'
                label += '>'

                label = label.format(label=bb_label, assembly=bb_dump)

                nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                dot_graph.add_node(nodes[bb_addr])

            # add edges
            for bb_src_addr in self._graph.node.keys():
                # Skip jmp/jcc to sub routines.
                if not bb_src_addr in self._bb_by_addr:
                    continue

                for bb_dst_addr, branch_type in self._bb_by_addr[
                        bb_src_addr].branches:
                    # Skip jmp/jcc to sub routines.
                    if not bb_dst_addr in self._bb_by_addr:
                        continue

                    dot_graph.add_edge(
                        Edge(nodes[bb_src_addr],
                             nodes[bb_dst_addr],
                             color=edge_colors[branch_type],
                             **edge_format))

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception as err:
            logger.error("Failed to save basic block graph: %s (%s)",
                         filename,
                         format,
                         exc_info=True)
コード例 #39
0
# node3 = Node("c");
# edge1 = Edge(node1, node2)
# edge2 = Edge(node2, node3)
# edge3 = Edge(node1, node3)
# graph.add_edge(edge1)
# graph.add_edge(edge2)
# graph.add_edge(edge3)
# graph.write_png("primer1_graph.png")
 
 
graph = Dot(graph_type = "digraph", rankdir = "BT")
graph.set_node_defaults(style = "filled", fillcolor = "grey")
node1 = Node("Nastavnik", color = "green")
node2 = Node("Student", color = "blue")
node1.set("shape", "record")
graph.add_node(node1)
graph.add_node(node2)
edge1 = Edge(node1, node2, label = "Predaje", color = "red")
graph.add_edge(edge1)
graph.write_png("primer2_graph.png")

#Zadatak
graph = Dot(graph_type = "digraph", rankdir = "BT")
graph.set_node_defaults(shape = "record")
teacher = Node("Teacher", label = "{Nastavnik|+ime:String \n+prezime:String|\n}")
course = Node("Course", label = "{Kurs|\n|\n}")
student = Node("Student", label = "{Student|\n|\n}")
lesson = Node("Lesson", label = "{Predavanja|\n|\n}")
tutorial = Node("Tutorial", label = "{Konsultacije|\n |\n}")
assessment = Node("Assessment", label = "{Ocenjivanje|\n|\n}")
coursework = Node("Coursework", label = "{Predispitne obaveze|\n|\n}")
コード例 #40
0
ファイル: sitemap.py プロジェクト: keithwoody/kwrawler-python
class Sitemap(object):
    URI_FAILURE = "failed to read URI '%s'"
    current_contents = None
    base_uri = None
    base_url = None
    current_uri = None
    current_url = None
    site_graph = None

    def __init__(self):
        self.site_dict = { 'pages': [],
                           'links': {},
                           'assets': { 'imgs': [],
                                       'scripts': [],
                                       'stylesheets': [] }
                         }

    def from_uri(self, uri, render_opts={}):
        """ sets the instance URI and renders a sitemap image
        """
        self.base_uri = uri
        self.base_url = urlparse(uri)
        if self.traverse_site( uri ):
            self.render_sitemap( render_opts )
        else:
            return self.URI_FAILURE % uri

    def build_site_graph(self):
        self.site_graph = Dot(graph_type='digraph')
        self.site_graph.set_label( 'Sitemap for "%s"' % self.base_uri )
        self.site_graph.set_simplify( True )
        # add nodes
        for page in self.site_dict['pages']:
            self.site_graph.add_node( page.to_node() )
        # add edges
        for page in self.site_dict['pages']:
            from_node = page.uri
            for link in page.attributes['links']:
              to_node = link
              self.site_graph.add_edge( Edge(from_node, to_node) )

    def render_sitemap(self, options={}):
        if self.site_graph is None:
            self.build_site_graph()
        file_fmt = 'png'
        if 'format' in options.keys() and options['format'] is not None:
            file_fmt = options['format']
        filename = 'sitemap.%s' % file_fmt
        if 'filename' in options.keys() and options['filename'] is not None:
            filename = options['filename']
        self.site_graph.write(filename, 'dot', file_fmt)
        # with open(filename, 'w') as sitemap_img:
        #     sitemap_img.write("TODO")


    def traverse_site(self, uri_str):
        if self.validate_uri( uri_str ):
          # populate site_dict
          print "%d traversing %s" % (int(time.time()), uri_str)
          page_dict = { 'assets': { 'imgs': [],
                                    'scripts': [],
                                    'stylesheets': []
                                  },
                        'links': []
                      }
          self.current_contents = urllib.urlopen( uri_str ).read()
          self.current_uri = uri_str
          self.current_url = urlparse( uri_str )
          self.base_url = self.base_url or self.current_url
          html_doc = BeautifulSoup( self.current_contents )
          # get all scripts, stylesheets and images
          for script in html_doc.find_all('script'):
              src = script.get('src')
              if src is not None:
                  page_dict['assets']['scripts'].append( src )
          sset = set(self.site_dict['assets']['scripts'])
          pset = set(page_dict['assets']['scripts'])
          # list of unique js sources for the site
          self.site_dict['assets']['scripts'] = list( sset | pset )

          for css in html_doc.select('link[rel="stylesheet"]'):
              href = css.get('href')
              if href is not None:
                  page_dict['assets']['stylesheets'].append( href )
          sset = set(self.site_dict['assets']['stylesheets'])
          pset = set(page_dict['assets']['stylesheets'])
          # list of unique stylesheets for the site
          self.site_dict['assets']['stylesheets'] = list( sset | pset )

          for img in html_doc.select('img'):
              src = img.get('src')
              if src is not None and src not in page_dict['assets']['imgs']:
                  page_dict['assets']['imgs'].append( src )
          sset = set(self.site_dict['assets']['imgs'])
          pset = set(page_dict['assets']['imgs'])
          # list of unique images for the site
          self.site_dict['assets']['imgs'] = list( sset | pset )

          # get all internal links on the page
          for link in html_doc.select('a'):
              href = link.get('href').split('#')[0]
              href_uri = urljoin( self.base_url.geturl(), href )
              if self.valid_internal_link(href):
                  if href_uri not in page_dict['links']:
                      page_dict['links'].append( href_uri )
                  if href_uri not in self.site_dict['links'].keys():
                      self.site_dict['links'][ href_uri ] = Link(href_uri, 'new')

          # add the current page and set it as processed
          self.site_dict['pages'].append( Page(self.current_uri, page_dict) )
          self.site_dict['links'][ self.current_uri ] = Link(self.current_uri, 'processed')
          # traverse any linked pages
          for uri, link_obj in self.site_dict['links'].iteritems():
              if link_obj.status is 'new':
                  self.traverse_site( uri )
          return True
        else:
          return False

    def valid_internal_link( self, href ):
        """ returns True if the argument domain is the same as the current url """
        href_url = urlparse( href )
        if href is not None and \
            href_url.netloc in ['', self.base_url.netloc] and \
            re.match(r"""(?!(?:mailto|javascript))""", href):
            return True
        else:
            return False

    def validate_uri(self, uri):
        """ validates a URI is well formed and readable
        """
        if uri is not None:
          u = urlparse( uri )
          conn = httplib.HTTPConnection( u.netloc )
          if u.scheme == 'https':
              conn = httplib.HTTPSConnection( u.netloc )
          path = u.path or "/"
          try:
              conn.request("HEAD", path)
              response = conn.getresponse()
              if response.status == 200:
                  return True
              else:
                  return False
          except Exception, e:
              print self.URI_FAILURE % uri
              return False
          finally:
コード例 #41
0
ファイル: basicblock.py プロジェクト: abforce/barf-project
    def save_ex(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        fontname = 'Ubuntu Mono'
        # fontname = 'DejaVu Sans Mono'
        # fontname = 'DejaVu Sans Condensed'
        # fontname = 'DejaVu Sans Light'
        # fontname = 'Liberation Mono'
        # fontname = 'DejaVu Serif Condensed'
        # fontname = 'Ubuntu Condensed'

        graph_format = {
            'graph_type' : 'digraph',
            'nodesep'    : 1.2,
            'rankdir'    : 'TB',
            'splines'    : 'ortho',
        }

        node_format_base = {
            'fontname'  : fontname,
            'fontsize'  : 9.0,
            'penwidth'  : 0.5,
            'rankdir'   : 'LR',
            'shape'     : 'plaintext',
        }

        node_format_entry = {
            'pencolor' : 'orange',
        }

        node_format_exit = {
            'pencolor' : 'gray',
        }

        node_format_entry_exit = {
            'pencolor' : 'gray',
        }

        edge_format = {
            'arrowhead' : 'vee',
            'arrowsize' : 0.6,
            'fontname'  : fontname,
            'fontsize'  : 8.0,
            'penwidth'  : 0.5,
        }

        edge_colors = {
            'direct'    : 'blue',
            'not-taken' : 'red',
            'taken'     : 'darkgreen',
        }

        try:
            dot_graph = Dot(**graph_format)

            # add nodes
            nodes = {}
            for bb_addr in self._graph.node.keys():
                # Skip jmp/jcc to sub routines.
                if not bb_addr in self._bb_by_addr:
                    continue

                bb_dump = self._dump_bb_ex(self._bb_by_addr[bb_addr], print_ir)

                if self._bb_by_addr[bb_addr].is_entry and not self._bb_by_addr[bb_addr].is_exit:
                    node_format = dict(node_format_base, **node_format_entry)
                elif self._bb_by_addr[bb_addr].is_exit and not self._bb_by_addr[bb_addr].is_entry:
                    node_format = dict(node_format_base, **node_format_exit)
                elif self._bb_by_addr[bb_addr].is_entry and self._bb_by_addr[bb_addr].is_exit:
                    node_format = dict(node_format_base, **node_format_entry_exit)
                else:
                    node_format = dict(node_format_base)

                if self._bb_by_addr[bb_addr].is_entry:
                    bb_label = "{} @ {:x}".format(self._name, bb_addr)
                else:
                    bb_label = "loc_{:x}".format(bb_addr)

                label  = '<'
                label += '<table border="1.0" cellborder="0" cellspacing="1" cellpadding="0" valign="middle">'
                label += '  <tr><td align="center" cellpadding="1" port="enter"></td></tr>'
                label += '  <tr><td align="left" cellspacing="1">{label}</td></tr>'
                label += '  {assembly}'
                label += '  <tr><td align="center" cellpadding="1" port="exit" ></td></tr>'
                label += '</table>'
                label += '>'

                label = label.format(label=bb_label, assembly=bb_dump)

                nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                dot_graph.add_node(nodes[bb_addr])

            # add edges
            for bb_src_addr in self._graph.node.keys():
                # Skip jmp/jcc to sub routines.
                if not bb_src_addr in self._bb_by_addr:
                    continue

                for bb_dst_addr, branch_type in self._bb_by_addr[bb_src_addr].branches:
                    # Skip jmp/jcc to sub routines.
                    if not bb_dst_addr in self._bb_by_addr:
                        continue

                    dot_graph.add_edge(
                        Edge(nodes[bb_src_addr],
                             nodes[bb_dst_addr],
                             color=edge_colors[branch_type],
                             **edge_format))

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception as err:
           logger.error(
                "Failed to save basic block graph: %s (%s)",
                filename,
                format,
                exc_info=True
            )
コード例 #42
0
    def renderProof(self, proof, nsMap={}):
        """
        Takes an instance of a compiled ProofTree and a namespace mapping (for constructing QNames
        for rule pattern terms) and returns a BGL Digraph instance representing the Proof Tree
        """
        try:
            import boost.graph as bgl
            bglGraph = bgl.Digraph()
        except:
            try:
                from pydot import Node, Edge, Dot
                dot = Dot(graph_type='digraph')
            except:
                raise NotImplementedError("No graph libraries")
        namespace_manager = NamespaceManager(Graph())
        vertexMaps = {}
        edgeMaps = {}
        #        for prefix,uri in nsMap.items():
        #            namespace_manager.bind(prefix, uri, override=False)
        visitedNodes = {}
        edges = []
        idx = 0
        #register the step nodes
        for nodeset in self.goals.values():
            if not nodeset in visitedNodes:
                idx += 1
                visitedNodes[nodeset] = nodeset.generateGraphNode(
                    str(idx), nodeset is proof)
            #register the justification steps
            for justification in nodeset.steps:
                if not justification in visitedNodes:
                    idx += 1
                    visitedNodes[
                        justification] = justification.generateGraphNode(
                            str(idx))
                    for ant in justification.antecedents:
                        if ant not in visitedNodes:
                            idx += 1
                            visitedNodes[ant] = ant.generateGraphNode(str(idx))
        for node in visitedNodes.values():
            dot.add_node(node)
        for nodeset in self.goals.values():
            for justification in nodeset.steps:
                edge = Edge(visitedNodes[nodeset],
                            visitedNodes[justification],
                            label="is the consequence of",
                            color='red')
                dot.add_edge(edge)

                for ant in justification.antecedents:
                    if justification.source:
                        edge = Edge(visitedNodes[ant.steps[0]],
                                    visitedNodes[nodeset],
                                    label="has antecedent",
                                    color='blue')
                        dot.add_edge(edge)
                    else:  #not isinstance(justification,InferenceStep) or not justification.source:#(visitedNodes[nodeset],visitedNodes[justification]) not in edges:
                        edge = Edge(visitedNodes[justification],
                                    visitedNodes[ant],
                                    label="has antecedent",
                                    color='blue')
                        #edge.label="has antecedents"
                        dot.add_edge(edge)
                        #edges.append((visitedNodes[nodeset],visitedNodes[justification]))

        return dot  #bglGraph
コード例 #43
0
ファイル: basicblock.py プロジェクト: abforce/barf-project
    def save(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        node_format = {
            'shape'    : 'Mrecord',
            'rankdir'  : 'LR',
            'fontname' : 'monospace',
            'fontsize' : '9.0',
        }

        edge_format = {
            'fontname' : 'monospace',
            'fontsize' : '8.0',
        }

        edge_colors = {
            'taken'     : 'green',
            'not-taken' : 'red',
            'direct'    : 'blue',
        }

        html_entities = {
            "!" : "&#33;",
            "#" : "&#35;",
            ":" : "&#58;",
            "{" : "&#123;",
            "}" : "&#125;",
        }

        try:
            dot_graph = Dot(graph_type="digraph", rankdir="TB")

            # Add nodes.
            nodes = {}
            for bb_addr in self._bb_by_addr:
                bb_dump = self._dump_bb(self._bb_by_addr[bb_addr], print_ir)

                # html-encode colon character
                for char, encoding in html_entities.items():
                    bb_dump = bb_dump.replace(char, encoding)

                label = "{{<f0> {:#010x} | {}}}".format(bb_addr, bb_dump)

                nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                dot_graph.add_node(nodes[bb_addr])

            # Add edges.
            for bb_src_addr in self._bb_by_addr:
                for bb_dst_addr, branch_type in self._bb_by_addr[bb_src_addr].branches:
                    # Skip jmp/jcc to sub routines.
                    if not bb_dst_addr in self._bb_by_addr:
                        continue

                    dot_graph.add_edge(
                        Edge(
                            nodes[bb_src_addr],
                            nodes[bb_dst_addr],
                            color=edge_colors[branch_type],
                            **edge_format))

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error(
                "Failed to save basic block graph: %s (%s)",
                filename,
                format,
                exc_info=True
            )
コード例 #44
0
class Grafo():
    def __init__(self, rotulo):
        self.__rotulo = rotulo
        self.__grafoTela = Dot(graph_type='graph')
        self.__vertices = list()
        self.__arestas = list()

    def getRotulo(self):
        return self.__rotulo

    def setRotulo(self, rotulo):
        self.__rotulo = rotulo

    def criarVertice(self, nome):
        vertice = Vertice(nome)
        self.__vertices.append(vertice)
        self.__grafoTela.add_node(vertice.getVertice())

    def criarVerticeComCoordenadas(self, nome, cx, cy):
        vertice = Vertice(nome)
        vertice.InserirCoordenadas(cx, cy)
        self.__vertices.append(vertice)
        self.__grafoTela.add_node(vertice.getVertice())

    def getVertices(self):
        return self.__vertices

    def getArestas(self):
        return self.__arestas

    def inserirAresta(self, nome, custo, vertice1, vertice2):
        for i in self.__vertices:
            if i.getNome() == vertice1:
                aux = i
            elif i.getNome() == vertice2:
                aux1 = i

        aresta = Aresta(nome, custo, aux, aux1)
        aux.setAdjacente(aux1)
        aux1.setAdjacente(aux)
        self.__grafoTela.add_edge(aresta.getAresta())
        self.__arestas.append(aresta)

    def removerVertice(self, vertice):
        conta = 0
        for i in self.__arestas:
            if i.getVertice1().getNome() == vertice:
                aux = i.getVertice2()
                contv = 0
                for j in aux.getLista():
                    if j.getNome() == vertice:
                        aux.getLista().pop(contv)
                contv += 1
                self.__arestas.pop(conta)
            elif i.getVertice2().getNome() == vertice:
                aux = i.getVertice1()
                contv = 0
                for j in aux.getLista():
                    if j.getNome() == vertice:
                        aux.getLista().pop(contv)
                    contv += 1
                self.__arestas.pop(conta)
            conta += 1
        conta = 0
        for i in self.__vertices:
            if i.getNome() == vertice:
                self.__vertices.pop(conta)
                break
            conta += 1

    def removerAresta(self, nome):
        conta = 0
        for i in self.__arestas:
            if i.getNome() == nome:
                self.__arestas.pop(conta)
                break
            conta += 1

    def verificarAdjacente(self, vertice1, vertice2):
        for i in self.__vertices:
            if i.getNome() == vertice1:
                for j in i.getLista():
                    if j.getNome() == vertice2:
                        return True
            elif i.getNome() == vertice2:
                for j in i.getLista():
                    if j.getNome() == vertice1:
                        return True
        return False

    def pegarCustoAresta(self, nome):
        for i in self.__arestas:
            if i.getNome() == nome:
                return i.getCusto()

    def retornarExtremosAresta(self, nome):
        for i in self.__arestas:
            if i.getNome() == nome:
                return i.getVertices()

    def exibirGrafo(self):
        l = []
        print(self.__rotulo)
        for i in self.__arestas:
            l = i.getVertices()
            aux2 = l.pop()
            aux1 = l.pop()
            l.append(aux1.getNome())
            l.append(i.getNome())
            l.append(str(i.getCusto()))
            l.append(aux2.getNome())
            concat = ''.join(l)
            print(concat)

    def showGrafoTela(self):
        l = []

        for i in self.__arestas:
            l = i.getVertices()
            aux2 = l.pop()
            aux1 = l.pop()
            l.append(aux1.getNome())
            l.append(i.getNome())
            l.append(str(i.getCusto()))
            l.append(aux2.getNome())
            concat = ''.join(l)
            print(concat)

    def carregarImagem(self):
        self.__grafoTela.write_png(Config.DIRETORIO_PADRAO_IMAGEM)

    def BuscaEmLargura(self, vsaida, vertice):
        visitados = list()
        fila = list()
        fila.append(vsaida)
        visitados.append(vsaida)
        while len(visitados) != len(self.__vertices) or len(fila) != 0:
            print(len(fila))
            for i in self.__vertices:
                if i.getNome() == fila[0]:
                    aux = i.getLista()
                    for j in aux:
                        if j.getNome() == vertice:
                            return j
                        elif j.getNome() not in visitados:
                            visitados.append(j.getNome())
                            fila.append(j.getNome())
            fila.pop(0)
        print('vertice não encontrado')

    def BuscaEmProfundidade(self, vsaida, vertice):
        visitados = list()
        pilha = list()
        cont = 0
        tamanho = len(self.__arestas)
        continuar = True
        visitados.append(vsaida)
        pilha.append(vsaida)
        while len(visitados) != len(self.__vertices) or len(pilha) != 0:
            cont = 0
            continuar = True
            i = self.__arestas[0]
            while cont < tamanho and continuar == True:
                while (i.getVertice1().getNome() !=
                       pilha[-1]) and (i.getVertice2().getNome() != pilha[-1]):
                    if cont >= (tamanho - 1):
                        break
                    else:
                        i = self.__arestas[cont]
                        cont += 1
                cont += 1
                if i.getVertice1().getNome() == pilha[-1]:
                    if i.getVertice2().getNome() not in visitados:
                        visitados.append(i.getVertice2().getNome())
                        pilha.append(i.getVertice2().getNome())
                        continuar = False
                    else:
                        i = self.__arestas[cont]
                elif i.getVertice2().getNome() == pilha[-1]:
                    if i.getVertice1().getNome() not in visitados:
                        visitados.append(i.getVertice1().getNome())
                        pilha.append(i.getVertice1().getNome())
                        continuar = False
                    else:
                        i = self.__arestas[cont]
                elif cont >= tamanho:
                    continuar = False
                    pilha.pop(len(pilha) - 1)
            if visitados[-1] == vertice:
                if i.getVertice1().getNome() == vertice:
                    return i.getVertice1()
                else:
                    return i.getVertice2()
        print('vertice não encontrado')

    def GerarArvoreMinima(self, vsaida):
        arvore = Grafo('Arvore Minima')
        arvore.criarVertice(vsaida)
        aresta = self.__arestas[0]
        menor = None
        aux = arvore.getVertices()
        verticev = []
        verticev.append(aux[len(aux) - 1].getNome())
        while len(aux) < len(self.__vertices):
            for j in aux:
                for i in self.__arestas:
                    if i.getVertice1().getNome() == j.getNome(
                    ) or i.getVertice2().getNome() == j.getNome():
                        if i.getVertice1(
                        ).getNome() not in verticev or i.getVertice2().getNome(
                        ) not in verticev:
                            if menor == None:
                                menor = i.getCusto()
                                aresta = i
                            elif i.getCusto() < menor:
                                menor = i.getCusto()
                                aresta = i
            if aresta.getVertice1().getNome() not in verticev:
                print('if vertice 1')
                arvore.criarVertice(aresta.getVertice1().getNome())
            else:
                print('if vertice 2')
                arvore.criarVertice(aresta.getVertice2().getNome())
            aux = arvore.getVertices()
            verticev.append(aux[len(aux) - 1])
            arvore.inserirAresta(aresta.getNome(), aresta.getCusto(),
                                 aresta.getVertice1().getNome(),
                                 aresta.getVertice2().getNome())
            menor = None
        return arvore

    def grafoConvexoEhPlanar(self):
        qtdeArestas = len(self.__arestas)
        qtdeVertices = len(self.__vertices)
        total = 0
        print("Vertices: ", qtdeVertices)
        print("Arestas: ", qtdeArestas)

        if qtdeVertices >= 3:
            total = 3 * qtdeVertices - 6
            print("Total 1: ", total)
            if not qtdeArestas <= total:
                print("condicao1")
                return False

            if not self.temCiclosDeComprimentoTres():
                total = 2 * qtdeVertices - 4
                print("Total 2: ", total)
                if not qtdeArestas <= total:
                    print("condicao2")
                    return False

            return True
        else:
            return False

    def temCiclosDeComprimentoTres(self):
        for vertice in self.__vertices:
            for adjacente in vertice.getLista():
                for adjacenteRetorno in adjacente.getLista():
                    if self.verificarAdjacente(vertice.getNome(),
                                               adjacenteRetorno.getNome()):
                        return True
        return False

    def coloreVertices(self):
        listaVertices = list()
        grauVertices = list()
        numeracaoCores = list()
        numeroCor = -1
        for i in self.__vertices:
            listaVertices.append(i.getNome())
            grauVertices.append(0)
            numeracaoCores.append(-1)

        for i in self.__arestas:
            v1 = i.getVertice1()
            v2 = i.getVertice2()
            index = listaVertices.index(v1.getNome())
            grauVertices[index] = grauVertices[index] + 1
            index = listaVertices.index(v2.getNome())
            grauVertices[index] = grauVertices[index] + 1

        for i in range(1, len(listaVertices)):
            for j in range(0, i):
                if (grauVertices[i] > grauVertices[j]):
                    aux = listaVertices[i]
                    temp = grauVertices[i]
                    listaVertices[i] = listaVertices[j]
                    grauVertices[i] = grauVertices[j]
                    listaVertices[j] = aux
                    grauVertices[j] = temp
        for i in range(0, len(listaVertices)):
            if (numeracaoCores[i] == -1):
                numeroCor = numeroCor + 1
                numeracaoCores[i] = numeroCor
                if (i + 1 < len(listaVertices)):
                    for j in range(i + 1, len(listaVertices)):
                        if (numeracaoCores[j] == -1):
                            if not (self.verificarAdjacente(
                                    listaVertices[i], listaVertices[j])):
                                numeracaoCores[j] = numeroCor
        for i in range(0, len(listaVertices)):
            print('Vertice : ')
            print(listaVertices[i])
            print('Grau : ')
            print(grauVertices[i])
            print('Cor : ')
            print(numeracaoCores[i])
        #azul-claro rosa verde marrom
        cores = list()
        #"#00c6c5", "#cc0132", "#538e6d", "#843907"
        # cores.append("#00c6c5")
        # cores.append("#cc0132")
        # cores.append("#538e6d")
        # cores.append("#843907")

        # pos = 0
        # for vertice in self.__vertices:
        # 	vertice.setVertice(cores[pos])
        # 	pos += 1
        # 	for adjacente in vertice.getLista():
        # 		for adjacenteDoAdjacente in adjacente.getLista():
        # 			vertice.setVertice(cores[pos])
        # 			pos += 1
        # 			if pos >= 4:
        # 				pos = 0

    def Dijkstra(self, vsaida, vchegada):
        fechados = list()
        estimativa = list()
        precedente = list()
        listaVertices = list()
        menor = -1
        selecionado = ""
        contador = len(self.__vertices)
        for i in self.__vertices:
            listaVertices.append(i.getNome())
            if (i.getNome() == vsaida):
                estimativa.append(0)
                precedente.append('')
            else:
                estimativa.append(-1)
                precedente.append('')
        for j in range(0, contador):
            for i in range(0, contador):
                if (listaVertices[i] not in fechados):
                    if (menor == -1):
                        menor = estimativa[i]
                        selecionado = listaVertices[i]
                    elif (estimativa[i] < menor and estimativa[i] > -1):
                        menor = estimativa[i]
                        selecionado = listaVertices[i]
            fechados.append(selecionado)
            menor = -1
            for k in self.__arestas:
                if (k.getVertice1().getNome() not in fechados
                        and k.getVertice2().getNome() == selecionado):
                    custo = k.getCusto()
                    indexAdjacente = listaVertices.index(
                        k.getVertice1().getNome())
                    indexAtual = listaVertices.index(selecionado)

                    if (estimativa[indexAdjacente] == -1):
                        estimativa[indexAdjacente] = custo
                        precedente[indexAdjacente] = selecionado
                    else:
                        custo = custo + estimativa[indexAtual]
                        if (custo < estimativa[indexAdjacente]):
                            estimativa[indexAdjacente] = custo
                            precedente[indexAdjacente] = selecionado
                elif (k.getVertice1().getNome() == selecionado
                      and k.getVertice2().getNome() not in fechados):
                    custo = k.getCusto()
                    indexAdjacente = listaVertices.index(
                        k.getVertice2().getNome())
                    indexAtual = listaVertices.index(selecionado)
                    if (estimativa[indexAdjacente] == -1):
                        estimativa[indexAdjacente] = custo
                        precedente[indexAdjacente] = selecionado
                    else:
                        custo = custo + estimativa[indexAtual]
                        if (custo < estimativa[indexAdjacente]):
                            estimativa[indexAdjacente] = custo
                            precedente[indexAdjacente] = selecionado

        for i in range(0, len(listaVertices)):
            print(listaVertices[i])
            print(estimativa[i])
            print(precedente[i])

    def AEstrela(self, vsaida, vchegada):
        distanciaLinhaReta = list()
        ordemLista = list()
        cobjetivo = list()
        proximo = vsaida
        aproximacoes = list()
        nodos = list()
        calculoDeslocamento = list()
        distancia = 0
        continua = True
        grafo = Grafo('A*')
        for i in self.__arestas:
            if (i.getCusto() == 0):
                v1 = i.getVertice1()
                v2 = i.getVertice2()
                c1 = v1.getCoordenadas()
                c2 = v2.getCoordenadas()
                custo = abs(c1[0] - c2[0]) + abs(c1[1] - c2[1])
                i.setCusto(custo)

        for i in self.__vertices:
            if (i.getNome() == vchegada):
                aux = i.getCoordenadas()
                cobjetivo.append(aux[0])
                cobjetivo.append(aux[1])
        for i in self.__vertices:
            aux = i.getCoordenadas()
            distancia = abs(cobjetivo[0] - aux[0]) + abs(cobjetivo[1] - aux[1])
            distanciaLinhaReta.append(distancia)
            ordemLista.append(i.getNome())  #ordemCidadesLista#
        grafo.criarVertice(proximo)
        while (continua):
            for i in self.__arestas:
                if (i.getVertice1().getNome() == proximo):
                    aproximacoes.append(i.getCusto())
                    nodos.append(i.getVertice2().getNome())
                elif (i.getVertice2().getNome() == proximo):
                    aproximacoes.append(i.getCusto())
                    nodos.append(i.getVertice1().getNome())
            contador = len(aproximacoes)
            for i in range(0, contador):
                index = ordemLista.index(nodos[i])
                aux = distancia + aproximacoes[i] + distanciaLinhaReta[index]
                calculoDeslocamento.append(aux)
            aux = min(calculoDeslocamento)
            index = calculoDeslocamento.index(aux)
            grafo.criarVertice(nodos[index])
            grafo.inserirAresta('------', aproximacoes[index], proximo,
                                nodos[index])
            distancia = distancia + aproximacoes[index]
            proximo = nodos[index]

            if (proximo == vchegada):
                continua = False
            aproximacoes.clear()
            nodos.clear()
            calculoDeslocamento.clear()
        return grafo
コード例 #45
0
def generate_dot_from_schema(metadata, name=None):
    graph = Dot(
        name or 'Database schema',
        splines='true',
        rankdir='LR',
    )

    graph.add_node(Node(
        'Description',
        shape='box',
        label=('<'
            'PK: Primary key<br />'
            'FK: Foreign key<br />'
            'UQ: Unique constraint<br />'
        '>'),
    ))


    for table_name, table in metadata.tables.items():
        soup = BeautifulSoup(f'''
            <table border="0" cellspacing="0" cellborder="1">
                <tr>
                    <td colspan="3" bgcolor="lightblue">
                        <font>{table_name}</font>
                    </td>
                </tr>
            </table>
        ''', 'html.parser')

        for column_name, column in table.columns.items():
            special = set()
            if column.unique:
                special.add('UQ')
            if column.primary_key:
                special.add('PK')
            if column.index:
                special.add('IX')
            if column.foreign_keys:
                special.add('FK')

            row_soup = BeautifulSoup(f'''
                <tr>
                    <td port="{column_name}_in">
                        <font>{','.join(special) if special else ' '}</font>
                    </td>
                    <td>
                        <font>{column_name}</font>
                    </td>
                    <td port="{column_name}_out">
                        <font>{str(column.type)}{'*' if column.nullable else ''}</font>
                    </td>
                </tr>
            ''', 'html.parser')
            soup.table.append(row_soup.tr)

            # Add edges
            for fk in column.foreign_keys:
                edge = Edge(
                    f'{table_name}:{column_name}_out:e',
                    f'{fk.column.table.name}:{fk.column.name}_in:w',
                )
                graph.add_edge(edge)

        node = Node(
            table_name,
            label=f'<{str(soup.table)}>',
            shape='none',
            dir='back',
            arrowhead='dot',
            arrowtail='crow',
        )

        graph.add_node(node)

    return graph
コード例 #46
0
ファイル: basicblock.py プロジェクト: Zke1ev3n/barf-project
    def save_ex(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """

        fontname = 'Ubuntu Mono'
        # fontname = 'DejaVu Sans Mono'
        # fontname = 'DejaVu Sans Condensed'
        # fontname = 'DejaVu Sans Light'
        # fontname = 'Liberation Mono'
        # fontname = 'DejaVu Serif Condensed'
        # fontname = 'Ubuntu Condensed'

        node_format = {
            'shape'     : 'plaintext',
            'rankdir'   : 'LR',
            'fontname'  : fontname,
            'fontsize'  : 9.0,
            'penwidth'  : 0.5,
            # 'style'     : 'filled',
            # 'fillcolor' : 'orange',
        }

        edge_format = {
            'fontname'  : fontname,
            'fontsize'  : 8.0,
            'penwidth'  : 0.5,
            'arrowsize' : 0.6,
            'arrowhead' : 'vee',
        }

        edge_colors = {
            'taken'     : 'darkgreen',
            'not-taken' : 'red',
            'direct'    : 'blue',
        }

        try:
            # for each conneted component
            for idx, gr in enumerate(networkx.connected_component_subgraphs(self._graph.to_undirected())):
                graph = Dot(graph_type="digraph", rankdir="TB", splines="ortho", nodesep=1.2)

                # add nodes
                nodes = {}
                for bb_addr in gr.node.keys():
                    # Skip jmp/jcc to sub routines.
                    if not bb_addr in self._bb_by_addr:
                        continue

                    dump = self._dump_bb_ex(self._bb_by_addr[bb_addr], print_ir)

                    label  = '<'
                    label += '<table border="1.0" cellborder="0" cellspacing="1" cellpadding="0" valign="middle">'
                    label += '  <tr><td align="center" cellpadding="1" port="enter"></td></tr>'
                    if self._bb_by_addr[bb_addr].label:
                        label += '  <tr><td align="left" cellspacing="1">{}</td></tr>'.format(self._bb_by_addr[bb_addr].label)
                    else:
                        label += '  <tr><td align="left" cellspacing="1">loc_{address:x}</td></tr>'
                    label += '  {assembly}'
                    label += '  <tr><td align="center" cellpadding="1" port="exit" ></td></tr>'
                    label += '</table>'
                    label += '>'

                    if self._bb_by_addr[bb_addr].is_entry:
                        node_format['style'] = 'filled'
                        node_format['fillcolor'] = 'orange'

                    label = label.format(address=bb_addr, assembly=dump)

                    nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                    graph.add_node(nodes[bb_addr])

                # add edges
                for bb_src_addr in gr.node.keys():
                    # Skip jmp/jcc to sub routines.
                    if not bb_src_addr in self._bb_by_addr:
                        continue

                    for bb_dst_addr, branch_type in self._bb_by_addr[bb_src_addr].branches:
                        # Skip jmp/jcc to sub routines.
                        if not bb_dst_addr in self._bb_by_addr:
                            continue

                        graph.add_edge(
                            Edge(nodes[bb_src_addr],
                            nodes[bb_dst_addr],
                            color=edge_colors[branch_type],
                            **edge_format))

                graph.write("%s_%03d.%s" % (filename, idx, format), format=format)
        except Exception as err:
           logger.error(
                "Failed to save basic block graph: %s (%s)",
                filename,
                format,
                exc_info=True
            )
コード例 #47
0
    def save(self, filename, print_ir=False, format='dot'):
        """Save basic block graph into a file.
        """
        node_format = {
            'shape': 'Mrecord',
            'rankdir': 'LR',
            'fontname': 'monospace',
            'fontsize': '9.0',
        }

        edge_format = {
            'fontname': 'monospace',
            'fontsize': '8.0',
        }

        edge_colors = {
            'taken': 'green',
            'not-taken': 'red',
            'direct': 'blue',
        }

        html_entities = {
            "!": "&#33;",
            "#": "&#35;",
            ":": "&#58;",
            "{": "&#123;",
            "}": "&#125;",
        }

        try:
            dot_graph = Dot(graph_type="digraph", rankdir="TB")

            # Add nodes.
            nodes = {}
            for bb_addr in self._bb_by_addr:
                bb_dump = self._dump_bb(self._bb_by_addr[bb_addr], print_ir)

                # html-encode colon character
                for char, encoding in html_entities.items():
                    bb_dump = bb_dump.replace(char, encoding)

                label = "{{<f0> {:#08x} | {}}}".format(bb_addr, bb_dump)

                nodes[bb_addr] = Node(bb_addr, label=label, **node_format)

                dot_graph.add_node(nodes[bb_addr])

            # Add edges.
            for bb_src_addr in self._bb_by_addr:
                for bb_dst_addr, branch_type in self._bb_by_addr[
                        bb_src_addr].branches:
                    # Skip jmp/jcc to sub routines.
                    if not bb_dst_addr in self._bb_by_addr:
                        continue

                    dot_graph.add_edge(
                        Edge(nodes[bb_src_addr],
                             nodes[bb_dst_addr],
                             color=edge_colors[branch_type],
                             **edge_format))

            # Save graph.
            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception:
            logger.error("Failed to save basic block graph: %s (%s)",
                         filename,
                         format,
                         exc_info=True)
コード例 #48
0
    def visualize(self, filename):
        """Save a graphical representation of this AF."""
        graph = Dot(graph_type='digraph')

        for arg in self.arg_f:  # Fixed arguments
            text = arg.text

            # Add any offers to the text that this argument may support
            for off, supp in self.supp_args.items():
                if arg in supp:
                    text += f"\n[{off}]"

            graph.add_node(
                Node(name=text, style="filled", fillcolor=cf.COLOR_FIXED))

        for arg in self.arg_u:  # Uncertain arguments
            graph.add_node(
                Node(name=arg.text,
                     style="filled",
                     fillcolor=cf.COLOR_UNCERTAIN))

        for arg in self.arg_c:  # Control arguments
            graph.add_node(
                Node(name=arg.text, style="filled",
                     fillcolor=cf.COLOR_CONTROL))

        for att in self.att_f:  # Fixed attacks
            if att.arg_start in self.arg_f | self.arg_u and \
              att.arg_end in self.arg_f | self.arg_u:
                texts = []

                for arg in [att.arg_start, att.arg_end]:
                    texts.append(arg.text)

                    for off, supp in self.supp_args.items():
                        if arg in supp:
                            texts[-1] += f"\n[{off}]"
                graph.add_edge(
                    Edge(texts[0],
                         texts[1],
                         color=cf.COLOR_FIXED,
                         dirType="forward"))
            else:
                print("Warning: attack in CAF but not its start or end")

        for att in self.att_u:  # Uncertain attacks
            if att.arg_start in self.arg_f | self.arg_u and \
              att.arg_end in self.arg_f | self.arg_u:
                texts = []

                for arg in [att.arg_start, att.arg_end]:
                    texts.append(arg.text)

                    for off, supp in self.supp_args.items():
                        if arg in supp:
                            texts[-1] += f"\n[{off}]"
                graph.add_edge(
                    Edge(texts[0],
                         texts[1],
                         color=cf.COLOR_UNCERTAIN,
                         dirType="both",
                         style="dashed"))
            else:
                print("Warning: attack in CAF but not its start or end")

        for att in self.att_b:  # Bidirectional attacks
            if att.arg_start in self.arg_f | self.arg_u and \
              att.arg_end in self.arg_f | self.arg_u:
                texts = []

                for arg in [att.arg_start, att.arg_end]:
                    texts.append(arg.text)

                    for off, supp in self.supp_args.items():
                        if arg in supp:
                            texts[-1] += f"\n[{off}]"
                graph.add_edge(
                    Edge(texts[0],
                         texts[1],
                         color=cf.COLOR_UNCERTAIN,
                         dirType="forward"))
            else:
                print("Warning: attack in CAF but not its start or end")

        for att in self.att_c:  # Control attacks
            if att.arg_start in self.arg_c and \
              att.arg_end in self.arg_c | self.arg_f | self.arg_u:
                texts = []

                for arg in [att.arg_start, att.arg_end]:
                    texts.append(arg.text)

                    for off, supp in self.supp_args.items():
                        if arg in supp:
                            texts[-1] += f"\n[{off}]"
                graph.add_edge(
                    Edge(texts[0],
                         texts[1],
                         color=cf.COLOR_CONTROL,
                         dirType="forward"))
            else:
                print("Warning: attack in CAF but not its start or end")

        graph.write_png(filename)
コード例 #49
0
ファイル: unflat.py プロジェクト: bin2415/unflat
def unflat(angr_proj, barf_proj, function):
    '''
    Given the angr proejct and its function address, unflat the cfg

    Args:
        angr_proj : angr project
        barf_proj : barf project
        fucntion: the analysed function
    
    Returns:

    '''

    #prelogue_block = None
    address = function.addr
    f_cfg = barf_proj.recover_cfg(start = address)
    blocks_list = f_cfg.basic_blocks
    prelogue_block = f_cfg.find_basic_block(address)
    try:
        main_dispatcher = prelogue_block.direct_branch
    except:
        return
    

    exit_blocks = list()
    normal_blocks = list()
    nop_blocks = list()
    predispatcher_address = 0

    ### First, need find the predispatcher
    for temp_block in blocks_list:
        if temp_block.direct_branch == main_dispatcher:
            predispatcher_address = temp_block.start_address
        elif len(temp_block.branches) == 0 and temp_block.direct_branch == None:
            exit_blocks.append(temp_block.start_address)
    ### End find the predispatcher
    
    ### Specify the normal blocks and nop blocks
    for temp_block in blocks_list:
        if temp_block.direct_branch == predispatcher_address and len(temp_block.instrs) != 1:
            normal_blocks.append(temp_block.start_address)
        elif temp_block.start_address != address and temp_block.start_address not in exit_blocks:
            nop_blocks.append(temp_block)
    ### End specify the blocks
    
    if len(normal_blocks) == 0:
        print("The function %s don't be obfuscated" % function.name)
        return

    ## Symbolic execution to find the blocks' sequence
    print(len(normal_blocks))
    normal_blocks.append(address)
    global normal_exit_blocks
    normal_exit_blocks = copy.deepcopy(normal_blocks)
    normal_exit_blocks.extend(exit_blocks)
    relationships = dict()
    patch_instrs = dict()
    
    for temp_block in normal_exit_blocks:
        relationships[temp_block] = list()
    
    for temp_start in normal_blocks:
        temp_block = f_cfg.find_basic_block(temp_start)
        branches = False
        #hook = list()
        for inst in temp_block.instrs:
            if inst.asm_instr.mnemonic.startswith('cmov'):
                patch_instrs[temp_start] = inst.asm_instr
                branches = True
            #elif inst.asm_instr.mnemonic.startswith('call'):
            #    hook.append(inst.addr)

        if branches: ##if it has branches, we should traverse the every branch
            relationships[temp_start].append(symbolic_compute(angr_proj, temp_start, claripy.BVV(1, 1), True))
            relationships[temp_start].append(symbolic_compute(angr_proj, temp_start, claripy.BVV(0, 1), True))
        else:
            relationships[temp_start].append(symbolic_compute(angr_proj, temp_start))
        
    ###End get the basic block relationships
    f_cfg.save(function.name+"_origin", format = "png")
    
    ###output the dot graph
    dot_graph = Dot(function.name+".dot")

    node_color = {
        'entry': 'orange',
        'exit': 'gray',
        'other': 'black',
    }

    node_format = {
        'fontname': 'monospace',
        'fontsize': 9.0,
        'penwidth': 0.5,
        'rankdir': 'LR',
        'shape': 'plaintext',
    }

    nodes = {}
    bb_tpl = '<'
    bb_tpl += '<table border="1.0" cellborder="0" cellspacing="1" cellpadding="0" valign="middle">'
    bb_tpl += '  <tr><td align="center" cellpadding="1" port="enter"></td></tr>'
    bb_tpl += '  <tr><td align="left" cellspacing="1">{label}</td></tr>'
    bb_tpl += '  {assembly}'
    bb_tpl += '  <tr><td align="center" cellpadding="1" port="exit" ></td></tr>'
    bb_tpl += '</table>'
    bb_tpl += '>'
        

    for node_address, node_list in relationships.items():
        temp_block = f_cfg.find_basic_block(node_address)
        asm_mnemonic_max_width = bb_get_instr_max_width(temp_block) + 1
        lines = list()
        if len(node_list) == 1:
            for dinstr in temp_block:
                dinstr = dinstr.asm_instr
                if dinstr.mnemonic.startswith('jmp'):
                    origin_str = 'jmp ' + (' ' * (asm_mnemonic_max_width - len(dinstr.mnemonic))) + hex(node_list[0])
                else:
                    oprnds_str = ", ".join([oprnd.to_string() for oprnd in dinstr.operands])
                    origin_str = dinstr.mnemonic + (' ' * (asm_mnemonic_max_width - len(dinstr.mnemonic)))
                    origin_str += " " + oprnds_str if oprnds_str else ""

                lines.append(render_asm(origin_str, dinstr.address))
        elif len(node_list) == 2:
            for dinstr in temp_block:
                dinstr = dinstr.asm_instr
                if dinstr.mnemonic.startswith('cmov'):
                    origin_str = 'j' + dinstr.mnemonic[4:]
                    origin_str += (' ' * (asm_mnemonic_max_width - len(origin_str)))
                    origin_str += ' ' + hex(node_list[0])
                    lines.append(render_asm(origin_str, dinstr.address))
                    origin_str = 'jmp'
                    origin_str += (' ' * (asm_mnemonic_max_width - len(origin_str)))
                    origin_str += hex(node_list[1])
                    lines.append(render_asm(origin_str, dinstr.address))
                    break
                else:
                    oprnds_str = ", ".join([oprnd.to_string() for oprnd in dinstr.operands])
                    origin_str = dinstr.mnemonic + (' ' * (asm_mnemonic_max_width - len(dinstr.mnemonic)))
                    origin_str += " " + oprnds_str if oprnds_str else ""
                    lines.append(render_asm(origin_str, dinstr.address))
        else:
            for dinstr in temp_block:
                dinstr = dinstr.asm_instr
                oprnds_str = ", ".join([oprnd.to_string() for oprnd in dinstr.operands])
                origin_str = dinstr.mnemonic + (' ' * (asm_mnemonic_max_width - len(dinstr.mnemonic)))
                origin_str += " " + oprnds_str if oprnds_str else ""
                lines.append(render_asm(origin_str, dinstr.address))
        
        bb_dump = "".join(lines)

        bb_addr = temp_block.address
        if temp_block.is_entry:
            bb_label = "{} @ {:x}".format(function.name, bb_addr)
        else:
            bb_label = "loc_{:x}".format(bb_addr)
        
        bb_formated = bb_tpl.format(label=bb_label, assembly=bb_dump)

        if temp_block.is_entry:
            bb_type = "entry"
        elif node_address in exit_blocks:
            bb_type = "exit"
        else:
            bb_type = "other"

        created_node = Node(temp_block.address, label = bb_formated, color = node_color[bb_type], **node_format)
        nodes[node_address] = created_node
        dot_graph.add_node(created_node)
        ###End write node

    ###Write Edge
    for node_address, node_list in relationships.items():
        if len(node_list) == 1:
            created_edge = Edge(nodes[node_address], nodes[node_list[0]], color = 'blue')
            dot_graph.add_edge(created_edge)
        elif len(node_list) == 2:
            created_edge = Edge(nodes[node_address], nodes[node_list[0]], color = 'darkgreen')
            dot_graph.add_edge(created_edge)
            created_edge = Edge(nodes[node_address], nodes[node_list[1]], color = 'red')
            dot_graph.add_edge(created_edge)
    ###End write edge

    dot_graph.write_png(function.name+"_unflat.png")
コード例 #50
0
ファイル: parse.py プロジェクト: nickzandbergen/box-of-pain

#exit()



for entity in activities.keys():
    graph.add_node(Activity(entity, activities[entity]))

for entity in entities.keys():
    labels = []
    if "prov:type" in entities[entity]:
        labels.append(entities[entity]["prov:type"])
    if "prov:label" in entities[entity]:
        labels.append(entities[entity]["prov:label"])
    dot.add_node(pydot.Node(entity, label = "|".join(labels), shape = "box"))
    graph.add_node(Entity(entity, entities[entity]))


for entity in derived.keys():
    #print "COMPARE this " + entity + " vs " + derived[entity]["prov:generatedEntity"]
    dot.add_edge(pydot.Edge(derived[entity]["prov:usedEntity"], derived[entity]["prov:generatedEntity"], label="DERIVED: " + derived[entity]["prov:label"]))
    graph.add_edge(Derived(graph.node_named(derived[entity]["prov:usedEntity"]), graph.node_named(derived[entity]["prov:generatedEntity"]), derived[entity]))
    
for entity in generated.keys():
    #print "YO " + str(generated[entity])
    #dot.add_edge(pydot.Edge(generated[entity]["prov:activity"], generated[entity]["prov:entity"], label = "GENERATED: " + generated[entity]["prov:label"]))
    dot.add_edge(pydot.Edge(generated[entity]["prov:entity"], generated[entity]["prov:activity"], label = "GENERATED: " + generated[entity]["prov:label"]))
    #graph.add_edge(Generated(graph.node_named(generated[entity]["prov:activity"]), graph.node_named(generated[entity]["prov:entity"]), generated[entity]))
    graph.add_edge(Generated(graph.node_named(generated[entity]["prov:entity"]), graph.node_named(generated[entity]["prov:activity"]), generated[entity]))
コード例 #51
0
# human labels are the majority labels for things
hl = human_labels
hl = [s.replace(':', '-') for s in hl]
hl = [s.replace('null', 'no-observation') for s in hl]


print(hl)

for i in num_labels:
    new_node = Node(str(hl[i]))
    col = colors_viridis(i)
    col = matplotlib.colors.rgb2hex(col)
    # print(matplotlib.colors.rgb2hex(col))
    new_node.set_color(col)
    g.add_node(new_node)
    print("Add node " + str(hl[i]))

for i_a in num_labels:
    best = sorted(zip(transmats[i_a], hl), reverse=True)[:3]

    for j_b in num_labels:
        a = hl[i_a]
        b = hl[j_b]
        edge = pydot.Edge(str(a), str(b))
        # edge.set_color('black')

        print("edge from " + a + " to " + b)
        prob_label = transmats[i_a][j_b]
        if (prob_label,b) in best:
            # print("found a best")
コード例 #52
0
        ref_edges.add((pdEdges['source'][i], pdEdges['target'][i]))
    
    if (pdEdges['source'][i], pdEdges['target'][i]) not in Edges:
        Edges[(pdEdges['source'][i], pdEdges['target'][i])] = 0
    
    Edges[(pdEdges['source'][i], pdEdges['target'][i])] += 1
    
for edge in Edges:
    GeneGraph[edge[0]]['adj'].append((edge[1], Edges[edge]))


G = Dot()

for node in GeneGraph:
    if node in ref_nodes:
        G.add_node(Node(name=node, width=str(GeneGraph[node]['length']), color='red'))
    else:
        G.add_node(Node(name=node, width=str(GeneGraph[node]['length']), color='blue'))

for node in GeneGraph:
    for _node in GeneGraph[node]['adj']:
        if (node, _node[0]) in ref_edges:
            G.add_edge(Edge(src=node, dst=_node[0], penwidth=str(np.sqrt(_node[1])), weight=str(_node[1]), color='red'))

        else:
            G.add_edge(Edge(src=node, dst=_node[0], penwidth=str(np.sqrt(_node[1])), weight=str(_node[1]), color='blue'))

for i in G.get_nodes():
    i.set_shape('box')
    i.set_style('filled')
コード例 #53
0
from pyd.atm_state import *
from pydot import Dot, Node, Edge

file = '/tmp/atm_state_machine.png'

dot = Dot(graph_type='digraph', comment='Atm State Machine')
nodes = {}
graph = {}
for name, state in State.get_subclasses_dict().iteritems():
    nodes.setdefault(name, Node(name))

for name, state in State.get_subclasses_dict().iteritems():
    for event, state in state.get_valid_events():
        edge = Edge(nodes.get(name), nodes.get(state))
        edge.set_label(event)
        graph.setdefault(name, []).append(edge)

for name, edges in graph.iteritems():
    for edge in edges:
        dot.add_node(nodes.get(name))
        dot.add_edge(edge)

dot.write_png(file)
コード例 #54
0
ファイル: dag.py プロジェクト: nicholas-owen/BABS-RNASeq
    # create a new graph and add the nodes to it
    graph = Dot()
    for name in names:

        # the label of the node
        if name_to_newlabel[name] != "":
            label = str(name_to_newlabel[name]) + "\n" + name_to_software[name]
        else:
            label = name_to_software[name]

        graph.add_node(
            Node(name,
                 fillcolor=name_to_color[name],
                 color=name_to_color[name],
                 style="filled",
                 fontname="cantarell bold",
                 fontcolor="#ffffff",
                 fontsize=22,
                 label=label))

    # get all the edges connected two processes
    edges = set()
    for name, paths in name_to_paths.items():
        for path in paths:
            for descendant in path:
                if (descendant in names) and (descendant != name):
                    edges.add((name, descendant))
                    break

    # add the edges to the graph
コード例 #55
0
ファイル: viewers.py プロジェクト: Lovestick/simpleai
    def create_graph(self, image_format, image_path):
        from pydot import Dot, Edge, Node

        graph = Dot(graph_type='digraph')

        graph_nodes = {}
        graph_edges = {}
        done = set()

        def add_node(node, expanded=False, chosen=False, in_fringe=False,
                     in_successors=False, solution=False):
            node_id = id(node)
            if node_id not in graph_nodes:
                label = node.state_representation()
                if hasattr(node, 'cost'):
                    label += '\nCost: %s' % node.cost
                if hasattr(node, 'heuristic'):
                    label += '\nHeuristic: %s' % node.heuristic
                if hasattr(node, 'value'):
                    label += '\nValue: %s' % node.value

                new_g_node = Node(node_id,
                                  label=label,
                                  style='filled',
                                  shape='circle',
                                  fillcolor='#ffffff',
                                  fontsize=self.font_size)

                graph_nodes[node_id] = new_g_node

            g_node =  graph_nodes[node_id]

            if expanded or chosen:
                g_node.set_fillcolor(self.fringe_color)
            if in_fringe:
                g_node.set_color(self.fringe_color)
                g_node.set_penwidth(3)
            if in_successors:
                g_node.set_color(self.successor_color)
                g_node.set_fontcolor(self.successor_color)
            if solution:
                g_node.set_fillcolor(self.solution_color)

            return g_node

        def add_edge_to_parent(node, is_successor=False, parent=None):
            if parent is None:
                parent = node.parent

            g_node = add_node(node, in_successors=is_successor)
            g_parent_node = add_node(parent)

            edge = Edge(g_parent_node,
                        g_node,
                        label=node.action_representation(),
                        fontsize=self.font_size)

            if is_successor:
                edge.set_color(self.successor_color)
                edge.set_labelfontcolor(self.successor_color)

            graph_edges[id(node), id(parent)] = edge

        if self.last_event.name == 'chosen_node':
            add_node(self.last_chosen, chosen=True)

        if self.last_event.name == 'finished':
            if self.solution_node:
                add_node(self.solution_node, solution=True)

        if self.last_event.name == 'expanded':
            for node, successors in zip(self.last_expandeds,
                                        self.last_successors):
                add_node(node, expanded=True)
                for successor_node in successors:
                    add_edge_to_parent(successor_node,
                                       is_successor=True,
                                       parent=node)

        for node in self.current_fringe:
            add_node(node, in_fringe=True)
            while node is not None and node not in done:
                if node.parent is not None:
                    add_edge_to_parent(node)
                else:
                    add_node(node)

                done.add(node)
                node = node.parent

        for node_id in sorted(graph_nodes.keys()):
            graph.add_node(graph_nodes[node_id])
        for node_id, parent_id in sorted(graph_edges.keys()):
            graph.add_edge(graph_edges[node_id, parent_id])

        graph.write(image_path, format=image_format)
コード例 #56
0
ファイル: viz.py プロジェクト: TiagodePAlves/MC202
class Grafo:
    def __init__(self, vertices, use_pydot=False):
        self.__verts__ = vertices
        self.__adjmx__ = [[] for i in range(vertices)]
        self.__arest__ = 0
        if use_pydot:
            self.__graph__ = Dot(graph_type='graph')
            for i in range(vertices):
                self.__graph__.add_node(Node(str(i)))
        else:
            self.__graph__ = None

    def aresta(self, v1, v2):
        if v2 in self.__adjmx__[v1]:
            return False
        self.__adjmx__[v1].append(v2)
        self.__adjmx__[v2].append(v1)
        self.__arest__ = self.__arest__ + 1
        if self.__graph__ is not None:
            self.__graph__.add_edge(Edge(str(v1), str(v2)))
        return True

    def aresta_aleat(self):
        done = False
        while not done:
            v1 = __random_gen__.randint(self.__verts__)
            v2 = v1
            while v2 == v1:
                v2 = __random_gen__.randint(self.__verts__)
            done = self.aresta(v1, v2)

    def conecta(self, alfa):
        max_art = (self.__verts__ * (self.__verts__ - 1)) // 2
        arestas = round(alfa * max_art)
        for _ in range(arestas):
            self.aresta_aleat()

    def show(self):
        if self.__graph__ is not None:
            from IPython.display import Image
            return Image(self.__graph__.create(format='png'))

    def contemC4(self):
        for v in range(self.__verts__):
            tem_caminho = [False] * self.__verts__

            for w in self.__adjmx__[v]:
                for u in self.__adjmx__[w]:
                    if u != v:
                        if tem_caminho[u]:
                            return v
                        else:
                            tem_caminho[u] = True
        return -1

    def testeC4(self, vert):
        def mostra_C4(v):
            if self.__graph__ is not None:
                for i in range(len(v)):
                    self.__graph__.get_node(str(v[i]))[0].set_style('filled')
                    self.__graph__.get_edge(str(v[i]), str(
                        v[(i + 1) % len(v)]))[0].set_style('bold')

        for v in self.__adjmx__[vert]:
            for w in self.__adjmx__[v]:
                if w != vert:
                    for u in self.__adjmx__[w]:
                        if u != vert and u != v:
                            for z in self.__adjmx__[u]:
                                if z == vert:
                                    mostra_C4([vert, v, w, u])
                                    return True
        return False
コード例 #57
0
def save_automaton_to_file(automaton,
                           path="LearnedModel",
                           file_type='dot',
                           display_same_state_trans=True,
                           visualize=False,
                           round_floats=None):
    """
    The Standard of the automata strictly follows the syntax found at: https://automata.cs.ru.nl/Syntax/Overview.
    For non-deterministic and stochastic systems syntax can be found on AALpy's Wiki.

    Args:

        automaton: automaton to be saved to file

        path: file in which automaton will be saved (Default value = "LearnedModel")

        file_type: Can be ['dot', 'png', 'svg', 'pdf'] (Default value = 'dot')

        display_same_state_trans: True, should not be set to false except from the visualization method
            (Default value = True)

        visualize: visualize the automaton

        round_floats: for stochastic automata, round the floating point numbers to defined number of decimal places

    Returns:

    """
    assert file_type in file_types
    if file_type == 'dot' and not display_same_state_trans:
        print("When saving to file all transitions will be saved")
        display_same_state_trans = True
    automaton_type = automaton_types[automaton.__class__]

    graph = Dot(path, graph_type='digraph')
    for state in automaton.states:
        graph.add_node(_get_node(state, automaton_type))

    for state in automaton.states:
        _add_transition_to_graph(graph, state, automaton_type,
                                 display_same_state_trans, round_floats)

    # add initial node
    graph.add_node(Node('__start0', shape='none', label=''))
    graph.add_edge(Edge('__start0', automaton.initial_state.state_id,
                        label=''))

    if file_type == 'string':
        return graph.to_string()
    else:
        try:
            graph.write(path=f'{path}.{file_type}',
                        format=file_type if file_type != 'dot' else 'raw')
            print(f'Model saved to {path}.{file_type}.')

            if visualize and file_type in {'pdf', 'png', 'svg'}:
                try:
                    import webbrowser
                    abs_path = os.path.abspath(f'{path}.{file_type}')
                    path = f'file:///{abs_path}'
                    webbrowser.open(path)
                except OSError:
                    traceback.print_exc()
                    print(f'Could not open the file {path}.{file_type}.',
                          file=sys.stderr)
        except OSError:
            traceback.print_exc()
            print(f'Could not write to the file {path}.{file_type}.',
                  file=sys.stderr)
コード例 #58
0
    def save_ex(self, filename, format='dot'):
        """Save basic block graph into a file.
        """
        fontname = 'Ubuntu Mono'
        # fontname = 'DejaVu Sans Mono'
        # fontname = 'DejaVu Sans Condensed'
        # fontname = 'DejaVu Sans Light'
        # fontname = 'Liberation Mono'
        # fontname = 'DejaVu Serif Condensed'
        # fontname = 'Ubuntu Condensed'

        node_format = {
            'shape': 'plaintext',
            'rankdir': 'LR',
            'fontname': fontname,
            'fontsize': 9.0,
            'penwidth': 0.5,
            # 'style'     : 'filled',
            # 'fillcolor' : 'orange',
        }

        edge_format = {
            'fontname': fontname,
            'fontsize': 8.0,
            'penwidth': 0.5,
            'arrowsize': 0.6,
            'arrowhead': 'vee',
        }

        edge_colors = {
            'direct': 'blue',
            'indirect': 'red',
        }

        label_fmt = '<'
        label_fmt += '<table border="1.0" cellborder="0" cellspacing="1" cellpadding="0" valign="middle">'
        label_fmt += '  <tr><td align="center" cellpadding="1" port="enter"></td></tr>'
        label_fmt += '  <tr><td align="left" cellspacing="1">{label}</td></tr>'
        label_fmt += '  <tr><td align="center" cellpadding="1" port="exit" ></td></tr>'
        label_fmt += '</table>'
        label_fmt += '>'

        try:
            dot_graph = Dot(graph_type="digraph",
                            rankdir="TB",
                            splines="ortho",
                            nodesep=1.2)

            # add nodes
            nodes = {}
            for cfg_addr in self._graph.node.keys():
                if cfg_addr != "unknown":
                    if cfg_addr in self._cfg_by_addr and not isinstance(
                            self._cfg_by_addr[cfg_addr],
                            str) and self._cfg_by_addr[cfg_addr].name:
                        cfg_label = self._cfg_by_addr[cfg_addr].name
                    else:
                        cfg_label = "sub_{:x}".format(cfg_addr)
                else:
                    cfg_label = "unknown"

                label = label_fmt.format(label=cfg_label)

                nodes[cfg_addr] = Node(cfg_addr, label=label, **node_format)

                dot_graph.add_node(nodes[cfg_addr])

            # add edges
            for cfg_src_addr in self._graph.node.keys():
                for cfg_dst_addr in self._edges.get(cfg_src_addr, []):
                    if cfg_dst_addr == "unknown":
                        branch_type = "indirect"
                    else:
                        branch_type = "direct"

                    dot_graph.add_edge(
                        Edge(nodes[cfg_src_addr],
                             nodes[cfg_dst_addr],
                             color=edge_colors[branch_type],
                             **edge_format))

            dot_graph.write("{}.{}".format(filename, format), format=format)
        except Exception as err:
            logger.error("Failed to save basic block graph: %s (%s)",
                         filename,
                         format,
                         exc_info=True)
コード例 #59
0
def plot_cfg(cfg,
             fname,
             format="png",
             path=None,
             asminst=False,
             vexinst=False,
             func_addr=None,
             remove_imports=True,
             remove_path_terminator=True,
             debug_info=False):

    dot_graph = Dot(graph_type="digraph", rankdir="TB")

    nodes = {}
    blocks = {}
    nmap = {}
    nidx = 0

    ccfg_graph = networkx.DiGraph(cfg.graph)
    filtered_addr = set()
    if func_addr != None:
        for node in ccfg_graph.nodes():
            if node.function_address not in func_addr:
                ccfg_graph.remove_node(node)
                filtered_addr.add(node.addr)
    else:
        if remove_imports:
            eaddrs = import_addrs(cfg.project)
            for node in ccfg_graph.nodes():
                if node == None:
                    continue
                if node.addr in eaddrs:
                    try:
                        ccfg_graph.remove_node(node)
                        filtered_addr.add(node.addr)
                    except:
                        pass
                    for pnode in node.predecessors:
                        try:
                            ccfg_graph.remove_node(pnode)
                            filtered_addr.add(node.addr)
                        except:
                            pass

        if remove_path_terminator:
            for node in ccfg_graph.nodes():
                if hasattr(node, 'is_simprocedure') and hasattr(
                        node, 'simprocedure_name'
                ) and node.is_simprocedure and node.simprocedure_name == "PathTerminator":
                    ccfg_graph.remove_node(node)
                    filtered_addr.add(node.addr)

    node_path = []
    if path != None:
        trace = list(filter(lambda _: _ not in filtered_addr, path.addr_trace))
        for i in range(len(trace)):
            nodes_at_addr = cfg.get_all_nodes(trace[i])
            if len(nodes_at_addr) == 1:
                node_path.append(nodes_at_addr[0])
            else:
                for n in nodes_at_addr:
                    if node_matches(n, trace[:i + 1]):
                        node_path.append(n)
                        break

    active_nodes = set(node_path)
    active_edges = {}

    if len(node_path) > 0:
        for s, t in zip(node_path[:-1], node_path[1:]):
            if s not in active_edges:
                active_edges[s] = set()
            active_edges[s].add(t)

    for node in sorted(filter(lambda _: _ != None, ccfg_graph.nodes()),
                       key=lambda _: _.addr):

        attributes = []
        if node.is_simprocedure:
            attributes.append("SIMP")
        if node.is_syscall:
            attributes.append("SYSC")
        if node.no_ret:
            attributes.append("NORET")
        if len(attributes) == 0:
            blocks[node.addr] = cfg.project.factory.block(addr=node.addr,
                                                          max_size=node.size)
        else:
            blocks[node.addr] = cfg.project.factory.block(addr=node.addr)

        nmap[node] = nidx
        nidx += 1
        label = "{{<f0> {:#08x} ({:#08x}) {} {}".format(
            node.addr, node.function_address, node.name, ' '.join(attributes))
        if not node.is_simprocedure:
            if asminst:
                label += "| " + print_asm(blocks[node.addr])
            if vexinst:
                label += "| " + print_vex(blocks[node.addr])
        if debug_info:
            label += "| " + node_debug_info(node)
        label += "}}"

        penwidth = '1'
        if node in active_nodes:
            penwidth = '3'

        if not node.is_simprocedure:
            nodes[nmap[node]] = Node(nmap[node],
                                     label=label,
                                     penwidth=penwidth,
                                     **default_node_attributes)
        else:
            if node.simprocedure_name == "PathTerminator":
                nodes[nmap[node]] = Node(nmap[node],
                                         label=label,
                                         penwidth=penwidth,
                                         style="filled",
                                         fillcolor="#dd5555",
                                         **default_node_attributes)
            else:
                nodes[nmap[node]] = Node(nmap[node],
                                         label=label,
                                         penwidth=penwidth,
                                         style="filled",
                                         fillcolor="#dddddd",
                                         **default_node_attributes)
        dot_graph.add_node(nodes[nmap[node]])

    #for n in ccfg.graph.nodes():
    #    nn = filter(lambda node: node in nmap, cfg.get_all_nodes(n.addr))
    #    if len(nn)>1:
    #        for n in nn[1:]:
    #            dot_graph.add_edge(Edge(nodes[nmap[nn[0]]],nodes[nmap[n]],style="invis"))

    edgeprop = {}
    for source, target in ccfg_graph.edges():
        if source == None or target == None:
            continue
        key = (nmap[source], nmap[target])

        edge_style = 'solid'
        if not key in edgeprop:
            if blocks[source.addr].vex.jumpkind == 'Ijk_Boring':
                if len(blocks[source.addr].vex.constant_jump_targets) > 1:
                    if len(blocks[source.addr].vex.next.constants) == 1:
                        if target.addr == blocks[
                                source.addr].vex.next.constants[0].value:
                            color = EDGECOLOR_CONDITIONAL_FALSE
                        else:
                            color = EDGECOLOR_CONDITIONAL_TRUE
                    else:
                        color = EDGECOLOR_UNKNOWN
                else:
                    color = EDGECOLOR_UNCONDITIONAL
            elif blocks[source.addr].vex.jumpkind == 'Ijk_Call':
                color = EDGECOLOR_CALL
                if len(blocks[source.addr].vex.next.constants) == 1 and blocks[
                        source.
                        addr].vex.next.constants[0].value != target.addr:
                    edge_style = 'dotted'
            elif blocks[source.addr].vex.jumpkind == 'Ijk_Ret':
                color = EDGECOLOR_RET
            else:
                color = EDGECOLOR_UNKNOWN

            penwidth = '1'
            if source in active_edges and target in active_edges[source]:
                penwidth = '3'

            edgeprop[key] = {
                "color": color,
                "penwidth": penwidth,
            }

        dot_graph.add_edge(
            Edge(nodes[nmap[source]],
                 nodes[nmap[target]],
                 color=edgeprop[key]["color"],
                 style=edge_style,
                 penwidth=edgeprop[key]["penwidth"],
                 **default_edge_attributes))

    dot_graph.write("{}.{}".format(fname, format), format=format)