Projet

Général

Profil

SpeADL Minus Tutorial » Historique » Version 4

Anonyme, 06/10/2014 15:34

1 1 Anonyme
h1. SpeADL Minus Tutorial
2
3 4 Anonyme
{{>toc}}
4
5 1 Anonyme
This is a tutorial for [[SpeADL Minus Reference|SpeADL⁻]]: creating a project, defining simple and composite components, implementing components.
6
7
h2. Objectives
8
9
The objective of this tutorial is to understand the basic workflow to follow when developing component-oriented applications with SpeADL in Eclipse/MAY.
10
11
We will create a simple composition of two components connected together following a typical client-server configuration.
12
The server will provide a service that will be used by the client.
13
They will be composed together in a big component.
14
A Java *main()* will be created to show how to execute the composition once.
15
A very simple GUI will be used to illustrate a self-contained component acting as an application.
16
17
h2. Installing Eclipse and MAY
18
19
This procedure is described on [[SpeADL MAY Project SetUp|this page]].
20
21
h2. Creating a New Project
22
23
First, we must create a project to work.
24
25
In Eclipse, go to the menus : *File / New / Java Project*.
26
27
Enter _Tutorial 1_ as the project name, verify that the *execution environment JRE* is 1.5 or above.
28
29
As a start, create a package in the *src* folder called _tutorial1_.
30
31
h2. The Server Component
32
33 3 Anonyme
h3. Organisation
34
35 1 Anonyme
Create the following hierarchy of packages (See [[SpeADL Best Practices#Project Organisation|this best practice]] for details on the matter):
36
* _tutorial1.server_
37
** _impl_
38
** _interfaces_
39
40 3 Anonyme
h3. Defining the Component
41
42 2 Anonyme
Create a SpeADL file:
43
* Right-click on tutorial1.server and select *New / File*.
44
* Enter _server.speadl_ as the file name.
45 1 Anonyme
46
Define the namespace we are going to work in as well as the _Server_ component:
47
<pre>
48
namespace tutorial1.server {
49
	component ServerComponentType {
50
		
51 3 Anonyme
	}
52
}
53
</pre>
54
55
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_.
56
57
h3. Defining a Port and its Interface
58
59
The _Server_ component will provide a port whose interface contains one method to give the contrary of a boolean.
60
61
Add it to the component declaration:
62
<pre>
63
namespace tutorial1.server {
64
	component ServerComponentType {
65
		provides service: NotSoNiceServiceType
66
	}
67
}
68
</pre>
69
70
Because the _NotSoNiceServiceType_ interface does not yet exist, there is an error in the SpeADL editor.
71
We will store the interface in the _tutorial1.server.interfaces_ package.
72
73
Either create it by hand or use the Quick Fix proposed by SpeADL editor by hovering on the error.
74
<pre>
75
package tutorial1.server.interfaces;
76
77
public interface NotSoNiceServiceType {
78
79
	public boolean contrary(boolean b);
80
}
81
</pre>
82
83
Save, and go back to the SpeADL editor for _server.speadl_.
84
Call the automatic import organiser with *Ctrl+Shift+O*.
85
Confirm that an import was added on top of the SpeADL file and save.
86
87
h3. Implementing the Component
88
89
Create a new Java class in _tutorial1.server.impl_ named ServerImpl, and edit it so that it extends ServerComponentType:
90
<pre>
91
package tutorial1.server.interfaces;
92
93
public interface NotSoNiceServiceType {
94
95
	public boolean contrary(boolean b);
96
}
97
</pre>
98
99
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.
100
101
Because there is abstract method to implement in _ServerComponentType_, there is errors in the Java editor.
102
Use the Quick Fix *Add unimplemented methods* by hovering on the error on the class name _ServerImpl_:
103
<pre>
104
public class ServerImpl extends ServerComponentType {
105
106
	@Override
107
	protected NotSoNiceServiceType make_service() {
108
		// TODO Auto-generated method stub
109
		return null;
110
	}
111
112
}
113
</pre>
114
115
Remove the *TODO* line and replace _null_ with _new NotSoNiceServiceType_, call completion (*Ctrl+Space*) and choose *NotSoNiceServiceType() Anonymous Inner Type*:
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 1 Anonyme
			}
128
		};
129 4 Anonyme
	}
130
}
131
</pre>
132
133
Remove the *TODO* line and replace _return false;_ by _return !b;_:
134
<pre>
135
package tutorial1.server.impl;
136
137
import tutorial1.server.ServerComponentType;
138
import tutorial1.server.interfaces.NotSoNiceServiceType;
139
140
public class ServerImpl extends ServerComponentType {
141
142
	@Override
143
	protected NotSoNiceServiceType make_service() {
144
		return new NotSoNiceServiceType() {
145
			
146
			@Override
147
			public boolean contrary(boolean b) {
148
				return !b;
149
			}
150
		};
151
	}
152
153
}
154
</pre>
155
156
h2. The Client Component
157
158
h3. Organisation
159
160
Create the following hierarchy of packages (See [[SpeADL Best Practices#Project Organisation|this best practice]] for details on the matter):
161
* _tutorial1.client_
162
** _impl_
163
** _interfaces_
164
** _datatypes_
165
166
h3. Defining the Component
167
168
Create a SpeADL file in _tutorial1.client_ named _client.speadl_.
169
170
Define the namespace we are going to work in as well as the _Client_ component:
171
<pre>
172
namespace tutorial1.client {
173
	component ClientComponentType {
174
		
175
	}
176
}
177
</pre>
178
179
Save and confirm that the *speadl-gen* folder contains a Java class generated in the package _tutorial1.client_ named _ClientComponentType_.
180
181
h3. Defining the Ports, their Interfaces and Datatypes
182
183
The Client component will provides a port computing a request and will need the _NotSoNiceServiceType_ to do its job:
184
<pre>
185
import tutorial1.server.interfaces.NotSoNiceServiceType
186
187
namespace tutorial1.client {
188
	
189
	component ClientComponentType {
190
		provides service: ASupeNiceService
191
		requires helper: NotSoNiceServiceType
192
	}
193
	
194
}
195
</pre>
196
197
Don't hesitate to exploit completion (*Ctrl+Space*) and automatic import organiser (*Ctrl+Shift+O*) to type the interfaces names.
198
199
As before, an error appears in the editor because _ASupeNiceService_ does not exists, create it in _tutorial1.client.interfaces_:
200
<pre>
201
package tutorial1.client.interfaces;
202
203
public interface ASupeNiceService {
204
205
	public Request compute(Request r);
206
	
207
}
208
</pre>
209
210
As we can see, we rely on a type named _Request_: create it in _tutorial1.client.datatypes_:
211
<pre>
212
package tutorial1.client.datatypes;
213
214
public class Request {
215
216
	public final boolean b;
217
218
	public Request(boolean b) {
219
		this.b = b;
220 1 Anonyme
	}
221
}
222
</pre>