First, we provide a brief recap of some useful information from other introductory pages.
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.
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.
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.
In order to begin working with VIATRA2, you'll first need to create a model space instance to store your models.
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.
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:
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.
There is two ways to create a simple model in VIATRA2:
For this example, we'll use Option 2.
<source lang="text"> entity(Test) { entity(A); entity(B); relation(R,A,B); } </source>
This defines a simple model in the "Test" namespace, with two nodes (A,B) and a relation R between them.
<source lang="text"> namespace test; machine hello { rule main() = seq { println("Hello VIATRA2 world!"); } } </source>
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.
<source lang=text> 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)); } } </source>
In this example, we'll create an reverse relation (edge) found for each Source-Target pair.
<source lang=text> 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"); } } } } </source>
On the screen, you can see the newly created relations in selection highlight.