Ejemplo n.º 1
0
    def test_remove_node(self):
        graph_data = GraphData()
        graph_data.create_index_on_property("qualified_name", "alias")

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {
            "qualified_name": "ArrayList.clear",
            "alias": ["clear"]
        })
        graph_data.add_node({"method"}, {
            "qualified_name": "List.clear",
            "alias": ["clear", "List.clear", "List clear"]
        })
        graph_data.add_relation(1, "related to", 2)
        graph_data.add_relation(1, "related to", 3)
        graph_data.add_relation(1, "related to", 4)
        graph_data.add_relation(2, "related to", 3)
        graph_data.add_relation(3, "related to", 4)

        result = graph_data.remove_node(node_id=1)
        self.assertIsNotNone(result)

        self.assertIsNone(graph_data.get_node_info_dict(node_id=1))
Ejemplo n.º 2
0
    def test_find_nodes_by_properties(self):
        graph_data = GraphData()
        graph_data.create_index_on_property("qualified_name", "alias")

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {
            "qualified_name": "ArrayList.clear",
            "alias": ["clear"]
        })
        graph_data.add_node({"method"}, {
            "qualified_name": "List.clear",
            "alias": ["clear", "List.clear", "List clear"]
        })

        match_nodes = graph_data.find_nodes_by_property(
            property_name="qualified_name", property_value="List.clear")
        print(match_nodes)
        self.assertIsNotNone(match_nodes)
        self.assertEqual(len(match_nodes), 1)
        self.assertEqual(match_nodes[0][GraphData.DEFAULT_KEY_NODE_ID], 5)

        match_nodes = graph_data.find_nodes_by_property(property_name="alias",
                                                        property_value="clear")

        print(match_nodes)
        self.assertIsNotNone(match_nodes)
        self.assertEqual(len(match_nodes), 2)
Ejemplo n.º 3
0
    def test_update_node(self):
        graph_data = GraphData()
        graph_data.add_node({"method", "entity"}, {
            "qualified_name": "ArrayList.remove",
            "version": "1.0"
        })
        graph_data.update_node_property_by_node_id(
            1, {
                "qualified_name": "ArrayList.remove",
                "version": "2.0",
                "parameter_num": 1
            })
        new_property = {
            "qualified_name": "ArrayList.remove",
            "version": "2.0",
            "parameter_num": 1
        }
        new_label = {"method", "entity"}
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_PROPERTIES], new_property)
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_LABELS], new_label)

        graph_data.update_node_property_value_by_node_id(
            1, "qualified_name", "ArrayList.add")
        new_property = {
            "qualified_name": "ArrayList.add",
            "version": "2.0",
            "parameter_num": 1
        }
        new_label = {"method", "entity"}
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_PROPERTIES], new_property)
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_LABELS], new_label)

        graph_data.update_node_by_node_id(1, {"class", "entity"}, {
            "qualified_name": "ArrayList.add",
            "version": "2.0",
            "parameter_num": 2
        })
        new_property = {
            "qualified_name": "ArrayList.add",
            "version": "2.0",
            "parameter_num": 2
        }
        new_label = {"method", "entity", "class"}
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_PROPERTIES], new_property)
        self.assertEqual(
            graph_data.get_node_info_dict(1)[
                GraphData.DEFAULT_KEY_NODE_LABELS], new_label)
Ejemplo n.º 4
0
    def test_save_and_load(self):
        graph_data = GraphData()
        graph_data.create_index_on_property("qualified_name", "alias")

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})

        graph_data.save("test.graph")
        graph_data: GraphData = GraphData.load("test.graph")
        self.assertEqual(graph_data.get_node_num(), 2)
Ejemplo n.º 5
0
    def export_all_graph_data(self, graph, node_label):
        accessor = DataExporterAccessor(graph=graph)
        nodes = accessor.get_all_nodes(node_label=node_label)
        graph_data = GraphData()

        for node in nodes:
            labels = [label for label in node.labels]
            graph_data.add_node(node_id=node.identity, node_labels=labels, node_properties=dict(node))

        print("load entity complete, num=%d" % len(nodes))
        relations = accessor.get_all_relation(node_label=node_label)
        print("load relation complete,num=%d" % len(relations))
        graph_data.set_relations(relations=relations)

        return graph_data
Ejemplo n.º 6
0
    def get_graph(self):
        graph_data = GraphData()

        graph_data.add_node({"method"}, {
            "qualified_name": "ArrayList.add()",
            "name": "ArrayList.add",
            "alias": ["ArrayList.add1", "add()", "add"]
        })
        graph_data.add_node({"method"}, {
            "qualified_name": "ArrayList.add(int)",
            "name": "ArrayList.add",
            "alias": ["ArrayList.add2", "add()", "add"]
        })

        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.clear"})

        return graph_data
Ejemplo n.º 7
0
    def test_merge(self):
        graph_data = GraphData()
        graph_data.create_index_on_property("qualified_name", "alias")

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {
            "qualified_name": "ArrayList.clear",
            "alias": ["clear"]
        })

        graph_data.merge_node(node_labels=["method", "merge"],
                              node_properties={
                                  "qualified_name": "ArrayList.clear",
                                  "alias": ["clear", "clear1"]
                              },
                              primary_property_name="qualified_name")
Ejemplo n.º 8
0
    def node_csv2graphdata(file, graph: GraphData = None, csv_id=GraphData.DEFAULT_KEY_NODE_ID, csv_labels =GraphData.DEFAULT_KEY_NODE_LABELS):
        '''
        :param file:  节点csv文件的全路径
        :param graph: 将要导入csv的graph,将要导入的graphdata,没有传参则新建
        :param csv_id: csv文件id所在列的列名,默认是id
        :param csv_labels: csv文件labels所在列的列名,默认是labels
        :return: 导入节点后的graphdata
        '''

        if not graph:
            graph = GraphData()
        count = 0
        with open(file, 'r', encoding="utf-8") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                row = dict(row)
                node_id = None
                node_labels = set([])
                node_dic = {}
                for row_k, row_v in row.items():
                    if row_k == csv_id:
                        node_id = eval(row_v)
                        continue
                    if row_k == csv_labels:
                        node_labels = eval(row_v)
                        continue
                    if row_v == '':
                        continue
                    if row_v[0] == '[':
                        try:
                            row_v_list = eval(row_v)
                            node_dic[row_k] = row_v_list
                        except BaseException:
                            node_dic[row_k] = row_v
                        continue
                    try:
                        row_v_int = int(row_v)
                        node_dic[row_k] = row_v_int
                    except BaseException:
                        node_dic[row_k] = row_v
                result = graph.add_node(node_labels, node_dic, node_id)
                if result != -1:
                    count = count + 1
        print("从", file, "一共导入graphdata节点个数:   ", count)
        return graph
Ejemplo n.º 9
0
    def test_get_graph(self):
        graph_data = GraphData()

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.clear"})

        print(graph_data.get_node_ids())
        print(graph_data.get_relation_pairs_with_type())

        graph_data.add_relation(1, "related to", 2)
        graph_data.add_relation(1, "related to", 3)
        graph_data.add_relation(1, "related to", 4)
        graph_data.add_relation(2, "related to", 3)
        graph_data.add_relation(3, "related to", 4)

        print(graph_data.get_relations(1, "related to"))
        print("get relation by type")
        print(graph_data.get_relations(relation_type="related to"))
Ejemplo n.º 10
0
    def test_get_graph_with_property(self):
        graph_data = GraphData()

        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.add"})
        graph_data.add_node({"override method"},
                            {"qualified_name": "ArrayList.pop"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.remove"})
        graph_data.add_node({"method"}, {"qualified_name": "ArrayList.clear"})

        # print(graph_data.get_node_ids())
        # print(graph_data.get_relation_pairs_with_type())

        graph_data.add_relation_with_property(1,
                                              "related to",
                                              2,
                                              extra_info_key="as")
        graph_data.add_relation_with_property(1,
                                              "related to",
                                              3,
                                              extra_info_key="ab")
        graph_data.add_relation_with_property(1,
                                              "related to",
                                              4,
                                              extra_info_key="cs")
        graph_data.add_relation_with_property(2,
                                              "related to",
                                              3,
                                              extra_info_key="ca")
        graph_data.add_relation_with_property(3, "related to", 4)

        print(graph_data.get_relations(1, "related to"))
        print("get relation by type")
        print(graph_data.get_relations(relation_type="related to"))
        t = graph_data.get_edge_extra_info(1,
                                           2,
                                           "related to",
                                           extra_key="extra_info_key")
        print(t)
Ejemplo n.º 11
0
 def test_exist_relation(self):
     graph_data = GraphData()
     graph_data.add_node({"method", "entity"}, {
         "qualified_name": "ArrayList.remove",
         "version": "1.0"
     })
     graph_data.add_node({"method", "entity"}, {
         "qualified_name": "LinkedList.add",
         "version": "1.0"
     })
     graph_data.add_node({"class", "entity"}, {
         "qualified_name": "ArrayList",
         "version": "1.0"
     })
     graph_data.add_relation(3, "hasMethod", 1)
     graph_data.add_relation(1, "belongTo", 3)
     self.assertEqual(graph_data.exist_any_relation(3, 1), True)
     self.assertEqual(graph_data.exist_any_relation(2, 1), False)
     new_relations = {(3, 'hasMethod', 1), (1, 'belongTo', 3)}
     self.assertEqual(graph_data.get_all_relations(1, 3), new_relations)