Grammar analysis system and method for Modelica language

By using ANTLR4 and data serialization modules in the syntax parsing system of the Modelica language, the Modelica text model is converted into a structured JSON format, which solves the difficulties in storage, transmission and recognition of Modelica models, and realizes efficient collaborative modeling and model management.

CN119918534APending Publication Date: 2025-05-02CHINA NUCLEAR POWER OPERATION TECH CORP
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411834875.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-13
Publication Date
2025-05-02

AI Technical Summary

Technical Problem

The text models used in the existing Modelica language have difficulty in reading and identifying model information in the storage, transmission, processing and other links, and it is difficult to support collaborative simulation modeling and model database storage management.

Method used

It provides a syntax parsing system for Modelica language, which uses the ANTLR4 syntax parsing module to perform lexical analysis and syntax analysis, generates a syntax tree, and converts the syntax tree into a Modelica object in Java language through the data access submodule. At the same time, the Modelica object is serialized into JSON format through the data serialization module, supporting model transmission and storage in JSON format.

Benefits of technology

It realizes efficient transmission and storage of Modelica models, supports collaborative modeling development and model version tracking management, and improves the efficiency of model data identification and management.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119918534A_ABST
    Figure CN119918534A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of Modelica language grammar analysis, and aims to solve the problem that model transmission and model details are difficult to read and recognize when an existing Modelica language uses a text form storage model. The invention discloses a grammar analysis system and method for a Modelica language. The system comprises an ANTLR4 grammar analysis module, a Modelica text code generation module, a data sequence and a deserialization module. According to the method, different model elements in a Modelica object are converted into character strings in a text format, and the character strings in the text format are combined to generate a complete Modelica model file in the text format. According to the method, mutual conversion between the text format and the structured JSON format of the simulation model based on the Modelica language is achieved, on the premise that the text format necessary for simulation of the Modelica model is met, the Modelica model can be transmitted and stored in a structured form, and large-scale collaborative modeling and model version tracking management based on the Modelica language are effectively supported.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the technical field of syntax parsing of Modelica language, and in particular, to a syntax parsing system and method for Modelica language. Background Art

[0002] In the field of modern engineering and simulation, Modelica, as a domain-independent physical system simulation modeling language, can easily achieve seamless integration of multi-domain models to build complex simulation systems. Modelica has been widely used in aerospace, automotive, power and other fields involving complex systems due to its advantages of multi-domain unified modeling, object-oriented modeling and non-causal modeling based on differential algebraic equations. For example, EDF in the power industry and Ford and Toyota in the automotive industry have all carried out multi-domain system engineering simulation modeling applications based on Modelica.

[0003] With the widespread application and development of simulation technology, simulation models are becoming larger and larger in scale and more and more integrated in fields and disciplines; this puts higher demands on the mutual collaboration between simulation modelers, and the collaborative development, efficient transmission and effective tracking management of simulation models are becoming crucial. However, the traditional Modelica language uses text format to express and store models. Models in text format have disadvantages such as low transmission efficiency and difficulty in identifying model details (special parsers are required), making it difficult to support application development in collaborative simulation modeling based on WEB technology and model database storage management based on tools such as PLM. Summary of the invention

[0004] The purpose of this application is to provide a syntax parsing system and method for the Modelica language, so as to solve the problem that the text model used in the existing Modelica language is difficult to read and recognize the model information in the storage, transmission, processing and other links.

[0005] In order to achieve the above objectives, this application provides the following technical solutions:

[0006] In a first aspect, the present application provides a syntax parsing system for Modelica language, comprising:

[0007] ANTLR4 grammar parsing module, used to perform lexical analysis and grammatical analysis on Modelica text code and generate a grammar tree;

[0008] Modelica text code generation module, used to generate corresponding Modelica text format models according to Modelica grammar rules from Modelica objects;

[0009] The data serialization and deserialization module is used to serialize Modelica objects in JAVA language into JSON format for transmission and storage; it also supports deserialization, converting Modelica models in JSON format into Modelica objects in JAVA language.

[0010] According to one embodiment of the present application, the ANTLR4 grammar parsing module includes:

[0011] The lexical analysis submodule is used to receive the Modelica file in the original text format and generate Tokens;

[0012] The syntax analysis submodule is used to perform syntax analysis on Tokens and build a syntax tree based on the generated Tokens;

[0013] The data access submodule is used to traverse and process the syntax tree and generate the corresponding ModelicaInstance object.

[0014] In a second aspect, the present application provides a syntax parsing method for Modelica language, comprising:

[0015] Step 1: Input the Modelica model file in the original text format into the syntax parsing system for the Modelica language to prepare for parsing;

[0016] Step 2: Use ANTLR4 to perform lexical analysis and decompose the input text format Modelica model file content into basic lexical units;

[0017] Step 3: Based on the generated lexical units, a syntax tree is constructed, syntax analysis is performed, and the structure and association relationship of the model elements are analyzed;

[0018] Step 4: Traverse the syntax tree through the data access submodule and convert the syntax tree into the corresponding Modelica instance object in Java language;

[0019] Step 5: Serialize the Modelica object into JSON format;

[0020] Step 6: Input the Modelica model file in JSON format into the syntax parsing system for the Modelica language;

[0021] Step 7: Convert the Modelica model in JSON format into a Modelica object in Java;

[0022] Step 8: Use the Modelica text code generation module to convert different model elements in the Modelica object into text-formatted strings, and merge the text-formatted strings to generate a complete text-formatted Modelica model file.

[0023] According to one embodiment of the present application, step 1 includes:

[0024] Step 1.1: Call the lexical analysis submodule of ANTLR4, perform word segmentation on the Modelica model according to the lexical rules, and convert the model code into a Token stream;

[0025] Step 1.2: Perform preliminary verification on the processed Token stream to ensure that each Token conforms to the expected lexical rules and mark the type and position of the Token;

[0026] Step 1.3: Pass the verified Token stream to the syntax analysis submodule for subsequent syntax analysis.

[0027] According to one embodiment of the present application, step 2 includes:

[0028] Step 2.1: Receive the Token stream passed from the lexical analysis submodule. Each Token contains its type, text value, and position in the original code.

[0029] Step 2.2: Initialize the grammar analyzer according to the grammar rule file of ANTLR4;

[0030] Step 2.3: The Token stream is parsed by the parser using the defined grammar rules. The parser reads the tokens in the Token stream one by one and parses them according to the predefined grammar rules.

[0031] Step 2.4: After parsing the token stream, generate an intermediate representation through the parser;

[0032] Step 2.5: The parser integrates the intermediate representation of the parsing into a syntax tree.

[0033] According to one embodiment of the present application, step 2.3 includes:

[0034] Step 2.3.1: Use the syntax analyzer to match the type of the current token according to the definition in the syntax rule file. If the match succeeds, continue to process the next token; if the match fails, throw a syntax error;

[0035] Step 2.3.2: In the process of matching rules, the parser builds the grammatical structure according to the grammatical rules;

[0036] Step 2.3.3: Recursively process the nested structures in the Modelica code through the parser.

[0037] According to one embodiment of the present application, step 3 includes:

[0038] Step 3.1: According to the syntax tree generated by ANTLR4, initialize the traverser of the data access submodule. The data access submodule implements node-by-node access to the syntax tree by rewriting the access method of each node;

[0039] Step 3.2: Define the corresponding Java class objects for the key elements of the Modelica code;

[0040] Step 3.3: Find the definition node of the model in the syntax tree. When accessing the node, create a new Java object instance to store the model information, extract the basic information of the model, and store it in the corresponding Java object;

[0041] Step 3.4: Find the variable definition node under the model node. When accessing the node, create a new Java object instance to store variable information, extract the variable information, and store it in the corresponding Java object;

[0042] Step 3.5: Find the equation definition node under the model node. When accessing the node, create a new Java object instance to store the equation information, extract the left and right expressions of the equation, and store them in the corresponding Java objects;

[0043] Step 3.6: Find the annotation definition node under the model node. When accessing the node, create a new Java object instance to store the annotation information, extract the annotation content, and store it in the corresponding Java object;

[0044] Step 3.7: For each child node under the model node, recursively call the corresponding access method to ensure that all nested structures and elements are correctly parsed and stored;

[0045] Step 3.8: After the traversal is complete, organize all the extracted information into a complete Modelica object.

[0046] According to one embodiment of the present application, step 4 includes:

[0047] Step 4.1: Convert Modelica objects to JSON format;

[0048] Step 4.2: Select the Modelica object to be serialized and determine the information of the Modelica object to be serialized;

[0049] Step 4.3: Execute the serialization process and call the relevant serialization method to convert the selected Modelica object into a JSON string;

[0050] Step 4.4: Handle serialization exceptions. During the serialization process, capture and handle any exceptions that occur.

[0051] Step 4.5: Output the Modelica model in JSON format, output the generated JSON string, and store it in a file or transfer it to a specified location for subsequent transmission and storage.

[0052] According to one embodiment of the present application, step 5 includes:

[0053] Step 5.1: Convert the JSON model to a Modelica object.

[0054] Step 5.2: Read JSON data, read the JSON string from the specified location, and ensure data integrity and correctness;

[0055] Step 5.3: Execute the deserialization process, call the relevant serialization method, and convert the JSON string into the corresponding Modelica object;

[0056] Step 5.4: Handle deserialization exceptions. During the deserialization process, capture and handle any exceptions that occur;

[0057] Step 5.5: Verify the deserialization results. Check the deserialized Modelica object to ensure that all attributes and data are restored correctly. Verify key fields and structures to ensure data consistency and integrity.

[0058] According to one embodiment of the present application, step 6 includes:

[0059] Step 6.1: Receive the Modelica object containing the complete model information;

[0060] Step 6.2: Initialize the generator object of the Modelica text code generation module;

[0061] Step 6.3: Generate the basic structure of the Modelica model in the form of a text string based on the model information in the Modelica object;

[0062] Step 6.4: Traverse the list of variables in the Modelica object and generate the corresponding text string fragments;

[0063] Step 6.5: Traverse the equation list in the Modelica object and generate the corresponding text string fragments;

[0064] Step 6.6: Traverse the list of comments and annotations in the Modelica object and generate the corresponding text string fragments;

[0065] Step 6.7: For nested models or components, recursively generate corresponding text string fragments;

[0066] Step 6.8: Format the generated Modelica code;

[0067] Step 6.9: Output the generated Modelica code to the specified location.

[0068] Compared with the prior art, the syntax parsing system and method for Modelica language provided by the present application has the following beneficial effects:

[0069] This application is based on ANTLR4 technology to realize the mutual conversion between the text format and structured JSON format of the simulation model based on the Modelica language. On the premise of meeting the necessary text format of the Modelica model simulation, the Modelica model can be transmitted and stored in a structured form, effectively supporting large-scale collaborative modeling and model version tracking management based on the Modelica language.

[0070] Furthermore, the present application realizes efficient data transmission in the collaborative modeling development process. When multiple users perform collaborative modeling, specific model node data can be transmitted according to user needs, avoiding the transmission of complete models or model libraries in text format.

[0071] Furthermore, this application realizes the effective storage and precise control of large-scale Modelica models. By storing the Modelica model in JSON format, the model can be easily stored in a database; at the same time, relevant tools can be used to effectively track the details of node changes in the model and precisely control the Modelica model version.

[0072] Furthermore, this application facilitates the application of Modelica models by different tools. Models in text format require specific parsers to obtain the internal organizational relationships of the model, and have poor versatility and scalability; while Modelica models expressed in JSON format facilitate the processing and application of Modelica models by other tools, reducing the difficulty of plug-in tool development. BRIEF DESCRIPTION OF THE DRAWINGS

[0073] In order to more clearly illustrate the technical solution of the present application, the following is a brief introduction to the drawings required for the technical description.

[0074] Figure 1A schematic diagram of the structure of the syntax parsing system for the Modelica language provided in this application;

[0075] Figure 2 The data flow diagram of the syntax parsing system for Modelica language provided in this application;

[0076] Figure 3 A flowchart of the syntax parsing method for Modelica language provided in this application;

[0077] Figure 4 A data flow diagram for syntax parsing of the Modelica language provided in this application;

[0078] Figure 5 Schematic diagram of the Modelica text code generation module provided for this application. DETAILED DESCRIPTION

[0079] The following is further explained in detail through specific implementation methods.

[0080] In order to more efficiently transmit and store Modelica models and to support collaborative simulation modeling and model version tracking and control based on the Modelica language, this application proposes a syntax parsing system for the Modelica language to realize the mutual conversion between Modelica text models and structured JSON models.

[0081] like Figure 1 As shown, the system provided by the present application constructs a complete Modelica object class (ModelicaInstance) in JAVA as the basic carrier of Modelica model information. The system includes an ANTLR4 grammar parsing module, a Modelica text code generation module, a data serialization module and a deserialization module.

[0082] like Figure 2As shown, the Modelica model text format is converted to the structured JSON format, and the ANTLR4 syntax parsing module is used to perform lexical analysis and syntax analysis on the Modelica text code to generate a syntax tree; the data access submodule is used to traverse and convert the syntax tree, and the information of the syntax tree is filled into the Modelica object class (ModelicaInstance); the serialization function in the data serialization / deserialization module is used to serialize the information in the ModelicaInstance into a Modelica model in JSON format. The Modelica model structured JSON format is converted to text format, and the deserialization function in the data serialization / deserialization module is used to extract the information in the Modelica structured JSON format model, fill it in and deserialize it into the ModelicaInstance object class, and the Modelica text code generation module is used to convert the information in the ModelicaInstance object class into a model in the Modelica text format.

[0083] Specifically, the ANTLR4 syntax parsing module contains three submodules, namely, the lexical analysis submodule, the grammatical analysis submodule and the data access submodule. The lexical analysis submodule receives the Modelica file in the original text format and generates Tokens; the grammatical analysis submodule performs grammatical analysis on the Tokens and builds a syntax tree based on the generated Tokens; the data access submodule traverses and processes the syntax tree and generates the corresponding ModelicaInstance object.

[0084] The Modelica text code generation module generates a model in the corresponding Modelica text format according to the Modelica syntax rules from the Modelica object (ModelicaInstance), ensuring that the model structure is correct and can be correctly recognized and processed by the Modelica simulation tool.

[0085] The data serialization / deserialization module uses GSON technology to serialize Modelica objects (ModelicaInstance) in JAVA language into JSON format for transmission and storage; it also supports deserialization, converting Modelica models in JSON format into Modelica objects (ModelicaInstance) in JAVA language.

[0086] Through the effective collaboration of the above modules, the system can effectively realize the complete conversion between the text format Modelica model and the structured (JSON format) Modelica model.

[0087] The system is a syntax parsing system for the Modelica language, which realizes the data conversion between the text format and the structured JSON format of the Modelica simulation model, and simultaneously supports the simulation requirements of the Modelica model (text format) and the requirements for effective storage, transmission, processing, and management of model data (structured JSON format), thereby supporting the collaborative simulation modeling and model asset management of the Modelica language in the WEB environment.

[0088] In addition, based on the above system, the present application also provides a syntax parsing method for Modelica language, such as Figure 3 and Figure 4 As shown, the method comprises the following steps:

[0089] Step 1: Input the Modelica model file in the original text format into the syntax parsing system for the Modelica language to prepare for parsing;

[0090] Step 2: Use ANTLR4 to perform lexical analysis and decompose the input text format Modelica model file content into basic lexical units (Tokens);

[0091] Step 3: Based on the generated tokens, build a syntax tree, perform syntax analysis, and parse the structure and association relationship of the model elements;

[0092] Step 4: Traverse the syntax tree through the data access submodule and convert the syntax tree into the corresponding Modelica instance object in Java language;

[0093] Step 5: Use the GSON library to write a data serialization module to serialize the Modelica object into JSON format;

[0094] Step 6: Input the Modelica model file in JSON format into the syntax parsing system for the Modelica language;

[0095] Step 7: Use the data deserialization module written based on the GSON library to convert the Modelica model in JSON format into a Modelica object in Java language;

[0096] Step 8: Use the Modelica text code generation module to convert different model elements in the Modelica object into text-formatted strings, and merge the text-formatted strings to generate a complete text-formatted Modelica model file.

[0097] Figure 5It shows that the syntax parsing system for the Modelica language converts different model elements (basic information, components, equations, connection equations, algorithms, annotations) into corresponding Modelica text string fragments, and finally merges these text string fragments to generate a complete Modelica model file in text format. In this way, the standardization and consistency of code generation are ensured, which can accurately reflect the structure and logic of the model.

[0098] In some embodiments, step 1 specifically includes:

[0099] Step 1.1: Call the lexical analysis submodule of ANTLR4, perform word segmentation on the Modelica model (code) according to the lexical rules, and convert the model code into a Token stream;

[0100] Step 1.2: Perform preliminary verification on the processed Token stream to ensure that each Token conforms to the expected lexical rules and mark the type and position of the Token;

[0101] Step 1.3: Pass the verified Token stream to the syntax analysis submodule for subsequent syntax analysis.

[0102] In some embodiments, in step 2, the token stream after lexical analysis is subjected to grammatical analysis. Step 2 specifically includes:

[0103] Step 2.1: The syntax analysis submodule first receives the Token stream passed from the lexical analysis submodule. Each Token contains its type, text value, and position in the original code.

[0104] Step 2.2: Initialize the grammar analyzer according to the grammar rule file (.g4 file) of ANTLR4;

[0105] Step 2.3: The parser uses the defined grammar rules to parse the Token stream. The parser reads the tokens in the Token stream one by one and parses them according to the predefined grammar rules.

[0106] Step 2.4: After parsing the token stream, the parser generates an intermediate representation (part of the abstract syntax tree), which contains the grammatical structure information of the code, but has not yet built a complete syntax tree;

[0107] Step 2.5: The parser integrates the parsed intermediate representation to generate a syntax tree.

[0108] In one embodiment, the parsing process in step 2.3 includes the following specific steps:

[0109] Step 2.3.1: The parser tries to match the type of the current token according to the definition in the grammar rule file. If the match succeeds, it continues to process the next token. If the match fails, a syntax error is thrown.

[0110] Step 2.3.2: In the process of matching rules, the parser constructs grammatical structures according to the grammatical rules. These structures may include declarations, expressions, statement blocks, etc.

[0111] Step 2.3.3: For nested structures in Modelica code (such as nested function calls, loops, conditional statements, etc.), the parser recursively processes these structures to ensure that each nested part is parsed correctly.

[0112] In some embodiments, in step 3, the data access submodule is used to traverse the syntax tree to extract key elements such as model information, variables, equations, and comments in the Modelica code, and the Modelica object class written in Java is instantiated using this information. Step 3 specifically includes:

[0113] Step 3.1: According to the syntax tree generated by ANTLR4, initialize the traverser of the data access submodule. The data access submodule implements node-by-node access to the syntax tree by rewriting the access method of each node;

[0114] Step 3.2: Define corresponding Java class objects for key elements of Modelica code (such as models, variables, equations, comments, etc.). These class objects are used to store information extracted from the syntax tree. The specific definitions include the model name, type, variable list, equation list, and comment list.

[0115] Step 3.3: Find the definition node of the model (such as the class_definition node) in the syntax tree. When accessing the node, create a new Java object instance to store the model information, extract the basic information of the model such as the name and type, and store it in the corresponding Java object;

[0116] Step 3.4: Find the variable definition node (such as component_clause node) under the model node. When accessing the node, create a new Java object instance to store variable information, extract the variable name, type, initial value and other information, and store it in the corresponding Java object;

[0117] Step 3.5: Find the equation definition node (such as equation_section node) under the model node. When accessing the node, create a new Java object instance to store the equation information, extract the left and right expressions of the equation, and store them in the corresponding Java objects;

[0118] Step 3.6: Find the annotation definition node (such as the annotation node) under the model node. When accessing the node, create a new Java object instance to store the annotation information, extract the annotation content, and store it in the corresponding Java object;

[0119] Step 3.7: For each child node under the model node, recursively call the corresponding access method to ensure that all nested structures and elements are correctly parsed and stored;

[0120] Step 3.8: After the traversal is completed, organize all the extracted information into a complete Modelica object (Java language).

[0121] In some embodiments, in step 4, the Modelica object is serialized into JSON format to facilitate data transmission and storage. Step 4 specifically includes:

[0122] Step 4.1: Prepare to convert Modelica objects to JSON format by initializing the serialization module using the GSON library, which provides flexible and efficient serialization and deserialization functions.

[0123] Step 4.2: Select the Modelica object to be serialized and determine the relevant information of the Modelica object to be serialized (such as model, variables, equations, and annotations).

[0124] Step 4.3: Execute the serialization process and call the relevant serialization method to convert the selected Modelica object into a JSON string. In this process, make sure that all the object's properties are correctly converted and output in JSON format.

[0125] Step 4.4: Handle serialization exceptions. During the serialization process, capture and handle possible exceptions (such as objects containing attributes that do not support serialization or data format problems) to ensure that the serialization process is stable and reliable.

[0126] Step 4.5: Output the Modelica model in JSON format, output the generated JSON string, and store it in a file or transfer it to a specified location for subsequent transmission and storage.

[0127] In some embodiments, in step 5, the Modelica model in JSON format is deserialized into a Modelica instance object. Step 5 specifically includes:

[0128] Step 5.1: Prepare to convert the JSON format model into a Modelica object by initializing the deserialization module using the GSON library;

[0129] Step 5.2: Read JSON data. Read the JSON string from the specified location to ensure data integrity and correctness. You can obtain JSON data from files, network requests, or other data sources.

[0130] Step 5.3: Perform the deserialization process and call the relevant serialization methods to convert the JSON string into the corresponding Modelica object. In this process, ensure that all JSON fields are correctly parsed and mapped to the properties of the Modelica object.

[0131] Step 5.4: Handle deserialization exceptions. During the deserialization process, capture and handle possible exceptions (such as JSON format errors or data type mismatches) to ensure that the deserialization process is stable and reliable.

[0132] Step 5.5: Verify the deserialization results. Check the deserialized Modelica object to ensure that all attributes and data are restored correctly. Verify key fields and structures to ensure data consistency and integrity.

[0133] In some embodiments, in step 6, a Modelica model in text format is generated according to the Modelica object. Step 6 specifically includes:

[0134] Step 6.1: Receive the Modelica objects containing the complete model information from the deserialized module. These objects contain information such as model, variables, equations, annotations, etc.

[0135] Step 6.2: Initialize the generator object of the Modelica text code generation module, which will be responsible for converting the attribute information in the Modelica object into a textual Modelica model;

[0136] Step 6.3: Generate the basic structure of the Modelica model in the form of a text string based on the model information in the Modelica object, including the model name, type and related comments;

[0137] Step 6.4: Traverse the list of variables in the Modelica object and generate the corresponding text string fragments. The definition of each variable includes information such as variable name, type and initial value;

[0138] Step 6.5: Traverse the equation list in the Modelica object and generate the corresponding text string fragments. The definition of each equation includes the left-hand side and right-hand side expressions of the equation.

[0139] Step 6.6: Traverse the list of comments and annotations in the Modelica object and generate the corresponding text string fragments to ensure that the comment content is correctly generated and inserted into the corresponding position to maintain the readability of the code and the integrity of the comments;

[0140] Step 6.7: For nested models or components, recursively generate the corresponding text string fragments to ensure that the nested structure is correctly parsed and generated, and the hierarchy and logical relationship of the model elements are maintained;

[0141] Step 6.8: Format the generated Modelica code to ensure that the code complies with the specifications and has good readability, including proper indentation, line breaks, and comment layout;

[0142] Step 6.9: Output the generated Modelica code to a specified location, such as a file or console.

[0143] This application is based on the Modelica model instance object class (ModelicaInstance) defined in the JAVA language based on the Modelica syntax specification. The instance object class completely contains all the key element information in the Modelica model (such as models, variables, equations, and annotations, etc.); on this basis, the conversion process from the Modelica model text format to the structured JSON format and the conversion process from the JSON format to the text format are realized.

[0144] In some embodiments, referring to the Modelica syntax specification, the Modelica model instance object class (ModelicaInstance) defined in the JAVA language includes the following steps:

[0145] (1) According to the Modelica syntax specification, define the Modelica model class and the organizational relationship between different model classes, such as the organizational relationship between model packages and different types of models (model, record), etc.

[0146] (2) According to the Modelica syntax specification, define the constituent elements of the Modelica model, such as components, equations, functions, annotations, and other Modelica model elements, as well as the combination relationship between these elements;

[0147] (3) According to the Modelica syntax specification, each type of model element is continuously decomposed until it is decomposed into basic elements defined by the syntax specification; for example, annotation information is decomposed into basic element objects such as points and lines, equations are decomposed into basic element objects such as variables and operators; components are decomposed into variable objects of basic types such as integer, real, and Boolean.

[0148] In some embodiments, converting the Modelica model text format to the structured JSON format comprises the following steps:

[0149] (1) The Modelica text model to be parsed is processed by the ANTLR4 lexical analysis module to generate a Token stream, remove useless characters, and ensure data consistency;

[0150] (2) Perform grammatical analysis on the token stream obtained after processing in (1), and use the grammatical rules of ANTLR4 to construct the Modelica syntax tree structure. This step can systematically extract the structural and logical information of the Modelica code, providing a basis for subsequent traversal and information extraction;

[0151] (3) The data access module uses the Visitor pattern to traverse the syntax tree obtained in (2), extract key elements in the code node by node (such as models, components, equations, and comments, etc.), and store this information in a defined Java object;

[0152] (4) The information extracted in (3) is serialized and the Java object is serialized into JSON format using GSON technology to facilitate data transmission and storage while ensuring data integrity.

[0153] In some embodiments, converting the Modelica model JSON format to text format includes the following steps:

[0154] (1) For the Modelica model in JSON format to be converted, use GSON technology to deserialize the JSON data stream into a GSON object, and copy the GSON object data to the Modelica instance object (ModelicaInstance) in the Java language;

[0155] (2) For the Modelica instance object in Java obtained by deserialization in (1), extract key elements according to the field attributes in the instance object and generate Modelica code in string form;

[0156] (3) Format and optimize the string-formatted Modelica code generated in (2) to ensure that the code structure is correct and can be correctly recognized and processed by the Modelica simulation tool, and output the final text-formatted Modelica model code.

[0157] The above description is only a specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by any technician familiar with the technical field within the technical scope disclosed in the present application should be covered within the protection scope of the present application.

Claims

1. A syntax parsing system for Modelica language, characterized in that: include: ANTLR4 grammar parsing module, used to perform lexical analysis and grammatical analysis on Modelica text code and generate a grammar tree; Modelica text code generation module, used to generate corresponding Modelica text format models according to Modelica grammar rules from Modelica objects; The data serialization and deserialization module is used to serialize Modelica objects in JAVA language into JSON format for transmission and storage; it also supports deserialization, converting Modelica models in JSON format into Modelica objects in JAVA language.

2. The syntax parsing system for Modelica language according to claim 1, characterized in that: ANTLR4 grammar parsing modules include: The lexical analysis submodule is used to receive the Modelica file in the original text format and generate Tokens; The syntax analysis submodule is used to perform syntax analysis on Tokens and build a syntax tree based on the generated Tokens; The data access submodule is used to traverse and process the syntax tree and generate the corresponding ModelicaInstance object.

3. A syntax parsing method for Modelica language, characterized in that: include: Step 1: Input the Modelica model file in the original text format into the syntax parsing system for the Modelica language to prepare for parsing; Step 2: Use ANTLR4 to perform lexical analysis and decompose the input text format Modelica model file content into basic lexical units; Step 3: Based on the generated lexical units, a syntax tree is constructed, syntax analysis is performed, and the structure and association relationship of the model elements are analyzed; Step 4: Traverse the syntax tree through the data access submodule and convert the syntax tree into the corresponding Modelica instance object in Java language; Step 5: Serialize the Modelica object into JSON format; Step 6: Input the Modelica model file in JSON format into the syntax parsing system for the Modelica language; Step 7: Convert the Modelica model in JSON format into a Modelica object in Java; Step 8: Use the Modelica text code generation module to convert different model elements in the Modelica object into text-formatted strings, and merge the text-formatted strings to generate a complete text-formatted Modelica model file.

4. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 1 includes: Step 1.1: Call the lexical analysis submodule to perform word segmentation on the Modelica model according to the lexical rules and convert the model code into a Token stream; Step 1.2: Perform preliminary verification on the processed token stream to ensure that each token conforms to the expected lexical rules and mark the type and position of the token; Step 1.3: Pass the verified Token stream to the syntax analysis submodule for subsequent syntax analysis.

5. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 2 includes: Step 2.1: Receive the Token stream passed from the lexical analysis submodule. Each Token contains its type, text value, and position in the original code. Step 2.2: Initialize the grammar analyzer according to the grammar rule file of ANTLR4; Step 2.3: The Token stream is parsed by the parser using the defined grammar rules. The parser reads the tokens in the Token stream one by one and parses them according to the predefined grammar rules. Step 2.4: After parsing the token stream, generate an intermediate representation through the parser; Step 2.5: The parser integrates the intermediate representation of the parsing into a syntax tree.

6. The syntax parsing method for Modelica language according to claim 5, characterized in that: Step 2.3 includes: Step 2.3.1: Use the syntax analyzer to match the type of the current token according to the definition in the syntax rule file. If the match succeeds, continue to process the next token; if the match fails, throw a syntax error; Step 2.3.2: In the process of matching rules, the parser builds the grammatical structure according to the grammatical rules; Step 2.3.3: Recursively process the nested structures in the Modelica code through the parser.

7. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 3 includes: Step 3.1: According to the syntax tree generated by ANTLR4, initialize the traverser of the data access submodule. The data access submodule implements node-by-node access to the syntax tree by rewriting the access method of each node; Step 3.2: Define the corresponding Java class objects for the key elements of the Modelica code; Step 3.3: Find the definition node of the model in the syntax tree. When accessing the node, create a new Java object instance to store the model information, extract the basic information of the model, and store it in the corresponding Java object; Step 3.4: Find the variable definition node under the model node. When accessing the node, create a new Java object instance to store variable information, extract the variable information, and store it in the corresponding Java object; Step 3.5: Find the equation definition node under the model node. When accessing the node, create a new Java object instance to store the equation information, extract the left and right expressions of the equation, and store them in the corresponding Java objects; Step 3.6: Find the annotation definition node under the model node. When accessing the node, create a new Java object instance to store the annotation information, extract the annotation content, and store it in the corresponding Java object; Step 3.7: For each child node under the model node, recursively call the corresponding access method to ensure that all nested structures and elements are correctly parsed and stored; Step 3.8: After the traversal is complete, organize all the extracted information into a complete Modelica object.

8. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 4 includes: Step 4.1: Convert Modelica objects to JSON format; Step 4.2: Select the Modelica object to be serialized and determine the information of the Modelica object to be serialized; Step 4.3: Execute the serialization process and call the relevant serialization method to convert the selected Modelica object into a JSON string; Step 4.4: Handle serialization exceptions. During the serialization process, capture and handle any exceptions that occur. Step 4.5: Output the Modelica model in JSON format, output the generated JSON string, and store it in a file or transfer it to a specified location for subsequent transmission and storage.

9. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 5 includes: Step 5.1: Convert the JSON model to a Modelica object. Step 5.2: Read JSON data, read the JSON string from the specified location, and ensure data integrity and correctness; Step 5.3: Execute the deserialization process, call the relevant serialization method, and convert the JSON string into the corresponding Modelica object; Step 5.4: Handle deserialization exceptions. During the deserialization process, capture and handle any exceptions that occur; Step 5.5: Verify the deserialization results. Check the deserialized Modelica object to ensure that all attributes and data are restored correctly. Verify key fields and structures to ensure data consistency and integrity.

10. The syntax parsing method for Modelica language according to claim 3, characterized in that: Step 6 includes: Step 6.1: Receive the Modelica object containing the complete model information; Step 6.2: Initialize the generator object of the Modelica text code generation module; Step 6.3: Generate the basic structure of the Modelica model in the form of a text string based on the model information in the Modelica object; Step 6.4: Traverse the list of variables in the Modelica object and generate the corresponding text string fragments; Step 6.5: Traverse the equation list in the Modelica object and generate the corresponding text string fragments; Step 6.6: Traverse the list of comments and annotations in the Modelica object and generate the corresponding text string fragments; Step 6.7: For nested models or components, recursively generate corresponding text string fragments; Step 6.8: Format the generated Modelica code; Step 6.9: Output the generated Modelica code to the specified location.

Citation Information

Cited By

  • System for converting rule into Python language

    CN120610752A