Friday, January 29, 2010

How to create an EMF model from a Java application ?


Is it possible to reuse EMF-based tools with Java source code ? Yes if you have your Java source code translated into an EMF model.

This is the aim of the Java component provided by MoDisco. This component is composed of a ECore definition of a java application and a discoverer which creates EMF models from an EClipse project containing Java source code.

The ECore definition declares all the concepts of the Java language : Package, ClassDeclaration, MethodDeclaration, Parameters, VariableDeclaration, IfStatement, Assignment, CastExpression, LineComment, ...

To create an EMF model using this metamodel, select your project and right-click on the menu "MoDisco>Discover Java model from Java project...". A wizard appears to select the analysis options :
  • You can extend the analysis to dependencies (the JAR used by the project).
  • You can use an incremental mode if you need to optimize the memory usage
  • You can limit the analysis to declaration members if you don't need a model of the instructions contained within the methods
  • You can use a filter to exclude Java classes during the analysis

The result is an EMF model which can be opened with the EMF reflexive editor or with the MoDisco generic browser. This browser allows accessing directly to instances of a specific class (for example all the MethodDeclarations) and navigating through all the references (composed or not).

This model can be used with other EMF-based tools to check architectural or coding norms, to generate other models (UML or Domain-Specific) or to regenerate an other application (if you modify the Java model, MoDisco provides templates to regenerate Java source code).

17 comments:

  1. hi
    does anyone know if there is a way to programmaticly generate the java model from a folder containing java source code?
    Cheers B

    ReplyDelete
  2. Hi,

    Here is an example of code showing how to programmatically create a java model from java source code :

    // Create a discoverer for a Java project
    DiscoverJavaModelFromJavaProject javaDiscoverer = new DiscoverJavaModelFromJavaProject();

    // Parameters of the discoverer
    Map javaDiscoveryParameters = new HashMap();
    javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_SILENT_MODE,true);
    javaDiscoveryParameters.put(DefaultDiscoverer.PARAMETER_BROWSE_RESULT,false);

    // Execute the discoverer (javaProject is a IJavaProject)
    javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);

    // Get the result model
    Resource javaModel=(Resource) javaDiscoveryParameters .get(DefaultDiscoverer.PARAMETER_TARGET_RESOURCE)

    If you need help, you can post your questions directly on the MoDisco forum :
    http://www.eclipse.org/forums/index.php?t=thread&frm_id=21&

    ReplyDelete
  3. Hi Fred,
    I used your example and got it to work in a simple EclipseApp setup...
    Unfortunatly there is a problem with the
    javaDiscoverer.discoverElement(javaProject, javaDiscoveryParameters);
    part:
    line 187 from the "DiscoverJavaModelFromJavaProject" class
    ===resource.getContents().add(model);===
    keeps throwing an Exception:
    java.lang.ArrayStoreException: org.eclipse.gmt.modisco.java.emf.impl.ModelImpl

    and after that in line 218 inside the catch

    "JavaActivator.getDefault()" returns null...

    do you have an idea why this could be the case?

    Thanx in advance
    Cheers
    Björn

    ReplyDelete
  4. Fred, thanks for this blog. Is it possible to do something similar for sql. In other words is there ECore definition for all sql language.

    I have need to reverse engineer oracle sql code and convert it to a different model(piglatin). Is it doable.

    ReplyDelete
  5. Hi Ganesh,
    Yes it is possible to do something similar for sql, but it is not provided by MoDisco ... yet.

    There are two things to do:

    1) Define an ECore model for sql. Depending on what you want to reverse-engineer (schema, queries or data), the model will not be the same. You can find here a basic metamodel for Database Schema : http://www.emn.fr/z-info/atlanmod/index.php/Ecore#RelationalDBSchema_1.0
    Or you can reuse the KDM model which contains a Data Package defining classes such as RelationalSchema, RelationalTable, UniqueKey, ReferenceKey, etc). This model is defined by the OMG and an implementation is provided by MoDisco (http://wiki.eclipse.org/MoDisco/Components/KDM).

    2) Develop a discoverer to create a model from existing source code. If you want to create a model from the schema definition, you have two possibilities: parse the schema definition (using xText for example ?) or use JDBC to get the schema definition directly from an instance of the database.

    If you develop such a component, it would be great if you could contribute it to MoDisco ;-)

    ReplyDelete
  6. Thanks for the prompt response. I would like to model sql queries and generate pig latin/hadoop code from it.

    I will definitely contribute to modisco if i am successful. Can you refer to a simple example for the two steps you mentioned in this blog.

    ReplyDelete
  7. So you need a IJavaProject, do you need any way to simulate that and execute it outside Eclipse?

    ReplyDelete
  8. Hi Fred, could you help me by providing me the java metamodel because i need it to create a model for a java application that i have its source code.
    thanks
    Mahmoud

    ReplyDelete
  9. very informative blog and useful article thank you for sharing with us , keep posting Ruby on Rails Online Training Hyderabad

    ReplyDelete
  10. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    internships provider in khammam
    internships in khammam for cse students 2019

    ReplyDelete
  11. Respect and that i have a dandy supply: Where To Buy Houses That Need Renovation total home renovation

    ReplyDelete