CN-116755704-B - Rule behavior model real-time code generation method based on rule description file
Abstract
The invention relates to a rule behavior model real-time code generation method based on a rule description file, belongs to the technical field of computer generation force, and solves the problem that development and maintenance efficiency of a model and execution efficiency of the model cannot be considered in the prior art. The method has the advantages that a rule modeling tool is utilized without manual hard coding, a rule modeling flow is simplified, maintainability of an obtained rule behavior model is improved, subsequent development is facilitated, a rule description file in a specific format is converted into an equivalent hard coding rule behavior model without using a rule interpretation reasoning engine, execution efficiency of the rule behavior model is improved, no requirement is required for specific hardware, generated codes accord with C/C++ language specifications, generated behavior model codes can be compiled and accelerated as long as a certain back-end platform (such as x86/arm and the like) exists in a C/C++ compiler, the back-end platform can operate, and most of the back-end platforms exist in the C/C++ compiler, so that the method is high in universality.
Inventors
- LI NI
- DAI JUNWEI
- GONG GUANGHONG
- LIU WENTAO
Assignees
- 北京航空航天大学
Dates
- Publication Date
- 20260508
- Application Date
- 20230516
Claims (8)
- 1. A rule behavior model real-time code generation method based on a rule description file is characterized by comprising the following steps: s1, acquiring a rule description file, and analyzing the rule description file to obtain rule set meta-information and a rule set comprising a plurality of sub-rule sets, wherein the sub-rule sets comprise a plurality of atomic rules, and each atomic rule comprises an atomic rule front part expression and an atomic rule back part expression; S2, performing lexical analysis processing on the front part expression and the back part expression, wherein the processing comprises splitting and converting to obtain an expression symbol stream; S3, carrying out grammar analysis processing on the expression symbol stream based on a recursion descent algorithm and an operator priority analysis algorithm, and constructing an expression abstract grammar tree; S4, carrying out semantic analysis processing on the expression abstract syntax tree to obtain a verified expression abstract syntax tree; S5, based on the verified expression abstract syntax tree corresponding to each sub-rule set, constructing to obtain abstract syntax trees of each sub-rule set; s6, traversing all sub-rule sets and atom rules based on the abstract syntax tree of the rule set, and performing function call processing of inserting reading variables to obtain the abstract syntax tree for generating codes; And S7, traversing each node in the abstract syntax tree for generating codes, generating hard coding behavior model source codes, and then carrying out combined compiling on the hard coding behavior model source codes and platform interface source codes to obtain a rule behavior model.
- 2. The method for generating a rule behavior model real-time code based on a rule description file according to claim 1, wherein said step S1 specifically comprises the following sub-steps: S101, deriving a rule description file from a knowledge base by using a rule editing tool; S102, analyzing the rule description file by using a rule set description file analysis tool to obtain rule set meta-information and a rule set comprising a plurality of sub-rule sets, and storing the rule set meta-information into a data structure for recording the rule set meta-information, wherein the rule set meta-information comprises complex data type definition, input situation variable information, output action variable information and intermediate state cache variable information; s103, traversing each atomic rule of each sub-rule set in the rule description file in turn by using a rule set description file analysis tool, acquiring a front part expression and a back part expression of each atomic rule in each sub-rule set, and numbering the front part expression and the back part expression.
- 3. The method for generating the real-time code of the rule behavior model based on the rule description file according to claim 2, wherein the step S2 specifically comprises the following sub-steps: step S201, judging whether the current character of the front part expression or the back part expression is a blank character, if so, adding one to the current character pointer until the current character is not the blank character; S202, judging the current character type, if the current character type is a number of 0-9, executing the step S203-A, if the current character type is an underline or English letter, executing the step S203-B, otherwise, executing the step S203-C; Step S203-A, from the current character, continuously reading the character until encountering a blank or a symbol, and returning the read fragment in a digital word size type; Step S203-B, continuously reading in characters from the current character until a blank or a symbol is encountered, judging whether the read-in character string is a keyword, if so, returning the read-in fragment with the keyword type, otherwise, returning the read-in fragment with the identifier type; S203-C, reading the character, and returning the segment containing the character in a symbol type; and S204, executing step S201 to step S202 on all the front piece expressions and the back piece expressions obtained in the step S1 to obtain an expression symbol stream.
- 4. The method for generating a rule behavior model real-time code based on a rule description file according to claim 3, wherein said step S3 specifically comprises the following sub-steps: S301, judging whether a current symbol in the symbol stream of the expression is a prefix operator, if so, reading the prefix operator to obtain a corresponding prefix expression, and if not, executing step S302 on the current symbol corresponding to the basic expression; Continuing judging and temporarily storing prefix operators on the rest of the expression symbol stream until all prefix operators and corresponding prefix expressions in the expression symbol stream are read in, and constructing and obtaining abstract syntax trees corresponding to the prefix expressions; S302, judging whether the current symbol of the basic expression is a literal quantity or an identifier, if so, executing the step S303, and if so, executing the step S304; s303, reading in the current symbol of the basic expression, constructing an abstract syntax tree corresponding to the literal quantity, and then executing step S305; Step S304, reading in the current symbol of the basic expression, constructing an abstract syntax tree corresponding to an identifier, then judging whether the subsequent symbol is an operator corresponding to the member access of the structure, the member access of the array and the function call, if yes, caching the operator and continuing to execute the step S302 on the symbol stream of the residual expression, constructing the abstract syntax tree corresponding to the member access of the structure, the member access of the array and the function call by utilizing the abstract syntax tree corresponding to the identifier and the abstract syntax tree corresponding to the literal quantity obtained in the step S303, and then executing the step S305; S305, judging whether the current symbol is a prefix operator, if so, constructing an abstract syntax tree corresponding to the prefix operator based on a prefix expression corresponding to the prefix operator by utilizing an operator priority analysis algorithm, and if not, directly storing the abstract syntax tree constructed currently; s306, executing step S302-step S306 on the expression symbol stream, and combining the abstract syntax tree corresponding to the prefix expression obtained in step S301 to obtain an expression abstract syntax tree.
- 5. The method for generating the real-time code of the rule behavior model based on the rule description file according to claim 4, wherein said step S4 specifically comprises the following sub-steps: S401, judging the current node type of the expression abstract syntax tree based on the rule set meta information recorded in the data structure in the step S102, inquiring and updating the current node type and returning the current node type if the current node type is an identifier or a literal quantity node, executing the step S402-A if the current node type is a function call node, executing the step S402-B if the current node type is a member access operation, and executing the step S402-C if the current node type is a prefix operation node; Step S402-A, executing step S401 on each real parameter node of the function call, searching a function reload table according to the function parameter type, selecting a real function with the function shape parameter type matched with the called real parameter type to replace the function call, updating the return type of the current node and returning; Step S402-B, executing step S401 on the node corresponding to the accessed value, then searching for a type definition to judge whether the current member access is feasible, if so, updating the return type of the current node to the member type and returning; Step S402-C, executing step S401 on the left side value and the right side value which participate in the prefix operation, then judging whether the types of the left side value and the right side value are real types, if so, updating the return type of the current node to the real type and returning, otherwise, directly returning; S403, executing the step S401 on all nodes of the expression abstract syntax tree to obtain the verified expression abstract syntax tree.
- 6. The method for generating a rule behavior model real-time code based on a rule description file according to claim 5, wherein said step S5 specifically comprises the following sub-steps: Step S501, creating a conditional branch node on an atomic rule on the verified expression abstract syntax tree, constructing a conditional branch statement in the form of an IF-THEN-ELSE, enabling a front part expression of the atomic rule to be used as the conditional expression of the conditional branch statement, enabling a rear part expression of the atomic rule to be used as the conditional expression of the conditional branch statement, enabling an abstract syntax tree corresponding to the residual atomic rule to be used as a false value branch of the conditional branch statement, and recursively carrying out the processing on each atomic rule in a sub-rule set to obtain the abstract syntax tree of the sub-rule set connected by a series of conditional branch statements and the conditional branch node; S502, executing a step S501 on each sub-rule set to obtain an abstract syntax tree of each sub-rule set; S503, merging the abstract syntax trees of all the sub-rule sets to obtain the abstract syntax tree of the rule set.
- 7. The method for generating the real-time code of the rule behavior model based on the rule description file according to claim 6, wherein said step S6 specifically comprises the following sub-steps: S601, constructing a set for recording the names of the loaded situation variables, wherein the set is initially empty; Step S602, traversing abstract syntax tree nodes of a front part expression and a back part expression of a current atomic rule, searching for a loaded situation variable name in the set for recording the loaded situation variable name, and adding function call of a situation variable reading function corresponding to the situation variable name which is not included in the set before the front part expression; Step S603, traversing each atomic rule of each sub-rule set in the abstract syntax tree of the rule set, and executing step S602 until all situation variable names accessed by each abstract syntax tree node are stored in the set, so as to obtain the abstract syntax tree for generating codes.
- 8. The method for generating the real-time code of the rule behavior model based on the rule description file according to claim 7, wherein said step S7 specifically comprises the following sub-steps: S701, traversing nodes of an abstract syntax tree for code generation, converting the abstract syntax tree for code generation into corresponding C/C++ codes, inputting the corresponding C/C++ codes into a source code file, and generating a corresponding header file; s702, sorting a platform interface template, and filling operations related to a behavior model in interface source codes, wherein the operations comprise reading functions of input situation variables and intermediate state variables; S703, calling a construction system to construct the source code file and the header file obtained in the step S701 and the interface template arranged in the step S702, and calling a compiler to compile and generate a rule behavior model, wherein the rule behavior model refers to a dynamic library or an executable file which can be used for a specific computer to generate a force simulation platform.
Description
Rule behavior model real-time code generation method based on rule description file Technical Field The invention relates to the technical field of army force generation by a computer, in particular to a rule behavior model real-time code generation method based on a rule description file. Background Tactical research by using a computer generated force technology is a research field which is paid attention to, and the application scene has higher requirement on the execution speed of a combat simulation system. Therefore, how to improve the execution efficiency of the computer generated force model is a technical problem to be solved. In the rule set-based behavior modeling process, there is a trade-off between execution efficiency and development and maintenance efficiency. The rule behavior model obtained by hard coding rules can be directly compiled into machine codes through a compiler, so that the machine codes have good execution efficiency, a general programming language is not designed for rule behavior modeling, the rule set model structure written by the general programming language is relatively unclear, so that certain development difficulty exists, the later period is difficult to maintain, the rule behavior model with clear structure can be obtained by using a special rule modeling tool, however, the rule behavior model can only be generally represented by rules in a specific format and can be interpreted and executed by a rule interpretation engine during execution, and the defect of the execution efficiency exists. In summary, the prior art has a problem that development and maintenance efficiency of a model and execution efficiency of the model cannot be considered. Disclosure of Invention In view of the above problems, the invention provides a rule behavior model real-time code generation method based on a rule description file, which solves the problem that the development and maintenance efficiency of a model and the execution efficiency of the model cannot be considered in the prior art. The invention provides a rule behavior model real-time code generation method based on a rule description file, which comprises the following steps: s1, acquiring a rule description file, and analyzing the rule description file to obtain rule set meta-information and a rule set comprising a plurality of sub-rule sets, wherein the sub-rule sets comprise a plurality of atomic rules, and each atomic rule comprises an atomic rule front part expression and an atomic rule back part expression; S2, performing lexical analysis processing on the front part expression and the back part expression, wherein the processing comprises splitting and converting to obtain an expression symbol stream; S3, carrying out grammar analysis processing on the expression symbol stream based on a recursion descent algorithm and an operator priority analysis algorithm, and constructing an expression abstract grammar tree; S4, carrying out semantic analysis processing on the expression abstract syntax tree to obtain a verified expression abstract syntax tree; S5, based on the verified expression abstract syntax tree corresponding to each sub-rule set, constructing to obtain abstract syntax trees of each sub-rule set; s6, traversing all sub-rule sets and atom rules based on the abstract syntax tree of the rule set, and performing function call processing of inserting reading variables to obtain the abstract syntax tree for generating codes; And S7, traversing each node in the abstract syntax tree for generating codes, generating hard coding behavior model source codes, and then carrying out combined compiling on the hard coding behavior model source codes and platform interface source codes to obtain a rule behavior model. Further, the step S1 specifically includes the following sub-steps: S101, deriving a rule description file from a knowledge base by using a rule editing tool; S102, analyzing the rule description file by using a rule set description file analysis tool to obtain rule set meta-information and a rule set comprising a plurality of sub-rule sets, and storing the rule set meta-information into a data structure for recording the rule set meta-information, wherein the rule set meta-information comprises complex data type definition, input situation variable information, output action variable information and intermediate state cache variable information; s103, traversing each atomic rule of each sub-rule set in the rule description file in turn by using a rule set description file analysis tool, acquiring a front part expression and a back part expression of each atomic rule in each sub-rule set, and numbering the front part expression and the back part expression. Further, the step S2 specifically includes the following sub-steps: step S201, judging whether the current character of the front part expression or the back part expression is a blank character, if so, adding one to the current character pointer until the curr