def walk(self): ''' ''' file = open('log1.txt', 'w') client = DefaultGraphAccessor() iterator = 0 possibility_list = [0] * RandomWalk.max_id adjacent_node_list = [0] * RandomWalk.max_id file.writelines("begin: " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '\n') file.flush() while iterator < self.generationNumber: file.writelines("iteration: " + str(iterator) + " " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '\n') file.flush() # print str(iterator) + " " + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) current_node_index = 1 step_length_list = [0] * RandomWalk.max_id for i in range(0, self.stepNumber): if (current_node_index > 0 and current_node_index < 7520) or current_node_index > 7524: if adjacent_node_list[current_node_index - 1] == 0: adjacent_node = client.get_adjacent_node_id_list(current_node_index) adjacent_node_list[current_node_index - 1] = adjacent_node else: adjacent_node = adjacent_node_list[current_node_index - 1] next_node_index = adjacent_node[random.randint(0, len(adjacent_node) - 1)] # print str(iterator) + " " + str(i) + " " + str(len(adjacent_node)) + " " + str( # next_node_index) if (next_node_index > 1 and next_node_index < 7520) or next_node_index > 7524: current_node_index = next_node_index if step_length_list[next_node_index - 1] == 0 and next_node_index != 1: step_length_list[next_node_index - 1] = i + 1 possibility_list[next_node_index - 1] += 1 - ( step_length_list[next_node_index - 1] / float(self.stepNumber)) iterator += 1 file.writelines("end: " + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '\n') file.flush() file.close() for i in range(0, RandomWalk.max_id): possibility_list[i] /= self.generationNumber if possibility_list[i] > 0: print str(i) + " " + str(possibility_list[i])
class TestGraphClient(TestCase): graphClient = None def setUp(self): self.graphClient = DefaultGraphAccessor(GraphClient()) self.nodeCleaner = NodeCleaner() def test_get_max_id_for_node(self): self.assertEqual(self.graphClient.get_max_id_for_node(), 697753) def test_get_adjacent_node_id_list(self): self.assertEqual(self.graphClient.get_adjacent_node_id_list(66666666), []) correct = [64289, 52628, 62565] self.assertEqual(self.graphClient.get_adjacent_node_id_list(7899), correct) def test_get_node_name_by_id(self): self.assertEqual(self.graphClient.get_node_name_by_id(66666666), None) self.assertEqual(self.graphClient.get_node_name_by_id(3444), "Adobe Device Central") def test_expand_node_for_directly_adjacent_nodes_to_subgraph(self): # self.assertEqual(self.graphClient.expand_node_for_adjacent_nodes_to_subgraph(3444), # "Adobe Device Central") pass def test_find_by_alias_name_property_exactly_match_from_label_limit_one( self): self.assertEqual( self.graphClient.find_one_by_alias_name_property( "entity", "Adobe Device Central"), None) interface = self.graphClient.find_one_by_alias_name_property( "api", "Interface PrintGraphics") self.assertEqual(93008, self.graphClient.get_id_for_node(interface)) def test_find_by_alias_name_property(self): self.assertEqual( self.graphClient.find_by_alias_name_property( "entity", "Adobe Device Central"), []) interfaces = self.graphClient.find_by_alias_name_property( "api", "Interface PrintGraphics") self.assertEqual(len(interfaces), 1) self.assertEqual(93008, self.graphClient.get_id_for_node(interfaces[0])) def test_get_relation_by_relation_id(self): relation = self.graphClient.get_relation_by_relation_id(470129) self.assertIsNone(relation) relation = self.graphClient.get_relation_by_relation_id(122211) self.assertEqual(122211, self.graphClient.get_id_for_node(relation)) self.assertEqual( 91, self.graphClient.get_id_for_node(relation.start_node())) self.assertEqual(29390, self.graphClient.get_id_for_node(relation.end_node())) subgraph = self.graphClient.get_relations_between_two_nodes_in_subgraph( 246029, 246030) relations_json = [] for r in subgraph.relationships(): r = { "id": self.graphClient.get_id_for_node(relation), "name": relation.type(), "start_id": self.graphClient.get_start_id_for_relation(relation), "end_id": self.graphClient.get_end_id_for_relation(relation) } print r subgraph = self.graphClient.get_relations_between_two_nodes_in_subgraph( 246029, 246033) self.assertEqual(subgraph, None) def test_find_node_by_id(self): node = self.graphClient.find_node_by_id(5444) self.assertEqual(5444, self.graphClient.get_id_for_node(node)) def test_search_nodes_by_name(self): nodes = self.graphClient.search_nodes_by_name("java") count = 0 for n in nodes: count = count + 1 self.assertEqual(10, count) nodes = self.graphClient.search_nodes_by_name("String buffer()") count = 0 for n in nodes: count = count + 1 self.assertEqual(10, count) def test_search_nodes_by_name_in_subgraph(self): subgraph = self.graphClient.search_nodes_by_name_in_subgraph("java") count = 0 for n in subgraph.nodes(): count = count + 1 self.assertEqual(10, count) subgraph = self.graphClient.search_nodes_by_name_in_subgraph( "String buffer()") count = 0 if subgraph is not None: for n in subgraph.nodes(): count = count + 1 self.assertEqual(10, count) def test_get_relations_between_two_nodes_in_subgraph(self): subgraph = self.graphClient.get_relations_between_two_nodes_in_subgraph( 48, 3600) self.assertEqual(None, subgraph) subgraph = self.graphClient.get_relations_between_two_nodes_in_subgraph( 48, 3643) self.assertEqual(2, len(subgraph.nodes())) self.assertEqual(1, len(subgraph.relationships())) def test_get_relations_between_two_nodes(self): record_list = self.graphClient.get_relations_between_two_nodes( 48, 3600) count = 0 for n in record_list: count = count + 1 self.assertEqual(0, count) record_list = self.graphClient.get_relations_between_two_nodes_in_subgraph( 48, 3643) count = 0 for n in record_list: count = count + 1 self.assertEqual(1, count) def test_cleaner(self): node = self.graphClient.find_node_by_id(444) self.assertEqual(self.nodeCleaner.get_clean_node_name(node), "fake news") node = self.graphClient.find_node_by_id(4444) self.assertEqual(self.nodeCleaner.get_clean_node_name(node), "") self.assertEqual(self.graphClient.get_id_for_node(Node("lll", a=3)), -1) self.assertEqual(self.graphClient.get_id_for_node(node), 4444) def test_get_shortest_path_to_name(self): name = self.graphClient.get_node_name_by_id(8000) subraph = self.graphClient.get_shortest_path_to_name_in_subgraph( 444, name) print subraph def test_get_shortest_path(self): record_list = self.graphClient.get_shortest_path(444, 8000, max_degree=2) self.assertEqual(0, count_record_list(record_list)) record_list = self.graphClient.get_shortest_path(444, 8000) self.assertNotEqual(None, record_list) self.assertEqual(1, count_record_list(record_list)) subgraph = self.graphClient.get_shortest_path_in_subgraph(444, 8000, max_degree=2) self.assertEqual(None, subgraph) subgraph = self.graphClient.get_shortest_path_in_subgraph(444, 8000, max_degree=6) self.assertNotEqual(None, subgraph) self.assertEqual(len(subgraph.nodes()), 7) self.assertEqual(len(subgraph.relationships()), 6) print subgraph def test_get_newest_nodes(self): node_list = self.graphClient.get_newest_nodes(10) self.assertEqual(10, len(node_list)) print(node_list) graphJsonParser = GraphJsonParser() returns = graphJsonParser.parse_node_list_to_json(node_list) print(returns)