Code knowledge graph construction method and device, code knowledge graph query method and device, equipment and medium

By building a code knowledge graph, obtaining and displaying the calling relationship between code snippets, the problem of inefficient utilization of existing code bases is solved and development efficiency is improved.

CN120448591APending Publication Date: 2025-08-08BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510360170.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-25
Publication Date
2025-08-08

AI Technical Summary

Technical Problem

As static storage and retrieval tools, existing code libraries are difficult to make full use of existing code resources, resulting in inefficiency among developers.

Method used

By building a code knowledge graph, obtain the call chain information in the target program, and convert it into structured data and import it into the graph database to display the call relationship between code snippets.

Benefits of technology

It significantly improves the efficiency of developers reading and debugging code, reduces time consumption, and enhances the efficiency of code resource utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120448591A_ABST
    Figure CN120448591A_ABST
Patent Text Reader

Abstract

The invention relates to a code knowledge graph construction method and device, a code knowledge graph query method and device, equipment and a medium. According to the method, the call chain information in the target program is obtained, and the call chain information comprises the structure information of the at least two code snippets with the call relation and the call time and the call sequence corresponding to the call relation. Further, converting the call chain information into structured data, and importing the structured data into a graph database to obtain a code knowledge graph, so that any node in the code knowledge graph corresponds to any code snippet in the at least two code snippets; edges in the code knowledge graph represent calling relations among different code snippets. In other words, the calling relation between the code snippets can be visually displayed through the code knowledge graph, the time of reading and debugging the codes by developers is shortened, and the development efficiency is remarkably improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computers, and in particular to a method, apparatus, device, and medium for constructing and querying a code knowledge graph. Background Art

[0002] In today's software development, code reuse and management play a vital role in improving development efficiency and code quality.

[0003] However, the current code library is only a tool for statically storing and retrieving code snippets, which has certain limitations. This makes it difficult for developers to fully utilize existing code resources, thereby reducing development efficiency. Summary of the Invention

[0004] In order to solve the above technical problems or at least partially solve the above technical problems, the present disclosure provides a method, device, equipment and medium for constructing and querying a code knowledge graph. Through the code knowledge graph, the calling relationship between code fragments can be intuitively displayed, which reduces the time developers spend reading and debugging code and significantly improves development efficiency.

[0005] In a first aspect, an embodiment of the present disclosure provides a method for constructing a code knowledge graph, the method comprising:

[0006] Acquire call chain information in the target program, the call chain information including structural information of at least two code snippets having a call relationship, and a call time and a call sequence corresponding to the call relationship;

[0007] Converting the call chain information into structured data;

[0008] The structured data is imported into a graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0009] In a second aspect, an embodiment of the present disclosure provides a query method, the method comprising:

[0010] Obtaining a query request, wherein the query request includes structural information of a target code snippet;

[0011] According to the structural information of the target code fragment, the code knowledge graph is queried to obtain the calling path of the target code fragment. The code knowledge graph is obtained by converting the call chain information in the target program into structured data and importing the structured data into a graph database. The call chain information includes the structural information of at least two code fragments with a calling relationship, and the calling time and calling sequence corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0012] In a third aspect, an embodiment of the present disclosure provides a device for constructing a code knowledge graph, the device comprising:

[0013] An acquisition module is used to acquire call chain information in a target program, wherein the call chain information includes structural information of at least two code snippets having a call relationship, and a call time and a call sequence corresponding to the call relationship;

[0014] A conversion module, configured to convert the call chain information into structured data;

[0015] An import module is used to import the structured data into a graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0016] In a fourth aspect, an embodiment of the present disclosure provides a query device, the device comprising:

[0017] An acquisition module, configured to acquire a query request, wherein the query request includes structural information of a target code snippet;

[0018] A query module is used to query the code knowledge graph based on the structural information of the target code fragment to obtain the calling path of the target code fragment. The code knowledge graph is obtained by converting the call chain information in the target program into structured data and importing the structured data into a graph database. The call chain information includes the structural information of at least two code fragments with a calling relationship, and the calling time and calling order corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0019] In a fifth aspect, an embodiment of the present disclosure provides an electronic device, including:

[0020] Memory;

[0021] processor; and

[0022] computer programs;

[0023] The computer program is stored in the memory and is configured to be executed by the processor to implement the method as described in the first aspect and the second aspect.

[0024] In a sixth aspect, an embodiment of the present disclosure provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to implement the methods described in the first and second aspects.

[0025] In a seventh aspect, an embodiment of the present disclosure provides a computer program product, including a computer program, which implements the methods described in the first and second aspects when executed by a processor.

[0026] The code knowledge graph construction, query method, apparatus, device and medium provided by the embodiments of the present disclosure obtain call chain information in the target program, and the call chain information includes structural information of at least two code snippets with a call relationship, and the call time and call sequence corresponding to the call relationship. Further, the call chain information is converted into structured data, and the structured data is imported into a graph database to obtain a code knowledge graph, so that any node in the code knowledge graph corresponds to any code snippet in the at least two code snippets, and the edges in the code knowledge graph represent the call relationship between different code snippets. In other words, the call relationship between code snippets can be intuitively displayed through the code knowledge graph, which reduces the time developers spend reading and debugging code and significantly improves development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0028] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0029] Figure 1 A flowchart of a method for constructing a code knowledge graph provided in an embodiment of the present disclosure;

[0030] Figure 2 A schematic diagram of an application scenario provided by an embodiment of the present disclosure;

[0031] Figure 3A flowchart of a method for constructing a code knowledge graph provided in an embodiment of the present disclosure;

[0032] Figure 4 A flowchart of a query method provided by an embodiment of the present disclosure;

[0033] Figure 5 A schematic diagram of a code knowledge graph provided in an embodiment of the present disclosure;

[0034] Figure 6 A flowchart of a method for constructing and querying a code knowledge graph provided in an embodiment of the present disclosure;

[0035] Figure 7 A schematic diagram of the structure of a device for constructing a code knowledge graph provided in an embodiment of the present disclosure;

[0036] Figure 8 A schematic diagram of the structure of a query device provided in an embodiment of the present disclosure;

[0037] Figure 9 A schematic diagram of the structure of an electronic device embodiment provided by the present disclosure. DETAILED DESCRIPTION

[0038] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features therein can be combined with each other in the absence of conflict.

[0039] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.

[0040] In today's software development, code reuse and management play a vital role in improving development efficiency and code quality. However, current code libraries are merely tools for statically storing and retrieving code snippets, which have certain limitations. This makes it difficult for developers to fully utilize existing code resources, thereby reducing development efficiency. To address this issue, the present disclosure provides a method for constructing a code knowledge graph, which is described below in conjunction with specific examples.

[0041] Figure 1A flow chart of a method for constructing a code knowledge graph provided in an embodiment of the present disclosure. The method is applied to a terminal device, or to a server or server cluster. Specifically, the method can be executed by a device for constructing a code knowledge graph, which can be implemented in software and / or hardware, and can be configured on a terminal device, such as a mobile phone, a PDA, a tablet computer, a wearable device with a display screen, a desktop computer, a laptop computer, an all-in-one machine, a smart home device, etc. Alternatively, the device can be configured on a server or a server cluster, wherein a server cluster can be a plurality of servers brought together to perform the same service, which appears to the client as if there is only one server. A server cluster can utilize multiple computers for parallel computing to obtain a higher computing speed, or it can use multiple computers for backup, so that the entire system can still operate normally after any machine breaks down. Specifically, the method can be applied to Figure 2 The application scenario shown includes a terminal device 21 and a server 22. In a feasible implementation, the terminal device 21 stores a target program, or a target program is stored in a database, and the terminal device 21 obtains the target program from the database, and the target program includes multiple code snippets. The terminal device 21 constructs a code knowledge graph about code snippets based on the target program. In another feasible implementation, the terminal device 21 stores a target program, and the server 22 constructs a code knowledge graph about code snippets based on the target program. Alternatively, the server 22 stores a target program, or a target program is stored in a database, and the server 22 obtains the target program from the database, and further, the server 22 constructs a code knowledge graph about code snippets based on the target program. The following introduces the method for constructing the code knowledge graph provided by the embodiment of the present disclosure in conjunction with the illustrations. Figure 1 As shown, the specific steps of this method are as follows:

[0042] S101. Acquire call chain information in a target program, where the call chain information includes structural information of at least two code snippets with a call relationship, and a call time and a call sequence corresponding to the call relationship.

[0043] For example, server 22 stores a target program that includes multiple code snippets. Server 22 can create an agent program that dynamically tracks the call relationships between different code snippets in the target program and records call chain information. This call chain information includes structural information about at least two code snippets with a call relationship, as well as the call time and call sequence corresponding to the call relationship. Specifically, the call relationships between different code snippets include static call relationships and dynamic call relationships. Static call relationships refer to determining the call relationships between functions, methods, or subroutines by analyzing source code or compiled intermediate code without actually running the program. Static call relationships are commonly used in scenarios such as code analysis, architecture understanding, and dependency management. For object-oriented languages such as Java or C++, virtual functions or interfaces may need to be processed. In these cases, static analysis may not be able to determine the specific method being called because polymorphism requires runtime determination. In this case, static call relationships may only analyze method calls within interfaces or base classes, but not the implementation of specific subclasses. In this case, more complex analysis, such as class hierarchy analysis or pointer analysis, may be required. Analysis of static call relationships requires considering the code structure, parsing function definitions and calls, and constructing a call graph. The call graph is typically a directed graph, with nodes representing functions and edges representing call relationships. Such a graph can help identify circular calls, unused functions, or complex dependencies. For Java, static call graphs can be generated using tools such as the Java package structure analysis tool (JDepend) or the Java static analysis framework (Soot). For C++, call graphs can be generated using the online class browser (Doxygen). While static call graphs may not fully cover all possible runtime behaviors, they are still helpful for understanding code structure and potential dependencies. Dynamic call graphs determine the call dependencies between functions, methods, or modules by monitoring the actual execution flow during program execution. Unlike static analysis, dynamic analysis relies on runtime program behavior and can capture dynamic features such as conditional branching, polymorphism, and reflection. Specifically, dynamic call graphs are obtained by running the program and monitoring the execution process through instrumentation, debugging, and logging. The order of function calls during program execution is recorded. For example, if A calls B, and B calls C, then ABC forms a call chain. In the disclosed embodiments, dynamic call relationships can be obtained through instrumentation. Instrumentation involves inserting monitoring logic into a program or code to collect runtime information, such as function calls, variable values, and performance data. For example, logs, counters, or tracing logic can be inserted into key locations in the target code, such as function entry and exit points, loop bodies, and exception handling. The inserted code records execution paths, parameters, return values, resource usage, and more.Specifically, there are two main types of instrumentation. One type is source code instrumentation, for example, directly modifying the source code. The other type is binary instrumentation, for example, inserting monitoring logic into a compiled binary file or runtime. In some embodiments, the instrumentation type can be selected based on needs, for example, binary instrumentation can be selected through performance analysis. In addition, embodiments of the present disclosure do not require global instrumentation in the program or code; for example, instrumentation in key functions or key code blocks can be sufficient.

[0044] Optionally, the structural information of any code snippet includes the class name or member method name corresponding to the code snippet. In addition, the structural information of any code snippet also includes the package path and attribute information corresponding to the code snippet.

[0045] For example, an agent program tracks code snippet A in the target program. When the agent program detects that code snippet A is being called, it queries the upstream code snippet that called code snippet A. For example, if the upstream code snippet is code snippet B, then code snippet B and code snippet A form a call chain. The agent program generates call chain information based on this call. This call chain information includes the structural information of code snippet A, the structural information of code snippet B, the call time, and the call order of this call. This call order refers to code snippet B calling code snippet A, that is, code snippet B pointing to code snippet A. It should be understood that this is only a schematic illustration of code snippet B calling code snippet A. The target program includes many code snippets, and the call relationship between different code snippets within these multiple code snippets can be a relatively complex process. Specifically, the same code snippet may be called by multiple upstream code snippets, and the same code snippet may also call multiple downstream code snippets. In addition, calls are not limited to occurring between two code snippets; they can also occur between multiple code snippets. For example, code snippet A calls code snippet B, code snippet B calls code snippet C, and code snippet C calls code snippet D. Therefore, code snippets A, B, C, and D constitute a call chain. Each time a call occurs or a call chain appears, the Agent program can record a call chain information item. This call chain information item can also be referred to as a call chain log or call chain data. Furthermore, the Agent program can also store each call chain information item it records in a pre-defined storage medium, such as a log file or a database. For example, the Agent program can store each call chain information item it records in a database. In this case, the call chain information item can also be recorded as a database record. Server 22 can retrieve each call chain information item recorded by the Agent program from the database.

[0046] S102: Convert the call chain information into structured data.

[0047] When the server 22 obtains any call chain information from the database, the server 22 converts the call chain information into structured data. For example, the server 22 converts the call chain information into structured data through a data parsing program. For example, the server 22 converts the call chain information into data in the Javascript Object Notation (JSON) format, data in the Comma-Separated Values (CSV) format, or data stored in other formats. Specifically, the structured data may include necessary information, such as the class name or member method name corresponding to the upstream code snippet, the class name or member method name corresponding to the called code snippet, the call time and the call order, etc.

[0048] In addition, the server 22 can also clean and filter the call chain information stored in the database to remove redundant and useless call chain information, such as removing call information of library functions or call paths with extremely low frequency. In addition, the server 22 can also deduplicate duplicate call chain information in the database. For example, if there are multiple records of code snippet B calling code snippet A, one of them will be retained to ensure the consistency of the call chain information.

[0049] S103. Import the structured data into a graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0050] For example, the embodiment of the present disclosure may choose a graph database to store structured data. The graph database includes nodes (Vertex) and edges (Edge). The nodes in the graph database represent code snippets, and the edges in the graph database represent the calling relationships between different code snippets. Specifically, the server 22 imports the structured data described above into the graph database through a data import script, thereby obtaining a code knowledge graph, so that any node in the code knowledge graph corresponds to any code snippet in the call chain, and the edges in the code knowledge graph represent the calling relationships between different code snippets in the call chain. For example, the server 22 imports the structured data described above into the graph database through the Application Programming Interface (API) or batch import tool provided by the graph database. The attribute information of the nodes in the code knowledge graph can include not only the structural information of the code snippets, but also metadata such as the usage scenarios and functional descriptions of the code snippets.

[0051] Optionally, any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

[0052] For example, each code snippet in the target program can be a code snippet at the class level, or a code snippet at the member method level. Since any node in the code knowledge graph corresponds to any code snippet in the call chain, the edge in the code knowledge graph represents the calling relationship between different code snippets in the call chain. Therefore, any node in the code knowledge graph can represent the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes, or the calling relationship between different member methods. In other words, the attribute information of the edge in the code knowledge graph includes the calling relationship between different classes, or the calling relationship between different member methods. In addition, the attribute information of the edge in the code knowledge graph can also include the calling time, calling order, etc.

[0053] The embodiment of the present disclosure obtains the call chain information in the target program, and the call chain information includes the structural information of at least two code snippets with a call relationship, and the call time and call order corresponding to the call relationship. Furthermore, the call chain information is converted into structured data, and the structured data is imported into a graph database to obtain a code knowledge graph, so that any node in the code knowledge graph corresponds to any code snippet in the at least two code snippets, and the edges in the code knowledge graph represent the call relationship between different code snippets. In other words, the call relationship between code snippets can be intuitively displayed through the code knowledge graph, which reduces the time developers spend reading and debugging code and significantly improves development efficiency.

[0054] Optionally, after importing the structured data into a graph database to obtain a code knowledge graph, the method further includes: obtaining updated call chain information in the target program; and updating the code knowledge graph based on the updated call chain information.

[0055] For example, as new functions are developed, code snippets in the target program may change. The server 22 may periodically run the Agent program to dynamically capture new call chain information and update the code knowledge graph based on the new call chain information. For example, a new code snippet is added to the target program, which triggers a new call relationship, thereby generating new call chain information. Based on the new call chain information, new nodes and new edges are added to the code knowledge graph. Alternatively, an existing code snippet in the target program is changed, triggering a new call relationship, thereby generating new call chain information. Based on the new call chain information, the existing nodes and edges in the code knowledge graph are modified.

[0056] Based on the above embodiment, the call chain information in the target program is obtained, including: Figure 3 The following steps are shown:

[0057] S301: Insert monitoring codes into each of the multiple code segments included in the target program.

[0058] For example, the code snippets in the target program are Java code. The Agent program created by server 22 is used to dynamically capture the call chain of the Java code. Therefore, the Agent program can also be called a Java Agent. Specifically, the target program runs in a container. Before the target program starts, the container loads the premain method. Additionally, the agentmain method is loaded using an append method while the target program is running. The premain method is primarily used to dynamically change and manipulate bytecode. The agentmain method is primarily used to dynamically insert and update agent packages while the target program is running. Furthermore, the Agent program's classes and properties can be stored in a designated file. The container reads the Agent program's classes and properties from the designated file and then loads the premain method and the agentmain method. This allows the Agent program to implement either the premain method or the agentmain method.

[0059] Specifically, the server 22 can use an agent program to insert monitoring code into each of the multiple code snippets included in the target program. For example, Java bytecode manipulation libraries can be used to enhance or modify the bytecode of a class. These libraries can be used to insert specific monitoring code during class loading to capture the call chain. Furthermore, the current call chain is maintained using a stack data structure. The call chain information is printed or recorded. Furthermore, the captured call chain information is stored in a suitable storage medium, such as a log file or database.

[0060] S302: Monitor, according to the monitoring code in any code snippet among the multiple code snippets, whether the any code snippet is called.

[0061] For example, the target program includes multiple code snippets. Taking any code snippet among the multiple code snippets, such as code snippet A, code snippet A includes monitoring code. When code snippet A is called, the monitoring code will be executed. At this time, the monitoring code monitors that code snippet A is called.

[0062] S303: When any of the code snippets is called, query the upstream code snippet that calls the any of the code snippets.

[0063] For example, after the monitoring code detects that code snippet A is called, the monitoring code further searches for the upstream code snippet that called code snippet A according to the call stack. For example, the upstream code snippet is code snippet B.

[0064] S304. Generate call chain information based on any code snippet and the upstream code snippet, where the call chain information includes structural information of any code snippet, structural information of the upstream code snippet, and the calling time and calling order of the upstream code snippet calling any code snippet.

[0065] For example, call chain information is generated based on code fragment A and code fragment B, and the call chain information includes structure information of code fragment A, structure information of code fragment B, calling time and calling order of code fragment B calling code fragment A.

[0066] In addition, the Agent program can not only record the calling relationship between code fragments, but also record the data dependency, logical dependency and other relationships between code fragments. In other words, the Agent program can record the reference relationship between code fragments, which includes calling relationship, data dependency, logical dependency and other relationships.

[0067] The disclosed embodiments use an agent program to dynamically track the call relationships between different code snippets in a target program, helping developers parse and understand the target program's execution flow and analyze the dependencies between code snippets. Because call chains are captured through dynamic tracing, complex relationships between code snippets can be accurately captured.

[0068] Figure 4 This is a flow chart of a query method provided by another embodiment of the present disclosure. The method is applied to Figure 2 The application scenario shown in FIG. Specifically, the method can be Figure 2 The server 22 shown in FIG. 1 is executed. The specific steps of the method are as follows:

[0069] S401: Obtain a query request, where the query request includes structural information of a target code segment.

[0070] For example Figure 2 As shown, the terminal device 21 sends a query request to the server 22, the query request including the structural information of the target code snippet. Optionally, the structural information of the target code snippet includes the class name or member method name corresponding to the target code snippet. Specifically, the server 22 receives the query request.

[0071] S402. According to the structural information of the target code snippet, query the code knowledge graph to obtain the calling path of the target code snippet. The code knowledge graph is obtained by converting the calling chain information in the target program into structured data and importing the structured data into a graph database. The calling chain information includes the structural information of at least two code snippets with a calling relationship, and the calling time and calling sequence corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code snippet of the at least two code snippets, and the edges in the code knowledge graph represent the calling relationship between different code snippets.

[0072] For example, after the server 22 receives the query request, it queries the code knowledge graph as described above based on the structural information of the target code snippet to obtain the call path of the target code snippet. The construction process of the code knowledge graph, as well as the structure and stored content of the code knowledge graph are as described above and will not be repeated here. Since the code knowledge graph adopts a graph database or a similar efficient data structure, the code knowledge graph can support complex query and analysis operations. Specifically, after the server 22 receives the query request, it uses the query language of the graph database to query the call chain. For example, when the structural information of the target code snippet includes the class name corresponding to the target code snippet, the server 22 queries the code knowledge graph based on the class name, determines the node in the code knowledge graph that stores the class name, and determines the call relationship between the class corresponding to the class name and other classes based on the edge between the node and other nodes, thereby forming a call path including the class and other classes. When the structural information of the target code snippet includes the member method name corresponding to the target code snippet, the server 22 queries the code knowledge graph based on the member method name, determines the node in the code knowledge graph that stores the member method name, and determines the calling relationship between the member method corresponding to the member method name and other member methods based on the edge between the node and other nodes, thereby forming a calling path including the member method and other member methods.

[0073] For example Figure 5What is shown is a schematic diagram of a code knowledge graph, which includes multiple nodes, including nodes corresponding to member method names and nodes corresponding to class names. For example, "Person", "student" and "teacher" are class names respectively. "is_adult", "validate_age" and "birthday" are member method names respectively. Among them, "is_adult" is used to determine whether the person is an adult, "validate_age" is used to verify age, and "birthday" is used to query birthday. In addition, the code knowledge graph also includes multiple edges, each edge has an arrow, and the direction of the arrow indicates the calling relationship between the nodes. For example, the arrowed edge between "is_adult" and "validate_age" indicates that "is_adult" calls "validate_age". When the server 22 receives the query request, it extracts the structural information of the target code snippet that the user wants to query from the query request. When the structural information of the target code snippet includes the class name corresponding to the target code snippet, for example, the query request includes "Person", the server 22 queries the code knowledge graph based on the class name, determines the node in the code knowledge graph that stores the class name, and determines the calling relationship between the class corresponding to the class name and the other classes based on the edge between the node and the other nodes, thereby forming a calling path including the class and the other classes, for example, the calling path is "Person"-"student"-"teacher". When the structural information of the target code snippet includes the member method name corresponding to the target code snippet, for example, the query request includes "validate_age", the server 22 queries the code knowledge graph based on the member method name, determines the node in the code knowledge graph that stores the member method name, and determines the calling relationship between the member method corresponding to the member method name and the other member methods based on the edge between the node and the other nodes, thereby forming a calling path including the member method and the other member methods, for example, the calling path is "is_adult"-"validate_age"-"birthday".

[0074] Optionally, any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

[0075] The disclosed embodiment uses the structural information of the target code snippet to query the code knowledge graph and obtain the call path of the target code snippet. This avoids developers having to read the target code to understand the call path of the target code snippet, reduces the time developers spend reading and debugging code, and significantly improves development efficiency.

[0076] Figure 6 This is a flowchart of the construction of a code knowledge graph and a query method provided by another embodiment of the present disclosure. Figure 6 As shown, the method includes the following steps:

[0077] S501. Create an Agent program.

[0078] S502: Insert a monitoring program into the target program.

[0079] S503: Insert a monitoring program into the code snippet at the class level.

[0080] S504: Insert a monitoring program into the code snippet at the member method level.

[0081] S505: When a code snippet is called, query the upstream code snippet and generate call chain information.

[0082] S506: Write the call chain information into the memory queue.

[0083] S507: Start a monitoring thread to monitor the memory queue.

[0084] S508: After deduplicating the call chain information in the memory queue, the deduplicated call chain information is reported to the analysis service.

[0085] S509: The analysis service receives the deduplicated call chain information.

[0086] S510: Write the deduplicated call chain information into the analysis queue.

[0087] S511: Start the analysis thread to monitor the analysis queue.

[0088] S512, data cleaning and filtering.

[0089] S513: Convert the call chain information into structured data.

[0090] S514: Import the structured data into the graph database.

[0091] S515. Query the reference relationship of the class.

[0092] S516. Obtain detailed information of the referencing class and the referenced class.

[0093] It can be understood that the implementation principles and specific processes of S501-S516 refer to the above-mentioned embodiments and will not be repeated here.

[0094] The disclosed embodiments directly capture real call chain information at runtime through an Agent program, avoiding the timeliness issues of static analysis or offline logs and ensuring the accuracy of the analysis results. Furthermore, by inserting monitoring code into the target program to capture call chain information, the target program does not need to modify the source code, ensuring seamless integration with existing code bases. This provides high flexibility and adaptability to various development frameworks and libraries. Storing and managing call chain information in the form of a code knowledge graph allows for intuitive display of the call relationships between member methods and classes. Furthermore, the code knowledge graph not only preserves call relationships but also enables complex queries and analysis. Using this rich query and analysis tool, the code knowledge graph allows for in-depth analysis down to the member method level, revealing the execution paths, dependencies, call relationships, and other reference relationships between code snippets in the target program. These reference relationships also facilitate analysis of performance issues in the target program, enabling developers to optimize and refactor the target program in a timely manner, improving its quality and performance. Maintenance personnel can use the code knowledge graph to quickly locate problematic code snippets, analyze their context and dependencies, and quickly resolve and troubleshoot anomalies. Furthermore, the Code Knowledge Graph, a shared map of code structure and call relationships, helps team members better understand and communicate code, enabling knowledge sharing and experience transfer. Through in-depth call chain analysis and optimization, redundant calls and resource waste are reduced, improving system efficiency and hardware resource utilization.

[0095] Figure 7 The schematic diagram of the structure of the device for constructing the code knowledge graph provided by the embodiment of the present disclosure. The device for constructing the code knowledge graph provided by the embodiment of the present disclosure can execute the processing flow provided by the embodiment of the method for constructing the code knowledge graph, such as Figure 7 As shown, the code knowledge graph construction device 60 includes:

[0096] An acquisition module 61 is configured to acquire call chain information in a target program, wherein the call chain information includes structural information of at least two code snippets having a call relationship, and a call time and a call sequence corresponding to the call relationship;

[0097] A conversion module 62, configured to convert the call chain information into structured data;

[0098] The import module 63 is used to import the structured data into the graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0099] Optionally, the structural information of any code snippet includes a class name or a member method name corresponding to the any code snippet.

[0100] Optionally, any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

[0101] Optionally, when the acquisition module 61 acquires the call chain information in the target program, it is specifically used to:

[0102] Inserting monitoring codes into each of the plurality of code segments included in the target program;

[0103] monitoring, according to a monitoring code in any code snippet among the plurality of code snippets, whether the any code snippet is called;

[0104] When any of the code snippets is called, querying the upstream code snippet that calls the any of the code snippets;

[0105] Call chain information is generated according to any one of the code snippets and the upstream code snippet, wherein the call chain information includes structural information of any one of the code snippets, structural information of the upstream code snippet, and the calling time and calling order of the upstream code snippet calling any one of the code snippets.

[0106] Optionally, the device 60 for constructing a code knowledge graph also includes an update module 64. The import module 63 imports the structured data into the graph database to obtain the code knowledge graph. The acquisition module 61 is also used to obtain the updated call chain information in the target program; the update module 64 is used to update the code knowledge graph according to the updated call chain information.

[0107] Figure 7 The device for constructing a code knowledge graph in the illustrated embodiment can be used to execute the technical solution of the above-mentioned method embodiment. Its implementation principle and technical effects are similar and will not be repeated here.

[0108] Figure 8 The query device provided by the embodiment of the present disclosure can execute the processing flow provided by the query method embodiment, such as Figure 8 As shown, the query device 70 includes:

[0109] An acquisition module 71 is configured to acquire a query request, wherein the query request includes structural information of a target code segment;

[0110] The query module 72 is used to query the code knowledge graph based on the structural information of the target code fragment to obtain the calling path of the target code fragment. The code knowledge graph is obtained by converting the call chain information in the target program into structured data and importing the structured data into a graph database. The call chain information includes the structural information of at least two code fragments with a calling relationship, and the calling time and calling order corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

[0111] Optionally, the structural information of the target code snippet includes a class name or a member method name corresponding to the target code snippet.

[0112] Optionally, any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

[0113] Figure 8 The query device of the illustrated embodiment can be used to execute the technical solution of the above-mentioned method embodiment. Its implementation principle and technical effects are similar and will not be described in detail here.

[0114] The above describes the internal functions and structure of the code knowledge graph construction device and the query device, which can be implemented as an electronic device. Figure 9 This is a schematic diagram of the structure of an electronic device embodiment provided by the present disclosure. Figure 9 As shown, the electronic device includes a memory 81 and a processor 82 .

[0115] The memory 81 is used to store programs. In addition to the aforementioned programs, the memory 81 may also be configured to store various other data to support operations on the electronic device. Examples of such data include instructions for any application or method operating on the electronic device, contact data, phone book data, messages, images, videos, etc.

[0116] The memory 81 can be implemented by any type of volatile or non-volatile memory device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.

[0117] The processor 82 is coupled to the memory 81 and executes the program stored in the memory 81 to implement the technical solution of the above method embodiment.

[0118] Further, if Figure 9 As shown, the electronic device may further include: a communication component 83, a power component 84, an audio component 85, a display 86 and other components. Figure 9 Only some components are shown schematically, which does not mean that the electronic device only includes Figure 9 Components shown.

[0119] The communication component 83 is configured to facilitate wired or wireless communication between the electronic device and other devices. The electronic device can access a wireless network based on a communication standard, such as WiFi, 2G or 3G, or a combination thereof. In an exemplary embodiment, the communication component 83 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 83 also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology and other technologies.

[0120] The power supply assembly 84 provides power to various components of the electronic device. The power supply assembly 84 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the electronic device.

[0121] The audio component 85 is configured to output and / or input audio signals. For example, the audio component 85 includes a microphone (MIC), which is configured to receive external audio signals when the electronic device is in an operating mode, such as a call mode, a recording mode, and a voice recognition mode. The received audio signal can be further stored in the memory 81 or transmitted via the communication component 83. In some embodiments, the audio component 85 also includes a speaker for outputting audio signals.

[0122] The display 86 includes a screen, which may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, slides, and gestures on the touch panel. The touch sensor may not only sense the boundaries of a touch or slide action, but also detect the duration and pressure associated with the touch or slide operation.

[0123] In addition, an embodiment of the present disclosure further provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the method described in the above embodiment.

[0124] Exemplary embodiments of the present disclosure further provide a computer program product, including a computer program, wherein when the computer program is executed by a processor of a computer, the computer is configured to enable the computer to implement the method described in the above embodiment.

[0125] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.

[0126] The foregoing description is intended only to provide specific embodiments of the present disclosure, intended to enable those skilled in the art to understand and implement the present disclosure. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure is not intended to be limited to the embodiments described herein, but rather to be construed in the broadest manner consistent with the principles and novel features disclosed herein.

Claims

1. A method for constructing a code knowledge graph, wherein: The method comprises: Acquire call chain information in the target program, the call chain information including structural information of at least two code snippets having a call relationship, and a call time and a call sequence corresponding to the call relationship; Converting the call chain information into structured data; The structured data is imported into a graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

2. The method according to claim 1, wherein The structural information of any code snippet includes the class name or member method name corresponding to the any code snippet.

3. The method according to claim 2, wherein: Any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

4. The method according to claim 1, wherein Get the call chain information in the target program, including: Inserting monitoring codes into each of the plurality of code segments included in the target program; monitoring, according to a monitoring code in any code snippet among the plurality of code snippets, whether the any code snippet is called; When any of the code snippets is called, querying the upstream code snippet that calls the any of the code snippets; Call chain information is generated according to any one of the code snippets and the upstream code snippet, wherein the call chain information includes structural information of any one of the code snippets, structural information of the upstream code snippet, and the calling time and calling order of the upstream code snippet calling any one of the code snippets.

5. The method according to claim 1, wherein After importing the structured data into a graph database to obtain a code knowledge graph, the method further includes: Obtaining updated call chain information in the target program; Update the code knowledge graph according to the updated call chain information.

6. A query method, wherein: The method comprises: Obtaining a query request, wherein the query request includes structural information of a target code snippet; According to the structural information of the target code fragment, the code knowledge graph is queried to obtain the calling path of the target code fragment. The code knowledge graph is obtained by converting the call chain information in the target program into structured data and importing the structured data into a graph database. The call chain information includes the structural information of at least two code fragments with a calling relationship, and the calling time and calling sequence corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

7. The method according to claim 6, wherein: The structural information of the target code fragment includes the class name or member method name corresponding to the target code fragment.

8. The method according to claim 7, wherein: Any node in the code knowledge graph represents the class or member method corresponding to any code snippet, and any edge in the code knowledge graph represents the calling relationship between different classes or the calling relationship between different member methods.

9. A device for constructing a code knowledge graph, wherein: The device comprises: An acquisition module is used to acquire call chain information in a target program, wherein the call chain information includes structural information of at least two code snippets having a call relationship, and a call time and a call sequence corresponding to the call relationship; A conversion module, configured to convert the call chain information into structured data; An import module is used to import the structured data into a graph database to obtain a code knowledge graph, wherein any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

10. A query device, wherein: The device comprises: An acquisition module, configured to acquire a query request, wherein the query request includes structural information of a target code snippet; A query module is used to query the code knowledge graph based on the structural information of the target code fragment to obtain the calling path of the target code fragment. The code knowledge graph is obtained by converting the call chain information in the target program into structured data and importing the structured data into a graph database. The call chain information includes the structural information of at least two code fragments with a calling relationship, and the calling time and calling order corresponding to the calling relationship. Any node in the code knowledge graph corresponds to any code fragment of the at least two code fragments, and the edges in the code knowledge graph represent the calling relationship between different code fragments.

11. An electronic device, wherein: include: Memory; processor; as well as computer programs; The computer program is stored in the memory and configured to be executed by the processor to implement the method according to any one of claims 1 to 8.

12. A computer-readable storage medium having a computer program stored thereon, wherein: When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.