Jump to: navigation, search

VIATRA2/GettingStarted/Hello World Transformation

< VIATRA2‎ | GettingStarted
Revision as of 15:40, 16 March 2010 by Bergmann.mit.bme.hu (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Basic concepts

First, we provide a brief recap of some useful information from other introductory pages.

Model space

VIATRA2 is a model transformation engine which can be used to manipulate abstract models (software, architecture, domain-specific) by creating model-to-model and model-to-code transformations. VIATRA2 stores models in the VPM Model Space, which supports the uniform representation of metamodels and models in a single logical container (as opposed to EMF where metamodels and models are separated), using an arbitrary number of metamodeling levels.

Transformations

Model-to-model and model-to-code transformations are specified using the VIATRA2 Textual Command Language (VTCL) which is a textual domain-specific programming language tailored for this specific task.

The main language features of the VTCL language are introduced here using simple examples.

Model import and export

VIATRA2 supports the programmatic import of metamodels and models from other tools using an Application Programming Interface (API). Model exports are supported using (i) code generation, or (ii) the VIATRA2 modeling API accessible from any Java program.


Simple examples

First steps

Model space wizard

In order to begin working with VIATRA2, you'll first need to create a model space instance to store your models.

  1. Create a new project in Eclipse. The project can be of any type.
  2. In the project, create a new file (right click|new|other).
  3. In the wizard, select "VIATRA2 VPM Model space" from the "VIATRA2 Framework Release3" category.
  4. Specify the file name and click Next (not Finish).
  5. On the modelspace fragments selection screen, select "VPM Core and Datatypes metamodel".
  6. Click Finish.
Fig.1 New Modelspace Wizard

Once the wizard is finished, you should have the new VPML file in your project. VPML files are XML serializations of the VIATRA2 model space, you can have any number of VPML files open at the same time, meaning that you can work with multiple model spaces concurrently.

Model space editor and VIATRA2 views

Double-click the newly created VPML file to open the VIATRA2 model editor (or select Open With|VIATRA2 model editor from the context menu). Open the VIATRA2 model spaces view (Window|Show view|Other|VIATRA2 Model spaces) and the VIATRA2 Textual Output view to get the basic home screen:

Fig.2 Basic home screen

Editing models with the editor

You can browse around the model space using the tree viewer. It is also practical to open the Properties view to see and edit the properties of model elements. The editor supports creating and deleting elements through the context menu. You can double click on any element to edit its name.

The basic elements in VIATRA2 are entities and relations. Entites represent graph nodes which relations represent graph edges. You can define instanceOf, superTypeOf relations between them (multi-level metamodeling). Entities are arranged into hierarchical name spaces which is displayed in the tree view of the model editor.

The Properties view uses the Fully Qualified names of elements for manipulation. FQNs are generated from the hierarchy, the FQN of a relation is the FQN of its source + the name of the relation (separated with a dot). If you enter an invalid FQN, the properties view will not save your changes.

You can save your model with Ctrl+S (File|Save) or File|Save as.

Creating a simple model

There is two ways to create a simple model in VIATRA2:

  1. Manual editing with the model editor;
  2. Using the textual modeling language (VTML) and importing it into the model space.

For this example, we'll use Option 2.

  • Create a new file in your project and enter "test.vtml" as the file name (File|New|File).
  • Double click the file and enter the following:
entity(Test)
{
	entity(A);
	entity(B);
	relation(R,A,B);
}

This defines a simple model in the "Test" namespace, with two nodes (A,B) and a relation R between them.

  • Save the file, and using drag-and-drop, drop the VTML file onto the "modelspace1" entry in the VIATRA2 Model spaces view.
Fig.3 Model space editor with imported VTML file

The Hello World transformation

  • Create a new file in your project, name it "helloworld.vtcl".
  • Double click on the file, the VTCL editor will open.
  • Enter the following:
namespace test;
machine hello
{
	rule main() = seq
	{
		println("Hello VIATRA2 world!");
	}
}
  • There are multiple possibilities to load this transformation into VIATRA2:
    • Drag-and-drop the VTCL file onto the "modelspace1" entry just like with model imports;
    • Simply save the file (Ctrl+S), it will be loaded.
    • If you have saved but performed another edit, pressing Alt+P (parse) will have the same effect as saving.
  • After the transformation is loaded, you can run the transformation by:
    • Pressing Alt+R in the editor (the editor has to have focus)
    • Double clicking on the (M) icon in the model spaces view
    • Right-click on the transformation in the model spaces view and select Run.
Fig. 4 The Hello World transformation

Advanced examples

Pattern matching

In this example, we'll construct a transformation which looks for pairs of entities (nodes) connected with a relation (edge) in the entire model space.

namespace test;
machine pm
{
	pattern connectedNodes(N1,N2) =
	{
		entity(N1);
		entity(N2);
		relation(R,N1,N2);
	}
	rule main() = seq
	{
		forall Src,Trg with find connectedNodes(Src,Trg) do
			println(fqn(Src) + " -> " + fqn(Trg));
	}
}
Fig.5 Results of execution

Model manipulation

In this example, we'll create an reverse relation (edge) found for each Source-Target pair.

namespace test;
machine pm
{
	pattern connectedNodes(N1,N2) =
	{
		entity(N1);
		entity(N2);
		relation(R,N1,N2);
	}
	rule main() = seq
	{
		forall Src,Trg with find connectedNodes(Src,Trg) do seq
		{
			println(fqn(Src) + " -> " + fqn(Trg));
			let NewR = undef in seq
			{
				new(relation(NewR,Trg,Src));
				rename(NewR,"reverse");
			}
		}
	}
}
Fig. 6 The results of the transformation

On the screen, you can see the newly created relations in selection highlight.