GenMyModel + Github + JMerge = (Generating -> Coding)*

Without any doubt, automatic code generation from models is a strong feature allowing to save time and to keep consistency between models and code. One of the feature proposed by GenMyModel is to generate java source code from your model into Github repositories (config is accessible from “Tools->Generate->Generate Configurations…”). Here, we show how GenMyModel generation capabilities can help you during your project development.


runconfig

This way, GenMyModel is considered as a contributor to your java project. And, if it is considered as a contributor, it should act like one, i.e, it should introduce novelty without blowing out your work. To do so, the GenMyModel engine takes advantage of eclipse JMerge technology.

JMerge had been developed for java files merging purpose and is extremely useful when code generation is involved. The code merging is based on annotations placed on top of java basic blocks (class, attributes or methods). These annotations indicate whether the block content is generated and whether it must be re-generated. As example is better than precept, here are two small java codes before their merging.

Input Example

[java title=”MyClass.java (generated)”]
package example;

/**
* @generated
*/
public class MyClass
{
/**
* @generated
*/
public int count;

/**
* @generated
*/
public int getCount() {
return count;
}

/**
* @generated
*/
public void countUpdate(int value) {
this.count += value;
}
}
[/java]

[java title=”MyClass.java (modified)”]
package example;

/**
* @generated
*/
public class MyClass
{
/**
* @generated
*/
private int count;

/**
* @generated NOT
*/
public int getCount() {
return this.count * 2;
}

public String toString() {
return this.count.toString();
}
}
[/java]

The first java class is generated by GenMyModel whereas the second is the result of a previous generation manually modified. The code merged by JMerge is the following:

Merge Result

[java title=”MyClass.java (merge result)”]
package example;

/**
* @generated
*/
public class MyClass
{
/**
* @generated
*/
public int count;

/**
* @generated NOT
*/
public int getCount() {
return this.count * 2;
}

public String toString() {
return this.count.toString();
}

/**
* @generated
*/
public void countUpdate(int value) {
this.count += value;
}
}
[/java]

What Happened?

Here, we can have a good overview of JMerge behavior. New annotated blocks generated from the model are simply inserted into the class (e.g., the “countUpdate” method). The content of annotated blocks already existing in the modified class with the exact same annotation are replaced by the generated content (e.g., the “count” attribute, the private visibility had been replaced by the public one). However, the content of blocks annotated by “@generated NOT” are let unchanged (e.g., the “getCount” method). Finally the non annotated methods are preserved by the merge. JMerge is really flexible and allows you to easily code without the fear of losing work.

Coding using both git and JMerge enables you to update your model and to use the following development schema:

1. Model your project
2. Generate Java code to your Github repository
3. Fill the blanks and code
4. Update your model
5. Re-generate Java code to your Github repository
6. … etc, loop over 4 and 5 until your project is done
So, GenMyModel + Github + JMerge = (Generating -> Coding)*

Working with git and JMerge, GenMyModel enables a powerful and flexible way of incrementally coding using models in the cloud. As some of you probably know, JMerge is currently designed to work into an eclipse context. In next article, I will explain how JMerge could be launched outside eclipse and give a small call example.

If you want to learn more about JMerge annotations used by GenMyModel, take a look at this little guide.

Love,

me
Vince
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

About the author

vince Team

Music, cards and beer lover...

Also PhD in Computer Science specialized in software modeling, code generation and model driven development. Interest/work in code generation, reverse engineering techniques, interpretation and compilation.