Strategy code generation method, device and equipment
By splitting code fragments using abstract syntax trees and dependency graphs, and combining large language models and dynamic context budget allocation algorithms, the problem of context window limitation in large file conversion is solved, achieving efficient and accurate code conversion.
Patent Information
- Application Number
- CN202610169087.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-02-05
- Publication Date
- 2026-05-08
AI Technical Summary
Existing code conversion technologies are limited by the context window of large language models, making them unable to effectively handle large files. Furthermore, they are inaccurate in handling complex nested logic, resulting in low conversion accuracy and a high susceptibility to errors.
By obtaining the abstract syntax tree of the script file, the code is split into multiple fragments based on the dependency graph, and each fragment is processed separately using a large language model. The dynamic context budget allocation algorithm and dependency relationship are combined to generate strategy code.
It breaks through the context window limitation of large language models, improves the conversion accuracy and efficiency of large files, reduces the complexity of nested logic processing, and improves the overall conversion efficiency and accuracy.
Smart Images

Figure CN121996254A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of code conversion technology, and in particular to a strategy code generation method, apparatus and device. Background Technology
[0002] Current mainstream code transformation technologies include rule-based template transformation, which maps fixed patterns in source code to target code using predefined transformation rules and templates. This approach handles predefined patterns, lacks flexibility, and cannot handle complex nested logic. Another approach uses large language models for transformation, but is limited by the model's context window (typically 4K-32K tokens), making it unable to handle large files; its understanding of domain-specific languages is inaccurate, resulting in a transformation accuracy of only about 60%-70%; and complex nested logic within the overall code file is prone to errors. The underlying technical flaw of this approach is that large language models output too many tokens when processing large files, exceeding the processing capacity of the large model. Summary of the Invention
[0003] The main objective of this application is to provide a strategy code generation method, apparatus, and device that aims to solve the technical problem of code conversion using large language models being limited by context windows.
[0004] To achieve the above objectives, this application proposes a strategy code generation method, which includes: Obtain the first script file and generate the corresponding abstract syntax tree based on the first script file; Based on the dependency graph corresponding to the abstract syntax tree, and based on the dependency graph, the code of the first script file is split to obtain multiple code fragments and determine the dependency relationships between each code fragment; Each code snippet and the first prompt word are input into a preset large language model, and the large language model outputs the strategy code corresponding to each code snippet in sequence, wherein the first prompt word includes at least the dependency relationship; Assemble the strategy codes corresponding to each of the code fragments to obtain the target strategy code file.
[0005] Furthermore, to achieve the above objectives, this application also proposes a strategy code generation apparatus, which includes: The syntax tree generation module is used to obtain the first script file and generate the corresponding abstract syntax tree based on the first script file. The code splitting module is used to split the code of the first script file according to the dependency graph corresponding to the abstract syntax tree, and according to the dependency graph, to obtain multiple code fragments and determine the dependency relationship between each code fragment; The code generation module is used to input each of the code fragments and the first prompt word into a preset large language model, and the large language model sequentially outputs the strategy code corresponding to each of the code fragments, wherein the first prompt word includes at least the dependency relationship; The code assembly module is used to assemble the strategy code corresponding to each of the code fragments to obtain the target strategy code file.
[0006] In addition, to achieve the above objectives, this application also proposes a strategy code generation device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the strategy code generation method described above.
[0007] One or more technical solutions proposed in this application have at least the following technical effects: This application first obtains a first script file, generates a corresponding abstract syntax tree based on the first script file, then splits the code of the first script file according to the dependency graph corresponding to the abstract syntax tree and the dependency graph, obtaining multiple code fragments and determining the dependencies between the code fragments. Then, each code fragment and a first prompt word are input into a preset large language model, which sequentially outputs the strategy code corresponding to each code fragment. The first prompt word includes at least the dependencies. Finally, the strategy codes corresponding to each code fragment are assembled to obtain a target strategy code file. The technical solution of this application abstracts the script file into an abstract syntax tree, and then determines its dependency graph to represent the dependency relationship in the abstract syntax tree. This makes the script file splitting process based on dependency relationship and syntax, ensuring the logical integrity of the split code fragments. Then, the split code fragments are transformed separately by a large language model. Compared with the traditional solution that directly transforms the complete code, splitting reduces the number of tokens that the large language model needs to process each time. Even if the code size of the first script file is greater than the context window limit of the large language model, the amount of code processed each time can be reduced by the aforementioned code splitting strategy. Therefore, the technical solution of this application breaks through the context window limit of the large language model. Moreover, compared with the complete code block, the nested logic of the split code fragments is simpler, and the large language model is easier to process and transform, ensuring the accuracy of the strategy code file obtained by transformation. Attached Figure Description
[0008] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0009] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 This is a flowchart illustrating the strategy code generation method of this application in Embodiment 1. Figure 2 This is a flowchart illustrating the code splitting process provided in Embodiment 1 of the strategy code generation method of this application; Figure 3 A flowchart illustrating the construction of a strategy syntax parser provided in Embodiment 2 of the strategy code generation method of this application; Figure 4 This is a schematic diagram of the module structure of the strategy code generation device according to an embodiment of this application; Figure 5 This is a schematic diagram of the device structure of the hardware operating environment involved in the strategy code generation method in this application embodiment.
[0011] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0012] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.
[0013] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific embodiments.
[0014] For ease of understanding, the full English names and Chinese definitions of the abbreviations, terms, and custom nouns involved in the embodiments of this application are explained in the table below.
[0015]
[0016] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device or policy code generation device capable of performing the above functions. The following description uses a policy code generation device as an example to illustrate this embodiment and the subsequent embodiments.
[0017] Based on this, embodiments of this application provide a strategy code generation method, referring to... Figure 1 , Figure 1 This is a flowchart illustrating the first embodiment of the strategy code generation method of this application.
[0018] In this embodiment, the strategy code generation method includes steps S10 to S40: Step S10: Obtain the first script file and generate the corresponding abstract syntax tree based on the first script file; The first script file refers to the Hive SQL script file uploaded by the user through the system interface. The Hive SQL script file is a file that needs to be converted into code. The ultimate goal of this application embodiment is to convert the script file into the corresponding strategy code file. The abstract syntax tree corresponding to the first script file reflects the code content, logic, and nesting relationships within the script file. Specifically, it represents the syntax structure of the programming language in a tree-like form, with each node in the tree representing a structure in the code.
[0019] Step S20: Based on the dependency graph corresponding to the abstract syntax tree, the code of the first script file is split into multiple code segments and the dependency relationships between the code segments are determined. After the abstract syntax tree is determined, the corresponding dependency graph is further used. The dependency graph can be used to represent the dependency relationship between nodes in the abstract syntax tree. For example, the input of a certain node depends on the output of other nodes, or according to the control logic of the abstract syntax tree, there is a fixed execution order between two nodes that perform actions. Therefore, if a later node depends on an earlier node, the later node can only be executed after the earlier node has been executed.
[0020] In addition, when splitting the code of the first script file according to the dependency graph, the following principles are mainly followed: After splitting the code, try to ensure that the logic of each code segment is complete, the coupling and semantic integrity within the code segment are strong, and the coupling and correlation between the code segments are weak. In other words, ensure that each code segment after splitting can execute its calculation logic based on fewer contextual relationships, so as to avoid affecting the overall code execution logic.
[0021] The dependencies between code segments represent the sequential execution logic between them. For example, the execution of code segment 2 depends on the output of code segment 1, so code segment 2 can be understood as depending on code segment 1. Alternatively, the structural dependencies between code segments can be represented as a segment dependency graph, where the parent node of code segment 2 is code segment 1, and so on, thus determining the execution order of each code segment. It should be noted that for two or more code segments that do not have dependencies on each other, the large language model can process them in parallel, effectively improving the conversion processing efficiency.
[0022] For example, each fragment is assigned a unique identifier (Fragment-1 (code fragment 1) to Fragment-5 (code fragment 5). Further, based on the dependencies between the code fragments, the corresponding parent-child relationships are recorded: for example, the parent node of Fragment-2 is Fragment-1; the parent node of Fragment-3 is Fragment-1; the parent node of Fragment-4 is Fragment-3; and the parent node of Fragment-5 is Fragment-1. Based on depth-first search, the execution order of each code fragment can be determined as: Fragment-2 → Fragment-4 → Fragment-3 → Fragment-5 → Fragment-1. Finally, a dependency graph in JSON format can be generated to represent the dependencies between the code fragments.
[0023] Step S30: Input each code fragment and the first prompt word into the preset large language model, and the large language model outputs the strategy code corresponding to each code fragment in sequence, wherein the first prompt word includes at least the dependency relationship; The pre-set large language model can be a general large language model that has been fine-tuned and trained in advance. This large language model has been learned through policy code conversion and can convert the input code fragments into corresponding policy codes. It can also use its natural language understanding ability to understand the meaning represented in the first prompt word and perform a conversion process on each code fragment in sequence to ensure the accuracy and completeness of the logical nesting between the policy codes corresponding to the converted code fragments.
[0024] The first prompt word can be a prompt word based on the dependencies between code snippets and other transformation rules. Other transformation rules can include task descriptions, strategy engine syntax rules, transformation instances, output requirements, output formats, etc.
[0025] Step S40: Assemble the strategy code corresponding to each code fragment to obtain the target strategy code file.
[0026] After obtaining the strategy code corresponding to each code fragment, the strategy code can be assembled and integrated according to a preset format or rules to obtain the target strategy code file.
[0027] In another feasible embodiment, in addition to outputting the target strategy code file, the large language model can also generate a detailed conversion report. For example, this conversion report may include: 1. Source file information: filename, number of lines, number of characters; Conversion statistics: Identified SQL statement types: 1 SELECT statement, 1 CASE expression, 5 WHEN clauses; Number of generated strategy code lines: 25 lines; Processing time: Total 18 seconds (3 seconds for AST parsing, 12 seconds for fragment conversion, 3 seconds for syntax validation); Splitting details: Split into 5 fragments, with an average of 650 tokens per fragment; Validation result: Syntax check passed, no errors; Confidence assessment: Based on the probability returned by the model, the confidence level of this conversion is 95%. Furthermore, according to user needs, the large language model can also output a visual syntax tree and a comparison file, which is used to display the correspondence between the SQL script and the strategy code. Thus, this embodiment of the application can achieve the following effects: users can directly download the strategy code file and deploy it to the strategy engine; users can view the conversion report to understand the processing details; if users have questions about a certain part, they can view the corresponding SQL fragment and conversion logic.
[0028] Traditional solutions are limited by the context window of large language models (typically 4K-32K tokens), and can only handle small files (approximately 200-500 lines). For large files, manual splitting is required, which can easily disrupt code logic. However, this application's embodiment uses an intelligent splitting method based on AST: it not only splits at the semantic level, ensuring the logical integrity of each segment, but experimental data shows that it can successfully handle large SQL files with over 10,000 lines, keeping the size of the split segments within 2000 tokens, and is compatible with mainstream LLMs. Therefore, when processing large files, accuracy does not decrease with increasing file size. A real-world application example shows that a complex 5000-line strategy script file, traditionally split into 20 segments and manually reassembled, takes 2 hours; the technical solution of this application automatically splits it into 85 semantic segments and automatically assembles them, taking only 8 minutes in total.
[0029] In traditional manual conversion methods, skilled developers need 4-6 hours to process 500 lines of script, including: understanding the SQL logic (1.5 hours), reviewing strategy documentation (1 hour), writing strategy code (2 hours), and debugging and testing (1.5 hours). The strategy code generation method in this application embodiment only takes about 3 minutes to process the same 500 lines of script (about 80 times faster). If manual review time is added (about 30 minutes), the total time is about 0.5-1 hour, resulting in an overall efficiency improvement of about 80%-90%. In actual business applications (considering complex cases and manual intervention): simple scripts (<200 lines): efficiency improvement of 50%; medium-complex scripts (200-1000 lines): efficiency improvement of 30%; complex scripts (>1000 lines): efficiency improvement of 20%, with a final weighted average efficiency improvement of about 30%-35%.
[0030] Furthermore, in one feasible embodiment, such as Figure 2 As shown, the dependency graph includes at least a data dependency graph and a control dependency graph. Step S20, which involves splitting the code of the first script file based on the dependency graph corresponding to the abstract syntax tree and the dependency graph itself, to determine multiple code segments and the dependencies between these code segments, may include: Step S21: Generate a data dependency graph corresponding to the abstract syntax tree based on the reference data corresponding to each variable in the abstract syntax tree. The data dependency graph is used to represent the data dependency relationship between variables. Specifically, data dependencies between variables can be constructed through the usage of variables in the AST. For example, for the following variables: `customer_id` (user ID): referenced in the SELECT list, from the FROM clause; `prd_wzh_jy_lvl`: referenced multiple times in the CASE expression, from the FROM clause; `cust_g_dz14_lmt`: referenced in the REGEXP expression, from the FROM clause; `seg_prd_wzh_jy_lvl`: an output alias belonging to the CASE expression.
[0031] The final constructed data dependency graph (DDG) is used to characterize the data dependencies between variables.
[0032] Step S22: Generate a control dependency graph corresponding to the abstract syntax tree based on the control statements corresponding to each expression in the abstract syntax tree. The control dependency graph is used to represent the control dependency relationship between control statements. Specifically, the structure of control flow in the abstract syntax tree is first identified: for example, the outer CASE expression controls all WHEN clauses; the THEN part of WhenClause2 controls nested CASE expressions; nested CASE expressions control their internal WHEN clauses; and finally, the corresponding control dependency graph (CDG) is constructed. The control dependency graph includes the parent statement and / or child statement corresponding to each control statement. The parent statement represents other control statements that the control statement depends on, and the child statement represents other control statements that depend on the control statement.
[0033] Step S23: Based on the data dependency graph and the control dependency graph, select at least one target splitting node from each node of the abstract syntax tree; Data dependency graphs and control dependency graphs represent the dependencies between nodes. When selecting a target split node, the principle to follow is that after splitting the node, the target split node has the fewest dependencies on other nodes and the least impact on the overall logic of the code.
[0034] Step S24: Split the code of the first script file according to the target splitting node to obtain multiple code fragments, and determine the dependency relationship between each code fragment according to the data dependency graph and control dependency graph.
[0035] Finally, the code of the first script file is split at the target splitting node to obtain multiple corresponding code snippets. Then, the dependencies between each code snippet (including data dependencies and control dependencies) are identified from the data dependency graph and control dependency graph.
[0036] In another feasible implementation, after splitting the code into segments based on the target splitting node, the dependencies can be further optimized. For example, the dependencies before optimization are: Fragment-2 depends on customer_table; Fragment-3 depends on customer_table; Fragment-4 depends on customer_table. After optimization: the common dependency customer_table is promoted to the global context, and its source is only described in a one-time context description; subsequently, each segment no longer carries the entire table repeatedly, but only records the variable names and types of directly dependent variables, effectively reducing the context description budget and token budget, and simplifying the code file size.
[0037] This application embodiment realizes the splitting of complex nested structures into 5 semantically complete segments, where each segment is controlled between 90-450 tokens, far below the MaxTokens=2000 limit of large language models. The number of dependency edges for each code segment is 4 (after optimization). Compared with simple splitting by line number, the code segment splitting strategy of this application embodiment improves semantic integrity by 100% and reduces dependency complexity by about 50%.
[0038] Furthermore, step S23, which selects at least one target splitting node among the nodes of the abstract syntax tree based on the data dependency graph and the control dependency graph, may include: Step S231: Select the root node, nested nodes, and conditional nodes in the abstract syntax tree as candidate splitting nodes. For example, candidate split nodes may include: Case Expression (root node), WhenClause1 (condition node), WhenClause2, Nested Case, WhenClause2.1, WhenClause2.2, and Else Clause.
[0039] Step S232: Based on the data dependency graph and the control dependency graph, identify the number of dependency edges between each candidate split node and other nodes. Data dependency graphs and control dependency graphs illustrate the dependencies between each node and other nodes. For example, the more nodes that are dependent on a node, the more dependency edges there are.
[0040] Step S233: Calculate the splitting revenue value corresponding to each candidate splitting node based on the number of lexical units covered by each candidate splitting node, the number of dependency edges with other nodes, and the preset penalty coefficient. Specifically, when calculating the splitting benefit value, the following formula can be used: Benefit(N) = TokenCount(N) / (1 + α × Dependency Count(N)), where, Benefit(N): the benefit value of node N, which measures the net benefit that splitting this node can bring; N is a candidate splitting node in the dependency graph, which can correspond to a syntax fragment or code block; Token Count(N): the number of tokens covered by the fragment of node N, which is the main benefit item in the benefit formula; Dependency Count(N): the number of dependency edges between node N and other nodes. More dependencies mean that more context needs to be carried after splitting, thus penalizing the benefit; α: dependency penalty coefficient, which controls the weight of the number of dependencies in the benefit calculation; the document sets α = 1.0.
[0041] Step S234: Based on the splitting profit value of each candidate splitting node and the preset constraints, select at least one target splitting node from each candidate splitting node.
[0042] The splitting profit value is one of the criteria for selecting target splitting nodes; the goal is to select candidate splitting nodes with higher splitting profit values. Constraints may include whether the number of tokens in the segments covered by the candidate splitting node is within a preset range, avoiding too few or too many tokens. Ultimately, the target splitting node with a relatively large splitting profit value and a moderate number of tokens in the covered segments is selected.
[0043] Furthermore, step S234, which selects at least one target splitting node from the candidate splitting nodes based on the splitting profit value of each candidate splitting node and preset constraints, may include: Step A10: Determine whether the first candidate splitting node with the largest splitting profit value satisfies the constraints. Step A20: If the constraint conditions are met, the first candidate split node is determined as the target split node, and it is determined whether the target split node contains other candidate split nodes. Step A30: If it contains other candidate split nodes, then select the candidate split nodes that satisfy the constraints among the other candidate split nodes contained in the target split node as the target split node, and return to the step of determining whether the target split node contains other candidate split nodes.
[0044] In this embodiment, a greedy selection algorithm can be used to select the target splitting node. The constraint condition can be 500 ≤ Token Count ≤ 2000. Then, the splitting reward values of each candidate splitting node are sorted as follows: CaseExpression(850.0) > WhenClause2(226.7) > Nested Case(225.0) > Else Clause(90.0) > ... According to the above sorting, Case Expression is preferred: Token Count = 850, which meets the constraint condition. However, since it is the root node (including other candidate splitting nodes), it needs to be further split. Then, WhenClause2 is selected as the splitting point: Token Count = 680, which meets the constraint condition. Its external dependency is cust_g_dz14_lmt (marked as context information). The split WhenClause2 also includes Nested Case, which also needs to be split: Token Count = 450, which meets the constraint condition. Its external dependency is prd_wzh_jy_lvl (marked as context information).
[0045] In another feasible embodiment, other candidate split nodes (WhenClause1, Else Clause) can also be used as target split nodes if the corresponding token quantity is appropriate, and can be used as single-sided fragments.
[0046] This application proposes a Semantic Boundary Identification Algorithm (SBIA) based on dependency analysis. Unlike simple fixed-size or keyword-based splitting, SBIA intelligently determines the target splitting node by constructing a dependency graph between fragments and calculating the splitting reward value. Thus, each split fragment maintains semantic integrity, does not disrupt code logic, supports source files of arbitrary size, and overcomes the limitations of the model context window. Through reward value calculation, it achieves an optimal balance between fragment size and dependency complexity. Compared to fixed-size splitting, the number of dependency edges is reduced by approximately 40%-60% (experimental data).
[0047] In one feasible embodiment, before the step of inputting each code fragment and the first prompt word into a preset large language model, and having the large language model sequentially output the strategy codes corresponding to each code fragment, the method may further include: Step B10: Assign context budgets to each code segment based on a preset dynamic context budget allocation algorithm; In this embodiment, a Dynamic Context Budget Allocation (DCBA) algorithm can be used to select the most valuable context information and corresponding context budget for each fragment. Specifically, the GPT-3.5 model can be used, with a maximum context window size of Max Context Window = 4096 tokens. Taking Fragment-2 as an example, the upper and lower budgets within the model window are calculated using the following formula: Budget(Fragment-2) = Max Context Window - Token Count(Fragment-2) - Safety Margin; Here, Token Count(Fragment-2) represents the number of tokens occupied by the content of code fragment 2 itself, which is 120 in the example. Safety Margin refers to the safety margin reserved for additional content such as system prompts and control commands, which is 512 tokens in the example. Budget(Fragment-2) refers to the remaining budget available for code fragment 2 in the model window, used to support dependency contexts or generate results. Then, a candidate context item list is prepared. The context item list includes: context item ID, such as CTX-1, CTX-2 up to CTX-11; type, such as syntax rules, variable definitions, parent node information, transformation rules, sibling nodes, and instance code, etc. It can also include content description, token count (the number of tokens occupied by this context item, measuring the budget required to put it into the context), Priority (importance or priority, usually between 0 and 1, used to sort the cost-effectiveness of optional items), and a boolean value (marking whether this item is required (true means it must be included, false means not required)). (Indicates optional items). Then, the required items are counted by summing the token counts of all required items. This sum is then compared to the remaining budget. If the sum is less than or equal to the remaining budget, the budget is sufficient, and all context items can be selected. Conversely, if the sum is greater than the remaining budget, the budget is insufficient. In this case, the cost-effectiveness of each optional item is calculated, and a greedy selection algorithm is used to select several context items from the available options, ensuring that the sum of the token counts of the selected context items (equal to the context budget corresponding to the code snippet) is less than or equal to the remaining budget.
[0048] Step B20: Generate the first prompt word based on the context budget corresponding to each code snippet and the dependencies between the code snippets.
[0049] The first prompt should include at least the following: task description, such as "You are a code transformation expert and need to convert Hive SQL code snippets into strategy engine code," strategy engine syntax rules (pre-set), output requirements, such as "Only output the converted strategy engine code, without explanatory text. The code should strictly conform to the above ANTLR4 syntax rules," output format, execution order of each code snippet (determined by the dependencies between code snippets; code snippets that are depended on or have no dependencies come first, followed by code snippets that depend on other code snippets, which can be referred to as depth order), and the context budget corresponding to each code snippet (used to represent the context items involved in the code snippet conversion process).
[0050] It should be noted that when converting code snippets that depend on other code snippets, contextual information needs to be added to the first prompt. For example, if Fragment-3 depends on the result of Fragment-4, after Fragment-4 is converted, the prompt for Fragment-3 will include the contextual information "The current snippet is in the second WHEN clause of the outer CASE expression; input variable: cust_g_dz14_lmt (type: STRING); the result of this WHEN clause is a nested expression, which has been converted to: [Insert the conversion result of Fragment-4 here]".
[0051] For example, for the code snippet of the root node Fragment-1, the conversion is performed after all code snippets have been converted. The prompt includes the code snippet to be converted and the conversion result of the sub-snippets, such as: "# code snippet to be converted ```sql CASE; [WhenClause1 - converted to...]; [WhenClause2 - converted to...]; [Else Clause - converted to...]; END AS seg_prd_wzh_jy_lvl; # conversion result of the sub-snippets; WhenClause1: [result of Fragment-2]; WhenClause2: [result of Fragment-3]; ElseClause: [result of Fragment-5]." Additionally, when processing the code snippet at the root node, it is necessary to combine the various sub-code snippets. The output requirement in the prompt may include "Combine the above three branches into a complete IF-ELSE IF-ELSE structure. Note: The first IF is already in WhenClause1, and subsequent ones are connected with ELSE IF. First, you need to declare the variable seg_prd_wzh_jy_lvl". It should be noted that the embodiments of this application are equivalent to proposing a Dynamic Context Budget Allocation (DCBA) algorithm. The core idea of this algorithm is to provide the most valuable context information for the transformation of each code segment under the premise of context window constraints in a large language model. DCBA models this problem as a priority-based knapsack problem and achieves optimal context allocation through dynamic priority calculation.
[0052] Specifically, the context information can be divided into five levels with different priorities, from highest to lowest: Level L1, type is syntax rules (ANTLR4), which are the formal syntax rules of the target language; Level L2, type is variable definitions, used to indicate the variable types and definition locations that the current fragment depends on; Level L3, type is parent node information, used to indicate the type and attributes of the parent node; Level L4, type is sibling node information, used to indicate the converted sibling node code; Level L5, example code, i.e., conversion examples of similar code.
[0053] Furthermore, the selection process of context items can be modeled as a 0-1 knapsack problem, with the goal of maximizing Σ(Priority_i×Selected_i), constrained by Σ(Size_i×Selected_i)≤Budget(F), where Selected_i ∈ {0, 1}, where: Priority_i` is the priority weight of context item i; `Size_i` is the token size of context item i; and Selected_i` indicates whether context item i is selected.
[0054] In another feasible embodiment, when the required context items exceed the remaining budget, the following hierarchical compression scheme can be adopted: For syntax rules, the compression strategy retains only the syntax rules relevant to the current segment (e.g., only IF statement rules); for variable definitions, the compression strategy retains only the type and name, omitting initial values; for example code, the compression strategy retains only the 1-2 most similar examples. For example, the compression ratio calculation formula can be: Compression Ratio = min(Required Budget / Available Budget, 1.0); Required Budget is the context budget required for the runtime; Available Budget is the available context budget; Compression Ratio uses min(RequiredBudget / Available Budget, 1.0) to constrain the compression ratio to not exceed 1. When the context budget required for the runtime is greater than the available budget, the ratio > 1, and is truncated to 1, indicating compression up to the full available budget; when the requirement is less than the available budget, compression is performed according to the actual ratio.
[0055] Thus, the embodiments of this application can convey the most valuable information within a limited model context window, and can dynamically adapt to fragments of different sizes, automatically adjust the context ratio, and avoid truncation caused by exceeding the window limit through a safety margin mechanism. Experimental data show that the conversion accuracy is improved by 15%-20% compared to randomly selecting the context.
[0056] In one feasible embodiment, step S10, which generates the corresponding abstract syntax tree based on the first script file, may include: Step S11: The first script file is formatted and standardized sequentially to obtain the first standardized code; Step S12: The lexical and grammatical structures of the first standardized code are parsed using a preset strategy language parser to obtain an abstract syntax tree.
[0057] For example, the SQL script is first formatted by the AST parsing module to unify the code style (indentation, spaces, line breaks, etc.), remove comments (single-line comments and multi-line comments), identify and extract pre-declarations such as global variable definitions and temporary table creation, and extract the list of input parameters for the strategy, such as customer_id, prd_wzh_jy_lvl, cust_g_dz14_lmt, etc. The preprocessed standardized code (i.e. the first standardized code) is stored as a temporary file.
[0058] Then, the Hive SQL parser is invoked to perform syntax analysis on the first standardized code. The process includes: first, the parser reads the standardized SQL code; lexical analysis is performed to identify keywords (SELECT, CASE, WHEN, etc.), identifiers, literals, etc.; syntax analysis is performed to construct an abstract syntax tree; each node in the abstract syntax tree is labeled with metadata such as type, attribute, and line number; and the AST is serialized into JSON format for subsequent processing.
[0059] In one feasible embodiment, step S40, which involves assembling the strategy codes corresponding to each code fragment to obtain the target strategy code file, may include: Step S41: Generate a policy file header based on the first script file. The policy file header is used to represent the file name, generation time, and source file. Step S42: Add the input parameter declarations, each strategy code, output statements, and the end of the strategy file to the strategy file header to obtain the first strategy code file. Step S43: Format the first strategy code file to obtain the target strategy code file.
[0060] In this embodiment, all converted strategy codes need to be combined into a complete target strategy code file according to the structure and information of the original first script file. First, a strategy file header is generated, for example, the strategy name is: Customer Risk Segmentation Strategy; generation time: 2025-10-13 14:30:25; source file: customer_risk_segmentation.sql (first script file name). Then, input parameter declarations are added, representing the names of various input parameters. Next, the strategy codes corresponding to each code segment are inserted, and output statements (used to control the output results) are added to the end of the strategy file (e.g., policycode END POLICY), resulting in the first strategy code file. Finally, the first strategy code file is formatted (e.g., indentation, line breaks, etc.) to obtain the target strategy code file.
[0061] Based on the first embodiment of this application, in the second embodiment of this application, the content that is the same as or similar to that in Embodiment 1 above can be referred to the above description, and will not be repeated hereafter. On this basis, as Figure 3 As shown, the policy syntax parser includes at least a syntax verification service; the policy code generation method may further include: Step C10: Obtain the strategy engine document, convert the strategy engine document to a lightweight markup language file; Step C20: Input the lightweight markup language file and the second prompt word into the preset large language model, and the large language model outputs the corresponding multiple grammar rules; Step C30: Merge all grammar rules into a grammar file, and generate the corresponding lexical analyzer and parser based on the grammar file; Step C40: Encapsulate the lexical analyzer and the syntax analyzer into a syntax verification service, wherein the syntax verification service includes at least a code verification interface.
[0062] This application provides a method for constructing a strategy syntax parser. First, the system receives the help documentation of the strategy engine, which includes multiple HTML (Hypertext Markup Language) files. Each file contains function descriptions, syntax rules, sample code, parameter tables, and other content.
[0063] For example, the Mark It Down (Lightweight Markup Language) tool can be used to batch convert HTML documents to Markdown format. Markdown conversion preserves the document structure: heading levels, code block tags, table structure, etc.; for images in HTML, it converts them into image captions (based on the image's alt attribute or surrounding text); and outputs 25 corresponding Markdown files (Lightweight Markup Language files) stored in a document library. The Markdown format effectively preserves the document's structured information (headings, lists, tables, etc.), making it easier for LLM (Lightweight Markup Language) to understand and improving conversion accuracy.
[0064] Furthermore, for the converted Markdown document, a large language model can be used to extract grammar rules. Taking the "conditional judgment grammar" document as an example, the grammar rule extraction steps include: first, reading the Markdown document content, then combining the document content with a preset prompt word template. The prompt word content may include: "You are a grammar rule expert. Please read the following grammar description document of the strategy engine, extract the grammar rules, and represent them using the BNF paradigm of ANTLR4. Requirements: Lexical rules should begin with an uppercase letter; grammar rules should begin with a lowercase letter; add Chinese comments for each rule; document content: [Insert Markdown document content here]". Then, the prompt word is sent to the large language model, which returns grammar rules in ANTLR4 format. The extracted rules can then be compiled and verified using the ANTLR4 tool. If compilation fails, the error message is fed back to the model for correction. After successful verification, the grammar rules are stored in the grammar rule base.
[0065] The next step is to merge all the extracted ANTLR4 rules into a complete grammar file, and then use the ANTLR4 tool to generate a lexical analyzer and a syntax analyzer for Java code; encapsulate this into a syntax verification service to provide a code verification interface; and establish a mapping table from syntax rules to natural language descriptions for error messages.
[0066] This application's embodiments formalize grammar rules using ANTLR4, an industry-standard standard for describing grammar rules. Large language models (LLMs) have good understanding capabilities, thus eliminating ambiguity in natural language descriptions and providing explicit grammatical constraints for LLMs. Combined with a closed-loop mechanism for grammar validation, errors are automatically detected and corrected. Experimental data shows that the conversion accuracy reaches 98%-100%. Comparative experiments (based on 100 test files) show: the accuracy of traditional methods is 69%, requiring an average of 8 manual corrections per file; the method of this application's embodiments achieves 99% accuracy, requiring no correction for 96 files and only 1 correction for each of the 4 files, reducing manual correction time by approximately 95%.
[0067] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the strategy code generation method of this application. Any simple modifications based on this technical concept are within the protection scope of this application.
[0068] This application also provides a strategy code generation device, please refer to... Figure 4 The strategy code generation device includes: Syntax tree generation module 10 is used to obtain a first script file and generate a corresponding abstract syntax tree based on the first script file. The code splitting module 20 is used to split the code of the first script file according to the dependency graph corresponding to the abstract syntax tree, and according to the dependency graph, to obtain multiple code fragments and determine the dependency relationship between each code fragment; The code generation module 30 is used to input each of the code fragments and the first prompt word into a preset large language model, and the large language model sequentially outputs the strategy code corresponding to each of the code fragments, wherein the first prompt word includes at least the dependency relationship; The code assembly module 40 is used to assemble the strategy codes corresponding to each of the code fragments to obtain the target strategy code file.
[0069] In one embodiment, the dependency graph includes at least a data dependency graph and a control dependency graph, and the code splitting module 20 is further configured to: Based on the reference data corresponding to each variable in the abstract syntax tree, a data dependency graph corresponding to the abstract syntax tree is generated. The data dependency graph is used to represent the data dependency relationship between variables. Based on the control statements corresponding to each expression in the abstract syntax tree, a control dependency graph corresponding to the abstract syntax tree is generated. The control dependency graph is used to represent the control dependency relationship between control statements. Based on the data dependency graph and the control dependency graph, at least one target split node is selected from each node of the abstract syntax tree; The code of the first script file is split according to the target splitting node to obtain multiple code fragments, and the dependency relationship between each code fragment is determined according to the data dependency graph and the control dependency graph.
[0070] In one embodiment, the code splitting module 20 is further configured to: The root node, nested nodes, and conditional nodes in the abstract syntax tree are selected as candidate splitting nodes. Based on the data dependency graph and the control dependency graph, identify the number of dependency edges between each candidate split node and other nodes; The splitting revenue value corresponding to each candidate splitting node is calculated based on the number of lexical units covered by each candidate splitting node, the number of dependency edges with other nodes, and the preset penalty coefficient. Based on the splitting profit value of each candidate splitting node and the preset constraints, at least one target splitting node is selected from the candidate splitting nodes.
[0071] In one embodiment, the code splitting module 20 is further configured to: Determine whether the first candidate splitting node with the largest splitting profit value satisfies the aforementioned constraint conditions; If the constraints are met, the first candidate splitting node is determined as the target splitting node, and it is determined whether the target splitting node contains other candidate splitting nodes. If it is included, then the candidate splitting node that satisfies the constraint condition among the other candidate splitting nodes included in the target splitting node is taken as the target splitting node, and the step of determining whether the target splitting node contains other candidate splitting nodes is returned.
[0072] In one embodiment, the strategy code generation apparatus further includes a prompt word generation module, which is used to: The preset dynamic context budget allocation algorithm is used to assign context budgets to each of the code segments. The first prompt word is generated based on the context budget corresponding to each of the code snippets and the dependencies between the code snippets.
[0073] In one embodiment, the syntax tree generation module 10 is further configured to: The first script file is formatted and standardized sequentially to obtain the first standardized code; The first standardized code is parsed using a pre-defined strategy language parser to obtain an abstract syntax tree.
[0074] In one embodiment, the strategy code generation apparatus further includes a parser construction module, which is used to: Obtain the strategy engine document, convert the strategy engine document into a lightweight markup language file; The lightweight markup language file and the second prompt word are input into a preset large language model, and the large language model outputs multiple corresponding grammar rules. The grammar rules are merged into a grammar file, and a corresponding lexical analyzer and syntax analyzer are generated based on the grammar file. The lexical analyzer and the syntax analyzer are encapsulated into a syntax verification service, wherein the syntax verification service includes at least a code verification interface.
[0075] In one embodiment, the code assembly module 40 is further configured to: Based on the first script file, a strategy file header is generated, which is used to represent the file name, generation time, and source file; By adding input parameter declarations, policy codes, output statements, and the end of the policy file to the header of the policy file, a first policy code file is obtained. The first strategy code file is formatted to obtain the target strategy code file.
[0076] The strategy code generation apparatus provided in this application, employing the strategy code generation method in the above embodiments, can solve the technical problem of code conversion using a large language model being limited by the context window. Compared with the prior art, the beneficial effects of the strategy code generation apparatus provided in this application are the same as those of the strategy code generation method provided in the above embodiments, and other technical features in the strategy code generation apparatus are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.
[0077] This application provides a strategy code generation device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the strategy code generation method in Embodiment 1 above.
[0078] The following is for reference. Figure 5 The diagram illustrates a structural schematic of a policy code generation device suitable for implementing embodiments of this application. The policy code generation device in the embodiments of this application may include, but is not limited to, mobile terminals such as mobile phones, tablets, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PMPs (Portable Media Players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital televisions and desktop computers. Figure 5 The strategy code generation device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.
[0079] like Figure 5As shown, the strategy code generation device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.) that can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM) 1004. The RAM 1004 also stores various programs and data required for the operation of the strategy code generation device. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, a touchscreen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage devices 1003 including, for example, magnetic tape, hard disk, etc.; and communication devices 1009. Communication device 1009 allows the policy code generation device to communicate wirelessly or wiredly with other devices to exchange data. While the figure shows policy code generation devices with various systems, it should be understood that implementation or possession of all the systems shown is not required. More or fewer systems may be implemented alternatively.
[0080] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.
[0081] The strategy code generation device provided in this application, employing the strategy code generation method in the above embodiments, can solve the technical problem of code conversion using a large language model being limited by the context window. Compared with the prior art, the beneficial effects of the strategy code generation device provided in this application are the same as those of the strategy code generation method provided in the above embodiments, and other technical features in this strategy code generation device are the same as those disclosed in the previous embodiment method, and will not be repeated here.
[0082] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any suitable manner in one or more embodiments or examples.
[0083] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
[0084] The above descriptions are merely some embodiments of this application and do not limit the scope of protection of this application. Any equivalent structural transformations made based on the technical concept of this application and the content of this specification and drawings, or direct / indirect applications in other related technical fields, are included within the scope of protection of this application. All actions involving the acquisition of signals, information, or data in this application are performed in accordance with the relevant data protection laws and policies of the country where the application is located and with authorization from the owner of the corresponding device.
Claims
1. A strategy code generation method, characterized in that, The strategy code generation method includes: Obtain the first script file and generate the corresponding abstract syntax tree based on the first script file; Based on the dependency graph corresponding to the abstract syntax tree, and based on the dependency graph, the code of the first script file is split to obtain multiple code fragments and determine the dependency relationships between each code fragment; Each code snippet and the first prompt word are input into a preset large language model, and the large language model outputs the strategy code corresponding to each code snippet in sequence, wherein the first prompt word includes at least the dependency relationship; Assemble the strategy codes corresponding to each of the code fragments to obtain the target strategy code file.
2. The strategy code generation method as described in claim 1, characterized in that, The dependency graph includes at least a data dependency graph and a control dependency graph. The step of splitting the code of the first script file into multiple code segments based on the dependency graph corresponding to the abstract syntax tree and the dependency graph, and determining the dependency relationships between the code segments, includes: Based on the reference data corresponding to each variable in the abstract syntax tree, a data dependency graph corresponding to the abstract syntax tree is generated. The data dependency graph is used to represent the data dependency relationship between variables. Based on the control statements corresponding to each expression in the abstract syntax tree, a control dependency graph corresponding to the abstract syntax tree is generated. The control dependency graph is used to represent the control dependency relationship between control statements. Based on the data dependency graph and the control dependency graph, at least one target split node is selected from each node of the abstract syntax tree; The code of the first script file is split according to the target splitting node to obtain multiple code fragments, and the dependency relationship between each code fragment is determined according to the data dependency graph and the control dependency graph.
3. The strategy code generation method as described in claim 2, characterized in that, The step of selecting at least one target split node among the nodes of the abstract syntax tree based on the data dependency graph and the control dependency graph includes: The root node, nested nodes, and conditional nodes in the abstract syntax tree are selected as candidate splitting nodes. Based on the data dependency graph and the control dependency graph, identify the number of dependency edges between each candidate split node and other nodes; The splitting revenue value corresponding to each candidate splitting node is calculated based on the number of lexical units covered by each candidate splitting node, the number of dependency edges with other nodes, and the preset penalty coefficient. Based on the splitting profit value of each candidate splitting node and the preset constraints, at least one target splitting node is selected from the candidate splitting nodes.
4. The strategy code generation method as described in claim 3, characterized in that, The step of selecting at least one target splitting node from the candidate splitting nodes based on the splitting profit value of each candidate splitting node and preset constraints includes: Determine whether the first candidate splitting node with the largest splitting profit value satisfies the aforementioned constraint conditions; If the constraints are met, the first candidate splitting node is determined as the target splitting node, and it is determined whether the target splitting node contains other candidate splitting nodes. If it is included, then the candidate splitting node that satisfies the constraint condition among the other candidate splitting nodes included in the target splitting node is taken as the target splitting node, and the step of determining whether the target splitting node contains other candidate splitting nodes is returned.
5. The strategy code generation method as described in claim 1, characterized in that, Before the step of inputting each of the code fragments and the first prompt word into a preset large language model, and having the large language model sequentially output the strategy codes corresponding to each of the code fragments, the method further includes: The preset dynamic context budget allocation algorithm is used to assign context budgets to each of the code segments. The first prompt word is generated based on the context budget corresponding to each of the code snippets and the dependencies between the code snippets.
6. The strategy code generation method as described in claim 1, characterized in that, The step of generating the corresponding abstract syntax tree based on the first script file includes: The first script file is formatted and standardized sequentially to obtain the first standardized code; The first standardized code is parsed using a pre-defined strategy language parser to obtain an abstract syntax tree.
7. The strategy code generation method as described in claim 6, characterized in that, The policy syntax parser includes at least a syntax validation service; The strategy code generation method further includes: Obtain the strategy engine document, convert the strategy engine document into a lightweight markup language file; The lightweight markup language file and the second prompt word are input into a preset large language model, and the large language model outputs multiple corresponding grammar rules. The grammar rules are merged into a grammar file, and a corresponding lexical analyzer and syntax analyzer are generated based on the grammar file. The lexical analyzer and the syntax analyzer are encapsulated into a syntax verification service, wherein the syntax verification service includes at least a code verification interface.
8. The strategy code generation method as described in claim 1, characterized in that, The step of assembling the strategy codes corresponding to each of the code fragments to obtain the target strategy code file includes: Based on the first script file, a strategy file header is generated, which is used to represent the file name, generation time, and source file; By adding input parameter declarations, policy codes, output statements, and the end of the policy file to the header of the policy file, a first policy code file is obtained. The first strategy code file is formatted to obtain the target strategy code file.
9. A strategy code generation device, characterized in that, The strategy code generation device includes: The syntax tree generation module is used to obtain the first script file and generate the corresponding abstract syntax tree based on the first script file. The code splitting module is used to split the code of the first script file according to the dependency graph corresponding to the abstract syntax tree, and according to the dependency graph, to obtain multiple code fragments and determine the dependency relationship between each code fragment; The code generation module is used to input each of the code fragments and the first prompt word into a preset large language model, and the large language model sequentially outputs the strategy code corresponding to each of the code fragments, wherein the first prompt word includes at least the dependency relationship; The code assembly module is used to assemble the strategy code corresponding to each of the code fragments to obtain the target strategy code file.
10. A strategy code generation device, characterized in that, The strategy code generation device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the strategy code generation method as described in any one of claims 1 to 8.
