## EGF Tutorial - Pattern. First steps.

This first tutorial explains how to create and execute simple patterns with EGF. A pattern is a solution to a recurrent generation problem. A pattern is based on a language, for instance Jet for model-to-text transformation. The pattern technique allows pattern inheritance, delegation to other patterns in order to elaborate more 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 and a set of pattern libraries.

Pattern nature. A pattern nature indicates to which language the pattern implementation must conform (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][Viewpoints]
- Create the Pattern Viewpoint node - On the viewpoints node, Commands: [New Child][Pattern Viewpoint]
- Create a pattern library - On the pattern viewpoint, Commands: [New Child][Library]
- Next, create a pattern - On the pattern library, Commands: [New Child][JetNature Pattern | JavaNature Pattern]
- Save the model
- Edit the pattern - On the pattern, Commands: [Right-Click][Edit]
- Set the pattern name

### 1.3. Application

Create a pattern with the Jet nature.

## 2. Definition of the pattern specification

### 2.1. Comprehension

Specification view. This view enables to set the super-pattern of the current pattern when pattern inheritance is needed, the pattern nature already set during the pattern creation, the pattern parameters.

Pattern query and parameter. The pattern applies queries over the resource that the pattern consumes, for instance a model. Each query record is successively stored in a parameter. The record process is described in the pattern implementation.

Pattern query. EGF proposes a default model 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 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
- Set the contract name, mode, and mandatory values

### 2.3. Application

Create an EClass parameter with the Basic query to navigate over the model.

## 3. Definition of the pattern implementation

### 3.1. Comprehension

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

Pattern methods. Three methods are predefined: header, init, and footer. Header is used for instance to declare the Jet declaration, init for the initialization of local variables. The user methods contain a description that conforms to the pattern nature, for instance Jet 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 without parameter, pattern call with parameter context 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).

### 3.2. Practice

The sequence of actions to access to a method implementation: double-click on a method of the method list. Next, a pattern templates editor allows to edit any other method.

- 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
- Set the contract name, mode, and mandatory values

### 3.3. Application

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

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

## 4. Configuration of the pattern execution

### 4.1. Comprehension - Definition of the reporter

Reporter. A reporter serves to report the result of the pattern implementation, for instance on the console or in a file. A reporter implements the PatternExecutionReporter class.

Report process. There are two methods to report a queries: 1) for each query instance with the loopFinished method, 2) when the query is completely process with the executionFinished method.

### 4.2. Practice

Create a reporter Java class like this one:

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

    import java.util.Map;

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

    public class MyReporter implements PatternExecutionReporter{

    	@Override
    	public void executionFinished(String output, PatternContext context) {
    		System.out.println("Result of pattern:\n"+output);

    	}

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

    	}

    }

### 4.3. Comprehension - Configuration of the execution

Pattern strategies. A pattern strategy indicates how to apply patterns onto a resource. For instance, a pattern-oriented strategy successively applies a set of patterns onto a resource; a model-oriented strategy applies a set of patterns successively onto each model element.

Reporter task. A task applies a strategy with a set of arguments: 1) the resource (e.g., a model URI), 2) the pattern(s) or the pattern library(ies), 3) the reporter. Those arguments are provided by a task invocation in a factory component.

### 4.4. Practice

The sequence of actions to create a task:

- Create a task
- On the Task, on the Task Production section, select a strategy, for instance PatternTask for one pattern, or DefaultStrategyTask for a pattern-oriented strategy
- Create Task Contracts:

- For PatternTask: pattern.id (String), pattern.execution.reporter (PatternExecutionReporter type) with reference of the reporter, domain (URI)
- For DefaultStrategyTask: patternIds (String), pattern.execution.reporter (PatternExecutionReporter type) with reference of the reporter , domain (URI)

The sequence of actions to configure the task invocation:

- Create a task invocation in production plan
- Associate the task invocation to the new task for pattern execution or an existing task
- Provide a value to every expected contract

### 4.5. Application

Create a task that uses PatternTask which uses the reporter previously created.

Create a task invocation for the new task.

Finally, execute the factory component.