[Model-driven software design] "Domain Architecture" code generation technology

      It is now time to present those recognized techniques as the basis for selecting or building MDSD tools - those aspects that can be decomposed from the domain architecture because of their more general nature. But a domain architecture cannot do without them: code generation techniques are an important foundation.

1. Code generation - the reason for choosing it

      In MDSD, there is a close connection between modeling, code generation, and architecture development—for example, architecture-completion code for an MDSD platform can be generated from a DSL.

1. Performance

          In many cases, because someone wants to achieve a certain level of performance while maintaining a certain degree of flexibility. Considering the performance they can achieve, traditional object-oriented techniques, such as frameworks, polymorphism or reflection, are not always sufficient. Using code generation theoretically dictates configuration -- which is where its flexibility lies -- and also generates efficient code.

2. Code size

         Another reason for choosing to use code generation is code size. If it is known at compile time which features you need at runtime, the generator only needs to put those parts in the code. This can help shrink the image. And vice versa, artifacts that overextend the source layer greatly magnify the image.

3. Analyzability

      Complex, general-purpose architectures or runtime systems tend to relocate programming language-related complexity on a proprietary configuration layer. They usually make heavy use of interpretation, which hampers the possibility of static analysis of program properties and occasionally impairs error detection. Instead, the generated programming language source code has the ability to analyze hand-written code.        

4. Early Error Detection

       Flexible systems often use weak typing to allow decisions to be made at runtime.

5. Platform Compatibility

        A typical example of using code generation in the context of MDA is to program application logic independently of the platform of implementation, which makes it easier to migrate to newer, potentially better platforms.

6. Language restrictions

       Most programming languages ​​have inconvenient constraints in their expression, and code generation can be used to bypass them.

7. Modular unit

        Cross-cutting features of a system, such as logging or persistence, are generally implemented locally—that is, not scattered throughout the application—through code generation.

8. Self-measurement

        The issue of self-measurement must be mentioned here. Self-measurement describes a program's access to itself. This allows programs to obtain information about themselves, for example about classes, their attributes and operations.

2. Classification

1. Metaprogramming

      Code generators are metaprograms that take specifications as input parameters and generate source code as input.

2. Separation/mixing of programs and source programs

      In the case where metaprograms and base programs are mixed, there is a common programming and metaprogramming language, and the source code components are not separated but mixed.

       Mixing programs and metaprograms is a very beneficial approach. However, the resulting system tends to be extremely complex, so its relevance to mainstream software development is very limited. The hybrid approach further implies that the target language is no longer a parameter for code generation.

     If programs and metaprograms are separated, the creation of the system is done in two phases. The metaprogram is run and creates the base program as output, then terminates. The program is unaware of the existence of the metaprogram. This spacing can be maintained through metaprogramming processes, including build processes.

     The method of separating meta-programs and base-programs does not support the meta-object protocol, but because of its lower complexity, it is more suitable for typical architecture-centric model-driven software development.

3. Implicit and explicit integration of generated and non-generated code

         The code produced by the implicit integration of these two program types already constitutes a kind of hybrid of generative and non-generative diamanté. As a result, there is no need to worry about the integration of the two classifications anymore.

        In other cases, the generated code is initially independent of the handwritten code sections. The two codes must be integrated in an appropriate way.

4. Relationship

       In general, there is a relationship between two aspects in which generators can be classified:

  • Generators that separate programs and metaprograms are typically used to create code that is separate from manually created code, and that code must be integrated by hand.
  • Generators that mix programs and metaprograms don't need this manual integration --- the generators already create the combined system.

5. Example of mixing program and source program

       The C++ preprocessor is a system of hybrid programs and metaprograms. The languages ​​used here are independent of each other: it is also possible to use the C++ preprocessor for other programming languages, since it processes the source code literally, and since the system is based on macro expansion, the preprocessor generates integrated generated source code for code and hand-created code.

3. Generation technology

          Classification of generators as follows:

1. Templates and filtering

        This generation technique describes the simplest case of code generation. Templates are used to replicate relevant parts of a textually represented model, for example via XML using XSLT.

      Generation using templating and filtering is fairly straight-forward and robust, though stylesheets can quickly become very complex.

2. Templates and Metamodels

       To avoid the problem of generating code directly from the model, it is possible to implement a multi-stage generator that first parses the XML, then instantiates a metamodel, which is finally used for generation along with templates.

3. Frame Processor

       Important Elements of a Frame Processor A frame is basically a specification of the code to be generated. Similar to classes in object-oriented languages, frames can be instantiated multiple times. During this instantiation, variables are bound to concrete values. Each instance can have its own slot values, just like classes.

4. API-based generator

       Probably the most popular types of code generators are API-based. They simply provide an API with which elements or languages ​​for the target platform can be generated. Conceptually, these generators are based on the abstract syntax of the target language and thus always target a language, or more precisely the abstract syntax of the target language.

5. Inline generation

      Inline generation refers to the case where "regular" source code contains artifacts of more source code or byte/machine code generated during compilation or some kind of preprocessing, such as C++ preprocessor directives or C++ templates.

6. Code quality

      Moving on to another mechanism that is very popular in the Java world: code quality. In Java, these are first used by JavaDoc, where special comments are used to make it possible to generate HTML documentation yourself. The extensible JavaDoc architecture makes it possible to plug in custom tags and code generators. XDoclet is perhaps the most popular example.

7. Code Interleaving

      Code interleaving describes the mixing of separate but syntactically complete and independent code segments.

8. Combining different technologies

      It is also possible to combine different code generation techniques. Open source tools create source code from templates. This source code also includes code quality. Generating this code based on templates takes place using Velocity, and is otherwise handled with XDoclet.

     Another popular combination is API-based generators, which can optionally read templates to simplify working with APIs.

9. Similarities and differences between different methods

      First, the different approaches are categorized based on criteria. Many other features, differences and commonalities need to be brought up. In principle, framework processors and API-based generators will build an AST of the system to be built.

10. Other systems

     Many other systems exist in the area of ​​code generation. However, they have little to do with MDSD.

  • The meta-object protocol allows access to compiler structures.
  • A number of tools exist primarily in the Java context that allow modification of the generated byte code.

Guess you like

Origin blog.csdn.net/zhb15810357012/article/details/131216069