Projet

Général

Profil

SpeADL Minus Java Reference » Historique » Version 5

Anonyme, 16/10/2014 10:02

1 3 Anonyme
h1. Java for SpeADL⁻ Reference Guide
2 1 Anonyme
3 2 Anonyme
In the [[SpeADL Minus Reference|SpeADL⁻ reference guide]] we saw how one can define components and composition of components.
4
We now present how to implement these components in Java.
5 1 Anonyme
6 4 Anonyme
h2. Terminology
7
8
The reader can refer to the [[MAY Terminology]] document to get an overview of the different terms used in SpeADL.
9
10
h2. SpeADL⁻
11
12
It is needed to understand the content of the [[SpeADL Minus Reference|SpeADL⁻ Reference Guide]] before reading the current document.
13
14 1 Anonyme
h2. Component Implementation
15
16
To implement a component, one has to extend the abstract class generated automatically by the Eclipse plugin.
17
For example, for the previous example _simple.stuffs.MyBeautifulComponent_ defined in SpeADL, a Java class _simple.stuffs.MyBeautifulComponent_ is generated (in the *speadl-gen* folder, different than the *src* folder).
18
19
It is not needed to look at the generated code to use it: when extending the class, some abstract methods will have to be implemented.
20
21
When implementing a component, one only has to take care of implementing the provided port, and can exploit the required ports without assuming anything about their implementation and who provides it.
22
This is one thing that makes a component fundamentally different from an object.
23
24
h3. Special Methods to Implement
25
26
Each provided port *p* of interface *I* must be implemented by overriding a method called *I make_p()* which returns an instance of the implementation for the port.
27
This instance is used for the whole life of the component, i.e., the *make_p()* method is called only once to construct the port when the component is instantiated.
28
29
Each part *p* of component class *C* has a corresponding abstract method *C make_p()* to override and which must return an instance of an implementation of *C*.
30
The bindings and other connections inside the components are totally taken care of by the generated code and the implementation only needs what is Java-specific.
31
32
Furthermore, optionally, a method *void start()* can be override as explained [[SpeADL_Minus_Reference#Component-Initialisation|below]].
33
34
h3. Special Methods to Exploit
35
36
The *requires()* method (inherited from the extended generated class) gives access to each of the required ports (e.g., _requires().port()_).
37
A port being an implementation of an interface (and not of an operation), it is then necessary to call the desired method on it (e.g., _requires().port().method()_).
38
39
The *provided()* method  (inherited from the extended generated class) gives access to each of the provided ports in the same manner.
40
41
It is possible to access to the provided ports of the part from within the implementation of a composite by using the method *parts()* (e.g., _parts().partName().portName().method()_).
42
43
h3. Examples
44
45
Implementing a component with a provided port:
46
<pre>
47
package testpackage;
48
49
import my.interfaces.AnotherJavaInterface;
50
import simple.stuffs.MySimpleComponent;
51
52
public class MySimpleComponentImpl extends MySimpleComponent {
53
54
	@Override
55
	protected AnotherJavaInterface make_p1() {
56
		return new AnotherJavaInterface() {
57
			@Override
58
			public Integer test() {
59
				return 10;
60
			}
61
		};
62
	}
63
64
}
65
</pre>
66
67
The same result can be obtained by implementing the port directly by the component implementation as follow:
68
<pre>
69
public class MySimpleComponentImpl extends MySimpleComponent implements AnotherJavaInterface {
70
71
	@Override
72
	public Integer test() {
73
		return 10;
74
	}
75
	
76
	@Override
77
	protected AnotherJavaInterface make_p1() {
78
		return this;
79
	}
80
}
81
</pre>
82
83
Exploiting a required port:
84
<pre>
85
package testpackage;
86
87
import my.interfaces.AJavaInterface;
88
import simple.stuffs.MyBeautifulComponent;
89
90
public class MyComponentImpl extends MyBeautifulComponent {
91
92
	@Override
93
	protected AJavaInterface make_portName() {
94
		return new AJavaInterface() {
95
			@Override
96
			public String aMethod(Integer param1) {
97
				return "" + param1 + " and " + requires().anotherPortName().test();
98
			}
99
		};
100
	}
101
}
102
</pre>
103
104
Implementing a component with parts, calling a part's provided port:
105
<pre>
106 5 Anonyme
public class ComplexCompImpl extends MyCompositeComponent {
107 1 Anonyme
108
	@Override
109
	protected MySimpleComponent make_s() {
110
		return new MySimpleComponentImpl();
111
	}
112
113
	@Override
114
	protected AnotherJavaInterface make_p1() {
115
		return new AnotherJavaInterface() {
116
			@Override
117
			public Integer test() {
118
				return parts().s().p1().test();
119
			}
120
		};
121
	}
122
123
	@Override
124
	protected MyBeautifulComponent make_b1() {
125
		return new MyComponentImpl();
126
	}
127
128
	@Override
129
	protected MyBeautifulComponent make_b2() {
130
		return new MyComponentImpl();
131
	}
132
133
	@Override
134
	protected MyBeautifulComponent make_b3() {
135
		return new MyComponentImpl();
136
	}
137
138
}
139
</pre>
140
141
h2. Component Instantiation
142
143
In order to instantiate a component from Java, one need an instance of an implementation of the component and to call the *newComponent()* method (present in the generated class) to get an instance of the component.
144
145
h3. Details
146
147
Only component without required port can be manually instantiated from Java: if a component has required ports, it must be composed with other components in a composite component.
148
149
Once we have an instance of a component, we can call the methods of its provided ports.
150
151
The same applies for composite components, the instantiation of the part of a composite is done automatically by the generated code.
152
153
h3. Example
154
155
<pre>
156
MySimpleComponent.Component c = new MySimpleComponentImpl().newComponent();
157
System.out.println(c.p1().test());
158
</pre>
159
160
h2. Component Initialisation
161
162
When the implementation of a component is instantiated (before calling *newComponent()*), its constructor is of course called but the component itself is not yet initialised: in particular its provided required ports and parts can't be called at that time.
163
164
h3. Details
165
166
In order to do some initialisation at the instantiation of a component (during the call to *newComponent()*), one can override the *void start()* method of the extended abstract class.
167
168
h3. Example
169
170
<pre>
171
public class MySimpleComponentImpl extends MySimpleComponent {
172
173
	@Override
174
	protected AnotherJavaInterface make_p1() {
175
		return new AnotherJavaInterface() {
176
			@Override
177
			public Integer test() {
178
				return 10;
179
			}
180
		};
181
	}
182
	
183
	@Override
184
	protected void start() {
185
		// do some initialisation using the requires() or the parts(), create a GUI, etc...
186
	}
187
}
188
</pre>
189
190
h2. Lifecycle of Component Initialisation at Instantiation
191
192
When *newComponent()* is called on a component implementation, this is what happens:
193
# The component is instantiated  (see below).
194
# The instance is started (see below).
195
196
h3. Component Instantiation
197
198
# For each part *partX* in the order of declaration
199
## The implementation is instantiated with the *make_partX()* method.
200
## A component is instantiated from the implementation following the current procedure.
201
# For each provided port *portX* in the order of declaration (starting with the super-component in case of specialisation)
202
## The interface implementation is instantiated with the *make_portX()* method.
203
204
h3. Component Instance Start
205
206
# For each part *partX* in the order of declaration
207
## The part is started following the current procedure.
208
# The implementation *start()* method is called.