def get_meta_path_count(rest_id1, rest_id2, k): client = Neo4j_helper.get_client() meta_graph = {} Type = 'UCL' def set_path_count(T, N): query = ('MATCH path=(S:B {id:"%s"})-%s-(E:U {id:"%s"}) ' 'RETURN DISTINCT count(path)' '' % (rest_id1, N, rest_id2)) num = client.run(query).evaluate() T += ('B', ) meta_graph[T] = num # only B-U-(..)-U-B would be taken into account for kth in range(1, k + 1): nodeString = '[]' typeString = ('B', ) for i in range(1, kth): nodeString += ('-(:U)-[]') typeString += ('U', ) set_path_count(typeString, nodeString) if kth == 1: for a in Type: nodeString = '[]' typeString = ('B', ) typeString += (a, ) nodeString += ('-(:%s)-[]' % a) set_path_count(typeString, nodeString) return meta_graph
def main(): '''\ %prog [options] ''' client = Neo4j_helper.get_client() graph_db_importer.show_statistics(client) return 0
def get_meta_path_count(rest_id1, rest_id2, k): def get_label(node): for label in node.labels(): return label def get_meta_path(path): meta_path = [] for i in range(kth): meta_path.append(get_label(path[c[i]])) return tuple(meta_path) client = Neo4j_helper.get_client() meta_paths = {} c = list(string.ascii_lowercase) for kth in range(1, k+1): nodeString = '[]' returnString = [] typeStringU = ('U',) for i in range(kth): nodeString += ('-(%s)-[]' % c[i]) returnString.append(c[i]) returnString = ', '.join(returnString) s = ('MATCH (:B {id:"%s"})-%s-(:B {id:"%s"}) ' 'RETURN DISTINCT %s' '' % (rest_id1, nodeString, rest_id2, returnString)) for path in client.run(s).data(): meta_path = get_meta_path(path) if meta_path not in meta_paths: meta_paths[meta_path] = 1 continue meta_paths[meta_path] += 1 return meta_paths
def get_number_com_customers(rest_id1, rest_id2): client = Neo4j_helper.get_client() s = ("MATCH (a:B {id: '%s'})-[]-(c:U)-[]-(b:B {id: '%s'}) " "RETURN DISTINCT a, b, c" "" % (rest_id1, rest_id2)) count = len(client.run(s).data()) return count
def get_local_network(rest_id1, k): def get_info(node): for label in node.labels(): break return node['id'], node['name'], label def update_nodes(node, id2seq, nodes, on_path='inner'): id_, name, label = get_info(node) if id_ not in id2seq: seq = len(id2seq) + 1 id2seq[id_] = seq nodes[seq] = {'id': id_, 'type': label} return id2seq[id_] client = Neo4j_helper.get_client() id2seq = {} nodes = {} edges = set([]) for kth in range(1, k + 1): nodeString = '' returnString = '' c = list(string.ascii_lowercase) for i in range(kth): nodeString += ('-[]-(%s)' % c[i]) returnString += (', %s' % c[i]) s = ('MATCH (S:B {id:"%s"})%s' 'RETURN DISTINCT S%s' '' % (rest_id1, nodeString, returnString)) for data in client.run(s).data(): seq_S = update_nodes( data['S'], id2seq, nodes, ) seq_a = update_nodes(data['a'], id2seq, nodes) edges.add((seq_S, seq_a)) if (kth > 1): i = 1 while i < kth: seq_c1 = update_nodes(data[c[i - 1]], id2seq, nodes) seq_c2 = update_nodes(data[c[i]], id2seq, nodes) edges.add((seq_c1, seq_c2)) if i + 1 < kth: seq_c3 = update_nodes(data[c[i + 1]], id2seq, nodes) edges.add((seq_c2, seq_c3)) i += 2 return nodes, edges
def main(): '''\ %prog [options] ''' graph = Neo4j_helper.get_client() # graph.delete_all() # create_nodes(graph, settings.GRAPH_FILE) # create_indexes(graph) # create_edges(graph, settings.GRAPH_FILE) show_statistics(graph) return 0
def get_paths(rest_id1, rest_id2, k): def get_info(node): for label in node.labels(): break return node['id'], node['name'], label def update_nodes(node, id2seq, nodes, on_path='inner'): id_, name, label = get_info(node) if id_ not in id2seq: seq = len(id2seq) + 1 id2seq[id_] = seq nodes[seq] = { 'name': name, 'type': label, 'id': id_, 'on_path': on_path } return id2seq[id_] client = Neo4j_helper.get_client() id2seq = {} nodes = {} edges = set([]) for kth in range(1, k + 1): nodeString = '[]' returnString = '' c = list(string.ascii_lowercase) for i in range(kth): nodeString += ('-(%s)-[]' % c[i]) returnString += (', %s' % c[i]) s = ('MATCH (S:B {id:"%s"})-%s-(E:B {id:"%s"}) ' 'RETURN DISTINCT S, E%s' '' % (rest_id1, nodeString, rest_id2, returnString)) for data in client.run(s).data(): seq_S = update_nodes(data['S'], id2seq, nodes, on_path='source') seq_E = update_nodes(data['E'], id2seq, nodes, on_path='destination') seq_a = update_nodes(data['a'], id2seq, nodes) edges.add((seq_S, seq_a)) if kth == 1: # append edges to the end node if k = 1 edges.add((seq_a, seq_E)) i = 1 while i < kth: seq_c1 = update_nodes(data[c[i - 1]], id2seq, nodes) seq_c2 = update_nodes(data[c[i]], id2seq, nodes) edges.add((seq_c1, seq_c2)) # append edge to next node if there is still nodes left if i + 1 < kth: seq_c3 = update_nodes(data[c[i + 1]], id2seq, nodes) edges.add((seq_c2, seq_c3)) # append edge to end node if i is the last node else: edges.add((seq_c2, seq_E)) i += 2 # append edge to end node if i+1 is the last node if i == k: edges.add((seq_c3, seq_E)) return nodes, edges
def get_customers(rest_id): client = Neo4j_helper.get_client() s = "MATCH (a:B {id:'%s'})-[]-(c:U) RETURN DISTINCT a, c" % rest_id customers = set([a['c']['id'] for a in client.run(s).data()]) return customers
def get_meta_path_count(rest_id1, rest_id2, k): client = Neo4j_helper.get_client() meta_graph = {} Type = 'UCL' def set_path_count(T, N): query = ('MATCH path=(S:B {id:"%s"})-%s-(E:U {id:"%s"}) ' 'RETURN DISTINCT count(path)' '' % (rest_id1, N, rest_id2)) num = graph.run(query).evaluate() T += ('B', ) meta_graph[T] = num for kth in range(1, k + 1): nodeString = '[]' typeString = ('B', ) if kth == 2: nodeString += ('-(:U)-[]-(:U)-[]') typeString += ( 'U', 'U', ) set_path_count(typeString, nodeString) else: for a in Type: nodeString = '[]' typeString = ('B', ) typeString += (a, ) nodeString += ('-(:%s)-[]' % a) if kth == 1: set_path_count(typeString, nodeString) else: if a == 'U': nextType = 'BU' else: nextType = 'B' temp_type_a = typeString temp_node_a = nodeString for b in nextType: typeString = temp_type_a nodeString = temp_node_a typeString += (b, ) nodeString += ('-(:%s)-[]' % b) temp_type_b = typeString temp_node_b = nodeString if kth != 4: if b == 'B': nextType = Type else: nextType = 'U' else: if b == 'B': nextType = 'U' else: nextType = 'BU' for c in nextType: typeString = temp_type_b nodeString = temp_node_b typeString += (c, ) nodeString += ('-(:%s)-[]' % c) if kth == 3: set_path_count(typeString, nodeString) else: if kth == 4: if c == 'U': nextType = 'U' else: nextType = Type else: if c == 'U': nextType = 'UB' else: nextType = 'U' temp_node_c = nodeString temp_type_c = typeString for d in nextType: typeString = temp_type_c nodeString = temp_node_c typeString += (d, ) nodeString += ('-(:%s)-[]' % d) if kth == 4: set_path_count(typeString, nodeString) else: if d == 'U': nextType = 'UB' else: nextType = Type temp_node_d = nodeString temp_type_d = typeString for e in nextType: typeString = temp_type_d nodeString = temp_node_d typeString += (e, ) nodeString += ('-(:%s)-[]' % e) set_path_count( typeString, nodeString) return meta_graph