Exemplo n.º 1
0
    def graph_query_vertices(self, query_dict=None, root_id=None, depth=None):

        if not root_id:
            root_id = self.graph.root_id
        root_data = self.graph._g.node[root_id]

        if not query_dict:
            match_func = lambda item: True
        else:
            match_func = create_predicate(query_dict)

        if not match_func(root_data):
            LOG.info('graph_query_vertices: root ' + str(root_id) +
                     ' does not match filter ' + str(query_dict) + ')')
            return None

        n_result = []
        visited_nodes = set()
        n_result.append(root_id)
        nodes_q = [(root_id, 0)]
        while nodes_q:
            node_id, curr_depth = nodes_q.pop(0)
            if (node_id in visited_nodes) or (depth and curr_depth >= depth):
                continue
            visited_nodes.add(node_id)
            (n_list, e_list) = self.graph._neighboring_nodes_edges_query(
                node_id, vertex_predicate=match_func)
            n_result.extend([v_id for v_id, data in n_list])
            nodes_q.extend([(v_id, curr_depth + 1) for v_id, data in n_list])

        graph = NXGraph('graph')
        graph._g = self.graph._g.subgraph(n_result)
        return graph