Projet

Général

Profil

SpeADL Minus Java Reference » Historique » Version 11

Anonyme, 24/10/2014 10:49

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