IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Model Driven Engineering

    MarkNV发表于 2013-11-01 17:37:00
    love 0

    1. Introduction

    A broad view of MDE (Model Driven Engineering) is transforming models of software into code.

    What are models? In software engineering, generally, graphs, all kinds of graphs. E.g. finite state machine (FSM), message sequence charts, logic circuit diagram, data flow model, petri net, network architecture, etc.

    MDE is a fancy database application.

    Recall MDE is about drawing all kinds of graphs then transfer them to code. In fact, any model could be mapped to a database, i.e. tables, constraints, thus, any model is intrinsically a database which it can be transfered to.

    Take class diagram to database as example, however, some things may be a slightly different when mapping class diagram to schema.

    • Databases do not allow set values, so set value attributes are mapped as foreign keys in database (* side hold 1/0..1 side).
    • Table inheritance: created a table for each class except abstract classes, then propagate inherited attributes to tables, ID is defined by root class.
    • Static members are ignored.
    • Methods are ignored.
    • Interfaces are ignored.

    Therefor MDE to users means draw a bunch of graphs, hit the button then programs are automatically generated. To MDE tools developers (us), we write program which is the business logic behind the magic button.

    2. Meta Models

    When we use tools such as Microsoft Visio to draw a graph, what happens beneath the surface? For example, if we draw a FSM diagram as bellow:

    fsm

    The meta-model of this fsm is:

    meta-fsm

    Thus it means we can represent every FSM as a database of tables.

    Schema:

    schema-fsm

    After we fill the tables with tuples, then we get a FSM. That is to say define a meta-model first and we could draw infinite FSM diagrams, each of them is a database. Sounds like class and meta-class? Same thing indeed.

    3. Constraints

    What if I draw an arrow from stop node to start node, which is not valid in FSM. Thus, we need constraints!

    Best practice: Prolog. A declarative language. Still take FSM diagram above as example:

    % databases
    dbase(fsm,[node,transition]).
    
    table(node,[nodeid,name,type]).
    table(transition,[transid,startsAt,endsAt]).
    
    node(nStart, start, start).
    node(nWakeUp, wakeup, state).
    node(nWork, work, state).
    node(nChill, chill, state).
    node(nTimeForMeal, timeformeal, state).
    node(nEat, eat, state).
    node(nTimeToBed, timetobed, state).
    node(nSleep, sleep, state).
    node(nStop, stop, stop).
    
    transition(t1, nStart, nWakeUp).
    transition(t2, nWakeUp, nWork).
    transition(t3, nWakeUp, nChill).
    transition(t4, nWakeUp, nTimeForMeal).
    transition(t5, nWork, nWork).
    transition(t6, nWork, nTimeForMeal).
    transition(t7, nWork, nChill).
    transition(t8, nWork, nTimeToBed).
    transition(t9, nChill, nChill).
    transition(t10, nChill, nTimeForMeal).
    transition(t11, nChill, nWork).
    transition(t12, nChill, nTimeToBed).
    transition(t13, nTimeForMeal, nEat).
    transition(t14, nEat, nWork).
    transition(t15, nEat, nChill).
    transition(t16, nTimeToBed, nSleep).
    transition(t17, nSleep, nStop).
    
    % Constraint a: The attribute 'nodeid' in node table must be unique
    error :- findall(X, node(X, _, _), L), not(is_set(L)).
    % other errors etc.
    constraint :- not(error).
    

    Prolog can be used to:

    • Define databases.
    • Define and evaluate constraints on databases.
    • Can also be used to define transformations from one database to another. (Discuss later)

    4. Transformation

    There two kinds of transformations in MDE:

    • Model to Text transformation (Code generation).
    • Model to Model transformation (Refactoring or map to another kind of graph).

    M2T in practice we use modified velocity template engine to write translate rules.

    M2M in practice we use prolog itself.

    The class diagram of code generated by M2T using prolog and velocity is as bellow:

    code-fsm

    5. Bootstrap MDE

    Like compilers, e.g. the first compiler of python can not write in python, but we could use C. After that we could write compilers using python itself to compile python, e.g. PyPy and CPython.

    The thing is the same in MDE tools.

    Note: There is no infinite recursion, because meta-model's meta-model (meta-meta-model) equals meta-model itself. Therefor it end up a loop.

    6. Recall

    MDE is about databases.

    M2T Procedure:

    1. Graph is translated into a set of tables. (Prolog)
    2. Meta-model constraints applied to validate or report errors. (Prolog)
    3. If no errors, code is generated by table-to-code scripts. (Velocity)

    Like compilers, we could use MDE tools to build MDE tools themselves.


    Acknowledgement:

    This article is my learning notes of course CS392F Automated Software Design, lectured by Prof. Don Batory. The core idea of this course is how to write programs to generate other programs in a scientific way, that is, not by hacking.



沪ICP备19023445号-2号
友情链接