Method for generating codes from OCL to SQL based on relational algebra

Through the OCL to SQL code generation method based on relational algebra, the problems of insufficient OCL semantic coverage and high SQL statement complexity in the existing technology are solved, and more efficient and concise SQL statement generation is achieved, which improves the readability of the system. and execution efficiency.

CN119441361AActive Publication Date: 2025-02-14NANJING UNIV
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411493497.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-24
Publication Date
2025-02-14
Estimated Expiration
2044-10-24

AI Technical Summary

Technical Problem

The existing OCL to SQL code generation scheme cannot cover enough OCL semantics, and the generated SQL statements are more complex, resulting in a decrease in readability and execution efficiency.

Method used

A set of OCL to SQL code generation methods based on relational algebra is designed, including defining a dedicated OCL grammar, relational algebra extension and corresponding grammar, using two sets of conversion algorithms to convert OCL to relational algebra and relational algebra to SQL, and through optimization The algorithm simplifies relational algebraic expressions to reduce the complexity of SQL statements.

Benefits of technology

It realizes a wider range of OCL semantic coverage, and the generated SQL statements are concise and efficient, improving readability and execution efficiency, solving the problems of insufficient OCL semantic coverage and high complexity of SQL statements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119441361A_ABST
    Figure CN119441361A_ABST
Patent Text Reader

Abstract

The invention discloses an OCL-to-SQL code generation method based on relational algebra, which designs relational algebra extension, and by proving that a semantic set of the extended relational algebra is equivalent to a given semantic set of OCL, the overall code generation work can be realized by firstly converting the OCL into a relational algebra expression and then converting the relational algebra expression into a given semantic set of the OCL. And converting the obtained relational algebraic expression into an SQL (Structured Query Language) query statement. In addition, in the process from the OCL to the relational algebra, the obtained expression is simplified through an optimization algorithm, so that the complexity of a result statement is reduced as much as possible. The method has the advantages of being automatic, efficient, high in optimization capacity, high in universality and the like, and compared with the prior art, the OCL semantic coverage degree and the execution efficiency of result statements are greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to an OCL to SQL code generation method based on relational algebra, belonging to the technical field of software development. Background Art

[0002] With the development of software engineering and the widespread application of model-driven engineering, modeling languages ​​represented by UML play an important role in the process of software development and verification. As a supplement to UML, OCL (Object Constraint Language) provides UML with more powerful expression capabilities. It allows users to use specific syntax to define specific constraints for classes and their attributes in the UML model to verify whether the model instances meet the constraints. In the actual production environment, developers often need to face the massive data in the database, and the existing OCL tools can only perform constraint validity checks on a single or a small number of instances, and cannot perform constraint verification on large quantities of data in a short time.

[0003] Based on the above requirements, converting OCL statements into database query languages, and then checking whether there are instances that violate constraints through query operations, has become a general solution for verifying the legitimacy of massive data. Taking relational databases as an example, many solutions for translating OCL statements into SQL codes have been introduced. The basic idea is to use SQL statements to query all data that do not meet the constraints. If the result is empty, it means that all data meet the given constraints. The existing solutions of this type have one thing in common: the use of translation templates as a bridge between OCL and SQL. These translation templates are defined based on expert knowledge, taking various OCL operators as input and outputting SQL statements of corresponding structures, that is, each OCL operator is matched with an SQL code block one by one. Therefore, the process of translating OCL statements can be divided into three steps: splitting the statement into individual operators; converting each operator into an SQL code block according to the translation template; and combining the SQL code blocks obtained by each step of the operator to obtain the final complete SQL statement.

[0004] The advantage of the above-mentioned translation method using templates is that the translation template written based on expert knowledge can ensure the correctness of each step of the translation operation, thereby ensuring the correctness of the entire translation process. However, it also has many defects. First, since the entire translation process is completely based on templates, the scope of OCL semantics that can be involved also depends entirely on how many operators the template set contains. The existing solutions have not been able to provide a set of templates that can fully cover the OCL semantics required by the actual production environment, and adding templates requires specific expert knowledge, which makes it difficult for this type of solution to cover enough OCL semantics; secondly, OCL statements with a certain degree of complexity often contain nested use of multiple operators. When translating such OCL statements, the template-based translation method will correspondingly obtain SQL statements with multiple intermediate tables and nested query statements, resulting in an increase in statement complexity, and a corresponding decrease in readability and execution efficiency. In summary, the existing OCL to SQL code generation solution faces two major problems: insufficient OCL semantic coverage and high complexity of generated SQL statements. Summary of the invention

[0005] Purpose of the invention: The present invention provides a method for generating OCL to SQL code based on relational algebra, the purpose of which is to solve the problems that the prior art cannot cover enough OCL semantics and the generated SQL statements are highly complex. The present invention designs and defines a set of special OCL grammars, which include first-order logic semantics and some non-first-order logic semantics, so that the grammar can meet the OCL semantic requirements in the actual production environment. Then, a special relational algebra extension and a grammar corresponding to the relational algebra are designed for the set of grammars. On the basis of the two sets of grammars, two sets of conversion algorithms are given, which correspond to the translation process from OCL to relational algebra and from relational algebra to SQL, respectively, so as to realize the conversion process from OCL to SQL with relational algebra as the intermediate carrier. Finally, the relational algebra expression as the intermediate result is simplified as much as possible by designing a special optimization algorithm, so as to achieve the purpose of reducing the complexity of the result statement.

[0006] Technical solution: A method for generating OCL to SQL code based on relational algebra, including the following contents: (1) Design a context-free grammar for OCL (Object Constraint Language) to describe the commonly used semantic sets in OCL invariants. These semantic sets include basic logical operations, set operations, class attributes and methods, etc., which can parse the input OCL statements and generate corresponding relational algebra expressions. In this way, the syntax and semantics of OCL can be accurately mapped to relational algebra expressions.

[0007] (2) Design a context-free grammar for relational algebra and its extensions, describe the basic operators of relational algebra, and add definitions of additional operators to meet the needs of transforming OCL to relational algebra. These operators can be used to generate relational algebra expressions and further converted into SQL statements.

[0008] (3) Design two conversion algorithms, one for converting OCL statements into relational algebra expressions and the other for converting relational algebra expressions into SQL query statements. These two algorithms complete code mapping by parsing the syntax tree, ensuring that OCL statements can be smoothly converted into SQL statements and maintaining semantic consistency.

[0009] (4) Design an optimization algorithm to detect and eliminate redundant operators that may be generated during the conversion from OCL to relational algebra. Use constraints to find the redundant parts of the relational algebra expression, and then reduce them so that the relational algebra expression can be simplified as much as possible. The subsequent translation process continues on the basis of the simplified relational algebra expression to avoid generating unnecessary SQL statements.

[0010] The present invention is independent of specific data storage media, relational database types, and database application scenarios, does not modify database-related software, and has good generalization.

[0011] The above (1) specifically includes: (11) As an important subset of the OCL language, OCL invariants encapsulate the most commonly used OCL semantic elements, including various arithmetic operators (such as addition, subtraction, multiplication, and division) and logical operators (such as AND, OR, and NOT), and also support some special operators (such as sorting and taking extreme values). These operators and operators enable OCL invariants to accurately describe the conditions that an object must meet in a specific state, thereby providing powerful expression capabilities in the modeling process. The present invention selects OCL invariants as the semantic set for transformation, while ensuring that the needs of actual scenarios are met and minimizing the semantic set that needs to be translated.

[0012] (12) OCL invariants are written based on the current UML class. Each invariant instance is usually composed of arithmetic and logical operation statements between the attributes of the current class. The final calculation result is a Boolean value, indicating whether the condition is established. A simple invariant may only involve comparisons and operations between attributes within the same class, while a complex invariant may involve attribute operations between different classes, and even include cross-class association operations, but the final result is still a Boolean value. This Boolean value result is used to verify the legitimacy of the object state and ensure the consistency and integrity of the model. The present invention selects OCL invariants as the input of the transformation method and SQL statements as the output of the transformation method. Whether the result returned by the SQL statement is empty represents whether the Boolean value result of the OCL invariant is true.

[0013] (13) The process of parsing OCL statements depends on two input files: the class definition file and the OCL statement file. The class definition file uses the JSON format to describe the classes and their properties in the current system; the OCL statement file is in plain text format and contains the OCL invariant statements that need to be parsed. The parsing process first reads and obtains detailed information about the current class from the class definition file, including the class name, properties, methods, and relationships between classes. Next, the OCL statement file is read and the OCL statements are parsed one by one based on the previously obtained class information. Each OCL statement will be checked for semantic correctness by an algorithm, and legal statements will be subsequently converted into corresponding relational algebra expressions.

[0014] The above (2) specifically includes: (21) Relational algebra expressions are constructed from a series of basic and advanced operators, including selection (σ), projection (π), Cartesian product (×), natural join ( ), etc., which are used to perform various data operations and queries. In addition, relational algebra expressions also include operations on the relations themselves, such as class attributes. Extended relational algebra retains these core operators and further adds special operators to meet the needs of OCL semantic transformation, such as set cardinality (count / size), extreme value (min / max), set sorting (order by), etc., which correspond one-to-one to the OCL special operators mentioned in (11). The addition of these extended operators does not affect the basic semantics of the original relational algebra and enables relational algebra to handle complex OCL constraints.

[0015] (22) The process described in (13) generates the corresponding relational algebraic expression by parsing the OCL statement. In this process, the OCL statement is gradually converted into an equivalent relational algebraic expression through an algorithm to ensure that the generated expression is semantically equivalent to the original OCL statement.

[0016] (23) Use the generated relational algebra expression to construct the corresponding SQL query statement. This process is also supported by a specific algorithm that ensures that the semantics are consistent when converting the relational algebra expression into SQL statements. The constraints defined by the original OCL statement are satisfied by all data instances if and only if the query result of the final generated SQL statement is empty.

[0017] The above (3) specifically includes: (31) The OCL to relational algebra conversion algorithm is implemented through the compilation framework provided by ANTLR. With the help of the previously defined OCL context-free grammar, the algorithm first parses the input OCL statement into an abstract syntax tree (AST). Each node of this syntax tree represents an OCL clause, including an operator and its operands. During the parsing process, when the algorithm detects that a node cannot be further subdivided (that is, the node becomes a leaf node of the AST), it will directly convert the operator and operand in the node into the corresponding relational algebra expression. Then, the algorithm starts from the leaf node and gradually backtracks upward, synthesizing each operator and operand array into a higher-level relational algebra expression. Finally, when backtracking to the root node, the algorithm will generate a complete relational algebra expression that can accurately describe the semantics expressed by the input OCL statement.

[0018] (32) The algorithm for converting relational algebra to SQL statements is implemented by the same idea as (31). In this process, the algorithm uses the extended relational algebra grammar to parse the relational algebra expression to generate the corresponding AST. At this time, the conversion result of each leaf node is the corresponding SQL statement, and when tracing back to the root node, the algorithm will nest all SQL clauses to form the final SQL statement, which can accurately reflect the logic and operation of the original relational algebra expression, thereby ensuring the semantic consistency of the entire conversion process.

[0019] The above (4) specifically includes: (41) The optimization algorithm optimizes the transformation result of each node in the process of transforming OCL to relational algebra described in (31) to prevent the relational algebra from containing redundant items and making the subsequent generated SQL statements too complicated. After processing the current node, the algorithm does not immediately backtrack to the upper node, but first performs redundancy detection.

[0020] (42) When performing redundancy detection, the algorithm will conduct a detailed inspection of the relational algebra expression just generated to identify possible repeated calculations, unnecessary operators, or other forms of redundancy. If redundant items are detected, the algorithm will simplify or rewrite the expression to remove these redundant parts. In this way, the generated relational algebra expression will be more concise, and the final converted SQL query statement will be more efficient, improving the performance of the overall conversion process.

[0021] An OCL to SQL code generation system based on relational algebra comprises: an OCL grammar definition module, a relational algebra grammar definition module, a two-transformation algorithm module and a relational algebra simplification algorithm module.

[0022] The OCL grammar definition module is modified and extended based on the definition of OCL first-order logic. It not only describes the OCL grammar content that conforms to first-order logic, but also covers some commonly used special operators, which can effectively meet the OCL semantic requirements in actual production.

[0023] The relational algebra grammar definition module is extended on the basis of the original relational algebra grammar so that its semantic set can completely cover the requirements of the OCL semantic set, thus ensuring the integrity and accuracy of the conversion process.

[0024] Two conversion algorithm modules, the two conversion algorithms are based on OCL grammar and relational algebra grammar respectively. The first algorithm realizes the conversion of OCL statements to relational algebra expressions, and the second algorithm further converts relational algebra expressions into SQL statements. These two algorithms work together to complete the full process conversion of OCL statements to SQL statements.

[0025] The optimization algorithm module analyzes the redundancy of relational algebra expressions. In the process of converting OCL statements to relational algebra expressions, the optimization algorithm performs redundancy detection on each intermediate result. If redundant items are found, they are simplified by rewriting, thus ensuring that the final generated SQL statement is also optimized, concise and efficient.

[0026] The process and method of system implementation are the same and will not be repeated here.

[0027] A computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the OCL to SQL code generation method based on relational algebra are implemented.

[0028] A computer-readable storage medium stores a computer program for executing the above-mentioned OCL to SQL code generation method based on relational algebra.

[0029] Beneficial effects: Compared with the prior art solutions, the present invention has the following advantages: (1) It has a wide semantic coverage and can define more complex OCL constraint statements, which is more in line with the user's needs for defining complex constraint conditions in actual scenarios.

[0030] (2) It has strong optimization capabilities and can automatically perform redundancy operations on relational algebra expressions. The generated results (i.e., SQL statements) are more readable and more efficient to execute.

[0031] (3) Strong interpretability, using a clear two-compilation method to translate OCL into SQL statements via relational algebra. Avoid using black box methods such as machine learning, and the semantic equivalence before and after the conversion is more intuitive.

[0032] (4) Good versatility. This method is not limited to a specific software platform or application. It only requires the input file to be an OCL statement and an OCL class definition in a given format. The output SQL statement can be applied to various relational databases, such as MySQL, and has high versatility. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 is an overall execution flow chart of an embodiment of the present invention; Figure 2 The figure is a flowchart of the architecture of an embodiment of the present invention and the working procedures between the various parts. DETAILED DESCRIPTION

[0034] The present invention is further explained below in conjunction with specific embodiments. It should be understood that these embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention. After reading the present invention, various equivalent forms of modifications to the present invention by those skilled in the art all fall within the scope defined by the claims attached to this application.

[0035] A method for generating OCL to SQL code based on relational algebra supports converting a given OCL constraint statement into a corresponding SQL query statement. The OCL to SQL code generation system based on relational algebra used by this method includes four key modules, namely OCL grammar definition, relational algebra grammar definition, two-transformation algorithm and relational algebra simplification algorithm. Specifically, it includes the following contents: (1) Design a context-free grammar for OCL (Object Constraint Language) to describe the commonly used semantic sets in OCL invariants. These semantic sets include basic logical operations, set operations, class attributes and methods, etc., which can parse the input OCL statements and generate corresponding relational algebra expressions. In this way, the syntax and semantics of OCL can be accurately mapped to relational algebra expressions.

[0036] (11) OCL invariants are a key subset of the OCL language, encapsulating commonly used arithmetic and logical operators (such as addition, subtraction, multiplication, division, and, or, not) and special operators (such as sorting and extreme value), which are used to accurately describe the conditions that an object must meet in a specific state. OCL invariants are selected as the semantic set for transformation to meet actual needs and reduce the semantic set that needs to be translated.

[0037] (12) OCL invariants are written based on the current UML class. They usually consist of arithmetic and logical operations between class attributes, and return a Boolean value to indicate whether the condition is true. Simple invariants involve operations on attributes within a class, and complex invariants may associate operations across classes, but the result is always a Boolean value. The OCL invariant is used as input and the SQL statement is used as output. Whether the SQL return result is empty represents whether the Boolean value result of the OCL invariant is true.

[0038] (13) The process of parsing OCL statements relies on two input files: a class definition file in JSON format and an OCL statement file in plain text format. The parsing process first reads the class definition file to obtain detailed information about the classes in the system, then reads the OCL statement file and parses the OCL invariants one by one based on the class information. Legal OCL statements are converted into corresponding relational algebra expressions through algorithms.

[0039] (2) Design a context-free grammar for relational algebra and its extensions, describe the basic operators of relational algebra, and add definitions of additional operators to meet the needs of transforming OCL to relational algebra. These operators can be used to generate relational algebra expressions and further converted into SQL statements.

[0040] (21) Relational algebra expressions consist of basic operators such as selection (σ), projection (π), Cartesian product (×), natural join ( ) and advanced operators to perform various data operations and queries. In addition, relational algebra also includes operations on the relations themselves, such as class attributes. On the basis of retaining these core operators, the extended relational algebra adds special operators that meet the needs of OCL semantic transformation, such as cardinality of sets (count / size), extreme values ​​(min / max), set sorting (order by), etc. These extended operators not only retain the basic semantics of relational algebra, but also enhance its ability to handle complex OCL constraints.

[0041] (22) Parse the OCL statements and gradually convert them into equivalent relational algebra expressions, ensuring that the generated expressions are semantically consistent with the original OCL statements.

[0042] (23) Based on the generated relational algebra expression, the corresponding SQL query statement is constructed. The specific algorithm ensures that the semantics of the relational algebra expression and the SQL statement remain consistent during the conversion process. When the query result of the final generated SQL statement is empty, it indicates that the constraints of the original OCL statement are satisfied by all data instances.

[0043] (3) Design two conversion algorithms, one for converting OCL statements into relational algebra expressions and the other for converting relational algebra expressions into SQL query statements. These two algorithms complete code mapping by parsing the syntax tree, ensuring that OCL statements can be smoothly converted into SQL statements and maintaining semantic consistency.

[0044] (31) The OCL to relational algebra conversion algorithm is implemented using the ANTLR compilation framework with the help of a defined OCL grammar. First, the input OCL statement is parsed into an abstract syntax tree (AST), where each node represents an OCL clause. During the parsing process, when a node cannot be further subdivided (i.e., becomes a leaf node), the algorithm directly converts the operator and operand of the node into a relational algebra expression. Then, the algorithm backtracks from the leaf node, gradually combining operators and operands, and finally generates a complete relational algebra expression at the root node, accurately expressing the input OCL semantics.

[0045] (32) Similarly, the relational algebra to SQL conversion algorithm is also implemented by generating AST. The algorithm uses the extended relational algebra grammar to parse the expression and generate the corresponding AST. In this process, the leaf nodes are converted into SQL statement fragments. As the algorithm traces back to the root node, it combines these fragments into a complete SQL statement, ensuring that it is consistent with the logic and operation of the original relational algebra expression, achieving accurate semantic conversion.

[0046] (4) Design an optimization algorithm to detect and eliminate redundant operators that may be generated during the conversion from OCL to relational algebra. Use constraints to find the redundant parts of the relational algebra expression, and then reduce them so that the relational algebra expression can be simplified as much as possible. The subsequent translation process continues on the basis of the simplified relational algebra expression to avoid generating unnecessary SQL statements.

[0047] The optimization algorithm optimizes the transformation result of each node in the process of transforming OCL to relational algebra described in (31) to prevent the generation of relational algebra containing redundant items, thereby avoiding the subsequent generated SQL statements from being too complicated. After processing the current node, the algorithm does not immediately backtrack to the upper node, but first performs redundancy detection.

[0048] (42) During the redundancy detection process, the algorithm will carefully examine the relational algebra expression just generated to identify possible repeated calculations, unnecessary operators, or other forms of redundancy. Once redundant items are detected, the algorithm will simplify or rewrite the expression to remove the redundant parts. In this way, the generated relational algebra expression will be more concise, and the SQL query statement finally converted will be more efficient, further improving the performance of the overall conversion process.

[0049] Figure 1 The overall execution flow chart of the example of the present invention is described, which shows how developers can use the relational algebra-based conversion method proposed in the present invention to assist in the conversion from OCL to SQL, so as to automatically generate efficient SQL statements, including the following contents: (1) Developers use JSON to write UML models corresponding to instances in the database.

[0050] (2) Developers use plain text to edit the OCL constraint statements that need to be converted based on the needs of the actual scenario.

[0051] (3) The developer uses the defined model as the configuration to initialize the transformation system and inputs the defined OCL constraints.

[0052] (4) The OCL to relational algebra conversion module executes the algorithm and parses the read OCL constraint statement into a syntax tree.

[0053] (5) The algorithm parses the syntax tree of the OCL statement step by step and executes the optimization algorithm during the transformation process of each node.

[0054] (6) Backtrack the OCL syntax tree to obtain the relational algebra expression.

[0055] (7) The relational algebra to SQL conversion module parses into a new relational algebra syntax tree.

[0056] (8) The algorithm parses the relational algebra syntax tree step by step, converts each node into a corresponding SQL code block, and backtracks through the relational algebra syntax tree to obtain the final SQL statement.

[0057] Figure 2 The architecture diagram of the example of the present invention is described. The modules involved in the diagram include OCL syntax tree parsing, relational algebra syntax tree parsing, OCL to relational algebra conversion algorithm, relational algebra to SQL conversion algorithm and data objects generated in the middle. These modules are described below: (1) OCL syntax tree parsing: OCL context-free grammar is defined, which can parse the input OCL statement into an OCL syntax tree for the next step of conversion into relational algebra.

[0058] (2) OCL to relational algebra conversion algorithm, reads the OCL syntax tree parsed in (1) and gradually converts it into a relational algebra expression.

[0059] (3) The relational algebra optimization algorithm is applied in the transformation process described in (2) to detect redundancy in the relational algebra expressions and simplify them as much as possible.

[0060] (4) Relational algebra syntax tree parsing: It defines a context-free grammar based on relational algebra and its extensions, and can parse the input relational algebra expression into a relational algebra syntax tree for the next step of converting it into an SQL statement.

[0061] (5) Relational algebra to SQL conversion algorithm, reads the relational algebra syntax tree parsed in (4) and gradually converts it into SQL statements.

[0062] Obviously, those skilled in the art should understand that the above-mentioned system and method of the present invention are directed to data models written in UML+OCL and their instances stored in a relational database. This means that as long as the data model and its constraints are met, it can be defined in the UML+OCL manner, and the data instance is stored in a relational database, the present invention can be used to generate code from OCL to SQL to complete the legality verification of a large number of data instances. In this way, the present invention is not limited to be combined with any specific hardware or software.

Claims

1. A method for generating OCL to SQL code based on relational algebra, characterized in that: Includes the following: (1) Design a context-free grammar for OCL. This grammar describes the common semantic set contained in OCL invariants and will be used to parse input OCL statements and generate corresponding relational algebra expressions. (2) Design a context-free grammar for extended relational algebra. This grammar describes the basic operators of relational algebra and the additional operators added to satisfy the transformation process. It will be used to generate OCL to relational algebra and relational algebra to SQL statements. (3) Design two transformation algorithms, one for the transformation from OCL to relational algebra and the other for the transformation from relational algebra to SQL; (4) Design an optimization algorithm that can detect whether redundant operators are generated during the OCL to relational algebra translation process and simplify the redundant results.

2. The OCL to SQL code generation method based on relational algebra according to claim 1, characterized in that: The above (1) specifically includes: (11) OCL invariants, as a subset of OCL, encapsulate the OCL semantics used, including arithmetic and logical operators, as well as special operators. (12) OCL invariants are written based on the current UML class. An invariant instance consists of arithmetic and logical operation statements between the attributes of the current class, and its return result is a Boolean value. Complex invariants can contain attribute operations between different classes, and the return result is still a Boolean value. (13) The input files required for parsing OCL statements are divided into class definition files and OCL statement files. The class definition files are in JSON format and the OCL statements are in text format. The overall process is: first read the class definition file to obtain the current class information, then read the OCL statement file, and parse the OCL statement based on the obtained class information. The final output result is a relational algebra expression that is semantically equivalent to the input OCL statement.

3. The OCL to SQL code generation method based on relational algebra according to claim 1, characterized in that: The above (2) specifically includes: (21) Relational algebra expressions are composed of basic operators, advanced operators, and the relations themselves. Extended relational algebra adds special operators to the above. The basic operators include selection, projection, and Cartesian product. The advanced operators include natural join. The special operators include cardinality of sets, extreme value, and set sorting. (22) The relational algebra expression is generated from the OCL statement parsed in the previous step in the process, and the semantic equivalence of the two is guaranteed by the algorithm; (23) The relational algebra expression will then be used to generate the corresponding SQL query statement, which is also guaranteed to be semantically equivalent by the corresponding algorithm.

4. The OCL to SQL code generation method based on relational algebra according to claim 1, characterized in that: The above (3) specifically includes: (31) The OCL to relational algebra conversion algorithm is implemented through the compilation framework provided by ANTLR. Using the defined OCL grammar, the algorithm will be able to parse OCL statements to obtain the corresponding relational algebra expressions. The specific process is as follows: First, the input OCL statement will be decomposed into a syntax tree through grammar parsing. Each node of the syntax tree will correspond to an OCL clause, including operators and operation parameters; then, when it is detected that the current statement cannot be further subdivided, the operators and parameters will be converted into relational algebra expressions starting from the leaf nodes, and then backtracked upward; finally, when backtracking to the root node, a complete relational algebra expression that can describe the entire OCL statement will be obtained; (32) The algorithm for converting relational algebra to SQL statements is the same as that in (31) above. In this process, the algorithm will use the extended relational algebra grammar defined above to parse the relational algebra expression into AST, and then backtrack from the leaf node to convert the AST into the corresponding SQL statement.

5. The OCL to SQL code generation method based on relational algebra according to claim 1, characterized in that: The above (4) specifically includes: (41) The main goal of the optimization algorithm is to avoid redundant items in the process of OCL to relational algebra transformation. The optimization algorithm is integrated in the process of OCL to relational algebra transformation. Specifically, after processing the current node each time, it does not immediately backtrack to the upper node, but first performs a redundant check; (42) During the redundancy detection process, the algorithm will carefully check the relational algebra expression just generated to look for possible repeated calculations, unnecessary operators, or other forms of redundancy; if redundancy is found, the algorithm will rewrite or simplify the expression to remove these redundant parts.

6. A OCL to SQL code generation system based on relational algebra, characterized in that: include: (1) OCL grammar definition module, which designs the context-free grammar of OCL. This grammar describes the common semantic set contained in the OCL invariants and will be used to parse the input OCL statements and generate the corresponding relational algebra expressions; (2) Relational algebra grammar definition module, which designs a context-free grammar for extending relational algebra. This grammar describes the basic operators of relational algebra and the additional operators added to satisfy the transformation process. It will be used to generate OCL to relational algebra and relational algebra to SQL statements. (3) Two transformation algorithm modules: design two transformation algorithms, one for the transformation process from OCL to relational algebra and the other for the transformation from relational algebra to SQL; (4) The two-transformation algorithm module designs an optimization algorithm that can detect whether redundant operators are generated during the OCL to relational algebra translation process and simplify the redundant results. The four parts are OCL grammar definition, relational algebra grammar definition, two-transformation algorithm, and relational algebra simplification algorithm.

7. A computer device, characterized in that: The computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the OCL to SQL code generation method based on relational algebra as described in any one of claims 1 to 5 are implemented.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for executing the OCL to SQL code generation method based on relational algebra as claimed in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Method for generating constraint expression related to model and constraint language editor

    CN101499003A

  • Query statement detection method and device and storage medium

    CN116610700A

  • Method and system for evaluating data

    US20110295906A1

  • Methods and systems for parsing and interpretation of mathematical statements

    US20130024487A1

  • System and method for relational database query answering and optimization based on natural algebra of k-relations

    WO2021086257A1