Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "VIATRA2/GettingStarted/Hello World Transformation"

(Basic concepts)
(Transformation language basics)
 
(One intermediate revision by one other user not shown)
Line 13: Line 13:
 
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.
 
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.
  
== Transformation language basics ==
 
  
=== The VIATRA2 transformation language ===
 
VIATRA2 has its own programming language called VTCL. Transformations written in  VTCL are stored in '''.vtcl''' files, and each of them describes a '''machine''' of the GTASM formalism. The definition of the '''control flow''' of the transformation is similar to the mathematical formalism of Abstract State Machines (ASM), and looks just like a conventional programming language (with a syntax somewhat resembling C); this is extended by the declarative features of Graph Transformation (GT).
 
 
Why would you want to write transformations in VTCL, instead of implementing them in a well-known programming environment like Java? First, VTCL has convenient access to the VPM model space, which acts as a common ground between heterogeneous modeling formalisms (although VPM also has a Java API). But more importantly, VTCL is a specialized language tailored to transformation specification, having language constructs for efficiently processing models, such as declarative model queries and even declarative manipulation. Should you still find the capabilities of VTCL limiting, there are ways to [[VIATRA2/UseCases/InvokeNativeCode|plug in arbitrary Java code]].
 
 
The [http://www.eclipse.org/gmt/VIATRA2/doc/ official page] hosts the official (slightly outdated) transformation language specification. This page of the wiki, aims to give a short tutorial of the language. A [[VIATRA2/Activity_Diagrams_to_Petri_Nets|more elaborate tutorial]] is also available. The [[VIATRA2]] wiki also contains additional reading material including important usage patterns and more advanced topics.
 
 
=== Language features: GTASM ===
 
The VTCL language is a textual syntax to define '''GTASM''' program models. GTASM is a combined approach that integrates two popular, intuitive, yet mathematically precise rule-based specification formalisms <ref name="GTASM">VarrÛ, D., Balogh, A.:The model transformation language of the VIATRA2 framework. Sci. Comput. Program. 68(3) (2007) 214ñ234</ref> , namely, graph transformation (GT) and abstract state machines (ASM) to manipulate graph based models.
 
 
<!--Since precise model-based systems development is the primary application area of VIATRA2, it necessitates that (i) the model transformations are specified in a mathematically precise way, and (ii) these transformations are automated so that the target mathematical models can be derived fully automatically. -->
 
 
* '''Graph patterns''' are the key concept in defining model transformations within VIATRA2. A pattern is a declarative query, specified by a collection of model elements arranged into a certain structure fulfilling additional constraints (as defined by attribute conditions or other patterns). Patterns can be matched on certain model instances.
 
 
* '''Graph transformation''' (GT)  <ref name="GT"> Ehrig, H. et al.:Handbook on Graph Grammars and Computing by Graph Transformation. Volume2 : Applications, Languages and Tools. World Scientific (1999)</ref> provides a high-level rule and pattern-based manipulation language for graph models. Graph transformation rules can be specified by using a left-hand side -- LHS (or precondition) pattern determining the applicability of the rule, and a right-hand side -- RHS (postcondition) pattern which declaratively specifies the result model after rule application. Elements that are present only in (the image of) the LHS are deleted, elements that are present only in the RHS are created, and other model elements remain unchanged. VIATRA2 also allows the definition of imperative actions associated with this formalism.
 
 
* Complex model transformation can be assembled from elementary graph patterns and graph transformation rules using '''abstract state machine''' (ASM)  <ref name="ASM">Bˆrger, E ., Sark, R.: Abstract State Machines. A method for High-Level System Design and Analysis. Springer-Verlag (2003)</ref> in case of the VIATRA2 framework. ASM provides complex model transformations with all the necessary control structures including the sequencing operators, rule invocation, variable declarations and updates, GT rule invocation etc.
 
 
More information on the semantic integration between graph transformation and abstract state machines can be found in <ref name="DaniPhD">Varro, D.: Automated Model Transformations for the Analysis of IT Systems,îPh.D. thesis, Budapest University of Technology and Economics, Department of Measurement and Information Systems (2004).</ref>.
 
  
 
== Simple examples ==
 
== Simple examples ==

Latest revision as of 15:40, 16 March 2010

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.

Back to the top