Rule behavior model real-time code generation method based on rule description file

By using a real-time code generation method based on rule description files, the development and maintenance efficiency of computer-generated force models is solved, achieving efficient generation of hard-coded behavior models that are applicable to most backend platforms.

CN116755704BActive Publication Date: 2026-05-08BEIHANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIHANG UNIV
Filing Date
2023-05-16
Publication Date
2026-05-08

AI Technical Summary

Technical Problem

Existing technologies cannot simultaneously achieve the efficiency of developing, maintaining, and executing computer-generated force models.

Method used

By using a real-time code generation method based on rule description files, rule editing tools are used to parse the rule description files, perform lexical, syntactic and semantic analysis, construct an abstract syntax tree, generate hard-coded behavior model source code, and compile it together with the platform interface source code.

Benefits of technology

It simplifies the rule modeling process, improves the maintainability and execution efficiency of rule behavior models, is applicable to most backend platforms, and requires no dedicated hardware support.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116755704B_ABST
    Figure CN116755704B_ABST
Patent Text Reader

Abstract

The present application relates to a kind of rule behavior model real-time code generation method based on rule description file, belong to computer generated force technical field, solve the problem that the development, maintenance efficiency and model execution efficiency of model cannot be considered in prior art.The present application utilizes rule modeling tool without manual hard coding, simplifies rule modeling process and improves the maintainability of the rule behavior model obtained, facilitate subsequent development;Specific format rule description file is converted into equivalent hard coded rule behavior model without using rule explanation reasoning engine, improve the execution efficiency of rule behavior model;There is no demand to specific hardware, the code generated complies with C / C++ language specification, as long as there is C / C++ compiler in certain back-end platform (such as x86 / arm etc.), the behavior model code generated can be compiled to speed up, then can be run in the back-end platform, and most of back-end platforms have C / C++ compiler, therefore the present method is widely used.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer-generated troop technology, specifically to a real-time code generation method based on a rule behavior model using a rule description file. Background Technology

[0002] Using computer-generated force technology for tactical research is a field of great interest, and this application scenario places high demands on the execution speed of combat simulation systems. Therefore, improving the execution efficiency of computer-generated force models is a technical challenge that needs to be addressed.

[0003] In rule-based behavior modeling, there is a trade-off between execution efficiency and development / maintenance efficiency. Hard-coded rules can be directly compiled into machine code, offering good execution efficiency. However, general-purpose programming languages ​​are not designed for rule-based behavior modeling, resulting in rule set models with unclear structures, leading to development difficulties and maintenance challenges. Dedicated rule modeling tools can produce well-structured rule-based behavior models; however, they typically only provide rule representations in a specific format, which are then interpreted and executed by a rule interpretation engine, resulting in performance limitations.

[0004] In summary, existing technologies cannot simultaneously achieve high efficiency in model development and maintenance, as well as efficient model execution. Summary of the Invention

[0005] In view of the above problems, the present invention provides a real-time code generation method for rule behavior models based on rule description files, which solves the problem that the existing technology cannot balance the development and maintenance efficiency of the model and the execution efficiency of the model.

[0006] This invention provides a real-time code generation method for rule behavior models based on rule description files, comprising the following steps:

[0007] Step S1. Obtain the rule description file and parse it to obtain the rule set meta-information and the rule set containing several sub-rule sets; wherein, the sub-rule set includes several atomic rules, and each atomic rule includes an atomic rule antecedent expression and an atomic rule consequent expression;

[0008] Step S2. Perform lexical analysis on the antecedent and consequent expressions, which includes splitting and transformation to obtain an expression symbol stream;

[0009] Step S3. Based on the recursive descent algorithm and the operator precedence analysis algorithm, perform syntax analysis on the expression symbol stream to construct an expression abstract syntax tree;

[0010] Step S4. Perform semantic analysis on the expression abstract syntax tree to obtain the validated expression abstract syntax tree;

[0011] Step S5. Based on the validated expression abstract syntax tree corresponding to each sub-rule set, construct the abstract syntax tree of each sub-rule set; merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set;

[0012] Step S6. Based on the abstract syntax tree of the rule set, traverse all sub-rule sets and atomic rules, perform function call processing for inserting and reading variables, and obtain the abstract syntax tree used to generate code;

[0013] Step S7. Traverse each node in the abstract syntax tree used to generate code, generate hard-coded behavior model source code, and then combine and compile it with the platform interface source code to obtain the rule behavior model.

[0014] Furthermore, step S1 specifically includes the following sub-steps:

[0015] Step S101. Use the rule editing tool to export the rule description file from the knowledge base;

[0016] Step S102. Use a rule set description file parsing tool to parse the rule description file to obtain rule set meta-information and a rule set containing several sub-rule sets. Store the rule set meta-information in a data structure for recording rule set meta-information. The rule set meta-information includes complex data type definitions, input situation variable information, output action variable information, and intermediate state cache variable information.

[0017] Step S103. Using the rule set description file parsing tool, traverse each atomic rule of each sub-rule set in the rule description file in turn, obtain the antecedent expression and consequent expression of each atomic rule in each sub-rule set, and number them.

[0018] Furthermore, step S2 specifically includes the following sub-steps:

[0019] Step S201. Determine whether the current character of the preceding or following expression is a whitespace character. If it is, increment the current character pointer by one until the current character is not a whitespace character.

[0020] Step S202. Determine the type of the current character. If it is a number from 0 to 9, proceed to step S203-A. If it is an underscore or an English letter, proceed to step S203-B. Otherwise, proceed to step S203-C.

[0021] Step S203-A. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and return the read segment as a numeric literal type;

[0022] Step S203-B. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and determine whether the read string is a keyword. If it is, return the read segment as a keyword type; otherwise, return the read segment as an identifier type.

[0023] Step S203-C. Read in the character and return the fragment containing the character as a symbol type;

[0024] Step S204. Perform steps S201 to S202 on all the antecedent and consequent expressions obtained in step S1 to obtain an expression symbol stream.

[0025] Furthermore, step S3 specifically includes the following sub-steps:

[0026] Step S301. Determine whether the current symbol in the expression symbol stream is a prefix operator. If it is, read in the prefix operator and obtain its corresponding prefix expression; otherwise, the current symbol corresponds to a basic expression, and execute step S302 for it.

[0027] The remaining expression symbol stream continues to be evaluated and temporarily stored for prefix operators until all prefix operators and their corresponding prefix expressions are read into the expression symbol stream, and an abstract syntax tree corresponding to each prefix expression is constructed.

[0028] Step S302. Determine whether the current symbol of the basic expression is a literal or an identifier. If it is a literal, proceed to step S303; if it is an identifier, proceed to step S304.

[0029] Step S303. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the literal, and then execute step S305;

[0030] Step S304. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the identifier, and then determine whether the subsequent symbol is an operator corresponding to structure member access, array member access, or function call. If so, cache the operator and continue to execute step S302 for the remaining expression symbol stream. Using the abstract syntax tree corresponding to the identifier and the abstract syntax tree corresponding to the literal obtained in step S303, construct the abstract syntax tree for structure member access, array member access, and function call, and then execute step S305; otherwise, directly execute step S305.

[0031] Step S305. Determine whether the current symbol is an infix operator. If it is, use the operator precedence parsing algorithm to construct the abstract syntax tree corresponding to the infix expression based on the infix expression corresponding to the infix operator. If it is not, directly save the currently constructed abstract syntax tree.

[0032] Step S306. Perform steps S302 to S306 on the expression symbol stream, and combine it with the abstract syntax tree corresponding to the prefix expression obtained in step S301 to obtain the expression abstract syntax tree.

[0033] Furthermore, step S4 specifically includes the following sub-steps:

[0034] Step S401. Based on the rule set meta-information recorded in the data structure described in step S102, determine the current node type of the expression abstract syntax tree. If it is an identifier or literal node, query and update the current node type and return. If it is a function call node, execute step S402-A. If it is a member access operation, execute step S402-B. If it is an infix operation node, execute step S402-C.

[0035] Step S402-A. For each argument node of the function call, execute step S401, then look up the function overload table according to the function parameter type, select the real function whose function parameter type matches the actual parameter type of the call to replace the function call, update the return type of the current node and return;

[0036] Step S402-B. Execute step S401 for the node corresponding to the accessed value, then check the type definition to determine if the current member access is feasible. If feasible, update the return type of the current node to the member type and return; if not feasible, return directly.

[0037] Step S402-C. Perform step S401 on the left and right values ​​involved in the infix operation, and then determine whether the types of the left and right values ​​are both real number types. If so, update the return type of the current node to real number type and return; otherwise, return directly.

[0038] Step S403. Perform step S401 on all nodes of the expression abstract syntax tree to obtain the validated expression abstract syntax tree.

[0039] Furthermore, step S5 specifically includes the following sub-steps:

[0040] Step S501. Create a new conditional branch node on an atomic rule in the validated expression abstract syntax tree, construct a conditional branch statement of the form IF-THEN-ELSE, so that the antecedent expression of the atomic rule is used as the conditional expression of the conditional branch statement, the consequent expression of the atomic rule is used as the conditional expression of the conditional branch statement, and the abstract syntax tree corresponding to the remaining atomic rules is used as the false branch of the conditional branch statement; recursively perform the above processing on each atomic rule in a sub-rule set to obtain an abstract syntax tree of the sub-rule set connected by a series of conditional branch statements and conditional branch nodes;

[0041] Step S502. Perform step S501 for each sub-rule set to obtain the abstract syntax tree of each sub-rule set;

[0042] Step S503. Merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set.

[0043] Furthermore, step S6 specifically includes the following sub-steps:

[0044] Step S601. Construct a set to record the names of the loaded situation variables. This set is initially empty.

[0045] Step S602. Traverse the abstract syntax tree nodes of the antecedent and consequent expressions of the current atomic rule. In the set used to record the names of loaded situation variables, find the names of loaded situation variables and add a function call to the situation variable reading function corresponding to the situation variable name not included in the set before the antecedent expression; store all accessed situation variable names in the set.

[0046] Step S603. In the abstract syntax tree of the rule set, traverse each atomic rule of each sub-rule set and execute step S602 until all the status variable names accessed by each abstract syntax tree node are stored in the set to obtain the abstract syntax tree used to generate code.

[0047] Furthermore, step S7 specifically includes the following sub-steps:

[0048] Step S701. Traverse the nodes of the abstract syntax tree used for code generation, convert the abstract syntax tree used for code generation into the corresponding C / C++ code, enter it into the source code file, and generate the corresponding header file;

[0049] Step S702. Organize the platform interface template and fill in the operations related to the behavior model in the interface source code, including the input functions for input situation variables and intermediate state variables;

[0050] Step S703. Call the build system to build the source code file and header file obtained in step S701 with the interface template prepared in step S702, and call the compiler to compile and generate the rule behavior model; wherein, the rule behavior model refers to a dynamic library or executable file that can be used for a specific computer-generated force simulation platform.

[0051] Compared with the prior art, the present invention has at least the following beneficial effects:

[0052] (1) Compared with existing hard-coded rule modeling, the real-time code generation method of rule behavior model based on rule description file of the present invention uses rule modeling tools without manual hard coding, simplifies the rule modeling process and improves the maintainability of the obtained rule behavior model, which is convenient for subsequent development.

[0053] (2) Compared with existing methods that use rule interpretation and inference engines to interpret and execute rule description files, the real-time code generation method for rule behavior models based on rule description files of the present invention can convert rule description files of a specific format into equivalent hard-coded rule behavior models without using rule interpretation and inference engines, thereby improving the execution efficiency of rule behavior models.

[0054] (3) Compared with existing methods that use dedicated hardware such as FPGA or GPU for acceleration, the real-time code generation method of rule behavior model based on rule description file of the present invention has no requirement for specific hardware. The generated code conforms to the C / C++ language specification. As long as a backend platform (such as x86 / arm) has a C / C++ compiler, the generated behavior model code can be compiled and thus accelerated, and can run on the backend platform. Since most backend platforms have C / C++ compilers, this method has strong versatility. Attached Figure Description

[0055] The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of the invention.

[0056] Figure 1 This is a flowchart of a real-time code generation method for a rule behavior model based on a rule description file, as disclosed in an embodiment of the present invention.

[0057] Figure 2 This is a schematic diagram of the rule description file used in the real-time code generation method for rule behavior model based on rule description file disclosed in an embodiment of the present invention;

[0058] Figure 3 This is a flowchart of expression parsing in the real-time code generation method for rule behavior models based on rule description files disclosed in an embodiment of the present invention;

[0059] Figure 4This describes the lexical analysis process of the expression parsing tool in the real-time code generation method for rule behavior models based on rule description files disclosed in an embodiment of the present invention. Detailed Implementation

[0060] To better understand the above-described objectives, features, and advantages of the present invention, the invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. It should be noted that, unless otherwise specified, the embodiments of the present invention and the features thereof can be combined with each other. Furthermore, the present invention can be implemented in other ways different from those described herein; therefore, the scope of protection of the present invention is not limited to the specific embodiments disclosed below.

[0061] Example 1

[0062] A specific embodiment of the present invention, such as Figure 1 As shown, a real-time code generation method for rule behavior models based on rule description files is disclosed, including the following steps:

[0063] Step S1. Obtain the rule description file and parse it to obtain the rule set meta-information and the rule set containing several sub-rule sets; wherein, the sub-rule set includes several atomic rules, and each atomic rule includes an atomic rule antecedent expression and an atomic rule consequent expression.

[0064] Step S2. Perform lexical analysis on the antecedent and consequent expressions. This process includes splitting and transformation to obtain an expression symbol stream; wherein the antecedent and consequent expressions are in string form.

[0065] Step S3. Based on the recursive descent algorithm and the operator precedence analysis algorithm, perform syntax analysis on the expression symbol stream to construct the expression abstract syntax tree.

[0066] Step S4. Perform semantic analysis on the expression abstract syntax tree to determine whether the access to input situation variable information conforms to the situation variable type declared in the rule set meta-information, and obtain the validated expression abstract syntax tree; where,

[0067] Access to input situation variable information refers to the access of variables by expressions. For example, the expression "x = p.x + 2" accesses two variables, x and p. The situation variable types declared in the rule set meta-information are obtained based on the set of rule set meta-information stored in step 102. The judgment here mainly refers to "judging whether the current member access is feasible" in step 403 and "judging whether both types are real number types" in step 404 of embodiment 2. The remaining steps are to provide information for these two judgments.

[0068] Step S5. Based on the validated expression abstract syntax tree corresponding to each sub-rule set, construct the abstract syntax tree of each sub-rule set; merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set.

[0069] Step S6. Based on the abstract syntax tree of the rule set, traverse all sub-rule sets and atomic rules, perform function call processing for inserting and reading variables, and obtain the abstract syntax tree used to generate code.

[0070] like Figure 3 The diagram shows the expression parsing process in the real-time code generation method for rule behavior models based on rule description files disclosed in this embodiment of the invention, corresponding to steps S2 to S4.

[0071] The purpose of inserting function calls to read variables is to support lazy loading and avoid redundant loading of unused input situation variables. Specifically, before accessing input situation variables and intermediate state variables, corresponding function calls need to be inserted to read the actual values ​​of the variables. Furthermore, for each input situation variable and intermediate state variable, a function call to read the variable must be inserted before access. Since output variables only need to be written and do not need to be read, no additional reading is required. Lazy loading means loading data only when it is actually accessed, rather than loading all data at the beginning. It is the effect of "inserting a function call to read a variable before the first access to a variable".

[0072] Step S7. Traverse each node in the abstract syntax tree used to generate code, generate the hard-coded behavior model source code, and then combine and compile it with the platform interface source code to obtain the rule-based behavior model; where,

[0073] The model generated by the real-time code generation method proposed in this solution has the same running performance as the original rule set behavior model in the simulation platform and can be directly replaced; the original rule set behavior model refers to the model obtained by combining the rule interpretation and reasoning engine with the rule description file.

[0074] Compared to existing hard-coded rule modeling, the real-time code generation method for rule behavior models based on rule description files in this invention utilizes rule modeling tools without manual hard coding, simplifying the rule modeling process and improving the maintainability of the resulting rule behavior model, facilitating subsequent development. Compared to existing methods that use rule interpretation and inference engines to interpret and execute rule description files, the real-time code generation method for rule behavior models in this invention can convert rule description files of a specific format into equivalent hard-coded rule behavior models without using rule interpretation and inference engines, improving the execution efficiency of rule behavior models. Compared to existing methods that use dedicated hardware such as FPGAs or GPUs for acceleration, the real-time code generation method for rule behavior models in this invention has no specific hardware requirements. The generated code conforms to the C / C++ language specification. As long as a backend platform (such as x86 / arm) has a C / C++ compiler, the generated behavior model code can be compiled and accelerated, and it can run on that backend platform. Since most backend platforms have C / C++ compilers, this method has strong versatility.

[0075] Example 2

[0076] Based on Example 1, step S1 can be further refined into the following sub-steps:

[0077] Step S101. Use the rule editing tool to export the rule description file from the knowledge base.

[0078] Specifically, the rule description file is in XML format. The rule description XML file contains the input situation variable information, output action variable information and intermediate state variable information of the rule set, the definition of complex data types involved in the rule set reasoning process, and several sub-rule sets of the rule set.

[0079] Step S102. Use a rule set description file parsing tool to parse the rule description file, obtaining the rule set metadata and a rule set containing several sub-rule sets, such as... Figure 2 As shown.

[0080] Store the rule set metadata into a data structure used to record the rule set metadata; wherein...

[0081] The rule set metadata includes complex data type definitions, input situation variable information, output action variable information, and intermediate state cache variable information; during storage, the rule set metadata is converted into a form that is easy for the program to process.

[0082] Specifically, input situation variable information includes the name and type of the input situation variable; output action variable information includes the name and type of the output action variable; intermediate state variable information includes the name and type of the intermediate state variable; complex data refers to data containing multiple basic data elements, such as location data, which is a type of complex data containing three floating-point elements: longitude, latitude, and altitude.

[0083] Step S103. Using the rule set description file parsing tool, sequentially traverse each atomic rule of each sub-rule set in the rule description file, obtain the antecedent and consequent expressions of each atomic rule in each sub-rule set, and number them; where,

[0084] The antecedent expression refers to the expression for the input situational variables in the antecedent, and the consequent expression refers to the expression for the output action variables in the consequent.

[0085] like Figure 4 As shown, step S2 can be further refined into the following sub-steps:

[0086] Step S201. Determine whether the current character of the preceding or following expression is a whitespace character. If it is, increment the current character pointer by one until the current character is not a whitespace character.

[0087] Step S202. Determine the type of the current character. If it is a number from 0 to 9, proceed to step S203-A. If it is an underscore or an English letter, proceed to step S203-B. Otherwise, proceed to step S203-C.

[0088] Step S203-A. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and return the read segment as a numeric literal type.

[0089] Step S203-B. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and determine whether the read string is a keyword. If it is, return the read segment as a keyword type; otherwise, return the read segment as an identifier type.

[0090] Step S203-C. Read in the character and return the fragment containing the character as a symbol type.

[0091] Step S204. Perform steps S201 to S202 on all the predecessor and consequent expressions obtained in step S1 to obtain an expression symbol stream with an identifier type.

[0092] Step S3 can be further broken down into the following sub-steps:

[0093] Step S301. Determine whether the current symbol in the expression symbol stream is a prefix operator. If it is, read in the prefix operator and obtain its corresponding prefix expression; otherwise, the current symbol corresponds to a basic expression, and execute step S302 for it.

[0094] The remaining expression symbol stream continues to be evaluated and temporarily stored for prefix operators until all prefix operators and their corresponding prefix expressions are read into the expression symbol stream, and an abstract syntax tree corresponding to each prefix expression is constructed.

[0095] Step S302. Determine whether the current symbol of the basic expression is a literal or an identifier. If it is a literal, proceed to step S303; if it is an identifier, proceed to step S304.

[0096] Step S303. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the literal, and then execute step S305.

[0097] Step S304. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the identifier, and then determine whether the subsequent symbol is an operator corresponding to structure member access, array member access, or function call. If so, cache the operator and continue to execute step S302 for the remaining expression symbol stream. Using the abstract syntax tree corresponding to the identifier and the abstract syntax tree corresponding to the literal obtained in step S303, construct the abstract syntax trees for structure member access, array member access, and function call, and then execute step S305; otherwise, directly execute step S305.

[0098] Step S305. Determine whether the current symbol is an infix operator. If it is, use the operator precedence parsing algorithm to construct the abstract syntax tree corresponding to the infix expression based on the infix expression corresponding to the infix operator. If it is not, directly save the currently constructed abstract syntax tree.

[0099] Step S306. Perform steps S302 to S306 on the expression symbol stream, and combine it with the abstract syntax tree corresponding to the prefix expression obtained in step S301 to obtain the expression abstract syntax tree.

[0100] Step S4 can be further broken down into the following sub-steps:

[0101] Step S401. Based on the rule set meta-information recorded in the data structure in step S102, determine the current node type of the expression abstract syntax tree. If it is an identifier or literal node, query and update the current node type and return. If it is a function call node, execute step S402-A. If it is a member access operation, execute step S402-B. If it is an infix operation node, execute step S402-C.

[0102] Step S402-A. For each argument node of the function call, execute step S401, then look up the function overload table according to the function parameter type, select the real function whose function parameter type matches the actual parameter type of the call to replace the function call, update the return type of the current node and return;

[0103] Step S402-B. Execute step S401 for the node corresponding to the accessed value, then check the type definition to determine if the current member access is feasible. If feasible, update the return type of the current node to the member type and return; if not feasible, return directly.

[0104] Step S402-C. Perform step S401 on the left and right values ​​involved in the infix operation, and then determine whether the types of the left and right values ​​are both real number types. If so, update the return type of the current node to real number type and return; otherwise, return directly.

[0105] Step S403. Perform step S401 on all nodes of the expression abstract syntax tree to obtain the validated expression abstract syntax tree.

[0106] Step S5 can be further broken down into the following sub-steps:

[0107] Step S501. Create a new conditional branch node on an atomic rule in the validated expression abstract syntax tree, construct a conditional branch statement of the form IF-THEN-ELSE, so that the antecedent expression of the atomic rule is used as the conditional expression of the conditional branch statement, the consequent expression of the atomic rule is used as the conditional expression of the conditional branch statement, and the abstract syntax tree corresponding to the remaining atomic rules is used as the false branch of the conditional branch statement; recursively perform the above processing on each atomic rule in a sub-rule set to obtain an abstract syntax tree of the sub-rule set connected by a series of conditional branch statements and conditional branch nodes.

[0108] Step S502. Perform step S501 for each sub-rule set to obtain the abstract syntax tree of each sub-rule set.

[0109] Step S503. Merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set.

[0110] Step S6 can be further broken down into the following sub-steps:

[0111] Step S601. Construct a set to record the names of the loaded situation variables. This set is initially empty.

[0112] Step S602. Traverse the abstract syntax tree nodes of the antecedent and consequent expressions of the current atomic rule. In the set used to record the names of loaded situation variables, find the names of loaded situation variables and add the function call of the situation variable reading function corresponding to the situation variable name not included in the set before the antecedent expression; store all accessed situation variable names in the set.

[0113] Step S603. In the abstract syntax tree of the rule set, traverse each atomic rule of each sub-rule set and execute step S602 until all the status variable names accessed by each abstract syntax tree node are stored in the set to obtain the abstract syntax tree used to generate code.

[0114] Step S7 can be further broken down into the following sub-steps:

[0115] Step S701. Traverse the nodes of the abstract syntax tree used for code generation, convert the abstract syntax tree used for code generation into the corresponding C / C++ code, enter it into the source code file, and generate the corresponding header file;

[0116] Step S702. Organize the platform interface template and fill in the operations related to the behavior model in the interface source code, including the input functions for input situation variables and intermediate state variables;

[0117] Step S703. Call the build system to build the source code file and header file obtained in step S701 with the interface template prepared in step S702, and call the compiler to compile and generate the rule behavior model; wherein, the rule behavior model refers to a dynamic library or executable file that can be used for a specific computer-generated force simulation platform.

[0118] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention.

Claims

1. A real-time code generation method for rule behavior models based on rule description files, characterized in that, Includes the following steps: Step S1. Obtain the rule description file and parse it to obtain the rule set meta-information and the rule set containing several sub-rule sets; wherein, the sub-rule set includes several atomic rules, and each atomic rule includes an atomic rule antecedent expression and an atomic rule consequent expression; Step S2. Perform lexical analysis on the antecedent and consequent expressions, which includes splitting and transformation to obtain an expression symbol stream; Step S3. Based on the recursive descent algorithm and the operator precedence analysis algorithm, perform syntax analysis on the expression symbol stream to construct an expression abstract syntax tree; Step S4. Perform semantic analysis on the expression abstract syntax tree to obtain the validated expression abstract syntax tree; Step S5. Based on the validated expression abstract syntax tree corresponding to each sub-rule set, construct the abstract syntax tree of each sub-rule set; merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set; Step S6. Based on the abstract syntax tree of the rule set, traverse all sub-rule sets and atomic rules, perform function call processing for inserting and reading variables, and obtain the abstract syntax tree used to generate code; Step S7. Traverse each node in the abstract syntax tree used to generate code, generate hard-coded behavior model source code, and then combine and compile it with the platform interface source code to obtain the rule behavior model.

2. The real-time code generation method for rule behavior models based on rule description files according to claim 1, characterized in that, Step S1 specifically includes the following sub-steps: Step S101. Use the rule editing tool to export the rule description file from the knowledge base; Step S102. Use a rule set description file parsing tool to parse the rule description file to obtain rule set meta-information and a rule set containing several sub-rule sets. Store the rule set meta-information in a data structure for recording rule set meta-information. The rule set meta-information includes complex data type definitions, input situation variable information, output action variable information, and intermediate state cache variable information. Step S103. Using the rule set description file parsing tool, traverse each atomic rule of each sub-rule set in the rule description file in turn, obtain the antecedent expression and consequent expression of each atomic rule in each sub-rule set, and number them.

3. The real-time code generation method for rule behavior models based on rule description files according to claim 2, characterized in that, Step S2 specifically includes the following sub-steps: Step S201. Determine whether the current character of the preceding or following expression is a whitespace character. If it is, increment the current character pointer by one until the current character is not a whitespace character. Step S202. Determine the type of the current character. If it is a number from 0 to 9, proceed to step S203-A. If it is an underscore or an English letter, proceed to step S203-B. Otherwise, proceed to step S203-C. Step S203-A. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and return the read segment as a numeric literal type; Step S203-B. Starting from the current character, continue reading characters until a whitespace character or symbol is encountered, and determine whether the read string is a keyword. If it is, return the read segment as a keyword type; otherwise, return the read segment as an identifier type. Step S203-C. Read in the character and return the fragment containing the character as a symbol type; Step S204. Perform steps S201 to S202 on all the antecedent and consequent expressions obtained in step S1 to obtain an expression symbol stream.

4. The real-time code generation method for rule behavior models based on rule description files according to claim 3, characterized in that, Step S3 specifically includes the following sub-steps: Step S301. Determine whether the current symbol in the expression symbol stream is a prefix operator. If it is, read in the prefix operator and obtain its corresponding prefix expression; otherwise, the current symbol corresponds to a basic expression, and execute step S302 for it. The remaining expression symbol stream continues to be evaluated and temporarily stored for prefix operators until all prefix operators and their corresponding prefix expressions are read into the expression symbol stream, and an abstract syntax tree corresponding to each prefix expression is constructed. Step S302. Determine whether the current symbol of the basic expression is a literal or an identifier. If it is a literal, proceed to step S303; if it is an identifier, proceed to step S304. Step S303. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the literal, and then execute step S305; Step S304. Read in the current symbol of the basic expression, construct the abstract syntax tree corresponding to the identifier, and then determine whether the subsequent symbol is an operator corresponding to structure member access, array member access, or function call. If so, cache the operator and continue to execute step S302 for the remaining expression symbol stream. Using the abstract syntax tree corresponding to the identifier and the abstract syntax tree corresponding to the literal obtained in step S303, construct the abstract syntax tree for structure member access, array member access, and function call, and then execute step S305; otherwise, directly execute step S305. Step S305. Determine whether the current symbol is an infix operator. If it is, use the operator precedence parsing algorithm to construct the abstract syntax tree corresponding to the infix expression based on the infix expression corresponding to the infix operator. If it is not, directly save the currently constructed abstract syntax tree. Step S306. Perform steps S302 to S306 on the expression symbol stream, and combine it with the abstract syntax tree corresponding to the prefix expression obtained in step S301 to obtain the expression abstract syntax tree.

5. The real-time code generation method for rule behavior models based on rule description files according to claim 4, characterized in that, Step S4 specifically includes the following sub-steps: Step S401. Based on the rule set meta-information recorded in the data structure described in step S102, determine the current node type of the expression abstract syntax tree. If it is an identifier or literal node, query and update the current node type and return. If it is a function call node, then execute step S402-A; if it is a member access operation, then execute step S402-B; if it is an infix operation node, then execute step S402-C. Step S402-A. For each argument node of the function call, execute step S401, then look up the function overload table according to the function parameter type, select the real function whose function parameter type matches the actual parameter type of the call to replace the function call, update the return type of the current node and return; Step S402-B. Execute step S401 for the node corresponding to the accessed value, then check the type definition to determine if the current member access is feasible. If feasible, update the return type of the current node to the member type and return; if not feasible, return directly. Step S402-C. Perform step S401 on the left and right values ​​involved in the infix operation, and then determine whether the types of the left and right values ​​are both real number types. If so, update the return type of the current node to real number type and return; otherwise, return directly. Step S403. Perform step S401 on all nodes of the expression abstract syntax tree to obtain the validated expression abstract syntax tree.

6. The real-time code generation method for rule behavior models based on rule description files according to claim 5, characterized in that, Step S5 specifically includes the following sub-steps: Step S501. Create a new conditional branch node on an atomic rule in the validated expression abstract syntax tree, construct a conditional branch statement in the form of IF-THEN-ELSE, so that the antecedent expression of the atomic rule is used as the conditional expression of the conditional branch statement, the consequent expression of the atomic rule is used as the conditional expression of the conditional branch statement, and the abstract syntax tree corresponding to the remaining atomic rules is used as the false branch of the conditional branch statement. The above process is recursively applied to each atomic rule in a sub-rule set to obtain an abstract syntax tree of the sub-rule set, which is connected by a series of conditional branch statements and conditional branch nodes. Step S502. Perform step S501 for each sub-rule set to obtain the abstract syntax tree of each sub-rule set; Step S503. Merge the abstract syntax trees of each sub-rule set to obtain the abstract syntax tree of the rule set.

7. The real-time code generation method for rule behavior models based on rule description files according to claim 6, characterized in that, Step S6 specifically includes the following sub-steps: Step S601. Construct a set to record the names of the loaded situation variables. This set is initially empty. Step S602. Traverse the abstract syntax tree nodes of the antecedent and consequent expressions of the current atomic rule. In the set used to record the names of loaded situation variables, find the names of loaded situation variables and add a function call to the situation variable reading function corresponding to the situation variable name not included in the set before the antecedent expression; store all accessed situation variable names in the set. Step S603. In the abstract syntax tree of the rule set, traverse each atomic rule of each sub-rule set and execute step S602 until all the status variable names accessed by each abstract syntax tree node are stored in the set to obtain the abstract syntax tree used to generate code.

8. The real-time code generation method for rule behavior models based on rule description files according to claim 7, characterized in that, Step S7 specifically includes the following sub-steps: Step S701. Traverse the nodes of the abstract syntax tree used for code generation, convert the abstract syntax tree used for code generation into the corresponding C / C++ code, enter it into the source code file, and generate the corresponding header file; Step S702. Organize the platform interface template and fill in the operations related to the behavior model in the interface source code, including the input functions for input situation variables and intermediate state variables; Step S703. Call the build system to build the source code file and header file obtained in step S701 with the interface template prepared in step S702, and call the compiler to compile and generate the rule behavior model; wherein, the rule behavior model refers to a dynamic library or executable file that can be used for a specific computer-generated force simulation platform.

Citation Information

Patent Citations

  • Code generation method and device, terminal and medium

    CN109947423A

  • Intelligent game training and simulation device for complex equipment system confrontation

    CN115394140A