Projet

Général

Profil

SpeADL Minus Java Reference » Historique » Version 2

Anonyme, 15/10/2014 17:28

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