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/Creating Transformations"

(Example)
(Creating Transformations)
Line 16: Line 16:
  
 
== The VIATRA2 transformation language ==
 
== The VIATRA2 transformation language ==
VIATRA2 has its own programming language called VTCL. The VTCL language is a textual syntax to define GTASM program models. 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).  
+
<!-- VIATRA2 has its own programming language called VTCL.-->
 +
The VTCL language is a textual syntax to define GTASM program models. 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]].  
 
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]].  
Line 23: Line 24:
  
 
== VTCL basics ==
 
== VTCL basics ==
The contents of the file form a single GTASM machine, indicated by the <code>machine</code> keyword. The machine declaration is optionally preceded by <code>namespace</code> header to declare a namespace for the machine; the '''full name''' of the machine (as seen in the ''VIATRA2 Model Spaces'' view) will consist of this namespace, a dot, and the regular name. The other type of header is <code>import</code>, which references a namespace in the metamodel; type names below that namespace can be shortened to their relative qualified names throughout the file.
+
The contents of the file form a single GTASM machine, indicated by the <code>machine</code> keyword. The machine declaration is often preceded by <code>namespace</code> header to declare a namespace for the machine; the '''full name''' of the machine (as seen in the ''VIATRA2 Model Spaces'' view) will consist of this namespace, a dot, and the regular name. The other type of header is <code>import</code>, which references a namespace in the VPM metamodel; type names below that namespace can be shortened to their relative qualified names throughout the file.
  
 
The procedure/method-like basic execution unit of ASM is the '''ASM rule'''. A rule is defined by code blocks introduced by the <code>rule</code> keyword, followed by the rule name, and then an arbitrary number of input (<code>in</code>), output (<code>out</code>), or in-out (<code>inout</code>) parameters enclosed in parentheses and tailed by an equality sign. A machine can have a '''main rule''', which serves as an entry point; it is executed when the user runs the transformation from the GUI (the dialog box will ask for input parameters). The rest of the ASM rules can be invoked by a <code>call</code> instruction in other rules of the same machine, or a different machine (qualifying the rule name by the full name of the called machine).
 
The procedure/method-like basic execution unit of ASM is the '''ASM rule'''. A rule is defined by code blocks introduced by the <code>rule</code> keyword, followed by the rule name, and then an arbitrary number of input (<code>in</code>), output (<code>out</code>), or in-out (<code>inout</code>) parameters enclosed in parentheses and tailed by an equality sign. A machine can have a '''main rule''', which serves as an entry point; it is executed when the user runs the transformation from the GUI (the dialog box will ask for input parameters). The rest of the ASM rules can be invoked by a <code>call</code> instruction in other rules of the same machine, or a different machine (qualifying the rule name by the full name of the called machine).
Line 34: Line 35:
 
As a less common GTASM element, '''ASM functions''' are a global associative (key-value) storage, that contributes to the state of the machine (but not to that of the model, i.e. it is not persisted on saving the model space). Keys are tuples of a chosen number of arguments  ('''arity'''). ASM funtions are defined by the keyword <code>asmfunction</code>, followed by the name of the ASM function, a slash, and the arity. Optionally, the contents can be initialized by an initialization block containing key-value assignments. The contents of the ASM function can be read or written all throughout the machine, as well as from other machines (the ASM function name will have to be qualified with the full name of the defining machine).
 
As a less common GTASM element, '''ASM functions''' are a global associative (key-value) storage, that contributes to the state of the machine (but not to that of the model, i.e. it is not persisted on saving the model space). Keys are tuples of a chosen number of arguments  ('''arity'''). ASM funtions are defined by the keyword <code>asmfunction</code>, followed by the name of the ASM function, a slash, and the arity. Optionally, the contents can be initialized by an initialization block containing key-value assignments. The contents of the ASM function can be read or written all throughout the machine, as well as from other machines (the ASM function name will have to be qualified with the full name of the defining machine).
  
VTCL uses C-like comments: <code>//</code> causes the VTCL parser to ignore the whole following line, while a pair of <code>/*</code> and <code>*/</code> ignore anything between them. Annotations (e.g. <code lang="java">@incremental</code> or <code  lang="java">@random</code>) are not comments; they are parsed and may provide special options on how certain elements should be interpreted. Many GTASM elements (most importantly, pattern and GT rule definitions) can be preceded by annotations, marked by a <code>@</code> and an annotation name, followed by an optional parentheses-enclosed list of key-value pairs.  
+
'''Variables''' are used throughout the language. Variable names are required to start with capital letters, to be easily distinguishable from model elements, that usually start with a lower case letter. To avoid ambiguities, the name of model elements starting with capital letters sometimes need to be enclosed in single quotation marks, e.g. <code>'Activity'</code>. The variables of the VTCL language are untyped by default; they can hold any kinds of value, including model elements (conforming to any type in the metamodel), strings, integers, etc., even arbitrary Java objects. In addition to the parameters, additional local variables can be used in patterns, GT rules and ASM rules. In ASM rules, they have to be introduced by the <code>let</code> ''variable'' = ''initialValue'', ''variable2'' = ''initialValue2'' <code>in</code> ''block'' construct, or by quantification (over pattern matches or rule applications). Often variables will be initialized with the special value '''undef''' to indicate that they have not yet been assigned.  
  
 +
VTCL uses C-like comments: <code>//</code> causes the VTCL parser to ignore the whole following line, while a pair of <code>/*</code> and <code>*/</code> ignore anything between them. Annotations (e.g. <code lang="java">@incremental</code> or <code  lang="java">@random</code>) are not comments; they are parsed and may provide special options on how certain elements should be interpreted. Many GTASM elements (most importantly, pattern and GT rule definitions) can be preceded by annotations, marked by a <code>@</code> and an annotation name, followed by an optional parentheses-enclosed list of key-value pairs.
  
=== Exercise===
+
== Exercise ==
 
This exercise relies on the ''Activity Diagrams to Petri Nets'' example transformation included in the [[VIATRA2/Examples|example transformations]] package. Make sure the corresponding VIATRA2 project is in your Eclipse workspace.  
 
This exercise relies on the ''Activity Diagrams to Petri Nets'' example transformation included in the [[VIATRA2/Examples|example transformations]] package. Make sure the corresponding VIATRA2 project is in your Eclipse workspace.  
  
 
Open the <code>Activity2PetriNet.vtcl</code> file included in the project. A ''VTCL Text Editor'' will appear, highlighting various elements of the syntax, as well as displaying warning or error indicators. The editor has some useful keystrokes, including <code>Alt+P</code> for parsing + loading the machine (same as dragging & dropping the file from the ''Navigator'' to the entry in the ''VIATRA2 Model Spaces'' view), or <code>Ctrl+S</code> for saving + parsing + loading.   
 
Open the <code>Activity2PetriNet.vtcl</code> file included in the project. A ''VTCL Text Editor'' will appear, highlighting various elements of the syntax, as well as displaying warning or error indicators. The editor has some useful keystrokes, including <code>Alt+P</code> for parsing + loading the machine (same as dragging & dropping the file from the ''Navigator'' to the entry in the ''VIATRA2 Model Spaces'' view), or <code>Ctrl+S</code> for saving + parsing + loading.   
  
Take a closer look at the vtcl code. Identify the GTASM machine and its entry point. Find the ASM rule, GT rule and graph pattern definitions, and identify their name and parameters. Try to find some ASM rule invocations and GT rule applications in the code (<code>call</code> and <code>apply</code> keywords, respectively). Hint: hit <code>Ctrl+F</code> while the VTCL file is selected to search in the text. Try to find some graph patterns used as part of a GT rule, either by local declaration or remote reference (<code>find</code> keyword). Try to find some graph patterns used within other graph patterns (<code>find</code> keyword). Try to find some patterns used for pattern matching in ASM rules, iterating over all matches (<code>forall</code>+<code>find</code> structure). Inspect the comments placed in the file, and try to discover which part of the code is responsible for what part of the process.
+
Take a closer look at the vtcl code. Identify the GTASM machine and its entry point. Find the ASM rule, GT rule and graph pattern definitions, and identify their name and parameters. Take a look at the bodies of some of these definitions, and try to identify the variables. Try to find some ASM rule invocations and GT rule applications in the code (<code>call</code> and <code>apply</code> keywords, respectively); hint: hit <code>Ctrl+F</code> while the VTCL file is selected to search in the text. Try to find some graph patterns used as part of a GT rule, either by local declaration or remote reference (<code>find</code> keyword). Try to find some graph patterns used within other graph patterns (<code>find</code> keyword). Try to find some patterns used for pattern matching in ASM rules, iterating over all matches (<code>forall</code>+<code>find</code> structure). Inspect the comments placed in the file, and try to discover which part of the code is responsible for what part of the process.
  
== ASM rule bodies  ==
+
== Developing transformations ==
In addition to the rule parameters, local variables can be introduced by the <code>let</code> ''variable'' = ''initialValue'', ''variable2'' = ''initialValue2'' <code>in</code> ''block'' construct, or by quantification.  
+
GUI, etc. TODO
TODO Capital, Types, Undef, ASMFunc (rule body?).
+
  
 +
 +
 +
 +
== ASM rule bodies  ==
 +
<!-- In addition to the rule parameters, local variables can be introduced by the <code>let</code> ''variable'' = ''initialValue'', ''variable2'' = ''initialValue2'' <code>in</code> ''block'' construct, or by quantification. Often variables will be initialized with the special value '''undef''' to indicate that they have not yet been assigned. Variable names are required to start with capital letters, to be easily identified. -->
 +
<!-- TODO Types, ASMFunc (rule body?). -->
 +
<!--
 
The most common control flow structure to define the body of a rule or a let/in block is the sequence block, delimited by <code>seq {</code> and <code>}</code>. This sequence block, similarly to the curly braces block in many C-like languages, contains semicolon-separated commands that are executed sequentially. Additional control flow elements include the <code>if (</code>''condition''<code>)</code> ''action'' construct for branching, with an optional <code>else</code> ''otherAction'' part. The loop construct <code>iterate</code> ''action'' repeatedly executes the action as long as it does not '''fail'''. Execution can fail by manually executing the instruction <code>fail</code> (typically to terminate a loop), or by an unsuccessful pattern matching (see later). The <code>try</code> ''action'' construct masks failures , with an optional <code>else</code> ''otherAction'' to execute in case a failure was caught. Finally, <code>skip</code> simply means doing nothing, and the aforementioned <code>call</code> invokes other ASM rules.
 
The most common control flow structure to define the body of a rule or a let/in block is the sequence block, delimited by <code>seq {</code> and <code>}</code>. This sequence block, similarly to the curly braces block in many C-like languages, contains semicolon-separated commands that are executed sequentially. Additional control flow elements include the <code>if (</code>''condition''<code>)</code> ''action'' construct for branching, with an optional <code>else</code> ''otherAction'' part. The loop construct <code>iterate</code> ''action'' repeatedly executes the action as long as it does not '''fail'''. Execution can fail by manually executing the instruction <code>fail</code> (typically to terminate a loop), or by an unsuccessful pattern matching (see later). The <code>try</code> ''action'' construct masks failures , with an optional <code>else</code> ''otherAction'' to execute in case a failure was caught. Finally, <code>skip</code> simply means doing nothing, and the aforementioned <code>call</code> invokes other ASM rules.
  
 
The most basic commands include <code>update</code> ''variable'' = ''value'', to change variables (or ASM functions), and <code>print</code>/<code>println</code>, to emit textual results / code onto the ''default'' tab of the ''VIATRA2 Textual Output'' view (or [[VIATRA2/UseCases/CodeGeneration|to other tabs or even files]]). The ASM part of VTCL also contains instructions with '''direct model space access''', the most important ones are the following. <code>ref</code> retrieves a model element by its fully qualified name. <code>new</code> and <code>delete</code> adds and removes model elements (as well as <code>supertypeOf</code> and <code>instanceOf</code> relationships!), respectively. <code>rename</code> changes the name of elements; <code>setFrom</code> and <code>setTo</code> manipulate relations, while <code>move</code> and <code>setValue</code> manipulate entities. Throughout these operations, type references can be shortened to a relative qualified name, by importing the namespace of a metamodel by the  <code>import</code> header before the machine declaration.
 
The most basic commands include <code>update</code> ''variable'' = ''value'', to change variables (or ASM functions), and <code>print</code>/<code>println</code>, to emit textual results / code onto the ''default'' tab of the ''VIATRA2 Textual Output'' view (or [[VIATRA2/UseCases/CodeGeneration|to other tabs or even files]]). The ASM part of VTCL also contains instructions with '''direct model space access''', the most important ones are the following. <code>ref</code> retrieves a model element by its fully qualified name. <code>new</code> and <code>delete</code> adds and removes model elements (as well as <code>supertypeOf</code> and <code>instanceOf</code> relationships!), respectively. <code>rename</code> changes the name of elements; <code>setFrom</code> and <code>setTo</code> manipulate relations, while <code>move</code> and <code>setValue</code> manipulate entities. Throughout these operations, type references can be shortened to a relative qualified name, by importing the namespace of a metamodel by the  <code>import</code> header before the machine declaration.
 +
-->
 +
<!-- TODO rule quant -->
  
TODO rule quant
+
See the [[VIATRA2/Examples/VTCL/ASM | ASM]] page for more information.
  
 
== Patterns  ==
 
== Patterns  ==
TODO
+
<!-- TODO -->
 
<code>find</code> ''patternName''(''parameters'') clause
 
<code>find</code> ''patternName''(''parameters'') clause
 +
 +
See the [[VIATRA2/Examples/VTCL/GraphPattern| Graph Pattern ]] page for more information.
  
 
== GT Rules  ==
 
== GT Rules  ==

Revision as of 15:00, 9 March 2010

Creating Transformations

GTASM

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. For this purpose, VIATRA2 have chosen to integrate two popular, intuitive, yet mathematically precise rule-based specification formalisms [1] , namely, graph transformation (GT) and abstract state machines (ASM) to manipulated graph based models. This combined approach is often called as GTASM.

  • Graph patterns are the basic concept in defining model transformations within VIATRA2. A pattern is 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) [2] 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.
  • Complex model transformation can be assembled from elementary graph patterns and graph transformation rules using abstract state machine (ASM) [3] 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 [4].

To formulate these constructs VIATRA2 uses its own programming language called VTCL.

The VIATRA2 transformation language

The VTCL language is a textual syntax to define GTASM program models. 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 plug in arbitrary Java code.

The official page hosts the official (slightly outdated) transformation language specification. This section of the wiki, including the following pages, aims to give an informal, introductory overview of the language. The wiki also contains additional reading material including important usage patterns and more advanced topics.

VTCL basics

The contents of the file form a single GTASM machine, indicated by the machine keyword. The machine declaration is often preceded by namespace header to declare a namespace for the machine; the full name of the machine (as seen in the VIATRA2 Model Spaces view) will consist of this namespace, a dot, and the regular name. The other type of header is import, which references a namespace in the VPM metamodel; type names below that namespace can be shortened to their relative qualified names throughout the file.

The procedure/method-like basic execution unit of ASM is the ASM rule. A rule is defined by code blocks introduced by the rule keyword, followed by the rule name, and then an arbitrary number of input (in), output (out), or in-out (inout) parameters enclosed in parentheses and tailed by an equality sign. A machine can have a main rule, which serves as an entry point; it is executed when the user runs the transformation from the GUI (the dialog box will ask for input parameters). The rest of the ASM rules can be invoked by a call instruction in other rules of the same machine, or a different machine (qualifying the rule name by the full name of the called machine).

Declarative model queries are called Graph Patterns. They are defined in code blocks indicated by the pattern (or shareable pattern) keyword, the pattern name, and an arbitrary number of (symbolic) parameters enclosed in parentheses (here, there is no distinction between input and output parameters). The pattern body follows, enclosed in curly braces; it describes constraints that the substituted value of the parameters must fulfill. Sometimes, there are multiple disjunctive pattern bodies separated by or. A match of the pattern is a substitution of each of its parameters (usually to model elements in the model space, less often to other values), such that all constraints are observed in at least one of the disjunctive pattern bodies. Patterns can be incorporated within each other (or a condition expression) using the pattern call feature (find keyword). Additionally, ASM rules can quantify the parameter values universally over all matches of a pattern (forall loop), or existentially just to find one match (choose keyword).

Declarative and semi-declarative model manipulations are called Graph Transformation Rules or GT rules for short. They are defined in code blocks indicated by the gtrule keyword, the GT rule name, an input-output parameter list like the one found at ASM rule declarations, and an equality sign followed by a pair of curly braces enclosing GT rule elements (precondition, postcondition, action). GT rules describe some model manipulation or other effect that is to be executed for matches of a precondition pattern (also known as left-hand-side or LHS). To define this effect, a GT rule contains either a declarative postcondition (right-hand-side or RHS), an imperative action sequence, or both. The precondition is indicated by the keyword precondition, followed either by a pattern call (find keyword) to the precondition pattern, or the inline definition of the precondition pattern. The optional postcondition is defined similarly. The optional imperative action sequence is indicated by the action keyword, followed by a pair of curly braces enclosing a sequence of instructions (similar to seq in ASM rule bodies). A rule application consists of taking a match of the LHS, replace it with the corresponding image of the RHS graph pattern (if any), and executing the action sequence (if any). The quantification syntax (forall, choose) can be used to apply gtrules on all possible or on one single match of the LHS.

As a less common GTASM element, ASM functions are a global associative (key-value) storage, that contributes to the state of the machine (but not to that of the model, i.e. it is not persisted on saving the model space). Keys are tuples of a chosen number of arguments (arity). ASM funtions are defined by the keyword asmfunction, followed by the name of the ASM function, a slash, and the arity. Optionally, the contents can be initialized by an initialization block containing key-value assignments. The contents of the ASM function can be read or written all throughout the machine, as well as from other machines (the ASM function name will have to be qualified with the full name of the defining machine).

Variables are used throughout the language. Variable names are required to start with capital letters, to be easily distinguishable from model elements, that usually start with a lower case letter. To avoid ambiguities, the name of model elements starting with capital letters sometimes need to be enclosed in single quotation marks, e.g. 'Activity'. The variables of the VTCL language are untyped by default; they can hold any kinds of value, including model elements (conforming to any type in the metamodel), strings, integers, etc., even arbitrary Java objects. In addition to the parameters, additional local variables can be used in patterns, GT rules and ASM rules. In ASM rules, they have to be introduced by the let variable = initialValue, variable2 = initialValue2 in block construct, or by quantification (over pattern matches or rule applications). Often variables will be initialized with the special value undef to indicate that they have not yet been assigned.

VTCL uses C-like comments: // causes the VTCL parser to ignore the whole following line, while a pair of /* and */ ignore anything between them. Annotations (e.g. @incremental or @random) are not comments; they are parsed and may provide special options on how certain elements should be interpreted. Many GTASM elements (most importantly, pattern and GT rule definitions) can be preceded by annotations, marked by a @ and an annotation name, followed by an optional parentheses-enclosed list of key-value pairs.

Exercise

This exercise relies on the Activity Diagrams to Petri Nets example transformation included in the example transformations package. Make sure the corresponding VIATRA2 project is in your Eclipse workspace.

Open the Activity2PetriNet.vtcl file included in the project. A VTCL Text Editor will appear, highlighting various elements of the syntax, as well as displaying warning or error indicators. The editor has some useful keystrokes, including Alt+P for parsing + loading the machine (same as dragging & dropping the file from the Navigator to the entry in the VIATRA2 Model Spaces view), or Ctrl+S for saving + parsing + loading.

Take a closer look at the vtcl code. Identify the GTASM machine and its entry point. Find the ASM rule, GT rule and graph pattern definitions, and identify their name and parameters. Take a look at the bodies of some of these definitions, and try to identify the variables. Try to find some ASM rule invocations and GT rule applications in the code (call and apply keywords, respectively); hint: hit Ctrl+F while the VTCL file is selected to search in the text. Try to find some graph patterns used as part of a GT rule, either by local declaration or remote reference (find keyword). Try to find some graph patterns used within other graph patterns (find keyword). Try to find some patterns used for pattern matching in ASM rules, iterating over all matches (forall+find structure). Inspect the comments placed in the file, and try to discover which part of the code is responsible for what part of the process.

Developing transformations

GUI, etc. TODO



ASM rule bodies

See the ASM page for more information.

Patterns

find patternName(parameters) clause

See the Graph Pattern page for more information.

GT Rules

TODO

Developing transformations

GUI, etc. TODO

a previous section
Cite error: <ref> tags exist, but no <references/> tag was found

Back to the top