An application programming interface migration method based on prompt learning
Patent Information
- Application Number
- CN202310486787.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-04
- Publication Date
- 2026-10-20
- Estimated Expiration
- 2043-05-04
AI Technical Summary
但是,这些数据不是平行的,不能用于有监督的翻译
[0022]将前步骤中构造完成的提示输入到大规模代码预训练模型中,获得预测输出,并从输出中提取出迁移后的代码,从而实现跨语言或跨库的包含应用程序编程接口的代码迁移。本发明使用大规模代码预训练模型CodeX进行实验与展示,模型预测结果能较自然而准确的完成包含应用程序编程接口的代码迁移任务。
Smart Images

Figure CN116541071B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to an application programming interface migration method based on prompting learning, specifically a code migration method based on prompting learning that involves cross-language or cross-library code containing application programming interfaces (APIs), belonging to the field of code migration technology in software development and maintenance. Background Technology
[0002] In software development and maintenance tasks, it is often necessary to migrate a project from using a source library or language to using a target library or language. However, the code blocks to be migrated often contain APIs, so how to migrate code containing APIs becomes the key to code migration.
[0003] Traditional rule-based code migration methods typically rely on meticulous, manual rule formulation, which is costly in terms of both time and effort. Work on cross-repository code migration is particularly lacking, primarily focusing on API substitution relationships without further investigation into code migration involving APIs. Cross-language code migration methods, including those based on statistical machine translation models, often employ an encoder-decoder architecture to train a pre-trained model, ultimately performing code migration through fine-tuning. However, these methods largely require high-quality parallel code as training datasets, despite the abundance of open-source repositories for various languages on GitHub. However, this data is not parallel and cannot be used for supervised translation. Therefore, the lack of high-quality parallel code data is one of the major bottlenecks hindering the development of code migration models. Furthermore, these works do not specifically address API migration. Summary of the Invention
[0004] Purpose of the Invention: Existing related works tend to rely on prior knowledge, such as high-quality parallel code. Therefore, when such knowledge cannot be collected or the collected knowledge is of low quality, the efficiency of code migration decreases. Furthermore, some related works perform code migration by manually formulating transformation rules, which consumes significant human and material resources and is costly. To address the problems and shortcomings of existing technologies, this invention proposes a code migration method based on prompting learning, applicable to cross-language or cross-library systems and incorporating Application Programming Interfaces (APIs). This invention argues that considering API migration and code migration separately can achieve better results in code migration work involving APIs.
[0005] This invention proposes an application programming interface (API) migration method based on prompting learning. Specifically, this method, given the source code to be migrated and the APIs used in the source code, designs a matching algorithm to calculate the target API most similar to the source API. Depending on the code migration task, this invention inputs the source code, source API, and target API as prompts in the form of code comments into a large-scale code pre-training model, and then extracts the migrated code from the model's predicted output. This technical solution needs to consider the following issues:
[0006] (1) How to design a matching algorithm to calculate the target API that is most similar to the source API;
[0007] (2) How to use existing data to construct prompts for input into the model to obtain transfer results.
[0008] For problem (1), this method collects a series of APIs containing formal parameters from the API documentation, distinguishes APIs with the same method name but different parameter lists, and constructs an API dataset with complete parameter lists. Then, an API migration map table is constructed based on the semantic similarity between source domain APIs and target domain APIs to record the semantic similarity between APIs. This method designs a matching algorithm to match suitable target domain API parameterized function declarations for APIs in a specified source domain. The algorithm comprehensively considers the similarity of parameter types, the similarity of parameter names, and the matching degree of parameter numbers, and combines the API semantic similarity obtained by querying the API migration map table.
[0009] Regarding question (2), research revealed that inputting hints into a large-scale code pre-training model allows the model to return the desired information. For both cross-language and cross-library code migration tasks, this invention designs two different hint construction methods. First, for a given pair of source APIs and target APIs with parameterized function declarations, the package name is removed from the declaration, but the class name and method name information are retained. For cross-library migration of code in the same programming language, since the source code and target code use the same programming language, only the libraries differ, the hint construction only needs to include the source domain API, target domain API, and the location of the source code. For cross-language code migration tasks, the hint construction must include the source domain API, target domain API, and the location of the source code, as well as the programming language types of the source and target domains. Finally, the source APIs and target APIs with retained class and method names, along with the source code to be migrated, are used to construct hints in the form of code comments according to different code migration tasks. These hints are then input into a large-scale code pre-training model to obtain predicted output, and the migrated code is extracted from the output, thus achieving cross-language or cross-library code migration including APIs. For example, in the experiment, the large-scale pre-trained model CodeX was used as an example. The model's prediction results can complete the code migration task that includes the application programming interface in a relatively natural and accurate manner.
[0010] This invention proposes an application programming interface (API) migration method based on prompt learning, specifically a code migration method for cross-language or cross-library tasks involving APIs, abbreviated as CMIAPL (Code Migration Including APIs based on Prompt Learning). Experimental evaluation shows that it has good performance in cross-library and cross-language code migration tasks involving APIs.
[0011] Technical Solution: This method for migrating application programming interfaces (APIs) based on prompting learning extracts the source API from the source code to be migrated. It then queries an API dataset constructed from API documentation to find parameterized function declarations of the source APIs that match the parameter information in the source code. By searching an API migration map table, it obtains a list of candidate target APIs similar to the source API and the similarity values between the candidate target APIs and the source API. Next, it queries the API dataset to find parameterized function declarations for both the source API and candidate target APIs. A matching algorithm is designed to calculate the similarity of parameter types, parameter names, and parameter counts between the source API and all candidate target APIs. Combined with the semantic similarity between the source API and candidate target APIs in the API migration map table, it matches suitable parameterized function declarations of target domain APIs to the specified source domain APIs. Finally, depending on the code migration task, it constructs prompts for a code pre-training model using source API parameterized function declarations, target API parameterized function declarations, and the source code to be migrated as code comments. These prompts are then input into a large-scale code pre-training model to obtain predicted output. The migrated code is extracted from the output, thus achieving cross-language or cross-library code migration containing application programming interfaces.
[0012] Specifically, it includes the following five parts:
[0013] (1) Extract the source API from the source code to be migrated, and obtain a list of candidate target APIs similar to the source API and the similarity value between the source API and the candidate target API by searching the API migration mapping table.
[0014] This method constructs an API migration map based on the semantic similarity between the source API and the target API, recording the semantic similarity between APIs. For a given source API, the API migration map can be retrieved to obtain a list of candidate target APIs similar to the source API, as well as the similarity value between the source API and the candidate target APIs.
[0015] (2) Obtain the parameterized function declarations of the source API and the target API by utilizing the API documentation of the API dataset.
[0016] A series of APIs with parameters were collected from the API documentation. APIs with the same method name but different parameter lists were distinguished, and an API dataset with complete parameter lists was constructed. For a given API, its parameterized function declaration can be obtained by querying the API dataset.
[0017] (3) By designing a matching algorithm, the similarity of parameter types, parameter names, and parameter quantity between the source API and all target APIs is calculated. Combined with the similarity values between the source API and candidate target APIs in the API migration mapping table, the best matching source API and target API parameterized function declarations are obtained.
[0018] This method designs a matching algorithm based on the source code and the source API in the source code to match the parameterized function declarations of APIs in the specified source domain with the appropriate parameterized function declarations of APIs in the target domain. The algorithm comprehensively considers the similarity of parameter types, the similarity of parameter names, and the matching degree of parameter quantity, and combines the API semantic similarity obtained by querying the API migration mapping table.
[0019] (4) Retain the class names and method names of the source API and the target API, and use the source API, the target API, and the source code to be migrated to construct hints for the pre-trained model in the form of code comments according to different code migration tasks.
[0020] First, the package names of parameterized function declarations in both the source and target APIs are removed, while the class and method names are retained. This is because package names are often diverse and misleading, while class and method names are closely related to the API's functionality. For both cross-language and cross-library code migration tasks, this invention designs two different suggestion construction methods. For cross-library migration of code in the same programming language, since the source and target code use the same programming language but different libraries, the suggestion construction only needs to include the source domain API, target domain API, and the location of the source code. For cross-language code migration tasks, the suggestion construction must include the source domain API, target domain API, the location of the source code, and the programming language types of both the source and target domains.
[0021] (5) Input the prompts into a large-scale code pre-training model to obtain the predicted output, extract the transferred code from the output, thereby realizing cross-language or cross-library code transfer with application programming interfaces.
[0022] The completed hints from the previous steps are input into a large-scale code pre-trained model to obtain the predicted output. The transferred code is then extracted from the output, thereby achieving cross-language or cross-library code migration including application programming interfaces (APIs). This invention uses the large-scale code pre-trained model CodeX for experiments and demonstrations. The model's prediction results can complete the code migration task including APIs relatively naturally and accurately.
[0023] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the application programming interface migration method based on prompting learning as described above.
[0024] A computer-readable storage medium storing a computer program that performs the application programming interface migration method based on prompting learning as described above. Attached Figure Description
[0025] Figure 1 This is a flowchart illustrating the implementation of the present invention. Detailed Implementation
[0026] The present invention will be further illustrated below with reference to specific embodiments. It should be understood that these embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. After reading the present invention, any modifications of the present invention in various equivalent forms by those skilled in the art will fall within the scope defined by the appended claims.
[0027] This method includes a flowchart of specific implementation embodiments, as shown below. Figure 1 As shown, the process begins by extracting the source API from the source code. Then, an API migration map is used to obtain candidate target APIs with similar functionalities to the source API. Next, an API dataset constructed from API documentation is queried to find parameterized function declarations of the source APIs that match the parameter information in the source code, and parameterized function declarations of candidate target APIs are also identified. Following this, a matching algorithm is designed to match suitable parameterized function declarations of target domain APIs with those of APIs in a specified source domain. The algorithm comprehensively considers the similarity of parameter types, parameter names, and parameter quantity, combined with the semantic similarity obtained from the API migration map. Finally, using the parameterized function declarations of the source APIs, target APIs, and the source code to be migrated, code hints are constructed in the form of code comments according to different code migration tasks. These hints are then input into a large-scale code pre-trained model to obtain predicted outputs. The migrated code is extracted from the outputs, thus achieving cross-language or cross-library code migration that includes application programming interfaces (APIs).
[0028] Hint-based learning-based cross-language or cross-library code migration methods that include application programming interfaces (APIs) include:
[0029] (1) Extract the source API to be migrated from the source code, and obtain the list of candidate target APIs and the similarity value between the source API and the candidate target API by searching the API migration mapping table.
[0030] This invention constructs an API migration map based on the semantic similarity between source domain APIs and target domain APIs, recording the semantic similarity between APIs. For a given source API, the API migration map can be retrieved to obtain a list of candidate target APIs and the similarity value between the source API and the candidate target APIs.
[0031] (2) Obtain the parameterized function declarations of the source API and the target API by utilizing the API documentation.
[0032] A series of APIs with parameters were collected from the API documentation. APIs with the same method name but different parameter lists were distinguished, and an API dataset with complete parameter lists was constructed. For a given API, its parameterized function declaration can be obtained by querying the API dataset.
[0033] (3) By designing a matching algorithm, the similarity of parameter types, parameter names, and parameter quantity between the source API and all candidate target APIs is calculated. Combined with the similarity values between the source API and candidate target APIs in the API migration mapping table, the best matching source API and target API parameterized function declarations are obtained.
[0034] For a given source API, several candidate target APIs and their similarity values to the source API can be obtained by consulting the API migration map table. Then, the parameterized function declarations of the source API and all candidate target APIs can be found by querying the API dataset. Next, this invention designs a matching algorithm to match suitable parameterized function declarations of target domain APIs for APIs in a specified source domain. This algorithm is used to calculate the parameterized function declarations of the source API. src With candidate target API tgt Similarity relationship between them:
[0035] Sim(API src API tgt = k*Sim(API) src′ API tgt′ )
[0036] If Cnt src =Cnt tgt When k=0, k=1;
[0037] If Cnt src =0 and Cnt tgt ≠0 or Cnt src ≠0 and Cnt tgt When = 0, if Sim(API) src′ API tgt′ If k > 0.5, then k = 0.5; otherwise, k = 0.1.
[0038] If Cnt src ≠0 and Cnt tgt ≠0 o'clock,
[0039] Sim(API) src API tgt The '(')' value represents the similarity between the source API and the candidate target API. src Cnt represents the number of parameters in the source API. tgt This indicates the number of parameters in the candidate target API. Indicates the type of the i-th parameter in the source API. i Similarity of parameter types between the target API and the candidate API. This represents the name of the i-th parameter nαme in the source API. i The similarity of parameter names between the source API and the candidate target API. p is a constant related to the number of parameters in the source API's parameter list and the number of parameters in the candidate target API's parameter list. Sim(API) src′ API tgt′ This represents the semantic similarity value between the source API and the candidate target API in the API migration map table. The specific algorithm is described below:
[0040] First, consider the similarity between the parameters of the source API and the candidate target API. Let Cnt denote the number of parameters in the parameter list of the source API. src Similarly, the number of parameters in the candidate target API is denoted as Cnt. tgt Separate all parameters in the parameter list into two lists. One list records the parameter types of the source API, denoted as... Where n is Cnt src Another list records the parameter names corresponding to the parameter types, denoted as... Where n is Cnt src The list stores elements in the same order as the parameters appearing in the source API. Similarly, for each candidate target API, its parameter types and names are stored. These are denoted as... Where n is Cnt tgt .
[0041] To find the best-matching source and target API parameterized function declarations as accurately as possible, this method also considers the API semantic similarity between the source API and the candidate target API when calculating the similarity between their parameters, i.e., Sim(API src′ API tgt′This allows for initial screening and ranking of candidate target APIs, as the source API and highly similar candidate target APIs are more likely to form a pair of APIs with the most similar functionality. Consider the following special case: if the number of parameters in the source API is Cnt... src =0 and the number of parameters Cnt of the candidate target API tgt ≠ 0 or the number of parameters in the source API (Cnt) src ≠0 and the number of parameters Cnt of the candidate target API tgt When = 0, the source API src With candidate target API tgt The similarity calculation formula is as follows:
[0042] Sim(API src API tgt = k*Sim(API) src′ API tgt′ )
[0043] Where k is a constant, when Sim(API) src′ API tgt′ When the similarity is greater than 0.5, k = 0.5; otherwise, k = 0.1. This ensures that when calculating the similarity between the source API and the candidate target API, the candidate target APIs with high semantic similarity selected from the API migration map are ranked slightly higher, but not by too many. Meanwhile, the candidate target APIs with low semantic similarity selected from the API migration map are ranked lower, but not negligible.
[0044] Next, we calculate the similarity between the parameter lists of the source API and the candidate target API. First, all parameter types are lowercase. If a parameter type contains "<>", the "<>" marker is removed, along with the content within it. Simultaneously, any possible punctuation marks in the parameter type names, such as commas, are removed. This process is applied to both the source API and the candidate target API. Specifically, different processing methods are set for different parameter types in different migration tasks. For cross-library migration tasks, since the source code and target code use the same programming language (e.g., the basic parameter types like int and bool used in the source and target codes are consistent), when the parameter types of the source API and the candidate target API are identical, the similarity between these two parameter types is denoted as Sim(type). src type tgt = 1. However, the parameter type may contain "[]" structures. Common sense tells us that `int` and `int[]` are essentially similar. Therefore, to distinguish them from two completely identical parameter types, let the similarity between parameter types like `it` and `int[]` be Sim(type) src typetgt = 0.95. If the parameter types of the source API or candidate target API are Object, T, or Class, the similarity of the parameter types is denoted as Sim(type) = 0.95. src type tgt = 0.95. For cross-language migration tasks, since the source code and target code use different programming languages—for example, the common basic type boolean—is represented as `boolean` in Java and `bool` in C#. Therefore, in this case, the similarity between these two parameter types is denoted as Sim(type) = 0.95. src type tgt = 1. For cases like string and char[], charsequence and char[], string and charsequence, the similarity between these two parameter types is denoted as Sim(type) = 1. src type tgt = 0.95. If the parameter type of the source API or candidate target API is Object, T, Class, or ICollection, the similarity of the parameter types is denoted as Sim(type). src type tgt = 0.95. For parameter type similarities other than this, the similarity is calculated based on the similarity of the parameter type names. This method utilizes a pre-trained BERT word embedding model. When a parameter type name is input, the model outputs the corresponding embedding representation. For the source API and the candidate target API, the embedding representation corresponding to the parameter type is denoted as typeVec. src and typeVec tgt The similarity between two parameter types is calculated using the following cosine similarity formula:
[0045] Sim(type src type tgt ) = cos(typeVec src typeVec tgt )
[0046] Because some source APIs and target APIs with similar functions may have inconsistent parameter orders—for example, the source API "junit.framework.Assert.assertEquals(String message, int expected, int actual)" and the similar candidate target API "org.testng.Assert.assertEquals(int actual, int expected, String message)" have different parameter orders—therefore, for the i-th parameter type of the source API… i The similarity between this parameter type and each parameter type of the candidate target API will be calculated, and the maximum similarity will be taken as the i-th parameter type of the source API. i The similarity of parameter types between the target API and the candidate API is calculated using the following formula:
[0047] Where i = 1, ..., Cnt src
[0048] Next, the parameter names are processed. First, all parameter names are lowercase, and any special characters or other characters that may be present are removed. If a parameter name contains "<>", then the "<>" and its contents are removed. The BERT word embedding model is then used to obtain the embedding representation of the parameter names. When a parameter name is input into the model, it outputs its corresponding embedding representation. For the source API and the candidate target API, the embedding representation of the parameter name is denoted as nameVec. src and nameVec tgt Then, calculate the similarity between the two parameter names using the following cosine similarity formula:
[0049] Sim(name src name tgt =cos(nameVec) src nameVec tgt )
[0050] Similarly, the order of parameters in the source API "junit.framework.Assert.assertEquals(String message, int expected, int actual)" is inconsistent with that of the similar candidate target API "org.testng.Assert.assertEquals(int actual, int expected, String message)". In such cases, the name of the i-th parameter of the source API is denoted as name. i Calculate the similarity between it and each parameter name of the candidate target API, and take the maximum value. The name is the i-th parameter name in the source API. i Similarity to parameter names of candidate target APIs:
[0051] Where i = 1, ..., Cnt src
[0052] Next, consider the number of API parameters. If the number of parameters in the source API is Cnt... src The number of parameters Cnt of the candidate target API tgt If none of them are 0, then the matching degree Sim between the i-th parameter of the source API and the parameter of the candidate target API is... i The calculation formula is:
[0053] Where i = 1, ..., Cnt src
[0054] Where p is a constant, when Cnt src =Cnt tgt And Cnt src When Cnt ≠ 0, p = 1; src ≠Cnt tgt And Cnt src When p ≠ 0, p = 0.95.
[0055] Finally, the semantic similarity Sim(API) between the source API and the candidate target API is obtained by consulting the API migration mapping table. src′ API tgt′ The final source API with parameters src With the target API tgt The similarity calculation formula is as follows:
[0056]
[0057] Then for Sim(API) src APItgt Sort the results from largest to smallest to find the source API. src Most similar target API tgt The source API src and target API tgt The source code to be migrated will be used as a hint for code migration.
[0058] (4) Retain the class names and method names of the source API and the target API, and use the source API, the target API, and the source code to be migrated to construct hints for the pre-trained model in the form of code comments according to different code migration tasks.
[0059] Large-scale pre-trained code models have been successfully used to assist programmers in completing various programming tasks, such as type prediction, code completion, and code repair. They can also be explored for code migration tasks. Hint learning is a common method for using large-scale pre-trained models to complete a single learning task. Its key lies in how to construct appropriate hints based on the actual task requirements.
[0060] To enable the model to understand and successfully perform code migration, for parameterized function declarations of given source and target APIs, only the class name and method name are saved, while the package name is removed. This invention designs two different suggestion construction methods for different migration tasks. For cross-library migration of code in the same programming language, since the source and target code use the same programming language but different libraries, the suggestion construction only needs to include the source domain API, target domain API, and the location of the source code. An example of a cross-library suggestion is shown below:
[0061] / / / source api
[0062] Assert.assertEquals(String message, double expected, double actual, double delta)
[0063] / / / target api
[0064] Assert.assertEquals(double actual, double expected, double delta, String message)
[0065] / / / source code
[0066] double esp = 17.2;
[0067] double comp=devolvido.comparticipaCom();
[0068] assertEquals("Doesn't Match", esp, comp, 0);
[0069] / / / target code
[0070] Adding comments like " / / / source api", " / / / target api", " / / / source code", and " / / / target code" to the above prompts guides the model to output migration code that uses the target API to achieve similar functionality to the source code.
[0071] For cross-language code migration tasks, the build prompt must include the source domain API, target domain API, and the location of the source code, as well as the programming language types of both the source and target domains. An example of a cross-language prompt is shown below:
[0072] / / / JAVA source API
[0073] File.mkdirs()
[0074] / / / C# target api
[0075] Directory.CreateDirectory(string path)
[0076] / / / JAVA source code
[0077] File f = new File(path);
[0078] f.mkdirs();
[0079] System.out.println("Directory is created");
[0080] / / / C# target code
[0081] The code above demonstrates a suggestion example for migrating Java code to C# code. The suggestion includes comments such as " / / / JAVA source api", " / / / C# target api", " / / / JAVA source code", and " / / / C# targetcode" to guide the model to output migration code that uses the target API and implements similar functionality to the source code.
[0082] (5) Input the prompts into a large-scale code pre-training model to obtain the predicted output, extract the transferred code from the output, thereby realizing cross-language or cross-library code transfer with application programming interfaces.
[0083] This method was demonstrated and experimented with using the large-scale code pre-trained model CodeX. The CodeX model is an improved version of the GPT-3 series models, trained on a corpus containing billions of lines of code and natural language text, and can handle programming languages such as Python, Java, and C#.
[0084] Finally, the hints constructed in the previous steps are input into the large-scale code pre-training model CodeX to obtain the predicted output, and the transferred code is extracted from the output, thereby realizing cross-language or cross-library code transfer that includes application programming interfaces.
[0085] Obviously, those skilled in the art should understand that the steps of the application programming interface migration method based on prompting learning in the above embodiments of the present invention can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. Optionally, they can be implemented using computer-executable program code, thereby storing them in a storage device for execution by the computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the embodiments of the present invention are not limited to any particular hardware and software combination.
[0086] Experimental Setup: This invention used the large-scale code pre-trained model CodeX for experiments. The CodeX model version was "code-davinci-002", and the temperature parameter was set to 0, meaning the model output had the lowest randomness. The experiments were run on a server with two Tesla T4 GPUs.
[0087] Evaluation metrics: (1) BLEU: BLEU scores are often used to measure the quality of machine translation. Here, BLEU scores are used to examine the n-gram matching between the generated target transfer code and the correct target transfer code. This invention calculates 1-gram matching. (2) CodeBLEU: CodeBLEU scores are used to evaluate the quality of the generated code by comparing the n-gram matching, weighted n-gram matching, syntactic matching, and semantic matching between the generated target transfer code and the correct target transfer code. Here, the weight of the n-gram matching is set to (0.25, 0.25, 0.25, 0.25). The weight of the weighted n-gram matching is set as follows: if the current word is a keyword, the weight is 1; otherwise, it is 0.2. Finally, the weighted sum of the similarities of the n-gram matching, weighted n-gram matching, syntactic matching, and semantic matching is the value of CodeBLEU. The weight of the weighted sum is (0.1, 0.1, 0.4, 0.4).
[0088] Experimental Data: This invention conducted cross-library and cross-language code migration experiments involving application programming interfaces (APIs). The libraries used in the cross-library code migration experiment are shown in Table 1. The cross-language code migration experiment implemented code migration from JAVA to C#. This invention prepared two reference files beforehand. First, an API migration mapping table was constructed based on the semantic similarity between the source domain API and the target domain API, recording the semantic similarity between APIs. Then, a series of APIs containing formal parameters were collected from the API documentation, and APIs with the same method name but different parameter lists were distinguished, constructing an API dataset with complete parameter lists. This invention also prepared a source code dataset with build hints. For the cross-library code migration experiment, 14 different source code segments containing APIs using the mockito library, 52 different source code segments containing APIs using the junit library, 13 different source code segments containing APIs using the log4j library, 15 different source code segments containing APIs using the gson library, and 15 different source code segments containing APIs using the commons-httpclient library were collected. For cross-language code migration, this invention collected 144 different source code segments containing APIs using the JAVA language to complete the code migration task from JAVA to C#.
[0089] Comparison Methods: Two different code migration methods were selected for comparison with CMIAPL:
[0090] (1)MigrationMapper, a method for cross-library API mapping of code in the same programming language using code commit records. In order to conduct comparative experiments, this invention uses code segments from the source and target libraries that implement similar functions contained in the code commit records as source code datasets for build hints, which are used by CodeX for code migration.
[0091] (2) MuST-CoST is a model that utilizes multilingual fragment denoising auto-encoding and multilingual fragment translation pre-training for code migration between different programming languages.
[0092] Experimental Results: CMIAPL was compared with these two methods, and the results are shown in Tables 2 and 3. It can be seen that CMIAPL achieved a BLEU score of 0.963 and a CodeBLEU score of 0.946 in cross-library code migration. In cross-language code migration, the BLEU score reached 0.997 and the CodeBLEU score reached 0.882. These results represent a significant improvement over the comparison methods, demonstrating the effectiveness of this invention in cross-library and cross-language code migration involving APIs.
[0093] Table 1. Five pairs of Java migration libraries
[0094]
[0095] Table 2 shows the cross-library code migration results.
[0096] MigrationMapper 0.898 0.929 CMIAPL 0.963 0.946
[0097] Table 3. Cross-language code migration effects
[0098] MuST-CoST 0.880 0.654 CMIAPL 0.997 0.882
Claims
1. A method for migrating application programming interfaces based on prompting learning, characterized in that, Extract the source API from the source code to be migrated, and obtain a list of candidate target APIs that implement similar functions to the source API by searching the API migration mapping table; The process involves querying the API dataset constructed from API documentation to obtain the parameterized function declarations of the source API and candidate target APIs. A matching algorithm is designed to calculate the similarity of parameter types, parameter names, and parameter counts between the source API and all candidate target APIs. This, combined with the semantic similarity values between the source API and candidate target APIs in the API migration mapping table, yields the best-matching source and target API parameterized function declarations, providing hints for the pre-trained model. Finally, these hints are input into a large-scale code pre-training model to obtain predicted outputs. The migrated code is then extracted from the outputs, enabling cross-language or cross-library code migration involving application programming interfaces (APIs). This process comprises the following five parts: (1) Extract the source API from the source code to be migrated, and obtain the list of candidate target APIs and the similarity value between the source API and the candidate target API by searching the API migration mapping table; (2) Obtain the parameterized function declarations of the source API and candidate target API by utilizing the API documentation; (3) By designing a matching algorithm, the similarity of parameter types, parameter names, and parameter quantity between the source API and all candidate target APIs is calculated. Combined with the similarity values between the source API and candidate target APIs in the API migration mapping table, the best matching source API and target API parameterized function declarations are obtained. (4) Retain the class names and method names of the source API and the target API, and use the source API, the target API, and the source code to be migrated to construct hints for the pre-trained model in the form of code comments according to different code migration tasks. (5) Input the prompts into a large-scale code pre-training model to obtain the predicted output, extract the transferred code from the output, thereby realizing cross-language or cross-library code transfer with application programming interfaces.
2. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, An API migration mapping table is constructed based on the semantic similarity between the source domain API and the target domain API to record the semantic similarity between APIs.
3. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, A series of APIs containing formal parameters were collected from the API documentation, and APIs with the same method name but different parameter lists were distinguished to construct an API dataset with complete parameter lists; for a given API, its parameterized function declarations were obtained by querying the API dataset.
4. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, Design a matching algorithm to match appropriate API parameterized function declarations in a specified source domain with API parameterized function declarations in a target domain. The algorithm comprehensively considers the similarity of parameter types, the similarity of parameter names, and the matching degree of parameter quantity, and combines the API semantic similarity obtained by querying the API migration mapping table.
5. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, Two different suggestion-based construction methods were designed for two types of code migration tasks: cross-language and cross-library. First, for a given pair of source API and target API parameterized function declarations, the package name was removed from the declaration, but the class name and method name information were retained. For cross-library migration of code in the same programming language, the suggestion-based construction only needs to include the source domain API, the target domain API, and the location of the source code. For cross-language code migration tasks, the suggestion-based construction must include the source domain API, the target domain API, the location of the source code, and the programming language types of the source and target domains.
6. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, The system inputs prompts into a large-scale code pre-trained model to obtain predicted outputs, and then extracts the transferred code from the outputs, thereby enabling cross-language or cross-library code transfer that includes application programming interfaces.
7. The application programming interface migration method based on prompting learning according to claim 1, characterized in that, Design a matching algorithm to match appropriate target domain API parameterized function declarations for a given source domain API parameterized function declaration; this matching algorithm is used to compute source API parameterized function declarations. src With candidate target API tgt Similarity relationship between them: Sim(API src ,FIRE tgt )=k*Sim(API src′ ,FIRE tgt′ ) If Cnt src =Cnt tgt When k=0, k=1; If Cnt src =0 and Cnt tgt ≠0 or Cnt src ≠0 and Cnt tgt When = 0, if Sim(API) src′ API tgt′ If k > 0.5, then k = 0.5; otherwise, k = 0.
1. If Cnt src ≠0 and Cnt tgt ≠0 o'clock, Sim(API) src API tgt The ) represents the similarity value between the source API and the candidate target API; Cnt src Cnt represents the number of parameters in the source API. tgt This indicates the number of parameters in the candidate target API. Indicates the type of the i-th parameter in the source API. i Similarity of parameter types between the target API and the candidate API; The name represents the i-th parameter name of the source API. i The similarity of parameter names to the candidate target API; p is a constant related to the number of parameters in the source API parameter list and the number of parameters in the candidate target API parameter list; Sim(API src′ API tgt′ The value represents the semantic similarity between the source API and the candidate target API in the API migration map table.
8. A computer device, characterized in that: The computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the application programming interface migration method based on prompting learning as described in any one of claims 1-6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program that performs the application programming interface migration method based on prompting learning as described in any one of claims 1-6.
Citation Information
Patent Citations
Cross-library API recommendation method in code migration process
CN117235138A
Quick depth velocity model for a large area based on deep learning
US20250244492A1