Check code reuse method based on knowledge graph, apparatus, and storage medium
By using knowledge graphs and related models, generating event types and parameter lists, finding and modifying related code segments, the problem that historical script programs cannot be reused is solved, and efficient reuse of code segments and improving program writing efficiency is achieved.
Patent Information
- Application Number
- PCT/CN2024/079430
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2024-02-29
- Publication Date
- 2025-09-04
AI Technical Summary
In the prior art, when developers write configuration item check scripts, the historical script program is unable to reference the code related to the historical script program, resulting in inefficient writing.
Using knowledge graphs and related models, we can obtain the description documents of the inspection project, generate event types and parameter lists, use the knowledge graph to find relevant code segments, and modify the parameter list to generate candidate code segments to build a knowledge graph to improve the accuracy and efficiency of code segment conversion.
It improves the accuracy and efficiency of code segment conversion, realizes efficient reuse of code segments, and improves program writing efficiency.
Smart Images

Figure CN2024079430_04092025_PF_FP_ABST
Abstract
Description
A knowledge graph-based inspection code reuse method, device and storage medium Technical Field
[0001] The present invention relates to the field of software development, and in particular to a knowledge graph-based code reuse inspection method, device, and storage medium. Background Art
[0002] In some cases, when developers write configuration item check scripts, they cannot reference relevant codes of historical scripts due to the low reusability of historical scripts, which reduces the efficiency of program writing.
[0003] Summary of the Invention
[0004] To achieve the above objectives, the present invention proposes a knowledge graph-based code reuse inspection method, device and storage medium, which can use knowledge graphs and related models to find code segments with similar structures and parameters and complete the conversion of code segments.
[0005] A knowledge graph-based inspection code reuse method proposed in an embodiment of the present invention includes: obtaining a description document corresponding to at least one inspection step in an inspection project; generating an event type and parameter list: utilizing an event type classification model and a parameter list extraction model to generate an event type and parameter list corresponding to each inspection step based on the description document; searching for related code segments: utilizing a knowledge graph to search for each inspection step based on the event type and the parameter list to obtain the most relevant N related code segments; wherein N is greater than or equal to 1; generating candidate code segments: modifying the related code segments based on the parameter list to generate candidate code segments. The knowledge graph and related models can be used to extract the type and parameter list of the description document, find code segments with similar structures and parameters, and complete the conversion of code segments.
[0006] In one embodiment, generating an event type corresponding to each inspection step further includes: determining a corresponding major category based on the inspection item; and determining a minor category of the inspection step within the inspection item based on the major category using a pre-built event type classification model; wherein each major category corresponds to at least one event type classification model; and the event type classification model is pre-built based on the inspection item. The pre-built classification model improves the accuracy of the classification of the descriptive text.
[0007] In one embodiment, generating a parameter list corresponding to each inspection step further includes: generating the parameter list based on a term list and / or named entity recognition, thereby achieving accurate extraction of the parameter list.
[0008] In one embodiment, the method further includes constructing a knowledge graph based on at least one inspection step in an existing inspection project and its corresponding event type, parameter list, and code snippet. Each operation step node in the knowledge graph is connected to its corresponding parameter list, event type, and code snippet. The knowledge graph establishes connections between all operation steps, improving the accuracy of candidate script queries and the success rate of conversion.
[0009] In one embodiment, generating the candidate code segment further includes: determining the parameter position in the relevant code segment based on a parameter identification and binding model; and modifying the code at the parameter position in the relevant code segment based on the parameter list to generate the candidate code segment, thereby adjusting the parameters and obtaining a code segment that can be directly used.
[0010] In one embodiment, obtaining the most relevant N related code segments after searching further includes: calculating similarity scores of nodes in the knowledge graph based on similarity of event types and / or the number of identical parameters in the parameter list; and selecting code segments corresponding to the top N nodes with the highest scores as related code segments based on the similarity scores. This enables finding code segments with similar structures and parameters through the knowledge graph.
[0011] An inspection code reuse device based on a knowledge graph proposed in an embodiment of the present invention includes: a description document acquisition module, configured to obtain a description document corresponding to at least one inspection step in an inspection item; an event type and parameter list generation module, configured to use an event type classification model and a parameter list extraction model to generate an event type and parameter list corresponding to each inspection step based on the description document; a related code segment search module, configured to use a knowledge graph to search for each inspection step based on the event type and the parameter list to obtain the most relevant N related code segments; wherein N is greater than or equal to 1; a candidate code segment generation module, configured to modify the related code segment based on the parameter list to generate a candidate code segment. The type and parameter list of the description document can be extracted through the knowledge graph and the related model, and code segments with similar structures and parameters can be found to complete the conversion of code segments.
[0012] An electronic device proposed in an embodiment of the present invention includes: at least one processor; and a memory coupled to the at least one processor, wherein the memory is used to store instructions, and when the instructions are executed by the at least one processor, the processor executes the method described above.
[0013] An embodiment of the present invention provides a computer-readable storage medium on which computer instructions are stored. When the computer instructions are executed, the method described in any of the above embodiments is executed.
[0014] A computer program product provided in an embodiment of the present invention includes a computer program. When the computer program is executed by a processor, the method described in any one of the above embodiments is implemented. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] The following drawings are only intended to illustrate and explain the present invention, and do not limit the scope of the present invention.
[0016] FIG1 is a flow chart of a method 100 for checking code reuse based on a knowledge graph according to an embodiment of the present invention.
[0017] FIG2 is a schematic diagram of the structure of a knowledge graph according to an embodiment of the present invention.
[0018] FIG3 is a structural diagram of a knowledge graph-based inspection code reuse device 200 according to an embodiment of the present invention.
[0019] FIG4 is a schematic diagram of an electronic device 300 according to an embodiment of the present invention.
[0020] The following are the descriptions of the reference numerals: DETAILED DESCRIPTION
[0021] In order to have a clearer understanding of the technical features, purposes and effects of the present invention, specific embodiments of the present invention are now described with reference to the accompanying drawings.
[0022] In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention may also be implemented in other ways different from those described herein. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0023] As used in this application and the claims, unless the context clearly indicates otherwise, the words "a," "an," "an," and / or "the" are not intended to refer to the singular but may include the plural. Generally speaking, the terms "comprises" and "include" only indicate the inclusion of the steps and elements specifically identified, and these steps and elements do not constitute an exclusive list. A method or apparatus may also include other steps or elements.
[0024] When writing a check script, there are usually multiple items that need to be checked. Every time a new configuration check item appears, a new script needs to be written. However, since the relevant code of the previous script cannot be referenced, the efficiency of program writing is greatly reduced.
[0025] Therefore, in an embodiment of the present invention, a more effective knowledge graph-based inspection code reuse method, device and storage medium are provided.
[0026] FIG1 is a flow chart of a method 100 for checking code reuse based on a knowledge graph according to an embodiment of the present invention. As shown in FIG1 , the method 100 may include:
[0027] Step S101: Obtain a description document corresponding to at least one inspection step in an inspection item.
[0028] Step S102, generating event types and parameter lists: utilizing the event type classification model and parameter list extraction model, generating event types and parameter lists corresponding to each inspection step based on the description document.
[0029] In this embodiment, generating the event type corresponding to each inspection step further includes: determining the corresponding major category based on the inspection item; based on the major category, determining the minor category of the inspection step in the inspection item through a pre-built event type classification model; wherein each major category corresponds to at least one event type classification model; and the event type classification model is pre-built based on the inspection item.
[0030] Specifically, when matching a code segment for an inspection item, a corresponding number of event type classification models are constructed based on the specific circumstances of the inspection item; when determining the event type, the major category of the inspection item (account password, authentication authorization, log audit, etc.) can be specified first, and then the content of the description text is analyzed through at least one classification model to determine the characteristic value of the decision tree and thus determine the sub-categories of the inspection steps, such as whether the description text contains password length, password expiration time, etc.
[0031] In this embodiment, generating a parameter list corresponding to each inspection step further includes: generating the parameter list based on a term list and / or named entity recognition.
[0032] Specifically, for the inspection item "Check the complexity of the password, the number of uppercase letters, lowercase letters, numbers and special characters in the password content is at least 1", the parameter extraction model can be used to extract the parameters involved in the parameter list as the number of uppercase letters, the number of lowercase letters, the number of numbers and the number of special characters.
[0033] Step S103, searching for relevant code segments: using the knowledge graph, based on the event type and the parameter list, searching for each of the inspection steps to obtain the most relevant N relevant code segments; wherein N is greater than or equal to 1.
[0034] In this embodiment, obtaining the most relevant N related code segments after the search further includes: calculating the similarity scores of the nodes in the knowledge graph based on the similarity of the event types and / or the number of identical parameters in the parameter list; and screening the code segments corresponding to the top N nodes with the highest scores as relevant code segments based on the similarity scores.
[0035] Step S104 , generating candidate code segments: based on the parameter list, modifying the relevant code segments to generate candidate code segments.
[0036] In this embodiment, generating the candidate code segment further includes: determining the parameter position in the relevant code segment based on a parameter identification and binding model; and modifying the code at the parameter position in the relevant code segment based on the parameter list to generate the candidate code segment.
[0037] Specifically, for a new configuration check item "Check the complexity of the password. The password must contain at least 1 uppercase letter and lowercase letter, and at least 2 numbers and special characters." We found N nodes with a high degree of similarity. The description text of one of the nodes is "Check the complexity of the password. The number of uppercase letters, lowercase letters, numbers and special characters in the password must be at least 1." We can modify the script program segment of the node, bind and modify the parameters one by one, and change "at least 1" to "at least 2" to generate a candidate code segment.
[0038] In this embodiment, the method further includes:
[0039] Step S105, constructing a knowledge graph: generating the knowledge graph based on at least one inspection step in an existing inspection project and its corresponding event type, parameter list and code segment; wherein each operation step node in the knowledge graph is respectively connected to the corresponding parameter list, event type and code segment.
[0040] Specifically, as shown in the knowledge graph in Figure 2, each operation step node in the knowledge graph has four edges connecting to other nodes. These three edges represent three types of connections: the parameter name, the corresponding script segment, and the event type. Different operation step nodes are indirectly connected through the parameter name node and the event type node.
[0041] FIG2 is a schematic diagram of the structure of a knowledge graph-based inspection code reuse device 200 according to an embodiment of the present invention. As shown in FIG2 , the device 200 may include:
[0042] The description document acquisition module 201 is configured to acquire a description document corresponding to at least one inspection step in an inspection item.
[0043] The event type and parameter list generating module 202 is configured to obtain a description document corresponding to at least one inspection step in an inspection item.
[0044] The related code segment search module 203 is configured to: use the knowledge graph, based on the event type and the parameter list, to search for each of the inspection steps to obtain the most relevant N related code segments; wherein N is greater than or equal to 1.
[0045] The candidate code segment generating module 204 is configured to modify the relevant code segments based on the parameter list to generate candidate code segments.
[0046] The present invention also provides an electronic device 300. FIG3 is a schematic diagram of an electronic device 300 according to an embodiment of the present invention. As shown in FIG3 , the electronic device 300 includes a processor 310 and a memory 320. The memory 320 stores instructions, wherein the instructions, when executed by the processor 310, implement the method 100 described above.
[0047] The present invention further provides a computer-readable storage medium having computer instructions stored thereon. When the computer instructions are executed, the method described above is executed.
[0048] The present invention also provides a computer program product, comprising a computer program, which implements the above-mentioned method when executed by a processor.
[0049] Some aspects of the methods and apparatus of the present invention may be performed entirely by hardware, entirely by software (including firmware, resident software, microcode, etc.), or by a combination of hardware and software. The above hardware or software may be referred to as "data blocks," "modules," "engines," "units," "components," or "systems." The processor may be one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DAPDs), programmable logic devices (PLCs), field programmable gate arrays (FPGAs), processors, controllers, microcontrollers, microprocessors, or combinations thereof. In addition, various aspects of the present invention may be embodied as computer products in one or more computer-readable media, the product including computer-readable program code. For example, computer-readable media may include, but are not limited to, magnetic storage devices (e.g., hard disks, floppy disks, tapes, etc.), optical disks (e.g., compact disks (CDs), digital versatile disks (DVDs), etc.), smart cards, and flash memory devices (e.g., cards, sticks, key drives, etc.).
[0050] Flowcharts are used herein to illustrate the operations performed by the methods according to the embodiments of the present application. It should be understood that the preceding operations are not necessarily performed in exact order. Instead, the various steps may be performed in reverse order or simultaneously. Furthermore, other operations may be added to these processes, or one or more operations may be removed from these processes.
[0051] It should be understood that although this specification is described according to various embodiments, not every embodiment contains only one independent technical solution. This narrative method of the specification is only for the sake of clarity. Those skilled in the art should regard the specification as a whole. The technical solutions in each embodiment can also be appropriately combined to form other implementation methods that can be understood by those skilled in the art.
[0052] The above description is only an illustrative embodiment of the present invention and is not intended to limit the scope of the present invention. Any equivalent changes, modifications and combinations made by those skilled in the art without departing from the concept and principle of the present invention shall fall within the scope of protection of the present invention.
Claims
1. A knowledge graph-based inspection code reuse method (100), characterized in that: The method comprises: Obtaining a description document corresponding to at least one inspection step in an inspection item (S101); Generate event type and parameter list (S102): Generate event type and parameter list corresponding to each inspection step based on the description document using the event type classification model and parameter list extraction model; Searching for relevant code segments (S103): using the knowledge graph, based on the event type and the parameter list, searching for each of the inspection steps to obtain the most relevant N relevant code segments; wherein N is greater than or equal to 1; Generating candidate code segments (S104): Based on the parameter list, modifying the relevant code segments to generate candidate code segments.
2. The method according to claim 1, characterized in that Generating the event type corresponding to each inspection step further includes: Determine the corresponding major category based on the inspection items; Based on the major categories, determining minor categories of inspection steps in the inspection items through a pre-built event type classification model; Each of the major categories corresponds to at least one event type classification model; the event type classification model is pre-constructed based on the inspection items.
3. The method according to claim 1, characterized in that Generating a parameter list corresponding to each inspection step further includes: The parameter list is generated based on a glossary and / or named entity recognition.
4. The method according to claim 1, wherein The method further comprises: Constructing a knowledge graph (S105): generating the knowledge graph based on at least one inspection step in an existing inspection project and its corresponding event type, parameter list, and code segment; Among them, each operation step node in the knowledge graph is connected to the corresponding parameter list, event type and code segment.
5. The method according to claim 1, wherein Generating candidate code segments further includes: Determining parameter locations in the relevant code segments based on a parameter identification and binding model; The candidate code segment is generated by modifying the code at the parameter position in the relevant code segment based on the parameter list.
6. The method according to claim 4, characterized in that Obtaining the most relevant N code segments after searching further includes: Calculating similarity scores of nodes in the knowledge graph based on similarity of event types and / or the number of identical parameters in the parameter list; Based on the similarity scores, code segments corresponding to the top N nodes with the highest scores are selected as relevant code segments.
7. A knowledge graph-based inspection code reuse device (200), characterized in that: The device further comprises: A description document acquisition module (201) is configured to acquire a description document corresponding to at least one inspection step in an inspection item; An event type and parameter list generating module (202) is configured to: generate an event type and parameter list corresponding to each inspection step based on the description document using an event type classification model and a parameter list extraction model; The relevant code segment search module (203) is configured to: utilize the knowledge graph, based on the event type and the parameter list, search for each of the inspection steps and obtain the most relevant N relevant code segments; wherein N is greater than or equal to 1; The candidate code segment generating module (204) is configured to: based on the parameter list, modify the relevant code segments to generate candidate code segments.
8. An electronic device (300), comprising: at least one processor (310); as well as A memory (320) coupled to the at least one processor (310), the memory (320) being configured to store instructions, which, when executed by the at least one processor (310), causes the processor (310) to perform the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having computer instructions stored thereon, wherein the computer instructions, when executed, perform the method according to any one of claims 1 to 6.
10. A computer program product, characterized in that The invention comprises a computer program, which implements the method according to any one of claims 1 to 6 when being executed by a processor.
Citation Information
Patent Citations
Code program recommendation method and device, electronic equipment and storage medium
CN114546360A
API knowledge graph self-adaptive construction and intelligent question and answer method based on mixed mode
CN114840657A
Spacecraft test script automatic generation method based on deep learning
CN115964273A
Intelligent auxiliary method and platform for software development
CN117369786A
Syntax Based Source Code Search
US20190303141A1
Cited By
Private intelligent code collaborative optimization method and system based on graph retrieval
CN121326348A