def graph_query_vertices(self, query_dict=None, root_id=None, depth=None, direction=Direction.BOTH, edge_query_dict=None): graph = NXGraph('graph') 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 edge_query_dict: edge_match_func = lambda item: True else: edge_match_func = create_predicate(edge_query_dict) if not match_func(root_data): LOG.info('graph_query_vertices: root %s does not match filter %s', str(root_id), str(query_dict)) return graph 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, direction=direction, vertex_predicate=match_func, edge_predicate=edge_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._g = self.graph._g.subgraph(n_result) LOG.debug('graph_query_vertices: find graph: nodes %s, edges %s', str(graph._g.nodes(data=True)), str(graph._g.edges(data=True))) LOG.debug('graph_query_vertices: real graph: nodes %s, edges %s', str(self.graph._g.nodes(data=True)), str(self.graph._g.edges(data=True))) return graph
def graph_query_vertices(self, query_dict=None, root_id=None, depth=None, direction=Direction.BOTH, edge_query_dict=None): graph = NXGraph('graph') if not root_id: root_id = self.graph.root_id root_data = self.graph._g.node[root_id] match_func = create_predicate(query_dict) if query_dict else None edge_match_func = create_predicate(edge_query_dict) \ if edge_query_dict else None if match_func and not match_func(root_data): LOG.info('graph_query_vertices: root %s does not match filter %s', str(root_id), str(query_dict)) return graph n_result = [] visited_nodes = set() n_result.append((root_id, self.graph.get_vertex(root_id).properties)) e_result = [] 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, direction=direction, vertex_predicate=match_func, edge_predicate=edge_match_func) n_result.extend(n_list) e_result.extend(e_list) nodes_q.extend([(v_id, curr_depth + 1) for v_id, data in n_list]) graph = NXGraph(graph.name, graph.root_id, vertices=self._vertex_result_to_list(n_result), edges=self._edge_result_to_list(e_result)) LOG.debug('graph_query_vertices: find graph: nodes %s, edges %s', str(graph._g.nodes(data=True)), str(graph._g.edges(data=True))) LOG.debug('graph_query_vertices: real graph: nodes %s, edges %s', str(self.graph._g.nodes(data=True)), str(self.graph._g.edges(data=True))) return graph
def graph_query_vertices(self, query_dict=None, root_id=None, depth=None, direction=Direction.BOTH, edge_query_dict=None): graph = self._create_new_graph('graph') if not root_id: root_id = self.graph.root_id root_data = self.graph._g.node[root_id] match_func = create_predicate(query_dict) if query_dict else None edge_match_func = create_predicate(edge_query_dict) \ if edge_query_dict else None if match_func and not match_func(root_data): LOG.info('graph_query_vertices: root %s does not match filter %s', str(root_id), str(query_dict)) return graph n_result = [] visited_nodes = set() n_result.append((root_id, self.graph.get_vertex(root_id).properties)) e_result = [] 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, direction=direction, vertex_predicate=match_func, edge_predicate=edge_match_func) n_result.extend(n_list) e_result.extend(e_list) nodes_q.extend([(v_id, curr_depth + 1) for v_id, data in n_list]) graph = self._create_new_graph( graph.name, graph.root_id, vertices=self._vertex_result_to_list(n_result), edges=self._edge_result_to_list(e_result)) LOG.debug('graph_query_vertices: find graph: nodes %s, edges %s', str(graph._g.nodes(data=True)), str(graph._g.edges(data=True))) LOG.debug('graph_query_vertices: real graph: nodes %s, edges %s', str(self.graph._g.nodes(data=True)), str(self.graph._g.edges(data=True))) return graph
def get_vertices_count(self, query_dict, group_by): vertices_counts = defaultdict(int) match_func = create_predicate(query_dict) if query_dict else None for node, node_data in self._g.nodes(data=True): if match_func is None or match_func(node_data): vertices_counts[node_data.get(group_by, '')] += 1 return vertices_counts
def graph_query_vertices(self, root_id, query_dict=None, depth=None, direction=Direction.BOTH, edge_query_dict=None): graph = self._create_new_graph('graph') root_data = self.graph._g.nodes[root_id] match_func = create_predicate(query_dict) if query_dict else None edge_match_func = create_predicate(edge_query_dict) \ if edge_query_dict else None if match_func and not match_func(root_data): LOG.info('graph_query_vertices: root %s does not match filter %s', root_id, query_dict) return graph n_result = [] visited_nodes = set() n_result.append((root_id, self.graph.get_vertex(root_id).properties)) e_result = [] 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, direction=direction, vertex_predicate=match_func, edge_predicate=edge_match_func) n_result.extend(n_list) e_result.extend(e_list) nodes_q.extend([(v_id, curr_depth + 1) for v_id, data in n_list]) graph = self._create_new_graph(graph.name) for v_id, data in n_result: graph._g.add_node(v_id, **data) for source_id, target_id, label, data in e_result: graph._g.add_edge(source_id, target_id, label, **data) return graph
def get_vertices_ids(self, query_dict): if not query_dict: return list(self._g.nodes()) vertices_ids = set() match_func = create_predicate(query_dict) for node, node_data in self._g.nodes(data=True): if match_func(node_data): vertices_ids.add(node) return vertices_ids
def get_vertices(self, vertex_attr_filter=None, query_dict=None): def check_vertex(vertex_data): return check_filter(vertex_data[1], vertex_attr_filter) if not query_dict: items = filter(check_vertex, self._g.nodes_iter(data=True)) return [vertex_copy(node, node_data) for node, node_data in items] elif not vertex_attr_filter: vertices = [] match_func = create_predicate(query_dict) for node, node_data in self._g.nodes_iter(data=True): v = vertex_copy(node, node_data) if match_func(v): vertices.append(v) return vertices else: return []
def graph_query_vertices(self, query_dict=None, root_id=None, depth=None, direction=Direction.BOTH): 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 %s does not match filter %s', str(root_id), 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, direction=direction, 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