def __init__(self, sentence_list, logger=None):
     self._api_searcher = APISearcher()
     if logger is None:
         self.logger = Logger("TreeView").get_log()
     else:
         self.logger = logger
     self._tree = self.get_api_tree_from_sentence_list(sentence_list)
    def test_API_entity_searcher(self):
        session = EngineFactory.create_session()
        searcher = APISearcher(session)
        result = searcher.search_api_entity("XML", result_limit=20)
        for api in result:
            print(api)

        self.assertEqual(20, len(result))
    def test_API_aliases_searcher(self):
        session = EngineFactory.create_session()
        searcher = APISearcher(session)
        result = searcher.search_api_aliases("XML")
        for post in result:
            print(post)

        self.assertEqual(500, len(result))
    def test_API_entity_searcher_in_tuple(self):
        session = EngineFactory.create_session()
        searcher = APISearcher(session)
        all_query_tuple = [
            ("XML", APIEntity.API_TYPE_ALL_API_ENTITY, 427),
            ("json", APIEntity.API_TYPE_ALL_API_ENTITY, 10),
            ("http", APIEntity.API_TYPE_METHOD, 31),
        ]
        for query, api_type, size in all_query_tuple:
            result = searcher.search_api_entity(query, api_type=api_type)
            for api_entity in result:
                print(api_entity)

            self.assertEqual(size, len(result))
    def test_API_entity_searcher_in_tuple_by_limit(self):
        session = EngineFactory.create_session()
        searcher = APISearcher(session)
        all_query_tuple = [
            ("XML", APIEntity.API_TYPE_ALL_API_ENTITY, 10, 10),
            ("json", APIEntity.API_TYPE_ALL_API_ENTITY, 10, 6),
            ("http", APIEntity.API_TYPE_METHOD, 20, 20),
            ("java", APIEntity.API_TYPE_CLASS, 15, 0),
        ]
        for query, api_type, limit, size in all_query_tuple:
            result = searcher.search_api_entity(query,
                                                api_type=api_type,
                                                result_limit=limit)
            for api_entity in result:
                print(api_entity)

            self.assertEqual(size, len(result))
Beispiel #6
0
    def test_search(self):
        api_entity_session = EngineFactory.create_session(autocommit=True)
        api_searcher = APISearcher(session=api_entity_session, )

        graph_client = GraphClient(server_number=1)

        search_util = SearchUtil(graph_client, api_searcher)
        result = search_util.search("string buffer", 10)
        print(result)
        self.assertEqual(len(result), 10)
    def test_search_api_entity_by_id_list(self):
        session = EngineFactory.create_session()
        searcher = APISearcher(session)

        all_query_tuple = [
            ([1, 2, 3, 1111, 4444, 6666,
              1], APIEntity.API_TYPE_ALL_API_ENTITY, 6),
            ([
                1, 2, 3, 4, 6, 7, 9, 1111, 4444, 6666, 1, 111, 444, 7777, 2345,
                12222, 12222, 12225, 12227
            ], APIEntity.API_TYPE_ALL_API_ENTITY, 17),
            ([1, 12222, 12225, 12227, 12228, 12220,
              12229], APIEntity.API_TYPE_METHOD, 6),
        ]
        for query_id_list, api_type, size in all_query_tuple:
            result = searcher.query_api_entity(query_id_list, api_type)
            for api_entity in result:
                print(api_entity)

            self.assertEqual(size, len(result))
Beispiel #8
0
graphClient = DefaultGraphAccessor(GraphClient(server_number=1))
logger.info("create graphClient")

api_entity_linker = APIEntityLinking()
logger.info("create api_entity_linker object")

questionAnswerSystem = QuestionAnswerSystem()
logger.info("create questionAnswerSystem")

dbSOPostSearcher = SOPostSearcher(EngineFactory.create_so_session(),
                                  logger=app.logger)
logger.info("create SO POST Searcher")

api_entity_session = EngineFactory.create_session(autocommit=True)
apiSearcher = APISearcher(session=api_entity_session, logger=app.logger)
logger.info("create API Searcher")

sql_heat_handler = SQLHeatHandler(session=EngineFactory.create_heat_session(),
                                  logger=app.logger)
logger.info("create SQL HeatHandler")

graphJsonParser = GraphJsonParser(graph_accessor=graphClient)
logger.info("create graphJsonParser Json Parser")

search_util = SearchUtil(graphClient, apiSearcher)

NEWEST_NODE = graphClient.get_newest_nodes(100)
logger.info("init newest node")

labelUtil = LabelUtil()
class APITree:
    def __init__(self, sentence_list, logger=None):
        self._api_searcher = APISearcher()
        if logger is None:
            self.logger = Logger("TreeView").get_log()
        else:
            self.logger = logger
        self._tree = self.get_api_tree_from_sentence_list(sentence_list)

    @property
    def tree(self):
        return self._tree

    @staticmethod
    def get_api_name_list(name):
        result = re.findall(r'(.*)\((.*)\).*', name, re.IGNORECASE)
        print("cut in piece")
        print(result)
        if len(result) == 0:
            return name, []
        else:
            parameters = result[0][1].split(",")
            return result[0][0] + "()", parameters

    def get_api_tree_from_sentence_list(self, sentence_list):
        api_list = []
        fail_num = 0
        for sentence in sentence_list:
            api_entity = {}
            api_entity["api_id"] = sentence["api_id"]
            api_entity["api_type"] = sentence["api_type"]
            api_entity["api_name"] = sentence["api_qualified_name"]
            api_entity["api_sentence"] = sentence
            if api_entity not in api_list:
                api_list.append(api_entity)
        class_martrix = []
        for api in api_list:
            api_name, parameters = self.get_api_name_list(api["api_name"])
            api_class_list = api_name.split(".")
            class_martrix.append({
                "api_list": api_class_list,
                "api_id": api["api_id"],
                "api_type": api["api_type"],
                "api_sentence": api["api_sentence"]
            })
        root_node = APINode(node_id=0,
                            name="root",
                            desc="The Root Of A Class Tree",
                            parent_id=None,
                            full_name="root")
        tree_list = [dict(root_node)]
        for line in class_martrix:
            parent_node_id = 0
            full_name = ""
            sentence_from_api_full_name = ".".join(line["api_list"])
            sentence_from_api_id = line["api_id"]
            sentence_from_api_type = line["api_type"]
            for class_slice in line["api_list"]:
                if full_name == "":
                    full_name = class_slice
                else:
                    full_name = ".".join([full_name, class_slice])
                node_id = APINode.has_child(class_slice, parent_node_id,
                                            tree_list)
                if node_id == -1:
                    node_id = len(tree_list)
                    api_entity = self._api_searcher.search_api_id_by_qualified_name(
                        full_name)
                    if not api_entity:
                        if sentence_from_api_full_name == full_name:
                            # print("============================")
                            print(full_name, sentence_from_api_id,
                                  sentence_from_api_type)
                            test_list.append(sentence_from_api_id)
                            api_id = sentence_from_api_id
                            desc = API_TYPE[sentence_from_api_type]
                        else:
                            fail_num += 1
                            # if self.logger:
                            #     self.logger.exception("exception occur in finding api=%s", full_name
                            print("^^^^^^^^^^^^^^^^^^^^^^^^^^")
                            api_id = None
                            desc = API_TYPE[0]
                    else:
                        get_list.append(api_entity[0])
                        desc = API_TYPE[api_entity[1]]
                        api_id = api_entity[0]
                    new_node = APINode(node_id=node_id,
                                       name=class_slice,
                                       desc=desc,
                                       parent_id=parent_node_id,
                                       full_name=full_name,
                                       api_id=api_id)
                    tree_list.append(dict(new_node))
                    APINode.add_child(tree_list[parent_node_id], node_id)
                parent_node_id = node_id
            self.insert_sentence_node_into_api_tree(line["api_sentence"],
                                                    parent_id=parent_node_id,
                                                    tree_list=tree_list)
        print("*********************************")
        print(fail_num)
        print("*********************************")
        return tree_list

    @staticmethod
    def insert_sentence_node_into_api_tree(sentence, parent_id, tree_list):
        new_sentence = SentenceNode(sentence,
                                    len(tree_list),
                                    desc="SENTENCE",
                                    parent_id=parent_id)
        APINode.insert_node_in_tree(new_sentence, tree_list)
Beispiel #10
0
    @staticmethod
    def find_parent_node_by_from_api_id(tree, api_id, sentence):
        parent_id = 0
        for node in tree:
            if "api_id" not in node.keys():
                continue
            elif node["api_id"] == api_id:
                parent_id = node["id"]
        return parent_id


if __name__ == "__main__":
    fail_sentence = [
        101, 103, 110, 118, 120, 124, 128, 129, 141, 142, 150, 156, 159, 160,
        161, 170, 176, 179, 182, 192, 196
    ]
    with open("test.json") as f:
        sentence_list = ast.literal_eval(f.read())
    api_tree = APITree(sentence_list).tree
    print(api_tree)
    print("=================================")
    ss = APISearcher()
    print(test_list)
    print(already_list)
    for id in api_tree[0]["child"]:
        if "sentence_id" in api_tree[id].keys():
            # api = ss.search_api_by_id(api_tree[id]["from_api_id"])
            # print(api)
            print id, api_tree[id]["from_api_id"] in test_list, api_tree[id][
                "from_api_id"] in get_list