Wednesday, October 5, 2011

Generation Gap Pattern vs Protected Regions in Xtext MDD

During the development of the awesome Xtext Protected Regions Support created by Daniel Dietrich, Daniel asked a really interesting question:

Just interested in your opinion / your 2 cents:

It's best practice to separate generated from non-generated code (when it comes to put it in a versioning system).
Therefor people use the generation gap pattern - the generated class is a base class (or s.th. similar), the manual written / generated-once classes implement the generated classes.

When using protected regions, all files (including the generated) have to be checked in. Some say, this ends up in a versioning disaster.

I prefer the protected regions approach. the base classes of the ggp nearly double the count of classes. this is a technical vehicle which makes no sense for the application. but how could be avoided, that generated classes are checked in?

my only answer is, that the generated code has to be cut of the files. empty files will disappear, protected regions are preserved. when checking out files, the generator has to be started. before checking in files, the generated code has to be stripped (into a special dir which will be checked in?).

this sounds like a technical vehicle, too. would that make sense?

To which replied: (note that these are my opinion, and I don't claim to be an expert, so feel free to challenge my assumptions and prove me wrong!) :-)

I have nothing against GAP. It definitely has its valid uses. So for certain problems, GAP is a valid solution, and is even preferable to regions.

About versioning, I'm not sure it's a "disaster". Redundant maybe, but dangerous? I don't think so. Avoiding to checkin generated files maybe preferable where the source DSL files are "authoritative" and the target files are "final".

Let me illustrate: wsdlimport. The WSDL file is an authoritative source. The generator is stable. Generated Java Proxy Classes are "dumb" target files. You can regenerate those anytime, no need for customization. And the generator is never changed so you can be sure generated files will actually compile and work. No need to checkin the Java targets.

Xtext can be used for projects of that kind, and very easy for that.

There are also projects that fall in the middle: need some customization. So you do GAP: generate the base class and the customizable subclass. This technique is excellent for some cases, but fails when:

  1. You're restricted by the class hierarchy in some way.
  2. Target language doesn't support subclassing, say: XML. Or my case, this would be yet another custom DSL.

There are also cases where GAP technically works, but the generated class structure is complex enough that separating the base class from the actual class makes it "unnatural". I'm sure you've seen stuff like that. Oh, let me give a concrete example from one of my prototypes:

def StringConcatenation genMainFile(String fileName) { val result = new StringConcatenation() result.append( augmenter.beforeMainFile(fileName) ) result.append( augmenter.aroundMainFile(fileName, [fn1 | { doMainFile(fn1, [ fn2 | augmenter.innerMainFile(fn2, [fn3 | { genImportBlock() genClassBlock() null }]) ]) }]) ) result.append( augmenter.afterMainFile(fileName) ) result }

Mind you the above code actually works, but it's damn ugly! At least with protected regions there will only be (marker) comments. And with most editors, comments can be folded and not so distracting, so it's much more bearable.

Some may say "it's only the base class, nobody will touch it". On the contrary, you'll see those structural methods on your debugging stack traces, just "perfect" at the time when you're in deep need of clear & cohesive program structure, but oh... you're buried in nested method calls. :-( AspectJ/AOP/weaving also has this problem, though it's manageable to some extent. They only fall apart when pushed too far, so moderate use is OK.

The third class is projects that require extensive customization. The source DSL only comprises of ~20% of the target, providing structure or supporting form, and gives places to fill, where these are filled by the programmer or some other DSL.

In my (currently hypothetical, but hopefully realized soon) the Entity->UI generator will not generate JSF/GWT forms/pages directly, but generate to an intermediate UI DSL. (you can argue this is Model-to-Model, not M2T, but hey, textual models are much easier to inspect/hack than something buried in XML!) The UI DSL can then be processed to generate JSF or GWT.

I'm not really interested in supporting class inheritance in a UI DSL. In fact the "class" concept itself may not exist in a UI domain, it only matters to OO world. So protected regions is the only option, and thankfully Xtext supports comments by default.

With that, it's possible to customize the generated UI DSL files right in the places where they're needed.

Another use case that I'm exploring, is two or more generators (which may or may not be sourcing the same model) generating to the same file, but in different regions. And the FSA should automagically merge them.

And then, add to the mix that the generators themselves are in constant development. That means the same source DSL when processed, may yield target files that are broken, uncompilable, buggy, etc. And the target files are needed because they form the foundation of yet another project, so unless the previous "working" target files can be recovered, the development of the derived project effectively stops.

For those uses cases, would I checkin the target files? Of course.

With all of the above said, I have nothing against GAP or un-checkin generated files. GAP & un-checkin may be common, but I believe there are classes of problems where they're inappropriate.

To learn Modeling with Eclipse Modeling Framework (EMF), I highly recommend the book EMF: Eclipse Modeling Framework.

11 comments:

  1. thx mentioning this project.
    2 years ago I blogged about these things:
    http://ekkes-corner.blogspot.com/2009/05/new-ways-around-generation-gap-pattern.html

    ekke

    ReplyDelete
  2. Concise package money, bare nothing, but it is a brand of coat of paint material light, the super invincible joker, very street van Cambridge satchel bag.
    Bag bag tassel part is made of genuine leather oh, very thick soft, package itself is the quality of the skin of PU, quality is good. Much super tassel walking around up born swaying posture, very attract eyeball oh.

    Europe and a bag of wind, appearance is very simple, less is more big oh. Bag body skeleton complex moment modelling, a bit cool taste.
    Give people a feeling of very warm is the purpose of Cambridge satchels, this season it continued the popular bright colour elements, such as pink fluro and yellow fluro. It is so fashion and beautiful. You can also embossing your names on your bag, you will like it very much.

    ReplyDelete
  3. awesome post... will help me a lot,it is Exactly what I was looking for! Thanks.

    ReplyDelete
  4. The next class Cheap RS Goldis tasks that want intensive personalization. The source Digital subscriber line simply includes ~20% with the focus on, offering composition or supporting variety, and offers areas in order to fill up,Cheap MapleStory Mesos wherever these are loaded by the developer as well as other DSL.

    ReplyDelete
  5. It can be feasible to receive also comfy at the rear of the wheel and forget the motor vehicle can break down CNC-602A injector cleaner & tester at particular points. Should you have not ready adequately you then will conclude up which has a major problem with your palms.

    ReplyDelete
  6. But it’s tough to explain to window anytime upgrade key regardless of whether that outpouring of anger is simply the echo chamber amplifying a vocal minority or irrespective of whether windows 7 home premium product key it really represents a widespread adverse view.

    ReplyDelete
  7. Guild Wars 2 Gold So if you feel like me, a person scarcely have the period.Next: several things can easily separate people very, Buy D3 Gold quickly, even if the sport recently became available.

    ReplyDelete
  8. Microsoft Project 2010 Download includes a timeline view that is automatically displayed above other views, showing a concise overview of the entire schedule. You can add tasks to the timeline and even print it for an attractive summary report of the entire project. Or you can paste it into an e-mail for an instant report with no fuss.
    Microsoft Visio 2010 Download helps you make diagrams look attractive with a wide range of formatting tools and design options, including modern shapes and visuals, a rich gallery of themes, and Live Preview.
    Adobe Acrobat 9 Pro Download includes Adobe LiveCycle Designer ES software for advanced form creation.

    ReplyDelete
  9. Microsoft wrecked Novell, and Ballmer experienced a good deal to accomplish with that. When he was first assigned to go office professional plus 2010 retail pack up Microsoft, certainly one of Ballmer's very first orders of enterprise was addressing corporate customers and inquiring what they desired from Microsoft.

    ReplyDelete
  10. in constant development. That means the same source DSL when processed, may yield target files that are broken, uncompilableWorld of Warcraft Gold Billig
    World of Warcraft Gold Kaufen

    ReplyDelete
  11. Your blog is really nice. If I may share some insight, traffic studies on blogs show most people read blogs on Mondays. online-essay-writer.org So it should encourage blogger to write new updates over the weekend primarily.

    ReplyDelete