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.
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.
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:
The meta-model of this fsm is:
Thus it means we can represent every FSM as a database of tables.
Schema:
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.
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:
There two kinds of transformations in MDE:
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:
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.
MDE is about databases.
M2T Procedure:
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.