Monthly Archives: February 2009

Reading notes: Textual DSLs and eclipse modeling (part 2)

Part 1 of the notes is located at http://www.frankdu.com/weblog/archives/46

The relative presentation slide is located at openArchitectureWare.org.

23. In XText, you start to work with defining concret syntax.

24. For existing meta model, use importMetamodel directive. Use preventMMGeneration to prvent any meta model generation.

25. Simple Editor Customization in Xtext
- Xtend, expression language used throughout oAW
- Constraint checks: oAW check language, based on Xtend
- OutlineView customization: override label(…) and image(…) for meta types
- Content Assist
- customize the font style for keyword (keyword only)

26. Xtext instantiates Ecore metamodels, which means that it can be processed with any EMF tool.
- Within oAW workflow: the only Xtext-specific aspect is using the generated parser. Xpand template language is powerful code generation tool. Easily traverse the model/meta model using Xtend language
- EMF way: EMF’s native resource mechanism (what are the details?)
- Your own code: use the generated parser.

27. NodeUtil with generated parser
- Typically you only work with AST (ecore file)
- Help to obtain info from the parser tree: element location, element text, parser tree node at certain offset.

28. Two phases for doing your DSL:
- designing your language
- building language tools
Xtext focuses on the second phase. Except from the phases, it is also important to provide framework that run the tasks defined your DSL.

29. oAW Xtext become a part of TMF project. The first release is expected in later half 2009.

30. A Xtext parser limitation. It’s impossible to add custom action code in the parser. Sometimes it results in ugly meta models, especially with building expression languages.

Reading notes: Textual DSLs and eclipse modeling (part 1)

The relative presentation slide is located at openArchitectureWare.org.

Part 2 of the notes is located at http://www.frankdu.com/weblog/archives/52

Below are my reading notes for Textual DSLs and text modeling in eclipse. I haven’t finished the ppt slides. Therefore, this is only part one.

1. EMF servers as the foundation. It provides Ecore Metamodel and framework tools like:
- editing
- transactions
- validation
- query
- distribution/persistence

2. GMF is used for building custom graphical editors based on EMF meta models. It is industry-proven technology. Based on GEF.

3. TMF is used for building custom textual editors. It is in incubation phase. There are two implementations: Xtext and TCS.

4. M2M (Model-to-Model) delivers an extensible framework for m2m transformation languages. ATL is M2M language from INRIA. QVT is an implementation.

5. M2T (Model-to-Text) focuses on transforming models into text (code generation, model serialization). For example, you may want to convert in-memory models into xml files for persistence/transportation purposes. You may want to use a parser to convert xml files back to models. There are 2 so-called frameworks:

- JET is code generation tools that are used by EMF
- Xpand is code generation tools that are part of M2T releases.

6. Xtext is originally from openArchitectureWare.com. It’s a good integration with eclipse. The oAW uses EMF as a basis, bases graphical editors on GMF, and all tooling are based on eclipse. Since Xtext has become part of eclipse TMF, there are two versions of Xtext: oAW Xtext, and TMF Xtext. The former is relatively mature. The latter is under active development, and expected to be first released sometime this year, namely in 2009.

7. DSL is a focused, processable language for addressing specific concerns in a specific domain. It is targeted to be a simple tool for a relatively complex domain. Therefore, in most cases, DSLs are human-readable to domain experts without any training. The popular DSL examples are SQL and Excel.

8. DSLs can be classified in many ways:
- configuration vs. customization
- internal vs. external
- graphical vs. textual

9. Xtext is a so-called framework tool for building external textual customization DSLs.

10. Is it possible to edit same model with both textual and graphical editing interfaces?
It might be possible. Consider one of the following: a. Visualize a subset of the model, using graphvis or prefuse. But it is typically read-only. b. Use different perspectives. Some of them use graphcial editor. It requires cross references between textual and graphical models). c. Edit the same model textually and graphically. Textual format is used as the serialization format from the graphical model. It requires writability and sync of both models!

11. Typically textual DSLs leverage one of many parser generators (ANTLR, Java CC, Lex/yacc). They help to generate a parser based on grammar definition. Consequencely, a parser tries to match text, and try to create a parse tree.

12. Typically, textual DSLs are transformed into an Abstract Syntax Tree (AST). It is ofen a binary tree. For exampe, the AST for 1 + 2*3:

Literal[1] AddExpression ( Literal[2] MultiplyExpression Literal[3])

Literal, AddExpression, and MultiplyExpression are binary nodes.

13. The AST can be taken as a model. But textual DSLs are written without careness of the AST. They can even be against AST.

14. Challenges in Xtext DSL implementation:
- Writting a parser is non-trivial.
- A parser generator makes life easier, but still not one for all.
- A parser generator only creates a matcher and/or a simplistic AST. You still need to further transform the model to easily processable form, and create an editor with syntax highlighting, code completion, etc.

Xtext is designed to ease unbearable burden of life like that.

15. Xtext is based on an EBNF grammar (what’s that? Why will it make a difference?). Xtext will generate:
- ANTLR-based parser
- EMF-based metamodel
- Eclipse editor with or extensible for: syntax highlighting, code completion, code folding, constraint checking, and so on.

16. Different Kinds of Xtext Rules:
- Type Rule
- String Rule
- Enum Rule
- Native Rule

17. Built-in Lexer Types in Xtext:
- ID
- STRING
- INT
- Comments ( Single line and multiple lines)
- Whitespace
The content of those rules is not transformed into the meta model. (How it matters?)

18. Built-in Reference Types in Xtext:
- Reference
- File Reference/Import

19. Abstract Type Rules are implicitly declared with a collection of OR-ed alternatives: R1 | R2 | R3. They will be mapped to abstract metaclass. The alternatives will become subclass. The common properties will be lifted into abstract superclass.

20. String Rules are declared in the format: String [rule_name]: [rule_definition];

21. Enum Rule is mapped to Enum in metamodel. Its format: Enum [rule_name]: [token_name="string"]+;

22. Native Rule. Example:
Native SL_COMMENT:
“‘#’ ~(‘n’|'r’)* ‘r’? ‘n’”;

Term:
1. EMF – Eclipse Modeling Framework
2. GMF – Graphical Modeling Framework
3. GEF – Graphical Editing Framework
4. TMF – Textual Modeling Framework
5. DSL – Domain Specific Langauge
6. JET – Jave Emitter Templates
7. AST – Abstract Syntax Tree

Continue to read: Part 2 of the notes is located at http://www.frankdu.com/weblog/archives/52

Eclipse: how to implement actions for annotation markers?

In the text editor, the markers are displayed on the vertical ruler. In Java source editor (CompilationUnitEditor class), you can click a problem marker, then a suggestion list will pop up. It’s pretty user friendly, isn’t it?

Then, how to implement our own clickable annotation marker? Generally the following steps address the issue:

  1. Add the extension org.eclipse.ui.editorActions.
  2. Add an editorContribution sub node. Config the contribution.
  3. Implement an AbstractRulerActionDelegate for the contribution. In its createAction method, return your customized action.
  4. Implement your customized action by extending SelectMarkerRulerAction. In the action, you may be interested to traverse through annotation model, to invoke the appropriate commands.

Examples are best tutorials.  Therefore, let’s take a look at Java source editor in org.eclipse.jdt.ui, step bey step:

1. The editorActions contribution in JDT UI plug-in:

Pay attention to actionID and targetID.

2. The AbstractRulerActionDelegate. It uses template pattern to let you create your own action:

3. The SelectMarkerRulerAction. It is needed to access the annotation model. If you don’t need to access annotation model, of course you can use other actions. The Java editor version is JavaSelectAnnotationRulerAction. It contains complicated relations. Therefore, to make it simple, here I paste my simple version:

Hope the example above helps.

How to open a perspective and view programmatically in eclipse?

It is very straightforward. Though there are other ways, I will only present one way for now:

Please note: Of course you need to add the error handling code as prompted by eclipse.

How to invoke an action from eclipse Welcome page?

Let’s start from the template intro project. In the root.xhtml file, add a link as below:

<a href=”http://org.eclipse.ui.intro/runAction?pluginId=com.frank.workbench.demo&amp;class=com.frankdu.workbench.actions.InstallRemoteFeatureAction”>
Test Install Action</a>

Then you are done. The next step is to impelement the action.

Please note: pay attention to the texts that are bold and italic-underlined.

The supported HTML tags in eclipse form UI

The reason why I like HTML most, is because its flexibility. Even rookies can master it quickly. In eclipse there is form UI, in which FormText supports parsing HTML-like tags to render user interface.

Thought it doesn’t support free HTML style, it is still a big advantage over traditional Java UI building. We can easily format a paragraph of stylish text with bullet list, image, and links. How many efforts do you need in traditional Java way?

However, bear in mind that it doesn’t support all tags.The limitation rule is “FormText control is not, nor will it ever be, a web browser“.

The supported tags are list below. They MUST be used in the required order.

1. <form> as the root tag.
2. <form> tag can contains <p> and <li>.
3. <p> and <li> can contains text, <b>, <span>, <img> and <a> tags.

Attributes:
<p vspace=”true/false“>
<img src=”img_key” />
<a href=”href“>link text</a>

<li> supports more attributes as explained below:

  • style – can be “text”, “bullet” and “image” (default is “bullet”)
  • value – not used for “bullet”; if style is “text”, the value will be rendered instead in place of a bullet; if style is “image”, value represents a key in the image table of an image to be rendered in place of a bullet
  • vspace – the same as for the ‘p’ tag.
  • indent – the number of pixels to indent text
  • bindent – the number of pixels to indent the bullet (this number is independent from ‘indent’ – be careful not to overlap them)

Reference:
http://www.eclipse.org/articles/Article-Forms/article.html