Wednesday, June 30, 2010

Using code generation under ClearCase

Those of you that know ClearCase also know that it requires the user to check out the file he wishes to edit before any editing can be done; kind of trying to edit a read-only file : you need to remove the read-only flag before anything else. Of course, this doesn't fit well with code generation tools such as Acceleo since the aim of such tools is to edit (or create) a number of files at once.

Until now that is :). Acceleo can now be used in conjunction with ClearCase or other pessimist-locking Version Control Systems (VCS). How does it work?

You might have noticed the "getGenerationStrategy()" method of the Java class we generate for your main mtl files; this is the method that matters for us. You simply need to change it from its default "return super.getGenerationStrategy()" to "return new WorkspaceAwareStrategy()" and you're done! Acceleo will now prompt the users of your generators to check out the files before attempting to modify them.

Two things to note :
  • You must change the javadoc of "getGenerationStrategy()" so that it doesn't contain "@generated" anymore : delete this tag, change it to "@not-generated", change it to "@generated NOT"... just don't leave this tag as-is or the Acceleo builder will override your change.
  • You cannot use this strategy in standalone mode, its goal is to integrate with the Eclipse workspace and its team providers.
The use case with ClearCase or pessimist locking VCSs is obvious, but you might realize that there is more to it that just these. You could be trying to regenerate a file that you set as read only, a file under SVN that requires a lock...

For example, with SVN you can set the property "svn:needs-lock" on a file to prevent any edition without prior locking. If you attempt to regenerate a file with such a property set with Acceleo, here is what you will get :

"java.io.FileNotFoundException: [...]\Library.java (Access denied)"

Pretty mean, huh? If you make use of the WorskpaceAwareStrategy though, you will get this instead :

All you have to do is to tick off the files for which you wish to take the lock. Files you do not check will not be locked for edition.

Friday, June 11, 2010

Package with URI '*' not found

I believe most of the users of EMF (developers or adopters alike) have seen this message at least once; and most probably wondered what was going on :).

This error message indicates that the package which NsURI is '*' hasn't been registered in the Package registry. Most of the time, that means you either a) launched your program standalone and didn't register the package correctly or b) you haven't installed the plug-in that provides that metamodel.

With Acceleo, encountering this exception is even more likely than normal, as the launch configuration for our generations allow for both standalone or plug-in generations. Selecting standalone without registering the needed package will inevitably lead to this error.

Solving it is easy : it is a matter of registering the needed packages (and, optionally, resource factories). How is it done? Here is the most classic example with UML :

Package with uri 'http://www.eclipse.org/uml2/2.1.0/UML' not found.

What do I need to do for my UML model to be loadable :

EPackage.Registry.INSTANCE.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
Resource.Factory.Registry.INSTANCE
.getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);


The same goes for every metamodel you might need, simply change UMLPackage by XxxPackage according to your metamodel. The Resource Factory is mandatory for UML, but your metamodel might not need one; simply ignore this line if you don't have a custom factory.

You need these two lines to be before the point where your model is loaded. For Acceleo, this is done in the generated Java launcher : simply change the implementation of the registerPackages and registerResourceFactories method to add these needed lines.

Tuesday, June 8, 2010

New Acceleo homepage

The Acceleo homepage now makes use of the template provided by Heiko Behrens.

You've seen it with MoDisco, you've seen it with Xtext, it's now our turn to change skin :).

This also makes use of the changes brought to the CSS by Fred Madiot.

Thanks to both of them !

Tuesday, June 1, 2010

What's new in Acceleo 3.0?

Acceleo 3.0 is on its way out. It is currently accessible in its third Release Candidate (RC3) version. The complete list of improvements and bug fixes that have been implemented in this version since the last stable release (version 0.8.1, see my previous post for explanation on this branch's version numbering) is humongous; following is the list of the most important ones. I may be succinct on the description of some of these, but you can get a full explanation of all of them in the Acceleo documentation, namely the User Guide. Be warned though, this will be a huge post :).

Compatibility and upgrading
  • Acceleo 3.0 is compatible with the three latest releases of Eclipse; Eclipse 3.4 (Ganymede), Eclipse 3.5 (Galileo) and Eclipse 3.6 (Helios). However, there are some limitations to this compatibility we maintain due to the changes that were implemented in OCL since then. For one, Acceleo modules that compile in Helios may or may not compile in Galileo and Ganymede (this will depend on the OCL features you make use of). However, Acceleo modules that compile in a given version will always compile in the subsequent Eclipse releases. Likewise, a module that has been compiled in Helios may be launched in Ganymede regardless of whether it would compile or not in that version, as our runtime shouldn't suffer from OCL breakages and improvements. All information about the compatibility will be maintained on the wiki.
  • We provide migration facilities for those of you that own Acceleo 2 templates. Specifically, we allow for the automatic migration of Acceleo 2 generation projects to Acceleo 3 projects. The automatic migration takes care of everything that could possibly be automated, leaving for you to fix no more than the Acceleo 2 features that don't have an identified mapping to Acceleo 3, or those few features that require a human hand to convert intelligently. The team is at your disposal for help if there still remains things you don't know how to convert to Acceleo 3 on the usual communication channels : the M2T newsgroup, mails, comments on this blog...
Tooling improvements
New views have been introduced to help you on several aspects of the generation cycles : module definition, launching, debugging. I'll detail each of these views one after the other.
  • First of all, the Result view allows you to get feedback on the traceability information we could retrieve during the evaluation of your modules. As long as this view has been opened, Acceleo will record every bit of information it can to synchronize your generated text with the generation module and source model. Ever wondered why such or such portion of text has been generated one way or another? What part of your generation module has generated the name of such or such method? From which element of your model has been generated such or such import? This is the view you can use to retrieve these pieces of information.
  • Next comes the Overrides view, which can be used to easily create templates or modules overriding other that are present in your instance of Eclipse. It will display every single module located either in your installed plug-ins or your workspace. All of these can be browsed down to the template level and a check box allows you to select those you wish to override. Once selected, you simply have to use the content assistant in the Acceleo Editor (CTRL+SPACE by default) and validate selected overrides for all of the overriding templates to be added to your module.
  • Last but not least, the Generation Patterns view has been introduced to help you implement behavior for an interface/class and all (or a subset of) its subtypes. For example, let's say you wish to implement a template named javaName which will generate the name of any classifier. This template will need some default behavior for interfaces and specific behavior for some classifiers. Open the Generation Patterns view, tick [template] for all selected types in its top part, and all of the types you wish to generate a template for in its bottom part. Once done, the content assist in the Acceleo editor will give you the choice [template] for all selected types which you just have to choose in order to generate all of the templates you asked for. Now that you no longer risk forgetting one or more subtypes, it is left to your discretion to implement the behavior for each of them.
  • Acceleo now provides a built-in profiler that can be used to keep track of evaluations and identify (and, hopefully, fix) bottlenecks in your generation process. The profiler is accessible through a right click on the module which execution you need to profile and the menu item Profile As => Acceleo Application.
  • The Acceleo debugger now provides you with the list of available variables at a given point in the evaluation process, along with the values of these variables for a given stack frame. Moreover, the displayed stack frames now represent Acceleo module element corresponding to your current expression stack. As for the previous release, debugging templates only requires you to set breakpoints in the Acceleo modules as you would usually with Eclipse : open an editor, double click in the left margin on the line you wish the breakpoint to be set, and launch the generation through the menu Debug As => Launch Acceleo Application.
  • Whenever possible, the warnings and errors Acceleo logs when an evaluation fails now sport the expression stack (with the lines they're at in the module files) instead of a stack trace corresponding to the Java code that was being executed at the time. This will allow for way easier debugging that ever before.

Editor Improvements
Apart from the performance improvements and minor bug fixes, the Acceleo module editor now provides refactoring actions and quick fixes.
  • First of all, you can now use the “usual” shortcut ALT+SHIFT+R to rename either modules, templates, queries, variables, ... in a coherent manner. This very same action can be accessed from right-clicking and selecting Refactor => Rename. The interface will allow you to preview the changes before validating them.
  • Portions of the module elements can be extracted as new templates through right-clicking and selecting Refactor => Extract Template (or simply using the shortcut ALT+SHIFT+T). Do note, however, that the selected text needs to be consistent. You cannot randomly select text that spans blocks of code and expect an adequate result.
  • The module editor now highlights all occurrences of the selected element when you click on it. For exemple if you place the cursor on the name of a template, all invocations of this template will be highlighted and markers will appear on the right ruler of the editor to indicate them.
  • Acceleo now provide quick fixes for some of the syntax errors detected in the editor, notably the possibility to create template or queries of a corresponding name.
  • It is recommended to use a bottom-up approach when developing generators ; Acceleo now eases this process by allowing you to initialize the content of a generation module with an example, and offering specific actions to transform these examples (static text) in dynamic OCL expressions. For example, changing all occurrences of MyClass to [c.name.toUpperFirst()/] in a single click.

Improvements and simplifications of the language
  • The MTL specification defines a standard library that offers a set of utility operations; in addition, Acceleo provides a non standard library to provide operations that greatly simplify the language and allow you to write more concise expressions, but are not part of the specification. This library has seen many additions since the 0.8 version, with things like Collections::sep(String) to insert a separator in-between every element contained by a collection, String::matches(String) to check whether a given String matches a regular expression, Collection::filter(Type) that can be used to filter out of a collection all elements that are not instances of a given type, Collection::reverse() to reverse the content of a given Collection... Take a look at the Acceleo documentation for a full reference of both standard and non standard library, along with the reference of the OCL standard library.
  • Acceleo allows you to alter the build mode, either Strict or Lax MTL compliance. Strict compliance meaning you'll get a warning for each use of a “non standard” operation.
  • Those of you that are familiar with the 2.x stream of Acceleo will be pleased to know that Acceleo 3 now provides the same post-processing facilities as its ancestor through the addition of a post directive on templates. Which allows for the modification of the text generated by a template “after generation”. For example, post(trim()) allows you to properly indent the content of the template while still leaving you the possibility to trim all unnecessary white spaces from the generated text.
  • The “+” operator can now be used whenever one of the operands is a String for concatenation, in order to avoid the verbose concat(String) OCL offers.
  • You can now use properties files to customize your generations through the addition of 4 variants of a getProperty operation in Acceleo 3. Once again, the operation reference can be looked at for the description of those services.
  • We made use of the facilities OCL provides in order to further simplify the navigation language : iterators of our [for] blocks are no longer mandatory (they'll be implicitly declared if needed and self can be used in their stead), you no longer need qualified names for your types when the metamodel is declared on the module header (or if it can be inferred from another of the declared metamodels), casting through oclAsType for all redundant expressions is no longer necessary...
  • For those expressions where OCL doesn't allow us to simplify the language, we added non standard operations. For example, sequence->select(oclIsKindOf(Type)).oclAsType(Type) is obviously redundant, but OCL doesn't allow for this expression not to define the explicit cast. In Acceleo, you can now write sequence->filter(Type) instead (see also my post on this specific issue).
  • Acceleo now allows for control characters (\n, \t, \'...) in OCL expressions.
  • Acceleo [for] blocks now declare an implicit “i” variable that can be used to retrieve the iteration count. For example, [for (Sequence{4, 5, 6})][i/], [/for] will generate 1, 2, 3.

Documentation
As advertised by Jonathan in one of his latest posts, the Acceleo documentation is complete and can be accessed from the integrated Eclipse Help through Help => Help Contents, in the Acceleo Model To Text category. We provide everything you need in order to get started with Acceleo; quick start, tutorial, user guide... and the full reference of OCL standard library and Acceleo standard and non standard libraries complete with explanations of the operations and examples.