An OCL to SQL code generation method based on relation algebra
By designing a dedicated OCL grammar and relational algebra conversion algorithm, the problems of insufficient OCL semantic coverage and high SQL statement complexity in existing technologies are solved, achieving efficient and concise OCL to SQL code generation, which is applicable to various relational databases.
Patent Information
- Application Number
- CN202411493497.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-10-24
- Estimated Expiration
- 2044-10-24
AI Technical Summary
Existing OCL to SQL code generation solutions cannot cover sufficient OCL semantics, and the generated SQL statements are too complex to meet the needs of actual production environments.
A dedicated OCL grammar was designed and defined, including first-order logical semantics and some non-first-order logical semantics. Relational algebra was used as an intermediate carrier, and specialized conversion and optimization algorithms were designed to realize the conversion process from OCL to SQL and optimize the generated SQL statements.
It achieves broader OCL semantic coverage, generates concise and efficient SQL statements, improves readability and execution efficiency, and is suitable for various relational databases.
Smart Images

Figure CN119441361B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to an OCL-to-SQL code generation method based on relational algebra, and belongs to the technical field of software development. BACKGROUND
[0002] With the development of software engineering and the wide application of model-driven engineering, modeling languages represented by UML play an important role in the process of software development and verification. OCL (Object Constraint Language) as a supplement to UML provides more powerful expression for UML. It allows users to define specific constraints for classes and their properties in UML models using specific syntax, so as to check whether the model instances meet the constraint conditions. In the actual production environment, developers often need to face massive data existing in the database, and the existing OCL tools can only perform single or small instance constraint legality checking, and cannot perform constraint verification on a large amount of data in a short time.
[0003] Based on the above-mentioned needs, OCL statements are correspondingly converted into database query language, so that whether there is an instance that violates the constraint is checked through the query operation, which becomes a general solution for massive data legality verification. Taking a relational database as an example, many solutions for translating OCL statements into SQL code have been put forward, and the basic idea is to use SQL statements to query all data that does not satisfy the constraint, and if the result is empty, it means that all data satisfies the given constraint. The existing solutions of this kind have a common point: a translation template is used as a bridge connecting OCL and SQL. These translation templates are defined based on expert knowledge, taking various OCL operators as input, and outputting corresponding structured SQL statements, that is, each OCL operator is one-to-one corresponding to a SQL code block, and thus the process of translating OCL statements can be divided into three steps: splitting the statement into various operators; converting each operator into a SQL code block according to the translation template; and combining the SQL code blocks obtained in each step of the operator to obtain the final complete SQL statement.
[0004] The translation method using templates has the advantages that the translation templates written based on expert knowledge can guarantee the correctness of each translation operation and thus 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 range of OCL semantics that can be involved completely depends on how many operators are included in the template set. The existing solutions have not been able to provide a template set that can completely cover the OCL semantics required in actual production environments, and adding templates requires specific expert knowledge, which makes it difficult for this type of solution to cover sufficient OCL semantics. Second, OCL statements with a certain degree of complexity often contain the 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 the complexity of the statements, and a corresponding decrease in readability and execution efficiency. In summary, the existing OCL-to-SQL code generation solutions face two major problems: insufficient OCL semantic coverage and high complexity of the generated SQL statements. SUMMARY
[0005] The present application provides an OCL-to-SQL code generation method based on relational algebra, aiming to solve the problems of insufficient OCL semantic coverage and high complexity of the generated SQL statements in the prior art. The present application designs and defines a set of special OCL grammar, including first-order logic semantics and part of non-first-order logic semantics, so that the grammar can meet the OCL semantic requirements in actual production environments. Then, a special relational algebra extension and the corresponding grammar for the relational algebra are designed for the set of grammar. On the basis of the two sets of grammar, two sets of conversion algorithms are given, corresponding to the translation process from OCL to relational algebra and from relational algebra to SQL, respectively. Thus, the OCL-to-SQL conversion process with relational algebra as an intermediate carrier can be realized. Finally, a special optimization algorithm is designed to simplify the relational algebra expression as an intermediate result as much as possible, achieving the purpose of reducing the complexity of the result statement.
[0006] TECHNICAL SOLUTION An OCL-to-SQL code generation method based on relational algebra, comprising the following contents:
[0007] (1) Designing a context-free grammar of OCL (Object Constraint Language) to describe the commonly used semantic set in OCL invariants. These semantic sets include basic logical operations, set operations, class attributes, and methods, etc., which can parse the input OCL statement and generate the corresponding relational algebra expression. Thus, the syntax and semantics of OCL are accurately mapped into the relational algebra expression.
[0008] (2) Design the context-free grammar of the relational algebra and its extension, describe the basic operation symbols of the relational algebra, and add the definition of additional operation symbols to meet the needs of OCL to relational algebra conversion. These operation symbols can be used to generate relational algebra expressions and further converted into SQL statements.
[0009] (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 successfully converted into SQL statements and maintaining semantic consistency.
[0010] (4) Design an optimization algorithm to detect and eliminate redundant operators that may be generated during the conversion of OCL to relational algebra. By finding the redundant part of the relational algebra expression through constraints, the part is reduced, 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.
[0011] The present application is independent of specific data storage media, relational database types, and database application scenarios, does not modify database-related software, and has good generality.
[0012] In the (1), specifically comprising:
[0013] (11) OCL invariant is an important subset of OCL language, encapsulating the most commonly used OCL semantic elements, including various arithmetic operators (such as addition, subtraction, multiplication, division) and logical operators (such as and, or, not), and also supports some special operators (such as sorting, extreme value). These operators and operators enable OCL invariants to accurately describe the conditions that objects must meet in a specific state, thereby providing powerful expression capabilities during modeling. The present application selects OCL invariants as the semantic set for conversion, while ensuring that the needs of actual scenarios are met, the semantic set for translation is minimized.
[0014] (12) OCL invariants are written based on the current UML class, and each invariant instance is usually composed of arithmetic and logical operation statements between the attributes of the current class, and the final calculation result is a Boolean value, indicating whether the condition is established. A simple invariant may only involve comparison and operation between attributes within the same class, while a complex invariant may involve attribute operation between different classes, or even cross-class association operation, but the final result is still a Boolean value. This Boolean result is used to verify the legality of the object state, ensuring the consistency and integrity of the model. The present application selects OCL invariants as the input of the conversion method, and SQL statements as the output of the conversion method, and whether the SQL statement returns a result is empty or not represents whether the Boolean result of the OCL invariant is true.
[0015] (13) The process of parsing OCL statements depends on two input files: class definition file and OCL statement file. The class definition file is in JSON format, describing the classes and their attributes in the current system; the OCL statement file is in plain text format, containing the OCL invariant statements to be parsed. The parsing process first reads and obtains the detailed information of the current class from the class definition file, including the name of the class, the attributes, the methods and the relationship between the classes. Next, the OCL statement file is read, and based on the previously obtained class information, the OCL statements are parsed one by one. Each OCL statement will be checked for its semantic correctness by an algorithm, and the legal statements will be subsequently converted into corresponding relational algebra expressions.
[0016] In the (2), specifically comprising:
[0017] (21) The relational algebra expression is composed of a series of basic operators and high-level operators, including selection (σ), projection (π), Cartesian product (×), natural join ( ), etc., which are used to perform various data operations and queries. In addition, the relational algebra expression also includes operations on relations themselves, such as class attributes, etc. The extended relational algebra further adds special operators to meet the needs of OCL semantic conversion on the basis of retaining these core operators, such as set size (count / size), extreme value (min / max), set sorting (order by), etc. These extended operators correspond one-to-one with 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 the relational algebra to handle complex OCL constraints.
[0018] (22) The corresponding relational algebra expression is generated by the process described in (13) by parsing the OCL statement. In this process, the OCL statement is gradually converted into an equivalent relational algebra expression by an algorithm, ensuring that the generated expression is semantically equivalent to the original OCL statement.
[0019] (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 remain 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 generated SQL statement is empty.
[0020] The above (3) specifically includes:
[0021] (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.
[0022] (32) The algorithm for converting relational algebra to SQL statements is implemented using the same approach as (31). In this process, the algorithm uses the extended relational algebra grammar to parse the relational algebra expression and generate the corresponding AST. At this point, the conversion result of each leaf node is the corresponding SQL statement. When tracing back to the root node, the algorithm nests all SQL clauses to form the final SQL statement, which accurately reflects the logic and operations of the original relational algebra expression, thereby ensuring the semantic consistency of the entire conversion process.
[0023] The above (4) specifically includes:
[0024] (41) The optimization algorithm optimizes the conversion results of each node in the OCL to relational algebra conversion process described in (31) to prevent the relational algebra from containing redundant items, which would make the subsequent SQL statements too complex. After processing the current node, the algorithm does not immediately backtrack to the upper level node, but first performs redundancy detection.
[0025] (42) In the execution of redundancy detection, the algorithm will conduct a detailed check on the just-generated relational algebra expression 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 also be more efficient, improving the overall performance of the conversion process.
[0026] An OCL-to-SQL code generation system based on relational algebra includes an OCL grammar definition module, a relational algebra grammar definition module, a two-time translation algorithm module, and a relational algebra simplification algorithm module.
[0027] The OCL grammar definition module is based on the definition of OCL first-order logic and has been modified and extended. It not only describes the OCL syntax content conforming to the first-order logic, but also covers some commonly used special operators, effectively meeting the OCL semantic needs in actual production.
[0028] The relational algebra grammar definition module is extended based on the original relational algebra syntax, making its semantic set completely cover the needs of the OCL semantic set, thereby ensuring the completeness and accuracy of the conversion process.
[0029] The two-time translation algorithm module has two translation algorithms based on the OCL grammar and the relational algebra grammar, respectively. The first algorithm realizes the conversion of OCL statements to relational algebra expressions, and the second algorithm further converts the relational algebra expressions to SQL statements. The two algorithms work together to complete the whole process conversion from OCL statements to SQL statements.
[0030] The optimization algorithm module analyzes the redundancy of the relational algebra expression. In the conversion process from OCL statements to relational algebra expressions, the optimization algorithm conducts redundancy detection on each intermediate result. If redundant items are found, the algorithm simplifies them through rewriting, thereby ensuring that the finally generated SQL statement is also optimized, concise and efficient.
[0031] The system implementation process and method are the same and will not be repeated.
[0032] A computer device includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the above computer program, the steps of the OCL-to-SQL code generation method based on relational algebra are realized.
[0033] A computer-readable storage medium stores a computer program for executing the OCL-to-SQL code generation method based on relational algebra.
[0034] Beneficial effects: Compared with the prior art scheme, the present application has the following advantages:
[0035] (1) Wide semantic coverage, can define more complex OCL constraint statements, and better meet the needs of users for defining complex constraint conditions in actual scenarios.
[0036] (2) Strong optimization capability, can automatically perform de-redundancy operation on the relational algebra expression, and the generated result (i.e. SQL statement) has stronger readability and higher execution efficiency.
[0037] (3) Strong interpretability, uses the clear two-compilation method to translate OCL into SQL statements via relational algebra. Avoids using black-box methods such as machine learning, and the semantic equivalence before and after conversion is more intuitive.
[0038] (4) Good versatility, the method is not limited to specific software platforms or applications, only requires that the input file is an OCL statement and OCL class definition in a given format, and the output SQL statement can be applied to various relational databases such as MySQL, and has high versatility. BRIEF DESCRIPTION OF DRAWINGS
[0039] Figure 1 is the overall execution flowchart of the embodiment of the present application;
[0040] Figure 2 is the architecture of the embodiment of the present application and the working flowchart between the parts. DETAILED DESCRIPTION
[0041] The present application will be further illustrated below in conjunction with specific embodiments, which should be understood as merely illustrating the present application and not limiting the scope of the present application. After reading the present application, those skilled in the art can make various equivalent modifications to the present application, which all fall within the scope defined by the claims attached hereto.
[0042] An OCL-to-SQL code generation method based on relational algebra, which 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 the method contains four key modules, namely OCL grammar definition, relational algebra grammar definition, two-conversion algorithm, and relational algebra simplification algorithm. Specifically includes the following contents:
[0043] (1) Design the context-free grammar of OCL (Object Constraint Language), which describes 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 statement and generate the corresponding relational algebra expression. Thus, the syntax and semantics of OCL are accurately mapped to the relational algebra expression.
[0044] (11) OCL invariants are a key subset of the OCL language that encapsulates commonly used arithmetic and logical operators (such as addition, subtraction, multiplication, division, and, or, not) and special operators (such as ordering, extreme value taking) for precisely describing conditions that objects must satisfy in a particular state. OCL invariants are chosen as the transformed semantic set to meet practical needs and reduce the semantic set that needs to be translated.
[0045] (12) OCL invariants are based on the current UML class and are usually composed of arithmetic and logical operations between class attributes, returning a Boolean value to indicate whether the condition is true or false. Simple invariants involve intra-class attribute operations, while complex invariants may operate across class associations, but the result is always a Boolean value. With OCL invariants as input and SQL statements as output, the SQL return result is empty, which represents whether the Boolean result of the OCL invariant is true.
[0046] (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 based on the class information. Legal OCL statements will be converted to corresponding relational algebra expressions through the algorithm.
[0047] (2) Design a context-free grammar for relational algebra and its extensions to describe the basic operators of relational algebra, while adding definitions of additional operators to meet the needs of OCL to relational algebra conversion. These operators can be used to generate relational algebra expressions and further converted to SQL statements.
[0048] (21) Relational algebra expressions are composed of basic operators (such as selection (σ), projection (π), Cartesian product (×), natural join ( ), etc.) and high-level operators for performing various data operations and queries. In addition, relational algebra also includes operations on relations themselves, such as class attributes, etc. On the basis of preserving these core operators, the extended relational algebra adds special operators to meet the needs of OCL semantic conversion, such as set size (count / size), extreme value taking (min / max), set ordering (order by), etc. These extended operators not only preserve the basic semantics of relational algebra, but also enhance its ability to handle complex OCL constraints.
[0049] (22) Parse OCL statements and gradually convert them to equivalent relational algebra expressions to ensure that the generated expressions are semantically consistent with the original OCL statements.
[0050] (23) Based on the generated relational algebra expression, construct the corresponding SQL query statement. The specific algorithm ensures that the semantics of the relational algebra expression and the SQL statement remain consistent during the conversion process. If the final generated SQL statement query result is empty, it indicates that the original OCL statement's constraint condition is satisfied by all data instances.
[0051] (3) Design two conversion algorithms, one for converting OCL statements to relational algebra expressions and the other for converting relational algebra expressions to SQL query statements. These two algorithms complete code mapping by parsing syntax trees, ensuring that OCL statements can be successfully converted to SQL statements while maintaining semantic consistency.
[0052] (31) The OCL-to-relational algebra conversion algorithm uses the ANTLR compilation framework and is implemented with a defined OCL grammar. First, the input OCL statement is parsed into an abstract syntax tree (AST), and each node in the syntax tree represents an OCL clause. During parsing, when a node cannot be further subdivided (i.e., it becomes a leaf node), the algorithm directly converts the operator and operand of the node to a relational algebra expression. Then, the algorithm backtracks from the leaf nodes upwards, gradually combining operators and operands, and finally generates a complete relational algebra expression at the root node, accurately expressing the input OCL semantics.
[0053] (32) Similarly, the relational algebra-to-SQL conversion algorithm is also implemented by generating an AST. The algorithm uses an extended relational algebra grammar to parse the expression and generate the corresponding AST. In this process, leaf nodes are converted to SQL statement fragments, and as the algorithm backtracks to the root node, it combines these fragments into a complete SQL statement, ensuring that it is consistent with the logic and operations of the original relational algebra expression, achieving accurate semantic conversion.
[0054] (4) Design an optimization algorithm to detect and eliminate redundant operators that may be generated during the conversion of OCL to relational algebra. By finding redundant parts in the relational algebra expression through constraint conditions, the algorithm performs reduction operations on these parts, thereby simplifying the relational algebra expression as much as possible. The subsequent translation process continues based on the simplified relational algebra expression, avoiding the generation of unnecessary SQL statements.
[0055] The optimization algorithm optimizes the conversion results of each node in the OCL-to-relational algebra conversion process described in (31) to prevent the generation of relational algebra containing redundant terms, thereby avoiding overly complex SQL statements generated subsequently. After processing the current node, the algorithm does not immediately backtrack to the upper layer node, but first performs redundancy detection.
[0056] (42) In the redundancy detection process, the algorithm will check the generated relational algebra expression in detail to identify possible redundant calculations, unnecessary operators or other forms of redundancy. Once a redundant item is detected, the algorithm will simplify or rewrite the expression to remove the redundant part. In this way, the generated relational algebra expression will be more concise, and the SQL query statement finally translated will also be more efficient, further improving the performance of the overall translation process.
[0057] Figure 1 The overall execution flowchart of the example of the present application is described, which shows how the developer uses the relational algebra-based translation method proposed by the present application to assist in the implementation of OCL to SQL translation, to automatically generate efficient SQL statements, including the following contents:
[0058] (1) The developer uses JSON to write the UML model corresponding to the instance in the database.
[0059] (2) The developer uses plain text to edit the OCL constraint statement to be translated according to the actual scene requirements.
[0060] (3) The developer initializes the translation system as a configuration using the defined model, and inputs the defined OCL constraint.
[0061] (4) The OCL-to-relational algebra translation module executes the algorithm to parse the read OCL constraint statement into a syntax tree.
[0062] (5) The algorithm parses the OCL statement syntax tree step by step, and executes the optimization algorithm in the translation process of each node.
[0063] (6) The OCL syntax tree is backtracked to obtain a relational algebra expression.
[0064] (7) The relational algebra-to-SQL translation module parses a new relational algebra syntax tree.
[0065] (8) The algorithm parses the relational algebra syntax tree step by step, and each node is translated into a corresponding SQL code block, and the final SQL statement is obtained by backtracking the relational algebra syntax tree.
[0066] Figure 2 The architecture diagram of the example of the present application is described, and the modules involved in the diagram include OCL syntax tree parsing, relational algebra syntax tree parsing, OCL-to-relational algebra translation algorithm, relational algebra-to-SQL translation algorithm, and intermediate generated data objects. The following describes these modules:
[0067] (1) OCL syntax tree parsing, which defines the context-free grammar of OCL and can parse the input OCL statement into an OCL syntax tree for the next step of translation into relational algebra.
[0068] (2) OCL to Relational Algebra Conversion Algorithm, reads the parsed OCL syntax tree in (1), and converts it into a relational algebra expression step by step.
[0069] (3) Relational Algebra Optimization Algorithm, applies during the conversion process described in (2), and performs redundancy detection on the relational algebra expression to simplify it as much as possible.
[0070] (4) Relational Algebra Syntax Tree Parsing, defines a context-free grammar for relational algebra and its extensions, and can parse the input relational algebra expression into a relational algebra syntax tree, which is used for the next step of conversion into a SQL statement.
[0071] (5) Relational Algebra to SQL Conversion Algorithm, reads the parsed relational algebra syntax tree in (4), and converts it into a SQL statement step by step.
[0072] Obviously, those skilled in the art should understand that the system and method of the above-mentioned embodiments of the present application are directed to data models written in UML+OCL and their instances stored on relational databases. This means that as long as the data model and its constraints can be defined using UML+OCL, and the data instances are stored on a relational database, the present application can be used for OCL to SQL code generation to complete the legality verification of a large number of data instances. In this way, the embodiments of the present application are not limited to any specific hardware or software combination.
Claims
1. A method for generating OCL to SQL code based on relational algebra, characterized in that, The application comprises the following contents: (1) designing a context-free grammar of OCL, which describes the common semantic set contained in OCL invariant, and will be used to parse the input OCL statement and generate the corresponding relational algebra expression; (2) designing a context-free grammar of extended relational algebra, which describes the basic operation symbols of relational algebra and additional operation symbols added for the transformation process, and will be used for the generation of OCL to relational algebra and relational algebra to SQL statement; (3) designing two transformation algorithms, respectively for the transformation process of OCL to relational algebra and relational algebra to SQL; (4) designing an optimization algorithm, which can detect whether a redundant operator is generated in the process of OCL to relational algebra translation, and simplify the result with redundancy; In the (1), specifically comprising: (11) OCL invariant, as a subset of OCL, encapsulates the used OCL semantics, including arithmetic operators and logical operators, and special operators set; (12) OCL invariant is written based on the current UML class, and an invariant instance is composed of arithmetic and logical operation statements between the attributes of the current class, and the 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 in the process of parsing OCL statement are divided into class definition file and OCL statement file, wherein the class definition file is in JSON format, and the OCL statement is in text format, and the overall process is: first, read the class definition file to obtain the current class information, then read the OCL statement file, parse the OCL statement based on the obtained class information, and finally output the result as a relational algebra expression equivalent to the semantic of the input OCL statement; In the (2), specifically comprising: (21) the relational algebra expression is composed of basic operators, high-level operators and relations, and the extended relational algebra additionally adds special operators on the basis; the basic operators include selection, projection and Cartesian product; the high-level operators include natural join; the special operators include the power of set, 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 ensured by the algorithm; (23) the relational algebra expression will be used to generate the corresponding SQL query statement in the following part, and the semantic equivalence of the two is also ensured by the corresponding algorithm; In the (3), specifically comprising: (31) The conversion algorithm from OCL to relational algebra is implemented through the compiling framework provided by ANTLR, using the defined OCL grammar. The algorithm can parse the OCL statement to obtain the corresponding relational algebra expression. The specific process is as follows: first, the input OCL statement will be parsed and decomposed into a syntax tree through grammar parsing. Each node of the syntax tree corresponds 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 are correspondingly converted into relational algebra expressions from the leaf nodes, and then backtracking upwards. Finally, when backtracking to the root node, a complete relational algebra expression that can express the entire OCL statement is obtained. (32) The conversion algorithm from relational algebra to SQL statement is the same as (31) above. In this process, the algorithm will use the extended relational algebra grammar defined in the previous section to parse the relational algebra expression into an AST, and then backtrack from the leaf nodes to convert the AST into the corresponding SQL statement. In the (4), specifically comprising: (41) The main goal of the optimization algorithm is to avoid generating redundant items in the conversion process from OCL to relational algebra. The optimization algorithm is integrated into the conversion process from OCL to relational algebra. Specifically, after processing the current node, the algorithm does not immediately backtrack to the upper node, but first performs a redundancy detection. (42) In the redundancy detection process, the algorithm carefully checks the newly generated relational algebra expression for possible redundant calculations, unnecessary operators, or other forms of redundant items. If redundancy is found, the algorithm will rewrite or simplify the expression to remove these redundant parts.
2. A system for OCL to SQL code generation based on relational algebra as claimed in claim 1, wherein, Comprising: (1) OCL grammar definition module, design the context-free grammar of OCL, which describes the common semantic set contained in OCL invariants, which will be used to parse the input OCL statement and generate the corresponding relational algebra expression; (2) Relational algebra grammar definition module, design the context-free grammar of extended relational algebra, which describes the basic operators of relational algebra and additional operators added to meet the conversion process, which will be used for the generation of OCL to relational algebra and the generation of relational algebra to SQL statement; (3) Twice conversion algorithm module, design two conversion algorithms, respectively for OCL to relational algebra and relational algebra to SQL conversion process; (4) Twice conversion algorithm module, design optimization algorithm, which can detect whether redundant operators are generated during the translation from OCL to relational algebra, and simplify the results with redundancy; The four parts are OCL grammar definition, relational algebra grammar definition, twice conversion algorithm and relational algebra simplification algorithm.
3. A computer device, characterized by: The computer device includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the above computer program, the steps of the OCL to SQL code generation method based on relational algebra as claimed in any one of claims 1 are implemented.
4. A computer-readable storage medium, characterized in that: The computer readable storage medium stores a computer program for performing the OCL-to-SQL code generation method based on relational algebra according to any one of claims 1.
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