DSL calculation expression operation cost evaluation method and optimization method
By converting DSL computed expressions into abstract syntax trees and evaluating their running costs, the problems of running cost evaluation and optimization of DSL computed expressions are solved, and the resource consumption cost is reduced and the computing efficiency is improved.
Patent Information
- Application Number
- CN202411484399.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-02-07
AI Technical Summary
In the field of industrial informatization, the operating cost of DSL calculation expressions increases with the increase in the number of business and rule operating costs, resulting in a linear or exponential increase in resource consumption costs, and is affected by personnel skill levels. The resource consumption cost of expressions with the same function is affected by the same function. Differently, how to scientifically simplify expressions to reduce resource consumption costs has become a difficult problem.
By processing DSL computed expressions into abstract syntax trees (ASTs) and calculating the running cost score of the expression based on the preset running cost evaluation rules of each node type, the accurate evaluation of the running cost of the DSL computed expression is achieved. The optimization of subsequent expressions provides the basis.
The accurate evaluation of the operating cost of DSL calculation expressions is achieved, providing a foundation for expression optimization, reducing resource consumption costs, and improving computing efficiency.
Smart Images

Figure CN119396405A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of industrial information technology, and in particular relates to a DSL calculation expression running cost evaluation method, optimization method and device. Background Art
[0002] Modern enterprises are undergoing various information and intelligent upgrades, especially in the industrial field. Enterprises will not only use various sensors, PLCs, DCSs, etc. to realize automatic control of industrial process closed-loop control and sequential control, but also use various network and database technologies to realize process data management for process data collection, storage and analysis. In these processes, there are a lot of demands for calculation and analysis of the collected relevant data, which has led to solutions for mathematical operations and program control flow rules in various DSL calculation expression processing requirements.
[0003] However, as the number of businesses and the cost of running rules continue to increase, resource consumption costs are also increasing linearly or even exponentially. In particular, due to the different skill levels of personnel, the resource consumption costs required for different expressions with the same function are also different. How to scientifically and accurately simplify expressions and reduce resource consumption costs has become an issue that cannot be underestimated. Summary of the invention
[0004] To solve the above problems, the purpose of the present invention is to provide a method, an optimization method and a device for evaluating the running cost of DSL calculation expressions. By processing the DSL calculation expression into an abstract syntax tree, and then calculating the running cost score of the DSL calculation expression according to the pre-set running cost evaluation rules corresponding to the type of each node, it is possible to accurately evaluate the running cost of the DSL calculation expression and provide a basis for the subsequent optimization of the expression. The DSL calculation expression is lexically analyzed and decomposed into a series of tags, and then a syntax analysis is performed to determine the hierarchical relationship between them based on the sequence relationship of the tags, and finally the abstract syntax tree is constructed. Each node in the tree represents an element or operation in the expression, and the result is used for subsequent evaluation and optimization according to the predetermined rules.
[0005] To achieve the above object, the technical solution of the present invention is: a DSL calculation expression running cost evaluation method, comprising the following steps:
[0006] Reading a DSL calculation expression in a string form, and processing the DSL calculation expression into a structured abstract syntax tree;
[0007] Traversing the abstract syntax tree structure of the DSL calculation expression, and matching corresponding running cost evaluation rules according to the type of each node in the abstract syntax tree structure;
[0008] The running cost score of the DSL calculation expression is calculated based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node.
[0009] Preferably, processing the DSL computation expression into a structured abstract syntax tree further comprises:
[0010] The lexical analyzer collects the lexical rules required by the DSL calculation expression, and converts the input DSL calculation expression string into corresponding tokens, which at least contain the atomic elements of the expression: keyword identifiers, literals, and operators. The keyword identifiers include variables, functions, and types, and the literals include numbers, strings, and Boolean values.
[0011] Through the parser, the grammar rules required by the DSL calculation expression are collected to analyze the token sequence and build a parse tree;
[0012] The parse tree is converted into an abstract syntax tree so that the nodes contain not only operator and operand information but also control structure information, thereby more clearly expressing the logical structure of the DSL calculation expression.
[0013] Preferably, the operating cost assessment rule further includes:
[0014] For constant nodes, the running cost score is 1 point;
[0015] For variable nodes, the running cost score is 2 points;
[0016] For addition / subtraction arithmetic operators, the running cost score is 3 points;
[0017] For multiplication / division arithmetic operators, the running cost score is 4 points;
[0018] Logical operator, the running cost score is 3 points;
[0019] Function call, the basic score is 10 points, and each parameter adds an additional 2 points;
[0020] Conditional functions, loop functions, and branch calculations are equivalent to function call rules.
[0021] Preferably, the operating cost assessment rule further includes:
[0022] Node depth: For every additional layer of node depth, the running cost score increases by 1 point;
[0023] Node width: Every time the number of nodes at the same level doubles, the overall level operation cost increases by 5 points.
[0024] Preferably, calculating the running cost score of the DSL calculation expression based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node further includes:
[0025] When each node n in the DSL calculation expression is n i , where i is the index value of the node, and each node n i The computational cost can be expressed as C(n i ), as follows:
[0026]
[0027] Among them, b(n i ) is the basic running cost of the node. If the node is a function type, the running cost of all its parameters is added. j (n i ), w j represents the weighted value corresponding to this parameter, k is the number of parameters, is the cumulative value of the operating cost of all parameters;
[0028] Node Depth Bonus: D(n i )=d(n i )×f, where d(n i ) represents the current node running cost, and f represents the weighted value of different depths;
[0029] Node Width Bonus: Where L represents the level of the node, w(L) represents the computational cost corresponding to the level, and g is the fraction of the extra width added for every 10 nodes.
[0030] The total operating cost calculation formula is:
[0031] T=∑ i (C(n i )+D(n i ))+∑ L W(L).
[0032] Based on the same concept, the present invention also provides a method for optimizing the running cost of a DSL calculation expression, which optimizes the running cost based on any one of the evaluation methods described above, and includes the following steps:
[0033] By traversing and analyzing the abstract syntax tree structure of the DSL calculation expression, the relationship and type of each node and its child nodes are analyzed;
[0034] Match the corresponding optimization strategy conversion rules and update the corresponding abstract syntax tree nodes;
[0035] Re-traverse the optimized abstract syntax tree, build and output a new optimized DSL calculation expression string.
[0036] Preferably, the optimization strategy conversion rules include:
[0037] Mathematical algebra simplification, using the principle of identity in mathematical formulas to simplify expressions through mathematical rules;
[0038] Constant folding, check nodes, and when they contain only constants, pre-calculate their results and replace the original nodes;
[0039] Common subexpression elimination, marking each operation’s subtree, detecting recurring structures, and replacing them with references to previously computed results, where the subtree includes both operators and operands;
[0040] Dead code elimination, identifying the parts of the expression that cannot be calculated, performing conversion and cleaning, and reducing redundant calculations.
[0041] Preferably, the optimization strategy conversion rule also includes an extended domain expert model, and the domain expert model is divided into two parts: matching and conversion, to meet special optimization processing in a specific field.
[0042] Preferably, the method further comprises: iteratively performing multiple rounds of expression optimization conversion based on the formula running cost index judgment basis, and finally finding the most effective optimization result by comparing the effects of different optimization strategies. The specific steps are as follows:
[0043] Initial input and evaluation, receiving the original DSL calculation expression and calculating the running cost score;
[0044] Traverse and execute the optimization strategies, optimize the DSL calculation expression according to multiple strategies, and obtain the optimized result and its corresponding running cost score;
[0045] Re-evaluate, compare the running cost score of the optimized DSL calculation expression with the score of the previous round, if the new running cost score is lower than the previous round, accept the current optimization; if there is no improvement, abandon the optimization or terminate the optimization.
[0046] Based on the same concept, the present invention also provides an electronic device, comprising:
[0047] A memory, the memory being used to store a processing program;
[0048] A processor, wherein the processor implements any one of the above-mentioned evaluation methods or optimization methods when executing the processing program.
[0049] Due to the adoption of the above technical solution, the present invention has the following advantages and positive effects compared with the prior art:
[0050] By processing the DSL calculation expression into an abstract syntax tree, and then calculating the running cost score of the DSL calculation expression according to the pre-set running cost evaluation rules corresponding to the type of each node, it is possible to accurately evaluate the running cost of the DSL calculation expression, providing a basis for the subsequent optimization of the expression. The DSL calculation expression is lexically analyzed and decomposed into a series of tokens, and then parsed, and the hierarchical relationship between them is determined by the sequence relationship of the tokens. Finally, the abstract syntax tree is constructed. Each node in the tree represents an element or operation in the expression, and the result is used for subsequent evaluation and optimization according to the predetermined rules. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] The specific embodiments of the present invention are further described in detail below with reference to the accompanying drawings, wherein:
[0052] Figure 1 It is a flow chart of the DSL calculation expression running cost evaluation method of the present invention;
[0053] Figure 2 It is a structural schematic diagram of the DSL calculation expression running cost optimization module of the present invention. DETAILED DESCRIPTION
[0054] The present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. The advantages and features of the present invention will become more apparent from the following description and claims. It should be noted that the drawings are all in a very simplified form and use non-precise ratios, which are only used to conveniently and clearly assist in explaining the purpose of the embodiments of the present invention.
[0055] It should be noted that all directional indications in the embodiments of the present invention (such as up, down, left, right, front, back, etc.) are only used to explain the relative position relationship, movement status, etc. between the components under a certain specific posture (as shown in the accompanying drawings). If the specific posture changes, the directional indication will also change accordingly.
[0056] First embodiment
[0057] Generally speaking, the calculation of operating costs includes two dimensions: time and space. The evaluation model proposed in the present invention is an operating cost evaluation model based on the premise of equal spatial resources (computing data, hardware resources) and taking the time dimension as an indicator.
[0058] See also Figure 1 This embodiment provides a DSL calculation expression running cost evaluation method, including the following steps:
[0059] Reading a DSL calculation expression in a string form, and processing the DSL calculation expression into a structured abstract syntax tree;
[0060] Traversing the abstract syntax tree structure of the DSL calculation expression, and matching corresponding running cost evaluation rules according to the type of each node in the abstract syntax tree structure;
[0061] The running cost score of the DSL calculation expression is calculated based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node.
[0062] The technical solution of this embodiment processes the DSL calculation expression into an abstract syntax tree, and then calculates the running cost score of the DSL calculation expression according to the pre-set running cost evaluation rules corresponding to the type of each node, so as to achieve accurate evaluation of the running cost of the DSL calculation expression and provide a basis for the subsequent optimization of the expression.
[0063] The evaluation model of this embodiment calculates the corresponding running cost index by identifying different features such as operands, operators, functions, etc. in DSL calculation expressions and combining them with their structures (depth, width). For example, the weights of operands (constants, variables) are processed as "add 1" and "add 2" respectively, the weight of unary operators is processed as "add 3", and the weight of function call nodes is processed as "add 10×number of parameters".
[0064] First, lexical analysis is performed on the DSL calculation expression to decompose it into a series of tokens. Then, syntactic analysis is performed to determine the hierarchical relationship between the tokens based on their sequence relationship. Finally, AST construction is completed. Each node in the tree represents an element or operation in the expression, and the result is used for subsequent evaluation and optimization.
[0065] Preferably, processing the DSL computation expression into a structured abstract syntax tree further comprises:
[0066] The lexical analyzer collects the lexical rules required by the DSL calculation expression, and converts the input DSL calculation expression string into corresponding tokens, which at least contain the atomic elements of the expression: keyword identifiers, literals, and operators. The keyword identifiers include variables, functions, and types, and the literals include numbers, strings, and Boolean values.
[0067] Through the parser, the grammar rules required by the DSL calculation expression are collected to analyze the token sequence and build a parse tree;
[0068] The parse tree is converted into an abstract syntax tree so that the nodes contain not only operator and operand information but also control structure information, thereby more clearly expressing the logical structure of the DSL calculation expression.
[0069] This embodiment provides a technical solution for processing the DSL calculation expression into a structured abstract syntax tree. Since the abstract syntax tree clearly expresses the information of operators, operands and control structures, the execution plan can be optimized, unnecessary calculations can be reduced, and the execution efficiency can be improved. This embodiment is only one of the implementation schemes. The abstract syntax tree corresponding to the DSL calculation expression can also be obtained by any known technical means in the prior art, so that the nodes of the syntax tree not only contain operator and operand information, but also control structure information, thereby more clearly expressing the logical structure of the DSL calculation expression. By analyzing the functional module that converts the DSL calculation expression into an abstract syntax tree (AST) structure, the corresponding abstract syntax tree (AST) is obtained, and then the depth first search (DFS: Depth First Search) rule is used to traverse each node, and the index value corresponding to a single node is obtained by evaluating the model, and finally accumulated to obtain the running cost index value of the entire DSL calculation expression. By analyzing the functional module that converts the DSL calculation expression into an abstract syntax tree (AST) structure, the corresponding abstract syntax tree (AST) is obtained, and then each group of left and right child nodes is recursively identified, and the group of nodes is optimized by matching the corresponding optimization strategy.
[0070] Preferably, the operating cost assessment rule further includes:
[0071] For constant nodes, the running cost score is 1 point;
[0072] For variable nodes, the running cost score is 2 points;
[0073] For addition / subtraction arithmetic operators, the running cost score is 3 points;
[0074] For multiplication / division arithmetic operators, the running cost score is 4 points;
[0075] Logical operator, the running cost score is 3 points;
[0076] Function call, the basic score is 10 points, and each parameter adds an additional 2 points;
[0077] Conditional functions, loop functions, and branch calculations are equivalent to function call rules.
[0078] Preferably, the operating cost assessment rule further includes:
[0079] Node depth: For every additional layer of node depth, the running cost score increases by 1 point;
[0080] Node width: Every time the number of nodes at the same level doubles, the overall level operation cost increases by 5 points.
[0081] By matching all types of elements of DSL calculation expressions, adding different addition coefficients according to their corresponding weight values and their structures (width, depth), we can comprehensively obtain accurate indicator values. By mapping and understanding the structures and nested relationships of various calculation expressions in detail, we can systematically analyze and process each element in the expression (such as operators, functions, variables, etc.). For example, the tool provides different weights for each element according to its characteristics (classification, depth, width, etc.). These weight factor scores are customized based on the resource consumption and performance impact of the elements during execution. Based on these detailed rules, we can obtain more accurate calculation expression running cost scores. In addition to conventional mathematical operations, calculation expressions in industrial scenarios often use logical processing such as conditions, loops, and functions. In addition to using basic mathematical algebra optimization strategies, this tool also uses many advanced optimization strategies, such as constant folding, common sub-expression elimination, short-circuit calculation, etc. These strategies can go deep into the core structure of the expression, more effectively simplify the calculation process, and reduce unnecessary calculations and memory usage.
[0082] For example, the running cost score of the constant node (Constant) is 1 point, because the constant value does not involve calculation or memory operation, so the running cost is the lowest; the running cost score of the variable node (Variable) is 2 points, because the variable may involve memory access, and the running cost is slightly higher than the constant; the addition / subtraction arithmetic operator (+ / -) has a running cost score of 3 points, which is a basic mathematical operation with a relatively low computational cost; the multiplication / division arithmetic operator (* / ) has a running cost score of 4 points, because multiplication and division operations usually consume more computing resources than addition and subtraction operations, so it has 1 more point than the addition / subtraction arithmetic operator; logical operators (And, Or, Not), the running cost score is 3 points, because logical operations involve conditional judgments and have little impact on performance; function call (Function), the basic score is 10 points, and at the same time, each additional parameter needs to add 2 points, because the running cost of function call is high, especially when there are many parameters, it may require additional computing resources; conditional function (if / ifs), loop function (Each), they are special function calls, and branch calculation is equivalent to the function call (Function) rule; node depth, for each additional layer of node depth, the running cost score increases by 1 point; node width, for each doubling of the number of nodes at the same level, the overall level running cost increases by 5 points.
[0083] Based on the above rules, preferably, calculating the running cost score of the DSL calculation expression based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node further includes:
[0084] Assume that each node n in the DSL calculation expression is n i , where i is the index value of the node, and each node n i The computational cost can be expressed as C(n i ), as follows:
[0085]
[0086] Among them, b(n i ) is the basic running cost of the node. If the node is a function type, the running cost of all its parameters is added. j (n i ), w j represents the weighted value corresponding to this parameter, k is the number of parameters, is the cumulative value of the operating cost of all parameters;
[0087] Node Depth Bonus: D(n i )=d(n i )×f, where d(n i ) represents the current node running cost, and f represents the weighted value of different depths;
[0088] Node Width Bonus: Where L represents the level of the node, w(L) represents the computational cost corresponding to the level, and g is the fraction of the extra width added for every 10 nodes.
[0089] The total operating cost calculation formula is:
[0090]
[0091] For example, suppose there is an expression a+f(b*c) that contains four arithmetic operations and functions. The above rules can be used to obtain the node definitions and scores, for example:
[0092] Variables a, b, c, each running cost is 2 points;
[0093] Multiplication*, running cost is 4 points;
[0094] The addition + running cost is 3 points;
[0095] The cost basis of running function f is 10 points, and each parameter adds 2 points;
[0096] Therefore, the operating cost calculation results are as follows:
[0097] T=(2+4+3+12)+(1+1)+0
[0098] The result is: 23
[0099] Second embodiment
[0100] Conventional calculation expression processing directly uses reverse Polish transformation and achieves efficient operation through stack operations, but based on this idea, no optimization is performed on the calculation expression. Some will monitor the operation performance of the expression, but this monitoring is based on statistics and analysis of the operation time consumption, which cannot change the operation efficiency.
[0101] Based on the same concept, the present invention also provides a method for optimizing the running cost of a DSL calculation expression, and performs running cost optimization based on any of the evaluation methods described above. Figure 1 , including the following steps:
[0102] By traversing and analyzing the abstract syntax tree structure of the DSL calculation expression, traversing each node of the abstract syntax tree to analyze the relationship and type of the node and its child nodes;
[0103] Match the corresponding optimization strategy conversion rules and update the corresponding abstract syntax tree nodes;
[0104] Re-traverse the optimized abstract syntax tree, build and output a new optimized DSL calculation expression string.
[0105] Compared with existing calculation formula optimization tools, the present invention is more in line with industrial application scenarios, and solves the problem of how to standardize and comprehensively calculate the running costs of DSL calculation expressions and perform precise optimization processing, thereby solving the problem of running cost waste caused by human factors and meeting the requirements of industrial systems, especially industrial Internet systems, for computing efficiency.
[0106] For example: use algebraic reduction and algebraic identity rules to complete identity processing of mathematical methods such as polynomial simplification, fraction simplification, and exponential simplification. Secondly, you can also use constant folding to directly replace the group of nodes that are all constants with calculations, or use common subexpression elimination to mark the information of each group of node operations (root nodes) and operands (left and right nodes). When encountering marked information, replace each group of nodes with previously marked references, and identify whether the expression conforms to the domain expert model, perform AST content conversion, and then traverse the optimized AST to generate optimized DSL expressions. In addition, there is domain expert model conversion.
[0107] Preferably, see Figure 2, the optimization strategy conversion rules include:
[0108] Mathematical algebra simplification, using the principle of identity in mathematical formulas to simplify expressions through mathematical rules;
[0109] For example, polynomial simplification:
[0110] The original formula is: 3x^2+2x+5x^2-3x+1, which is simplified to: 8x^2-x+1.
[0111] Fraction simplification:
[0112] Original formula: (x^2+2x) / (x+2)+x / (x+2), simplified: (x^2+3x) / (x+2).
[0113] Exponential simplification:
[0114] Original formula: (a^2*b^3)*(a^3*b^2), simplified: a^5*b^5.
[0115] Trigonometric simplification:
[0116] Original formula: sin^2(x)+cos^2(x), simplified: 1 (based on trigonometric identities).
[0117] Logarithmic simplification:
[0118] Original formula: log(a)+log(b), simplified: log(ab).
[0119] Constant folding, check nodes, and when they contain only constants, pre-calculate their results and replace the original nodes;
[0120] Common Subexpression Elimination (CSE), which marks the subtree of each operation, detects repeated structures, and replaces them with references to previously calculated results, where the subtree includes operators and operands;
[0121] Dead code elimination, identifying the parts of the expression that cannot be calculated, performing conversion and cleaning, and reducing redundant calculations.
[0122] Continue to see Figure 2 In addition to the above optimization strategies, it also supports extended domain expert models. These expert models are divided into two parts: matching and conversion, to meet special optimization processing in specific fields.
[0123] Preferably, the optimization strategy conversion rule also includes an extended domain expert model, and the domain expert model is divided into two parts: matching and conversion, to meet special optimization processing in a specific field.
[0124] There are many domain expert models in the industrial field. These optimizations are often used in specific usage scenarios. In addition to some domain expert models included in the built-in library of the tool, domain expert models based on AST rules can also be implemented in a customized way. By calculating the operating cost index and optimizing the conversion at different stages, users can better implement the evaluation and optimization of calculation expressions. When users need to customize and extend the domain expert model, they can customize the research and development based on the AST parsing conversion rules, and load it into the DSL calculation formula expression optimization module as an extension plug-in.
[0125] First, calculate the computational cost index before optimization and use it as the benchmark. Then use different optimization strategies to convert and optimize it, and calculate again to obtain the operating cost index after the conversion and optimization. Keep the best index (the lower the score, the better). Compare the best index with the benchmark before optimization. If the optimized index is better, keep this index as the new benchmark and use the optimized expression as the new expression to be optimized. Repeat the above steps and perform a new round of optimization until the operating cost evaluation value becomes stable and finally obtains the calculation expression with the best operating cost evaluation value.
[0126] Preferably, the method further comprises: iteratively performing multiple rounds of expression optimization conversion based on the formula running cost index judgment basis, and finally finding the most effective optimization result by comparing the effects of different optimization strategies. The specific steps are as follows:
[0127] Initial input and evaluation, receiving the original DSL calculation expression and calculating the running cost score;
[0128] Traverse and execute the optimization strategies, optimize the DSL calculation expression according to multiple strategies, and obtain the optimized result and its corresponding running cost score;
[0129] Re-evaluate, compare the running cost score of the optimized DSL calculation expression with the score of the previous round, if the new running cost score is lower than the previous round, accept the current optimization; if there is no improvement, abandon the optimization or terminate the optimization.
[0130] Based on the original expression running cost score, multiple rounds of expression optimization are automatically performed. By comparing the running cost scores of different optimization strategies, the most effective optimization method is selected and applied. After each round of optimization, the system re-evaluates the running cost score of the expression and compares it with the previous optimal score. The version with the largest running cost reduction is selected as the basis for the next round of optimization. This process continues until the running cost score no longer improves significantly, thus ensuring that the final output expression is optimal in terms of performance and resource efficiency.
[0131] Input the DSL calculation formula expression into the "iterative optimization processing module", and then directly obtain the result, compare and verify the optimization result with the source expression, and finally obtain the optimal result.
[0132] Based on the same concept, the present invention provides an electronic device, comprising:
[0133] The memory is used to store the processing program; the processor implements any of the above-mentioned evaluation methods or optimization methods when executing the processing program.
[0134] The embodiments of the present invention are described in detail above in conjunction with the accompanying drawings, but the present invention is not limited to the above embodiments. Even if various changes are made to the present invention, if these changes fall within the scope of the claims of the present invention and their equivalents, they still fall within the protection scope of the present invention.
Claims
1. A DSL calculation expression running cost evaluation method, characterized in that: The steps include: Reading a DSL calculation expression in a string form, and processing the DSL calculation expression into a structured abstract syntax tree; Traversing the abstract syntax tree structure of the DSL calculation expression, and matching corresponding running cost evaluation rules according to the type of each node in the abstract syntax tree structure; The running cost score of the DSL calculation expression is calculated based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node.
2. The DSL calculation expression running cost evaluation method according to claim 1, characterized in that: Processing the DSL computation expression into a structured abstract syntax tree further includes: The lexical analyzer collects the lexical rules required by the DSL calculation expression, and converts the input DSL calculation expression string into corresponding tokens, which at least contain the atomic elements of the expression: keyword identifiers, literals, and operators. The keyword identifiers include variables, functions, and types, and the literals include numbers, strings, and Boolean values. Through the parser, the grammar rules required by the DSL calculation expression are collected to analyze the token sequence and build a parse tree; The parse tree is converted into an abstract syntax tree so that the nodes contain not only operator and operand information but also control structure information, thereby more clearly expressing the logical structure of the DSL calculation expression.
3. The DSL calculation expression running cost evaluation method according to claim 1, characterized in that: The operating cost assessment rules further include: For constant nodes, the running cost score is 1 point; For variable nodes, the running cost score is 2 points; For addition / subtraction arithmetic operators, the running cost score is 3 points; For multiplication / division arithmetic operators, the running cost score is 4 points; Logical operator, the running cost score is 3 points; Function call, the basic score is 10 points, and each parameter adds an additional 2 points; Conditional functions, loop functions, and branch calculations are equivalent to function call rules.
4. The DSL calculation expression running cost evaluation method according to claim 3, characterized in that: The operating cost assessment rules further include: Node depth: For every additional layer of node depth, the running cost score increases by 1 point; Node width: Every time the number of nodes at the same level doubles, the overall level operation cost increases by 5 points.
5. The DSL calculation expression running cost evaluation method according to claim 1, characterized in that: Calculating the running cost score of the DSL calculation expression based on the abstract syntax tree structure and the running cost evaluation rule corresponding to each node further includes: When each node n in the DSL calculation expression is n i , where i is the index value of the node, and each node n i The computational cost can be expressed as C(n i ), as follows: Among them, b(n i ) is the basic running cost of the node. If the node is a function type, the running cost of all its parameters is added. j (n i ), w j represents the weighted value corresponding to this parameter, k is the number of parameters, is the cumulative value of the operating cost of all parameters; Node Depth Bonus: D(n i )=d(n i )×f, where d(n i ) represents the current node running cost, and f represents the weighted value of different depths; Node Width Bonus: Where L represents the level of the node, w(L) represents the computational cost corresponding to the level, and g is the fraction of the extra width added for every 10 nodes. The total operating cost calculation formula is: T=∑ i (C(n i )+D(n i ))+∑ L W(L)。 6. A method for optimizing the running cost of a DSL calculation expression, which optimizes the running cost based on the evaluation method described in any one of claims 1 to 5, characterized in that: The steps include: By traversing and analyzing the abstract syntax tree structure of the DSL calculation expression, the relationship and type of each node and its child nodes are analyzed; Match the corresponding optimization strategy conversion rules and update the corresponding abstract syntax tree nodes; Re-traverse the optimized abstract syntax tree, build and output a new optimized DSL calculation expression string.
7. The DSL calculation expression running cost optimization method according to claim 6, characterized in that: The optimization strategy conversion rules include: Mathematical algebra simplification, using the principle of identity in mathematical formulas to simplify expressions through mathematical rules; Constant folding, check nodes, and when they contain only constants, pre-calculate their results and replace the original nodes; Common subexpression elimination, marking the subtree of each operation, detecting recurring structures, and replacing them with references to previously computed results, where the subtree includes both operators and operands; Dead code elimination, identifying the parts of the expression that cannot be calculated, performing conversion and cleaning, and reducing redundant calculations.
8. The DSL calculation expression running cost optimization method according to claim 7, characterized in that: The optimization strategy conversion rule also includes an extended domain expert model, which is divided into two parts: matching and conversion, to meet special optimization processing in a specific domain.
9. The DSL calculation expression running cost optimization method according to claim 7, characterized in that: The method further includes: iteratively performing multiple rounds of expression optimization conversion based on the formula running cost indicator judgment basis, and finally finding the most effective optimization result by comparing the effects of different optimization strategies. The specific steps are as follows: Initial input and evaluation, receiving the original DSL calculation expression and calculating the running cost score; Traverse and execute the optimization strategies, optimize the DSL calculation expression according to multiple strategies, and obtain the optimized result and its corresponding running cost score; Re-evaluate, compare the running cost score of the optimized DSL calculation expression with the score of the previous round, if the new running cost score is lower than the previous round, accept the current optimization; if there is no improvement, abandon the optimization or terminate the optimization.
10. An electronic device, characterized in that: include: A memory, the memory being used to store a processing program; A processor, wherein when executing the processing program, the processor implements the evaluation method or optimization method according to any one of claims 1 to 9.
Citation Information
Cited By
Mathematical expression diagnosis method and device based on abstract syntax tree and semantic network
CN121118886A
Mathematical expression diagnosis method and device based on abstract syntax tree and semantic network
CN121118886B