## EGF Tutorial - Pattern. First steps.

This tutorial explains how to create and execute simple patterns with EGF. A pattern is a solution to a recurrent problem. A pattern is based on a language, for instance Jet for model-to-text transformations. The pattern technique allows pattern inheritance, delegation to other patterns in order to elaborate reusable and customizable generation solutions.

## 1. Creation of pattern

### 1.1. Comprehension

Pattern organization. Patterns are organized by pattern libraries. A pattern library contains a set of patterns.

Pattern nature. A pattern nature indicates which language the pattern implementation conforms to (e.g. Java, Jet).

Pattern views. A pattern is visualized with three different folders: 1) Overview, 2) Specification which represents the external view of a pattern, 3) Implementation which represents the internal view of a pattern and where the pattern is implemented.

### 1.2. Practice

The sequence of actions to create a pattern:

- Create the viewpoints node - On a factory component, Commands: [New Child][Viewpoint Container]
- Create the Pattern Viewpoint node - On the Viewpoint Container node, Commands: [New Child][Pattern Viewpoint]
- Create a pattern library - On the pattern viewpoint, Commands: [New Child][Library]. Set the library name in order to identify it later.
- Next, create a pattern - On the pattern library, Commands: [New Child][JetNature Pattern | JavaNature Pattern]
- Edit the pattern - On the pattern, Commands: [Double-click] or [Right-Click][Open]
- Set the pattern name

### 1.3. Application

Create a pattern with the Jet nature.

Figure 1. Example of new pattern

## 2. Definition of the pattern specification

### 2.1. Comprehension

Specification view. This view enables to set 1) the super-pattern of the current pattern for pattern inheritance, 2) the pattern nature already set during the pattern creation, 3) the pattern parameters.

Pattern query and parameter. The pattern applies queries over a resource that the pattern consumes, for instance a model. Each query record is successively associated to a parameter. This record is processed by the pattern implementation.

Pattern query. EGF proposes a default query. The query list is extensible.

Pattern parameter. The pattern type is either a class defined in an ecore model or a Java type (class or interface).

### 2.2. Practice

The sequence of actions to define pattern parameters and their query:

- In the Specification view, in the Parameters section - Command: [+], set the parameter name
- Set the parameter type - Command: choose the model and a class type
- Set the query - Command: choose the query, for instance Basic query

### 2.3. Application

Create an EClass parameter. The default name of a pattern parameter is parameter. In the example, it is renamed aClass. The default is empty but it corresponds to the Domain query.

Figure 2. Pattern Specification View

## 3. Definition of the pattern implementation

### 3.1. Comprehension

Implementation view. This view enables to define pattern methods, the method orchestration, and local variables.

Pattern methods. Four methods are predefined: header, init, preCondition and footer. Header is used for instance to declare the Jet declaration or the import in Java, init for the initialization of local variables (cf. explanation below), preCondition to filter records onto a query result. The user creates as many as methods s/he wants to implement the pattern. Each pattern method contents is supposed to conform to the pattern nature, for instance Jet-based code.

Pattern method orchestration. The method orchestration defines the order to call available methods and patterns: methods local to the current pattern, methods from the super-patterns, pattern call with parameter context passing, pattern call with injection.

Pattern variables. The pattern variables are accessible by any method of the current pattern. The type is either a class defined in an ecore model or a Java type (class or interface). Pattern variables can be initialized in the init method, update and used in the pattern methods.

Precondition. A pattern is applied over a resource. The precondition body in written in Java, returns a boolean, and selects whether the current record for resource query is applicable or not.

Header. The header is an header for generation. In Jet, it is used to set general parameters, such the package where Java classes are generated, the imports.

### 3.2. Practice

The sequence of actions to manage the implementation methods:

- Use the palette to add/delete/rename/reorder a method.
- To edit method, double-click on a method of the Implementation methods list. Next, a pattern templates editor allows editing any other method.

The sequence of actions to manage variables:

- In the variables, to add a variable, Command: [+], set the parameter name
- Set the parameter type - Command: click on the type and select the model / Java type

The sequence of actions to manage the method orchestration:

- Use the palette to add/delete/rename/reorder methods
- Drag and drop a method from the Implementation methods list
- For a super-method call, after clicking on +, choose the method call option, and next select the super-method.

As explained in section 5 (Pattern translation), the pattern is translated to be executed. Save (Ctrl-s) the pattern method and the pattern to have the pattern translated.

### 3.3. Application

Implement the default body method. Add the following Jet code:

    Hello <%= aClass.getName() %>!

Drag and drop the method in the pattern orchestration part.

## 4. Configuration of the pattern execution

### 4.1. Comprehension - Principle of pattern execution

The following figure depicts how patterns are applied onto a domain resource.

Figure 3. Pattern Execution

Pattern strategies. A pattern strategy indicates how to navigate over a resource and how to apply patterns. For instance, a pattern-oriented strategy successively applies a set of patterns onto a resource; on the contrary, a model-oriented strategy applies a set of patterns successively onto each model element. Those two strategies are proposed by default with EGF.

Tasks to execute patterns. A task must be introduced in order to put in action all the elements described. This task must know:

1. The resource applied on the patterns, for instance a model,
2. The selection of pattern(s) or the pattern library(ies),
3. The reporter (see below).

Domain resource. Patterns are applied onto a resource, such as a domain model.

Pattern execution engines. Patterns have a nature (e.g., Jet, Java). Then, at execution time, an engine executes a pattern according its nature. On the pattern flow, different pattern execution engines can be activated.

Reporter. A reporter is responsible for the rendering and persistence of the pattern results. It is typically useful for model-to-text transformation. Each pattern provides an output result that must be consolidated in one or several files. A reporter serves to report the result of the pattern implementation, for instance on the console or in a file.

### 4.2. Practice

A task is used to execute a pattern or a set of patterns. This task must know: 1) the domain model, 2) the pattern or pattern library list, 3) the pattern strategy, and 4) the reporter. This tutorial uses the Model-driven pattern strategy. The use of pattern reporter is explained below.

The sequence of actions to create a task for pattern execution:

- Domain creation. This step can be reused independently of the pattern usage.

- Creation of the domain viewpoint where domains are declared - On the Viewpoint Container, Command: [New Child][Domain Viewpoint]
- Creation of a domain reference - On the Domain Viewpoint, for EMF, Command: [New Child][EMF Domain]. Next, in the Uri property, providing an ecore Uri like platform:/plugin/org.eclipse.egf.model/model/Fcore.ecore

- Configuration of the pattern call.

- Create of production plan in the factory component - On the Factory Component, Command: [New Child][Production Plan]
- Creation of the pattern execution task - On the Production Plan node, Command: [Right-Click][Invoke Activity], and choose the Domain driven pattern strategy task [Task Java]
- Click on Next and select only domain and pattern.ids. Click on Finish.
- Set the domain - On the new Activity / Invocation Contract Container / domain Invocation Contract, Command: 1) [Right-Click][New Child][Type Domain], 2) Set the Domain property of this Domain URI with domain URI declared in the Domain viewpoint
- Set the pattern library - On the new Activity / Invocation Contract Container / pattern.ids Invocation Contract, Command: [New Child][Type Pattern List]; Next, click on the [Elements] property, select the pattern library newly created

### 4.3. Application

Create a pattern task invocation as described above. Example of domain: platform:/plugin/org.eclipse.egf.model/model/Fcore.ecore.

Finally, execute the factory component. The output is displayed on the console. The last line sums up all the previous lines.

### 4.4. Fixing pattern method errors

Patterns to be compiled are compiled when a pattern is saved. Errors may be raised and, as a consequence, the pattern execution would fail. Then:

- At anytime, it is possible to regenerate all pattern classes and methods: right-click on the Pattern viewpoint, and apply the [Generate Patterns] command on the Pattern viewpoint.
- Fixing the errors. In the Eclipse Package Explorer, locate the errors in the generated methods, update the pattern methods accordingly, and save the pattern methods.

### 4.5. Comprehension - Pattern Reporter

Reporter class. A reporter is a Java class which implements the PatternExecutionReporter class.

Collecting the result. There are two methods to collect the execution result of one pattern: 1) the loopFinished method collects the result for one pattern and for one query tuple, 2) the executionFinished method collects the result for one pattern, which is the aggregation of the loopFinished method results.

Reporting the result. The result is generally reported in with the selected media, e.g. a file.

For more complex reports, refer to the org.eclipse.egf.emf.docgen.html plug-in which examplifies the case of result in a file and a specific folder.

### 4.6. Practice

1. Create a reporter Java class like this one:

    Package org.eclipse.egf.examples.uc1.reporter;

    import java.util.Map;

    import org.eclipse.egf.core.EGFCorePlugin;
    import org.eclipse.egf.model.pattern.PatternContext;
    import org.eclipse.egf.model.pattern.PatternExecutionReporter;

    public class MyReporter implements PatternExecutionReporter{

    	public void executionFinished(String output, PatternContext context) {
    		// Message on the default console
    		System.out.println("=> Result of pattern:\n"+output);
    		// Message on the EGF console
    		EGFCorePlugin.getDefault().logInfo("=> Result of pattern:\n"+output);
    	}

    	public void loopFinished(String output, String outputWithCallBack,
    			PatternContext context, Map<String, Object> parameterValues) {
    		// TODO Auto-generated method stub

    	}

    }

2. Complete the task with the reporter parameter:

- Add a contract to the task created before - Command: on the Inovation Contract Container, [New Child][Invocation Contract]
- In Behaviour/Invoked Contract property, select the pattern.execution.reporter
- Add the Type Pattern Execution Reporter - Command: on the Invocation Contract, [New Child][Type Pattern Execution Reporter], and select the pattern reporter class in the value property
- Execute the factory component to see the new result

## 5. Pattern translation

The pattern contents is translated into an executable form. This takes into account pattern inheritance and other pattern mechanisms not explained in this tutorial (e.g., pattern call). Next, at execution time, a pattern engine, specific to the pattern nature, executes a pattern.

Patterns are translated on the fly. One case requires to manually force pattern rebuild: pattern inheritance. Indeed, children are not notified that a super-pattern in the hierarchy is update. To force this translation:

- On the Pattern viewpoint which contains all the pattern libraries - Command: [Right-Click][Generate Patterns]