## EGF Tutorial - Factory Component. First steps.

This first tutorial explains how to create and execute simple factory components with EGF.

## 1. Creation of factory component and task

### 1.1. Comprehension

Generation activities. A generation can be decomposed into generation stages down to generation leaves that realize a generation task. In EGF, this corresponds to factory components that delegate their generations to other factory components until tasks that realizes a generation task.

Storage. Factory components and tasks are stored in fcore files.

### 1.2. Practice

The sequence of actions to create a factory component:

- First, create a project and a folder where the factory components and tasks will be located
- Commands: [File][New][EGF Activity]
- Select a project or a project folder, and set the fcore file name
- Next, select Factory Component as model object
- Click on Finish; the Factory Component is created
- Set the factory component name to better identify it later

The sequence of actions is the same for creating a task: select Task as model object instead of Factory Component.

### 1.3. Application

Create a Factory component named HelloWorld and a Task named HelloWorldTask.

## 2. Creation of task contracts and task implementation

### 2.1. Comprehension

Contracts. In EGF, a task parameter is named Contract. Task contracts are contained in a Task Contract Container. A Contract has a Name, Mode (IN, OUT, IN_OUT), is mandatory or not, has a Type, and possibly a default value.

Implementation. It is necessary to differentiate the task declaration, defined in a fcore model, and the task implementation, defined in a Java class. The task implementation is the means to execute any generation tool, or to do more. The Java class implements an ITaskProduction interface. It can implement three methods: preExecute, doExecute, postExecute. The task methods have a context parameter to get and work with the task contracts. The link between the Task and the task Java class is realized at the task level.

### 2.2. Practice

Task modeling - The sequence of actions to create task contracts:

- Creation of the task contract container - Command: on a task, [New Child][Task Contract Container]
- Creation of a task contract - Command: on the task contract container, [New Child][Task Contract]
- Set the contract name, mode, and mandatory values
- Definition of the task contract type - Command: on the task contract, [New Child][String|Integer...]
- Set the default value on the contract type

### 2.3. Application

Create a Task contract 'value' of String contract type with the (default) value 'World'.

### 2.4. Practice

Task implementation - The sequence of actions to create and associate a task Java Class:

- Create a Class that implements the ITaskProduction Interface
- Implement the task class
- Associate the task with the task Java class - Command: on the task, on the value property of the Task production section, select a task java class

### 2.5. Application

Create a task Java class 'HelloWorld' that displays an parameterized Hello message. Example of code:

    import org.eclipse.core.runtime.IProgressMonitor;
    import org.eclipse.egf.core.EGFCorePlugin;
    import org.eclipse.egf.core.producer.InvocationException;
    import org.eclipse.egf.fprod.producer.context.ITaskProductionContext;
    import org.eclipse.egf.fprod.producer.invocation.ITaskProduction;

    public class HelloWorld implements ITaskProduction {

    	@Override
    	  public void doExecute(final ITaskProductionContext context, final IProgressMonitor monitor_p) throws InvocationException {
    		String value  = taskProductionContext.getInputValue("value", String.class);
    		String outputMessage = new String ("Hello " + value + "!");
    		// Message on the default console
    		System.out.println(outputMessage);
    		// Message on the EGF console
    	    EGFCorePlugin.getDefault().logInfo(outputMessage);
    	}

    	@Override
    	  public void preExecute(final ITaskProductionContext context, final IProgressMonitor monitor_p) throws InvocationException {
    		// TODO Auto-generated method stub

    	}

    	@Override
    	  public void postExecute(final ITaskProductionContext context, final IProgressMonitor monitor_p) throws InvocationException {
    		// TODO Auto-generated method stub

    	}

    }

## 3. Task invocation in a factory component

### 3.1. Comprehension

Orchestration. >The same task can be invoked by different factory components and several times inside the same factory component. The last point underlies the need of orchestration. EGF proposes a simple orchestration mode named production plan. A production is a sorted list of task invocations. A task invocation calls a task with the values required by the task contracts.

### 3.2. Practice

The sequence of actions to create a production plan:

- Commands: on a factory component, [New Child][Production Plan]

The sequence of actions to create a task invocation in a production plan:

- Commands: on a production plan, [New Child][Task Invocation]
- Set the task Name
- In the invoke section of the task invocation, set the Activity property: select the task among the proposed tasks

The sequence of actions to create task invocation values in a task invocation:

- First, create an invocation context - Commands: on a task invocation, [New Child][Invocation Context Container]
- For each task contract to be valued - Commands: on an invocation context container, [New Child][Invocation Context]
- Set the invocation context Name
- On the invocation context, in the Activity Contract property of the invoke section, select the concerned task contract to be valued
- Definition of an Invocation Context value - Commands: on an invocation context, [New Child][the activity contract type]
- Set the value

The two last steps are necessary when the default value of the task contract must be redefined or is undefined and madatory.

### 3.3. Application

Create a task invocation without Invocation Context (the default value of the task contract is used) and one with.

## 4. Execution of task and factory component

### 4.1. Comprehension

Execution mode. There are two modes to execute a task or factory component: 1) dynamically, directly on the model, 2) in a deployed mode. The dynamic execution is useful to simultaneously develop and test tasks and factory components. Before execution, it is controled that the model is valid. The EGF production settings are accessible via the EGF preferences.

### 4.2. Practice

The sequence of actions to execute a task or factory component dynamically:

- Commands: [Right-Click][Run EGF Activity...]: on a task or a factory component