A method for constructing low-coupling simulation behavior rule resources based on reflection mechanism
By building a rule mapping table and registration program through the reflection mechanism, the rule resources and the simulation system are decoupled, which solves the problem of tight coupling between the rule resources and the simulation system, reduces maintenance costs and compilation time, and supports rapid iterative rule updates and testing.
Patent Information
- Application Number
- CN202510819122.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-18
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2045-06-18
AI Technical Summary
In the existing technology, rule resources are tightly coupled with the simulation system, resulting in high cost for rule modification and maintenance, long compilation time, and difficulty for ordinary users to independently update and maintain rules.
A low-coupling simulation behavior rule resource construction method based on reflection mechanism is adopted. By constructing rule mapping table and rule resource registration program, the decoupling of rule resources and simulation system is achieved, allowing rule resources to be managed and modified independently, reducing dependence on the simulation system.
It reduces rule modification and compilation time, reduces user workload, supports rapid iterative rule generation and testing, and shortens the time from rule editing to running effect feedback.
Smart Images

Figure CN120315677B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of simulation, and in particular to a method for constructing low-coupling simulation behavior rule resources based on a reflection mechanism. Background Art
[0002] Rules are crucial resources in simulation, determining the behavior of simulated entities within the environment. Rules acquire the current state of the environment and entities in the simulation in real time and, based on predefined rule logic, determine the actions that entities should perform. Entities interact with the environment according to the actions determined by the rules and participate in the simulation calculations. Therefore, rules can be viewed as entities (or "agents") with intelligent decision-making capabilities. Rules can be expressed in various forms, such as state machines, behavior trees, and flow charts. Behavior trees are widely used in simulation systems and gaming. Furthermore, when updating or maintaining these rule resources is necessary, users can use specialized rule modeling and management tools to add, delete, or edit rules.
[0003] A drawback of the current implementation of rule resources in simulation systems is the tight coupling between the rule resources and the simulation system. Whenever a rule needs to be added, deleted, or modified, not only must the rule itself be adjusted, but other parts of the simulation system must also be modified accordingly, and the entire system must be recompiled. Since ordinary users typically lack the ability to modify code, these operations require the involvement of professionals, which undoubtedly increases system maintenance costs. Furthermore, since simulation systems typically contain very large code volumes, each compilation takes a long time, and any changes to the rule resources require a significant amount of time to compile. Summary of the Invention
[0004] Based on this, it is necessary to provide a low-coupling simulation behavior rule resource construction method based on reflection mechanism to address the above technical issues, including:
[0005] Constructing a rule mapping table, the rule mapping table is used to dynamically store a mapping relationship from a rule identifier to a function pointer, the function pointer points to a rule instance construction function, the rule instance construction function is a template function, is used to create a rule instance according to the rule type, and returns a pointer to the memory address of the rule instance;
[0006] When a new rule addition instruction triggered by a user is received, a header file and a corresponding source file of the new rule are generated. The header file is used to declare the rule type of the new rule, and the source file is used to define the new rule type declared in the header file. The source file contains a rule logic execution function, and the content of the rule logic execution function is generated according to the rule logic edited by the user;
[0007] In the rule resource registration program, add a reference to the header file of the new rule to obtain the rule type of the new rule;
[0008] In the rule resource registration program, add a registration function for the new rule, wherein the registration function is a template function and accepts the rule type and rule identifier of the new rule as input parameters;
[0009] Recompile the rule resource registration program and the source file of the new rule;
[0010] Based on the compiled rule resource registration program, according to the rule type and rule identifier of the new rule, the registration function of the new rule is called to generate a mapping relationship corresponding to the new rule and insert the mapping relationship into the rule mapping table.
[0011] In some implementations, the source file for generating the new rule includes:
[0012] Obtaining the rule logic of the new rule edited by the user;
[0013] Based on the rule logic, a predefined rule logic execution function is called to generate the source file.
[0014] In some embodiments, the compiled rule resource registration program, based on the rule type and rule identifier of the new rule, calls the registration function of the new rule to generate a mapping relationship corresponding to the new rule and inserts it into the rule mapping table, including:
[0015] Using the compiled rule resource registration program, reference and parse the header file to obtain the rule type of the new rule;
[0016] The compiled rule resource registration program is used to call the registration function of the new rule, and based on the rule type and rule identifier of the new rule, a mapping relationship corresponding to the new rule is generated and inserted into the rule mapping table.
[0017] In some embodiments, further comprising:
[0018] When receiving a rule deletion instruction triggered by the user, delete the header file and corresponding source file of the rule to be deleted;
[0019] In the rule resource registration program, deleting the reference to the header file of the rule to be deleted;
[0020] In the rule resource registration program, deleting the registration function of the rule to be deleted;
[0021] Recompile the rule resource registration program;
[0022] Based on the compiled rule resource registration program, the mapping relationship corresponding to the to-be-deleted rule is deleted from the rule mapping table.
[0023] In some embodiments, further comprising:
[0024] When receiving a rule modification instruction triggered by a user, locate the source file of the rule to be modified;
[0025] Modify the rule logic in the rule logic execution function in the source file of the rule to be modified;
[0026] Recompile the source file of the rule to be modified.
[0027] In some embodiments, further comprising:
[0028] When a simulation request triggered by a user is received, a rule resource list required for the simulation is obtained, wherein the rule resource list stores rule identifiers of the rules required for the current simulation;
[0029] Querying the rule mapping table according to the rule resource list to determine the corresponding item of the rule identifier of the required rule;
[0030] Take out the function pointer stored in the corresponding item;
[0031] Run the rule instance construction function pointed to by the function pointer;
[0032] Get the rule instance pointer returned by the function and store it as a class member variable.
[0033] The above-mentioned low-coupling simulation behavior rule resource construction method based on the reflection mechanism decouples the rule resources from the simulation engine code, allowing the independent management and modification of the rule resources without modifying the simulation engine code. Therefore, changes in the rules no longer require recompilation and deployment of the simulation engine, thus reducing the user's workload. Recompilation is only for the rule resource registration program and the affected rule source files, rather than the entire simulation system, that is, only the changed parts need to be recompiled, thus shortening the compilation time. In addition, the dynamic loading of rule resources allows the simulation system to use newly compiled rule resources without restarting. Since the modification and compilation time of rule resources is reduced, users can see the running effect of the modified rules more quickly, which supports the implementation of multiple rounds of rapid iteration of automatic rule generation and testing functions. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 The figure is a flowchart of a rule resource construction method in one embodiment of the present invention.
[0035] Figure 2 This is an example diagram of a rule resource registration program and source files in one embodiment of the present invention.
[0036] Figure 3 This is an example diagram of the rule management class in one embodiment of the present invention.
[0037] Figure 4 The figure is a flowchart of a rule resource construction method in a specific embodiment of the present invention.
[0038] Figure 5 This is an example diagram of a rule resource registration program and source files in a specific embodiment of the present invention.
[0039] Figure 6 This is an example diagram of the rule management class in a specific embodiment of the present invention. DETAILED DESCRIPTION
[0040] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.
[0041] Rules are crucial resources in simulation, determining the behavior of simulated entities within the environment. Rules acquire the current state of the environment and entities in the simulation in real time and, based on predefined rule logic, determine the actions that entities should perform. Entities interact with the environment according to the actions determined by the rules and participate in the simulation calculations. Therefore, rules can be viewed as entities (or "agents") with intelligent decision-making capabilities. Rules can be expressed in various forms, such as state machines, behavior trees, and flow charts. Behavior trees are widely used in simulation systems and gaming. Furthermore, when updating or maintaining these rule resources is necessary, users can use specialized rule modeling and management tools to add, delete, or edit rules.
[0042] A drawback of the current implementation of rule resources in simulation systems is the tight coupling between the rule resources and the simulation system. Whenever a rule needs to be added, deleted, or modified, not only must the rule itself be adjusted, but other parts of the simulation system must also be modified accordingly, and the entire system must be recompiled. Since ordinary users typically lack the ability to modify code, these operations require the involvement of professionals, which undoubtedly increases system maintenance costs. Furthermore, since simulation systems typically contain very large code volumes, each compilation takes a long time, and any changes to the rule resources require a significant amount of time to compile.
[0043] To address the current problems caused by the tight coupling of rule resources with simulation systems, such as requiring modifications and recompilation of other parts of the simulation system to modify rules, increasing maintenance costs and lengthy compilation times, this paper proposes a method for constructing low-coupling simulation behavior rule resources based on a reflection mechanism. The reflection mechanism enables programs to dynamically obtain object type information (such as class name and inheritance structure), member variables, and methods at runtime, and perform operations such as dynamically creating object instances or calling methods. This method consists of two phases: rule resource preparation and rule resource application.
[0044] 1. Rule resource preparation stage
[0045] First, a rule mapping table is created to store the mapping from a rule identifier in the form of a string or the like to a function pointer, where the function pointer points to a rule instance construction function.
[0046] Secondly, after operating the rule modeling tool to create new rules, delete existing rules, or edit rule logic, the code of the rule resource and the rule registration program must be updated and recompiled to support the simulation system to call the changed rule resources. This includes three situations: adding new rules, deleting rules, and modifying rules. Figure 1 .
[0047] (1) When a new rule is created, the rule modeling tool completes the following four steps:
[0048] The first step is to create the header file and source file of the new rule;
[0049] The second step is to add a reference to the rule header file in the code of the rule resource registration program;
[0050] The third step is to add the registration function of the rule in the code of the rule resource registration program;
[0051] The fourth step is to recompile the rule resource registration program and the source file of the rule.
[0052] (2) When an existing rule is deleted, the rule modeling tool completes the following four steps:
[0053] The first step is to delete the header file and source file of the rule;
[0054] The second step is to delete the reference to the rule header file in the code of the rule resource registration program;
[0055] The third step is to delete the registration function of the rule in the code of the rule resource registration program;
[0056] Step 4: Recompile the rule registration program.
[0057] (3) When an existing rule is modified, the rule modeling tool does not modify the code of the rule resource registration program. It only modifies the rule logic in the corresponding rule class source file according to the change of the rule logic. The following two steps are completed (see Figure 1 ):
[0058] The first step is to modify the rule logic in the rule class in the rule source file;
[0059] The second step is to recompile the source file of the rule.
[0060] Rules created using the rule modeling tool will generate a rule header file and a rule source file. The rule header file is responsible for declaring a new type based on the type name of a rule class. The rule source file is responsible for defining the rule type declared in the header file. It uses a unified format, see Figure 2 In the rule source file, the class contains a member function named "Rule Logic Execution Function". The content of this function is automatically generated by the rule modeling tool based on the rule logic edited by the user.
[0061] The rule resource registration program includes two parts: header file reference and rule registration function. Figure 2 The rule resource registration procedure in . The header file is referenced to obtain the type declaration of the rule class to be registered so that this rule type can be used as the input of the rule registration function.
[0062] The rule registration function is a template function, see Figure 2 The rule registration function in [1] takes two inputs: the rule type and the rule identifier, both provided by the rule registration program. The rule registration function is responsible for inserting a new mapping from the rule identifier to the function pointer of the rule instance construction function in the rule mapping table, with the rule type as the input of the function pointer.
[0063] The rule instance construction function is also a template function, see Figure 2 The rule instance construction function in . Its input is a rule type, provided by the rule registration function. The rule instance construction function is responsible for creating an instance of the input rule type, that is, a rule instance, and returning a pointer to the memory address of the instance.
[0064] 2. Rule resource application stage
[0065] The present invention also designs a rule management class, which includes a constructor and a rule logic execution function. The constructor includes four steps, see Figure 3 :
[0066] The first step is to query the rule mapping table to obtain the corresponding item of a rule identifier;
[0067] The second step is to retrieve the function pointer stored in the item;
[0068] The third step is to run the rule instance construction function pointed to by the function pointer;
[0069] The fourth step is to obtain the rule instance pointer returned by the function and store it as a class member variable.
[0070] The simulation system calls rule resources in two stages:
[0071] (1) Simulation system initialization phase
[0072] When the simulation system starts, it preprocesses the rule resources based on the list of rule resources required for the current simulation. This list of rule resources is parsed from the simulation plan and contains the rule identifiers of the rules required for the current simulation. For each rule, the simulation system first creates an instance of the rule management class and initializes it with the globally unique rule identifier as a parameter. Initialization then calls the constructor of the rule management class.
[0073] The simulation system determines when to schedule a rule management class instance with a specific identifier based on the scenario. Specifically, the created rule management class instances are organized according to the chronological order and logical relationships described in the simulation scenario. The simulation system's behavioral engine, such as the behavior tree engine, determines when to trigger each rule management class instance for execution.
[0074] (2) Simulation operation phase
[0075] After the simulation system has been started and initialized, if the simulation system needs to trigger the execution of a rule, it will call the rule logic execution function within the corresponding rule management class instance. Due to the use of the bridge pattern, the rule logic execution function of the rule management class will call the rule logic execution function of the rule instance stored in its member variable.
[0076] The rule management class serves as the interface between the simulation engine, behavior tree engine, and other parts of the simulation system, as well as the simulation resources. Since the code for the rule management class remains unchanged throughout the above process, other parts of the simulation system do not need to be modified to accommodate changes in rule resources. New rules can be called simply by identifying the corresponding rule.
[0077] After the simulation system is released, rules can be flexibly added, modified, and deleted. Users no longer need to modify the simulation engine code to adapt to changes in rule resources, reducing additional workload. After rule resources change, only a short time is required to compile the rule resources, and there is no need to recompile the simulation system, saving waiting time from rule resource construction to simulation deduction running, thereby speeding up the time from rule editing to obtaining feedback on running effects, and providing support for the implementation of automatic rule generation and testing functions that require multiple rounds of rapid iterations.
[0078] In practical applications, the C++ language is used to implement the low-coupling simulation behavior rule resource construction method based on the reflection mechanism proposed in this invention, which is divided into two stages: rule resource preparation and rule resource application:
[0079] 1. Rule resource preparation stage
[0080] First, use the map container provided by the C++ STL library (Standard Template Library) to create a rule mapping table ID2RuleMap, which is used to store the mapping from rule identifiers in the form of strings to a function pointer. The function pointer points to the rule instance construction function generateRuleObject.
[0081] Secondly, after operating the rule modeling tool to create new rules, delete existing rules, or edit rule logic, the code of the rule resource and the rule registration program must be updated and recompiled to support the simulation system to call the changed rule resources. This includes three situations: adding, deleting, and modifying. Figure 4 .
[0082] (1) When a new rule is created, the rule modeling tool completes the following four steps:
[0083] The first step is to create a new rule with the .h suffix header file and the .cpp suffix source file;
[0084] The second step is to add a reference to the rule's .h suffix header file in the code of the rule resource registration program ruleSourcesRegistration.cpp;
[0085] Step 3: In the code of rule resource registration program ruleSourcesRegistration.cpp, add the registration function registerRuleClass<rule type of the new rule>;
[0086] The fourth step is to recompile the rule registration program and the .cpp suffix source file of the rule.
[0087] (2) When an existing rule is deleted, the rule modeling tool completes the following four steps:
[0088] The first step is to delete the .h suffix header file and .cpp suffix source file of this rule;
[0089] The second step is to delete the reference to the rule's .h suffix header file in the code of the rule resource registration program ruleSourcesRegistration.cpp;
[0090] Step 3: In the code of the rule resource registration program ruleSourcesRegistration.cpp, delete the registration function registerRuleClass <rule type of the rule to be deleted>;
[0091] Step 4: Recompile the rule registration program.
[0092] (3) When an existing rule is modified, the rule modeling tool does not modify the code of the rule resource registration program ruleSourcesRegistration.cpp. It only modifies the rule logic in the corresponding rule class .cpp suffix source file according to the change of the rule logic. Specifically, complete the following two steps:
[0093] The first step is to modify the rule logic in the rule class in the .cpp suffix source file of the rule;
[0094] The second step is to recompile the .cpp suffix source file of the rule.
[0095] Rules created using the rule modeling tool will generate a rule header file with a .h suffix and a rule source file with a .cpp suffix. The .h suffix header file is responsible for declaring a new type based on the type name of a rule class. The .cpp suffix source file is responsible for defining the rule type declared in the .h suffix header file. It uses a unified format, see Figure 5 ,The class contains a member function named run, which is responsible for the rule logic execution.,The content in the function is automatically generated by the rule modeling,tool based on the rule logic edited by the user.
[0096] The rule resource registration program ruleSourcesRegistration.cpp includes two parts: the reference to the .h suffix header file and the rule registration function registerRuleClass. Figure 5 The purpose of referencing the header file with the .h suffix is to obtain the type declaration of the rule class to be registered, so that this rule type can be used as the input of the rule registration function registerRuleClass.
[0097] The rule registration function registerRuleClass is a template function, see Figure 5 The full form is registerRuleClass<rule class type>(rule identifier). It takes two inputs: the rule type and the rule identifier, both provided by the rule registration process. The rule registration function registerRuleClass is responsible for inserting a new mapping from the rule identifier to the function pointer of the rule instance construction function generateRuleObject in the rule mapping table ID2RuleMap constructed using the C++ map container. This function pointer takes the rule type as input.
[0098] The rule instance construction function generateRuleObject is also a template function, see Figure 5 The full form is generateRuleObject<rule type>. Its input is a rule type, provided by the rule registration function registerRuleClass. The rule instance constructor generateRuleObject is responsible for creating an instance of the input rule type, i.e., a rule instance, and returning a pointer to the memory address of the instance.
[0099] 2. Rule resource application stage
[0100] The present invention also designs a rule management class ruleManager, which includes a constructor and a rule logic execution function run. The constructor includes four operation steps, see Figure 6 :
[0101] The first step is to query the rule mapping table ID2RuleMap to obtain the corresponding item of a rule identifier;
[0102] The second step is to retrieve the function pointer stored in the item;
[0103] The third step is to run the rule instance construction function generateRuleObject pointed to by the function pointer;
[0104] The fourth step is to obtain the rule instance pointer returned by the function and store it as a class member variable.
[0105] The simulation system calls rule resources in two stages:
[0106] (1) Simulation system initialization phase
[0107] When the simulation system starts, it preprocesses the rule resources based on the list of rule resources required for the simulation. This list is parsed from the simulation plan in JSON format and contains the rule identifiers of the rule resources required for the simulation. For each rule, the simulation system first creates an instance of the rule manager class, ruleManager, and initializes it with the globally unique rule identifier as a parameter. Initialization calls the constructor of the rule manager class, ruleManager.
[0108] The created instance of the ruleManager class will be registered as a node in the behavior tree engine along with the trigger conditions for each rule in the simulation plan, such as time and logic. One instance of the ruleManager class corresponds to one behavior tree node.
[0109] (2) Simulation operation phase
[0110] After the simulation system is started and initialized, the Behavior Tree engine determines when to schedule the execution of each rule. Specifically, the Behavior Tree engine determines which behavior tree node to activate at which simulation time based on the trigger conditions in the simulation plan. When a behavior tree node is activated, it calls the rule logic execution function (run) within the corresponding rule manager class instance (ruleManager). Due to the use of the bridge pattern, the rule logic execution function (run) of the rule manager class (ruleManager) calls the rule logic execution function (run) of the rule instance stored in its member variable.
[0111] The rule manager class, ruleManager, serves as the interface between the simulation engine, behavior tree engine, and other parts of the simulation system, as well as simulation resources. Since the code for the rule manager class, ruleManager, remains unchanged throughout the above process, other parts of the simulation system do not need to be modified to accommodate changes in rule resources. New rules can be invoked simply by identifying the corresponding rule identifier.
[0112] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.
Claims
1. A method for constructing low-coupling simulation behavior rule resources based on reflection mechanism, characterized in that: The following steps are included in sequence: Constructing a rule mapping table, the rule mapping table is used to dynamically store a mapping relationship from a rule identifier to a function pointer, the function pointer points to a rule instance construction function, the rule instance construction function is a template function, is used to create a rule instance according to the rule type, and returns a pointer to the memory address of the rule instance; When a new rule addition instruction triggered by a user is received, a header file and a corresponding source file of the new rule are generated. The header file is used to declare the rule type of the new rule, and the source file is used to define the new rule type declared in the header file. The source file contains a rule logic execution function, and the content of the rule logic execution function is generated according to the rule logic edited by the user; In the rule resource registration program, add a reference to the header file of the new rule to obtain the rule type of the new rule; In the rule resource registration program, add a registration function for the new rule, wherein the registration function is a template function and accepts the rule type and rule identifier of the new rule as input parameters; Recompile the rule resource registration program and the source file of the new rule; Based on the compiled rule resource registration program, according to the rule type and rule identifier of the new rule, the registration function of the new rule is called to generate a mapping relationship corresponding to the new rule and insert the mapping relationship into the rule mapping table.
2. The method according to claim 1, characterized in that The source file for generating new rules includes: Obtaining the rule logic of the new rule edited by the user; Based on the rule logic, a predefined rule logic execution function is called to generate the source file.
3. The method according to claim 1, characterized in that The rule resource registration program after compilation is based on the rule type and rule identifier of the new rule, calling the registration function of the new rule to generate a mapping relationship corresponding to the new rule and inserting it into the rule mapping table, including: Using the compiled rule resource registration program, reference and parse the header file to obtain the rule type of the new rule; The compiled rule resource registration program is used to call the registration function of the new rule, and based on the rule type and rule identifier of the new rule, a mapping relationship corresponding to the new rule is generated and inserted into the rule mapping table.
4. The method according to claim 1, wherein Also includes: When receiving a rule deletion instruction triggered by the user, delete the header file and corresponding source file of the rule to be deleted; In the rule resource registration program, deleting the reference to the header file of the rule to be deleted; In the rule resource registration program, deleting the registration function of the rule to be deleted; Recompile the rule resource registration program; Based on the compiled rule resource registration program, the mapping relationship corresponding to the to-be-deleted rule is deleted from the rule mapping table.
5. The method according to claim 1, characterized in that Also includes: When receiving a rule modification instruction triggered by a user, locate the source file of the rule to be modified; Modify the rule logic in the rule logic execution function in the source file of the rule to be modified; Recompile the source file of the rule to be modified.
6. The method according to any one of claims 1 to 5, characterized in that Also includes: When a simulation request triggered by a user is received, a rule resource list required for the simulation is obtained, wherein the rule resource list stores rule identifiers of the rules required for the current simulation; Querying the rule mapping table according to the rule resource list to determine the corresponding item of the rule identifier of the required rule; Take out the function pointer stored in the corresponding item; Run the rule instance construction function pointed to by the function pointer; Get the rule instance pointer returned by the function and store it as a class member variable.
Citation Information
Patent Citations
Rule description file-based rule behavior model real-time code generation method
CN116755704A
Data compiling method, electronic equipment, storage medium and program product
CN119621070A