def read_acdfg(filename):
    try:
        f = open(filename, 'rb')
        acdfg = ProtoAcdfg()
        acdfg.ParseFromString(f.read())
        acdfg_obj = Acdfg(acdfg)
        for dNode in acdfg.data_node:
            data_node_obj = DataNode(int(getattr(dNode, 'id')), dNode.name, getattr(dNode, 'type'), dNode.data_type)
            acdfg_obj.add_node(data_node_obj)
        for mNode in acdfg.method_node:
            arg_ids = mNode.argument
            arg_list = [acdfg_obj.get_node_from_id(j) for j in arg_ids]
            if mNode.invokee:
                rcv = acdfg_obj.get_node_from_id(mNode.invokee)
            else:
                rcv = None
            method_node_obj = MethodNode(int(mNode.id), mNode.name, rcv, arg_list)
            acdfg_obj.add_node(method_node_obj)
        for rNode in acdfg.misc_node:
            misc_node_obj = Node(NodeType.regular_node, int(rNode.id))
            acdfg_obj.add_node(misc_node_obj)
        for ctrl_edge in acdfg.control_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, ctrl_edge)
            cedge_obj = ControlEdge(ctrl_edge.id, src, tgt)
            acdfg_obj.add_edge(cedge_obj)
        for dedge in acdfg.def_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, dedge)
            dedge_obj = ControlEdge(dedge.id, src, tgt)
            acdfg_obj.add_edge(dedge_obj)
        for uedge in acdfg.use_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, uedge)
            uedge_obj = UseEdge(uedge.id, src, tgt)
            acdfg_obj.add_edge(uedge_obj)
        for tedge in acdfg.trans_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, tedge)
            tedge_obj = TransitiveEdge(tedge.id, src, tgt)
            acdfg_obj.add_edge(tedge_obj)
        f.close()
        return acdfg_obj
    except IOError:
        print('Could not open: ', filename, 'for reading in binary mode.')
        assert False
def read_acdfg(filename):
    try:
        f = open(filename, 'rb')
        acdfg = ProtoAcdfg()
        acdfg.ParseFromString(f.read())
        acdfg_obj = Acdfg(acdfg)
        for dNode in acdfg.data_node:
            data_node_obj = DataNode(int(getattr(dNode, 'id')), dNode.name, getattr(dNode, 'type'), dNode.data_type)
            acdfg_obj.add_node(data_node_obj)
        for mNode in acdfg.method_node:
            arg_ids = mNode.argument
            arg_list = [acdfg_obj.get_node_from_id(j) for j in arg_ids]
            if mNode.invokee:
                rcv = acdfg_obj.get_node_from_id(mNode.invokee)
            else:
                rcv = None
            method_node_obj = MethodNode(int(mNode.id), mNode.name, rcv, arg_list)
            acdfg_obj.add_node(method_node_obj)
        for rNode in acdfg.misc_node:
            misc_node_obj = Node(NodeType.regular_node, int(rNode.id))
            acdfg_obj.add_node(misc_node_obj)
        for ctrl_edge in acdfg.control_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, ctrl_edge)
            cedge_obj = ControlEdge(ctrl_edge.id, src, tgt)
            acdfg_obj.add_edge(cedge_obj)
        for dedge in acdfg.def_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, dedge)
            dedge_obj = ControlEdge(dedge.id, src, tgt)
            acdfg_obj.add_edge(dedge_obj)
        for uedge in acdfg.use_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, uedge)
            uedge_obj = UseEdge(uedge.id, src, tgt)
            acdfg_obj.add_edge(uedge_obj)
        for tedge in acdfg.trans_edge:
            (src, tgt) = get_node_obj_from_ids(acdfg_obj, tedge)
            tedge_obj = TransitiveEdge(tedge.id, src, tgt)
            acdfg_obj.add_edge(tedge_obj)
        f.close()
        return acdfg_obj
    except IOError:
        print('Could not open: ', filename, 'for reading in binary mode.')
        assert False