Projet

Général

Profil

SpeADL Minus Java Reference » Historique » Version 10

Anonyme, 17/10/2014 16:24

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