SpeADL Minus Tutorial » Historique » Version 3
Anonyme, 06/10/2014 15:18
| 1 | 1 | Anonyme | h1. SpeADL Minus Tutorial |
|---|---|---|---|
| 2 | |||
| 3 | This is a tutorial for [[SpeADL Minus Reference|SpeADL⁻]]: creating a project, defining simple and composite components, implementing components. |
||
| 4 | |||
| 5 | h2. Objectives |
||
| 6 | |||
| 7 | The objective of this tutorial is to understand the basic workflow to follow when developing component-oriented applications with SpeADL in Eclipse/MAY. |
||
| 8 | |||
| 9 | We will create a simple composition of two components connected together following a typical client-server configuration. |
||
| 10 | The server will provide a service that will be used by the client. |
||
| 11 | They will be composed together in a big component. |
||
| 12 | A Java *main()* will be created to show how to execute the composition once. |
||
| 13 | A very simple GUI will be used to illustrate a self-contained component acting as an application. |
||
| 14 | |||
| 15 | h2. Installing Eclipse and MAY |
||
| 16 | |||
| 17 | This procedure is described on [[SpeADL MAY Project SetUp|this page]]. |
||
| 18 | |||
| 19 | h2. Creating a New Project |
||
| 20 | |||
| 21 | First, we must create a project to work. |
||
| 22 | |||
| 23 | In Eclipse, go to the menus : *File / New / Java Project*. |
||
| 24 | |||
| 25 | Enter _Tutorial 1_ as the project name, verify that the *execution environment JRE* is 1.5 or above. |
||
| 26 | |||
| 27 | As a start, create a package in the *src* folder called _tutorial1_. |
||
| 28 | |||
| 29 | h2. The Server Component |
||
| 30 | |||
| 31 | 3 | Anonyme | h3. Organisation |
| 32 | |||
| 33 | 1 | Anonyme | Create the following hierarchy of packages (See [[SpeADL Best Practices#Project Organisation|this best practice]] for details on the matter): |
| 34 | * _tutorial1.server_ |
||
| 35 | ** _impl_ |
||
| 36 | ** _interfaces_ |
||
| 37 | ** _datatypes_ |
||
| 38 | |||
| 39 | 3 | Anonyme | h3. Defining the Component |
| 40 | |||
| 41 | 2 | Anonyme | Create a SpeADL file: |
| 42 | * Right-click on tutorial1.server and select *New / File*. |
||
| 43 | * Enter _server.speadl_ as the file name. |
||
| 44 | 1 | Anonyme | |
| 45 | Define the namespace we are going to work in as well as the _Server_ component: |
||
| 46 | <pre> |
||
| 47 | namespace tutorial1.server { |
||
| 48 | component ServerComponentType { |
||
| 49 | |||
| 50 | 3 | Anonyme | } |
| 51 | } |
||
| 52 | </pre> |
||
| 53 | |||
| 54 | Save and confirm that the *speadl-gen* folder was automatically added to the project source folders and that it contains a Java class generated in the package _tutorial1.server_ named _ServerComponentType_. |
||
| 55 | |||
| 56 | h3. Defining a Port and its Interface |
||
| 57 | |||
| 58 | The _Server_ component will provide a port whose interface contains one method to give the contrary of a boolean. |
||
| 59 | |||
| 60 | Add it to the component declaration: |
||
| 61 | <pre> |
||
| 62 | namespace tutorial1.server { |
||
| 63 | component ServerComponentType { |
||
| 64 | provides service: NotSoNiceServiceType |
||
| 65 | } |
||
| 66 | } |
||
| 67 | </pre> |
||
| 68 | |||
| 69 | Because the _NotSoNiceServiceType_ interface does not yet exist, there is an error in the SpeADL editor. |
||
| 70 | We will store the interface in the _tutorial1.server.interfaces_ package. |
||
| 71 | |||
| 72 | Either create it by hand or use the Quick Fix proposed by SpeADL editor by hovering on the error. |
||
| 73 | <pre> |
||
| 74 | package tutorial1.server.interfaces; |
||
| 75 | |||
| 76 | public interface NotSoNiceServiceType { |
||
| 77 | |||
| 78 | public boolean contrary(boolean b); |
||
| 79 | } |
||
| 80 | </pre> |
||
| 81 | |||
| 82 | Save, and go back to the SpeADL editor for _server.speadl_. |
||
| 83 | Call the automatic import organiser with *Ctrl+Shift+O*. |
||
| 84 | Confirm that an import was added on top of the SpeADL file and save. |
||
| 85 | |||
| 86 | h3. Implementing the Component |
||
| 87 | |||
| 88 | Create a new Java class in _tutorial1.server.impl_ named ServerImpl, and edit it so that it extends ServerComponentType: |
||
| 89 | <pre> |
||
| 90 | package tutorial1.server.interfaces; |
||
| 91 | |||
| 92 | public interface NotSoNiceServiceType { |
||
| 93 | |||
| 94 | public boolean contrary(boolean b); |
||
| 95 | } |
||
| 96 | </pre> |
||
| 97 | |||
| 98 | Notice that completion can be exploited as _ServerComponentType_ is an actual Java type, but that calling *F3* on it redirects directly to the SpeADL file. |
||
| 99 | |||
| 100 | Because there is abstract method to implement in _ServerComponentType_, there is errors in the Java editor. |
||
| 101 | Use the Quick Fix *Add unimplemented methods* by hovering on the error on the class name _ServerImpl_: |
||
| 102 | <pre> |
||
| 103 | public class ServerImpl extends ServerComponentType { |
||
| 104 | |||
| 105 | @Override |
||
| 106 | protected NotSoNiceServiceType make_service() { |
||
| 107 | // TODO Auto-generated method stub |
||
| 108 | return null; |
||
| 109 | } |
||
| 110 | |||
| 111 | } |
||
| 112 | </pre> |
||
| 113 | |||
| 114 | Remove the *TODO* line and replace _null_ with _new NotSoNiceServiceType_, call completion (*Ctrl+Space*) and choose *NotSoNiceServiceType() Anonymous Inner Type*: |
||
| 115 | |||
| 116 | <pre> |
||
| 117 | public class ServerImpl extends ServerComponentType { |
||
| 118 | |||
| 119 | @Override |
||
| 120 | protected NotSoNiceServiceType make_service() { |
||
| 121 | return new NotSoNiceServiceType() { |
||
| 122 | |||
| 123 | @Override |
||
| 124 | public boolean contrary(boolean b) { |
||
| 125 | // TODO Auto-generated method stub |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | }; |
||
| 129 | 1 | Anonyme | } |
| 130 | } |
||
| 131 | </pre> |