A device state analysis method and system based on DSL and dynamic compilation technology
By using DSL and dynamic compilation technology to parse the equipment status parameters of the ground station monitoring system, the problem of poor flexibility caused by hard coding is solved, and equipment status parsing with high flexibility and easy maintenance is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HEFEI ZHONGKE TIANTONG TECHNOLOGY CO LTD
- Filing Date
- 2025-08-18
- Publication Date
- 2026-06-02
AI Technical Summary
The hard-coded logic for parsing device status parameters in existing ground station monitoring systems results in poor flexibility and makes it difficult to adapt to changes in different devices or protocols.
Device status parameters are described using domain-specific language (DSL) files. C# intermediate code is generated using T4 text templates, and assemblies are dynamically generated using the Roslyn compiler. Reflection technology is used to achieve mutual conversion between structures and byte arrays, and plugins are supported to extend the parsing method.
It improves the system's flexibility and adaptability, shortens the configuration time for parsing rules, supports adding new device protocols without redeploying the system, and is easy to maintain.
Smart Images

Figure CN121233116B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of ground station monitoring systems, and in particular to a method and system for analyzing device status based on DSL and dynamic compilation technology. Background Technology
[0002] Currently, the most tedious and complex task in the field of ground station monitoring systems is parsing the status parameters of different devices from different manufacturers. This status data is typically transmitted in the form of byte arrays, and parsing these byte arrays to obtain the device's status information is a complex and error-prone task. Traditional methods usually use hard-coded parsing logic, which requires highly skilled developers, has a long development cycle, and lacks flexibility, making it difficult to adapt to changes in different devices or protocols. Summary of the Invention
[0003] To address the existing problems, this invention provides a device status resolution method and system based on DSL and dynamic compilation technology, the specific solution of which is as follows:
[0004] A device status resolution method based on DSL and dynamic compilation technology includes the following steps:
[0005] S1, Import the domain-specific language (DSL) file containing device status parameters, wherein the DSL file defines field attributes and mapping rules in a declarative syntax;
[0006] S2 uses the T4 text template engine to parse DSL files and dynamically generate C# intermediate code.
[0007] S3 uses the Roslyn compiler to dynamically compile the C# intermediate code in memory to generate an executable assembly;
[0008] S4, at runtime, loads the assembly via reflection and receives the device status byte array data frame;
[0009] S5 matches the structure type corresponding to the byte array and converts the byte array into a structure instance through memory mapping;
[0010] S6 uses reflection to parse the structure fields and their properties, and calls built-in or extended parsing methods to convert the structure into a generic parameter class object;
[0011] S7, obtain and output an instantiated object containing the parsed device parameter data;
[0012] S8 instantiates objects to transmit, display, or save device parameters according to actual needs.
[0013] Preferably, the attributes in step S1 include at least one of field description, data grouping, unit, numerical range, hexadecimal conversion flag, and byte reserved bits.
[0014] Preferably, the C# intermediate code generated in step S2 includes: a Struct definition, Attribute fields, and an Enum type; wherein the Mapping relationship is converted into an Enum type.
[0015] Preferably, in step S1, the DSL file is defined by grouping according to device units, with each unit corresponding to a structure. Field attributes support nested declarations, and the mapping relationship is defined in the form of key-value pairs to describe the state.
[0016] Preferably, step S6 includes a built-in parsing method, which includes bit manipulation, byte order conversion, and numerical scaling; and an extended parsing method, which extends the semantics of the DSL by adding new feature classes.
[0017] Preferably, the system based on any of the above-described device state resolution methods based on DSL and dynamic compilation technology includes:
[0018] A DSL parser is used to parse DSL files, which describe device status parameters, including field names, descriptions, group names, reserved bits, range values, mapping relationships, and bit resolution characteristics.
[0019] A code generator used to parse DSL files using the T4 text template engine and dynamically generate C# intermediate code;
[0020] The dynamic compilation module uses the intermediate C# code generated by Roslyn to generate dynamic assemblies;
[0021] The reflection invocation module is used to invoke the types and methods of dynamically compiled C# intermediate code via reflection, and to process device status parameters.
[0022] Preferably, the DSL parser is specifically implemented as follows:
[0023] The parsing module converts the DSL file into an intermediate representation, including field definitions and mapping relationships;
[0024] The mapping relationship management module processes the field mapping relationships defined in the DSL file and applies them to the generated C# intermediate code.
[0025] Preferably, the code generator includes:
[0026] The T4 template engine generates C# intermediate code templates based on the DSL definition, including field declarations, attribute tags, and methods.
[0027] The generation module outputs the code generated from the T4 template as a C# source file.
[0028] Preferably, the dynamic compilation module includes:
[0029] The compiler interface uses Roslyn to compile C# source files and generate dynamic link libraries (DLLs).
[0030] The compilation result processing module loads the compiled assembly into the application.
[0031] Preferably, the reflection invocation module includes:
[0032] Type loader, dynamically loads generated C# intermediate code types via reflection;
[0033] The method invoker invokes methods in the generated type via reflection to handle the parsing and conversion of device state parameters.
[0034] The beneficial effects of this invention are as follows:
[0035] This application has the advantages of high flexibility, strong adaptability and easy maintenance, and can effectively solve the problems of hard-coded parsing logic, poor flexibility and difficulty in adapting to different devices or protocol changes in the existing technology. Attached Figure Description
[0036] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0037] Figure 1 This is a flowchart of the method of the present invention;
[0038] Figure 2 Define the code graph for DSL built-in methods;
[0039] Figure 3 Example code diagram for DSL;
[0040] Figure 4 This is a diagram of the parsed state data. Detailed Implementation
[0041] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0042] This invention provides a device status parsing method and system based on DSL and dynamic compilation technology to solve the problems of hard-coded parsing logic, poor flexibility, and difficulty in adapting to changes in different devices or protocols in existing technologies. Device status parameters are described through DSL files, C# code is generated using T4 templates, and then an assembly is dynamically compiled using Roslyn. Finally, reflection is used to achieve mutual conversion between structures and byte arrays. The system incorporates commonly used byte parsing methods, capable of parsing status parameters for most devices, and also supports plugin-based extensions of the parsing method to extend the DSL language.
[0043] The input to this invention system is a parameter specification document for a certain device; the output is the actual data after parsing the parameters of each unit of the device.
[0044] Specifically, such as Figure 1 As shown, a device status resolution method based on DSL and dynamic compilation technology includes the following steps:
[0045] S1, Import a domain-specific language (DSL) file containing device status parameters. The DSL file defines field attributes and mapping rules using declarative syntax. Attributes include at least one of the following: field description, data grouping, unit, numerical range, hexadecimal conversion flag, and byte reserved bits. The DSL format is as follows: Figure 2 As shown. Each line describes a state parameter. Each unit corresponds to a C# struct, including fields and their properties. The mapping defines the mapping from field values to state descriptions and corresponds to a C# enum. For example... Figure 3 The image shown is a sample code diagram for a DSL.
[0046] S2 uses the T4 text template engine to parse DSL files and dynamically generate C# intermediate code. The generated C# intermediate code includes: struct definitions, attribute fields, and enumeration types. The mapping relationship is converted into an enumeration type, and the DSL parsing method is converted into an attribute field.
[0047] S3 uses the Roslyn compiler to dynamically compile the C# intermediate code in memory, generating an executable assembly.
[0048] S4 loads assemblies via reflection at runtime and receives device status byte array data frames.
[0049] S5 matches the byte array to the corresponding struct type and converts the byte array into a struct instance through memory mapping. If no matching struct is found, parsing is abandoned.
[0050] S6, by using reflection to parse the structure fields and their characteristics, calls the built-in or extended parsing methods to convert the structure into a generic parameter class object; the built-in parsing methods in step S6 include bit operations, byte order conversion, and numerical scaling; the extended parsing methods extend the semantics of the DSL by adding new characteristic classes. For example... Figure 4 As shown.
[0051] S7 obtains and outputs an instantiated object containing the parsed data of the device parameters.
[0052] S8 instantiates objects to transmit, display, or save device parameters according to actual needs.
[0053] A system based on any of the above-described device status resolution methods based on DSL and dynamic compilation technology includes:
[0054] The DSL parser is used to parse DSL files, which describe device status parameters, including field names, descriptions, group names, reserved bits, range values, mapping relationships, and bit resolution characteristics. The DSL parser includes: a parsing module, which converts the DSL file into an intermediate representation, including field definitions and mapping relationships; and a mapping relationship management module, which processes the field mapping relationships defined in the DSL file and applies them to the generated C# intermediate code.
[0055] The code generator is used to parse DSL files using the T4 text template engine and dynamically generate C# intermediate code. The code generator includes: the T4 template engine, which generates C# intermediate code templates based on the DSL definition, including field declarations, attribute tags, and methods; and the generation module, which outputs the code generated by the T4 template as a C# source file.
[0056] The dynamic compilation module uses the intermediate C# code generated by Roslyn to produce a dynamic assembly. The dynamic compilation module includes: a compiler interface, which uses Roslyn to compile C# source files and generate a dynamic link library (DLL); and a compilation result processing module, which loads the compiled assembly into the application.
[0057] The reflection invocation module is used to invoke the types and methods of dynamically compiled C# intermediate code via reflection, and to process device state parameters. The reflection invocation module includes: a type loader, which dynamically loads the generated C# intermediate code types via reflection; and a method invoker, which invokes the methods in the generated types via reflection to handle the parsing and conversion of device state parameters.
[0058] This application significantly reduces the configuration time for parsing rules; adding new device protocols only requires writing a DSL file, eliminating the need for system redeployment; furthermore, this application supports plug-in extensions to customize parsing methods. This application boasts advantages such as high flexibility, strong adaptability, and ease of maintenance, effectively solving the problems of hard-coded parsing logic, poor flexibility, and difficulty in adapting to changes in different devices or protocols in existing technologies.
[0059] Those skilled in the art will further appreciate that the various illustrative logic blocks, modules, circuits, and algorithm steps described in conjunction with the embodiments disclosed herein can be implemented as electronic hardware, computer software, or a combination of both. To clearly illustrate this interchangeability between hardware and software, the various illustrative components, blocks, modules, circuits, and steps are described above in a generalized manner in terms of their functionality. Whether such functionality is implemented as hardware or software depends on the specific application and the design constraints imposed on the overall system. Those skilled in the art may implement the described functionality in different ways for each specific application, but such implementation decisions should not be construed as departing from the scope of the invention.
[0060] Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A device state analysis method based on DSL and dynamic compilation technology, characterized in that, Includes the following steps: S1, Import the domain-specific language (DSL) file containing device status parameters, wherein the DSL file defines field attributes and mapping rules in a declarative syntax; S2 uses the T4 text template engine to parse DSL files and dynamically generate C# intermediate code. S3 uses the Roslyn compiler to dynamically compile the C# intermediate code in memory to generate an executable assembly; S4, at runtime, loads the assembly via reflection and receives the device status byte array data frame; S5 matches the structure type corresponding to the byte array and converts the byte array into a structure instance through memory mapping; S6 uses reflection to parse the structure fields and their properties, and calls built-in or extended parsing methods to convert the structure into a generic parameter class object; S7, obtain and output an instantiated object containing the parsed device parameter data; S8 instantiates objects to transmit, display, or save device parameters according to actual needs; The attributes in step S1 include at least one of the following: field description, data grouping, unit, numerical range, hexadecimal conversion flag, and byte reserved bits; The C# intermediate code generated in step S2 includes: a Struct definition, Attribute fields, and an Enum type; where the Mapping relationship is converted to an Enum type. In step S1, the DSL file is defined by device unit, with each unit corresponding to a structure. Field attributes support nested declarations, and the mapping relationship is defined in the form of key-value pairs to describe the status. In step S6, the system has built-in general byte parsing methods, including bit operations, byte order conversion, and numerical scaling; it also supports plugin extensions of parsing methods, which can extend the semantics of the DSL by adding new feature classes.
2. The system for device state resolution based on DSL and dynamic compilation techniques as claimed in claim 1, wherein, include: A DSL parser is used to parse DSL files, which describe device status parameters, including field names, descriptions, group names, reserved bits, range values, mapping relationships, and bit resolution characteristics. A code generator used to parse DSL files using the T4 text template engine and dynamically generate C# intermediate code; The dynamic compilation module uses the intermediate C# code generated by Roslyn to generate dynamic assemblies; The reflection invocation module is used to invoke the types and methods of dynamically compiled C# intermediate code via reflection, and to process device status parameters.
3. The system of claim 2, wherein, The DSL parser is specifically implemented as follows: The parsing module converts the DSL file into an intermediate representation, including field definitions and mapping relationships; The mapping relationship management module processes the field mapping relationships defined in the DSL file and applies them to the generated C# intermediate code.
4. The system of claim 3, wherein, The code generators include: The T4 template engine generates C# intermediate code templates based on the DSL definition, including field declarations, attribute tags, and methods. The generation module outputs the code generated from the T4 template as a C# source file.
5. The system of claim 2, wherein, The dynamic compilation module includes: The compiler interface uses Roslyn to compile C# source files and generate dynamic link libraries (DLLs). The compilation result processing module loads the compiled assembly into the application.
6. The system of claim 2, wherein, The reflection invocation module includes: A type loader, which dynamically loads the generated C# intermediate code types by reflection; A method invoker, which invokes the methods in the generated types by reflection, handles the resolution and conversion of the device state parameters.
Citation Information
Patent Citations
Dynamic compiling method and device, equipment and storage medium
CN114756238A