## 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 the generation leaves which realize the effective generation tasks, such as producing an API. In EGF, this corresponds to factory components which delegate their generations, by an activity invocation, to other factory components until tasks which realize a generation task. A task is implemented in a language, such as Java or Ant.

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

Figure 1. EGF Activity Relationships

### 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
- Creation of a Factory Component - Commands: [File][New][Other...]/[EGF folder][EGF Activity]
- Select a project or a project folder, and set the fcore file name
- Click on Finish or Next, and select Factory Component for model object
- Click on Finish; the Factory Component is created
- Set the factory component name in the Properties view to better identify it later

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

A fcore file is a resource able to contain several activities, i.e. several factory components or tasks. In order to add a new activity in the same fcore file:

- Right-Click on an activity - Commands: [New Sibling]
- Select the activity type to create

### 1.3. Application

During this tutorial, a Java class will be implemented. Then, it is better to start with a Java project.

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

Figure 2. New EGF Activity

## 2. Creation of task contracts and task implementation

### 2.1. Comprehension

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

Figure 3. Activity Contracts

Task declaration and implementation. A task realizes an effective work. It is necessary to differentiate the task declaration, defined in a fcore model, from the task implementation, written in a language such as Java. After its declaration, a task can be invoked by different factory components. The task implementation is the means to execute any tool, or any code in the given language. The Java class implements an ITaskProduction interface. It can implement three methods: preExecute, doExecute, postExecute. The task methods have a context parameter to get, set and work with the task contracts. The link between the Task and the implementation class is realized at the task level.

Figure 4. Java Task and Java Task implementation

### 2.2. Practice

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

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

### 2.3. Application

Create a Task contract 'value' of String contract type with the (default) value 'World', as shown in the following figure.

Figure 5. Java Task Example

### 2.4. Practice

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

- Create a Class which implements the ITaskProduction Interface
- Implement the task class
- In the fcore of the task, declare the task as a Java Task - Command: on the [Task], set the Task/kind property to java
- Associate the task with the task Java class - Command: on the [Task], on the Implementation property, select a Java task class

Figure 6. Java Task / Java class association

At this stage, the task is executable. Cf. section 4.

### 2.5. Application

Create a 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.ftask.producer.context.ITaskProductionContext;
    import org.eclipse.egf.ftask.producer.invocation.ITaskProduction;

    public class HelloWorld implements ITaskProduction {

    	public void doExecute(ITaskProductionContext productionContext,
    			IProgressMonitor monitor) throws InvocationException {
    		String value  = productionContext.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);
    	}

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

    	}

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

    	}

    }

Associate the HelloWorld class to the HelloWorldTask Java task.

## 3. Task invocation in a factory component

### 3.1. Comprehension

Orchestration. The same task / factory component can be invoked by different factory components; the same factory component can invoke several factory component and/or tasks. Invocations to factory components and tasks are defined in an orchestration. EGF proposes a simple orchestration type named production plan. A production plan is a sorted list of activity invocations. A task invocation calls a task with the values required by the task contracts.

Figure 7. Factory Component Orchestration

### 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 an invocation in a production plan:

- Commands: on a production plan, [Invoke Activity] and select an activity. Next menu allows selecting parameters involved in the invocation.

The sequence of actions to set values for the invocation:

- Commands: on an invocation contract, [New Child][the activity contract type, e.g. String]
- 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

In the HelloWorld factory component, create a task invocation without invocation contract (the default value of the task contract is used) and another one with an invocation contract and the associated value.

## 4. Execution of task and factory component

### 4.1. Comprehension

Execution during the development phase. During the development phase, there exist two modes to execute a task or factory component. 1) Dynamically, directly on a factory component or on a task, when the model is open. This mode is useful to simultaneously develop and test tasks and factory components. Before execution, it is controled that the model is valid. 2) On the fcore resource. This avoids to open the fcore resource.

Execution on the target platform. After deployment in the target platform, the factory components, tasks, and fcore resources can be normally executed.

Preferences. 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

The sequence of actions to execute a fcore resource:

- Commands: [Right-Click][Run EGF Activity...] on the fcore file
- If there are several factory components or tasks in the same fcore file, then it is necessary to select the activity to be executed