SpeADL Minus Reference » Historique » Version 2
Anonyme, 03/10/2014 16:54
| 1 | 1 | Anonyme | h1. SpeADL Minus Reference |
|---|---|---|---|
| 2 | |||
| 3 | 2 | Anonyme | {{>toc}} |
| 4 | |||
| 5 | 1 | Anonyme | In SpeADL, a set of abstractions are provided to define traditional component-oriented architectures. |
| 6 | With it, it is possible to define components and compositions of components, called composites, and to implement them in Java while keeping a strong link between definition and implementation by relying on an Eclipse plugin. |
||
| 7 | |||
| 8 | A component is made of two elements: a component class definition using SpeADL and an implementation using Java. |
||
| 9 | From the SpeADL definition, an abstract Java class is automatically generated and then relied upon through the Java extension mechanism to implement it in a safe manner. |
||
| 10 | |||
| 11 | h2. Namespaces |
||
| 12 | |||
| 13 | Components and composites are defined inside namespace using the keyword *namespace*. |
||
| 14 | A namespace plays the exact same role as a package in Java. |
||
| 15 | |||
| 16 | In a SpeADL file, there can be many as namespace (as well as nested ones) as wanted. |
||
| 17 | Hence a namespace does not have to follow the name of the directory it is located in as in Java. |
||
| 18 | |||
| 19 | Here is an example of namespace declarations: |
||
| 20 | <pre> |
||
| 21 | namespace simple { |
||
| 22 | |||
| 23 | namespace things { |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | namespace simple.stuffs { |
||
| 28 | } |
||
| 29 | </pre> |
||
| 30 | |||
| 31 | Each namespace declaration can contain any component as desired as we are going to see. |
||
| 32 | |||
| 33 | h2. Imports |
||
| 34 | |||
| 35 | As in Java, it is possible to import existing types into a file to avoid referring to them with their fully qualified name (i.e., including their package or namespace) using the keyword *import*. |
||
| 36 | |||
| 37 | The syntax is similar to Java: |
||
| 38 | <pre> |
||
| 39 | import java.util.Collection |
||
| 40 | import java.util.* |
||
| 41 | import simple.stuffs.* |
||
| 42 | </pre> |
||
| 43 | |||
| 44 | Notice that namespace of components are also considered to import component class definitions, and that there is no semi-colon ";" at the end of the line. |
||
| 45 | |||
| 46 | The imports can be automatically handled and reorganised in Eclipse using the *Ctrl-Shift-O* shortcut as in the Java editor. |
||
| 47 | |||
| 48 | h2. Components and Ports |
||
| 49 | |||
| 50 | A component is made of a component class definition and an implementation: it can then be instantiated |
||
| 51 | The definition gives it a name and a list of ports that are either provided or required by the component. |
||
| 52 | Each port has a name and an interface. |
||
| 53 | An interface is understood as a Java interface, i.e., a collection of methods. |
||
| 54 | |||
| 55 | A component that provides a port must thus provide an implementation for its interface. |
||
| 56 | Inversely, a component that requires a port can use in its implementation the methods of the interface of the required port. |
||
| 57 | |||
| 58 | A component with required ports must be composed with other components so that there exist an actual implementation of the interface of the required port: this is covered in the next section. |
||
| 59 | |||
| 60 | 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. |
||
| 61 | This is what makes a component fundamentally different from an object. |
||
| 62 | |||
| 63 | A component is defined using the following syntax: |
||
| 64 | <pre> |
||
| 65 | import my.interfaces.* |
||
| 66 | |||
| 67 | namespace simple.stuffs { |
||
| 68 | |||
| 69 | component MySimpleComponent { |
||
| 70 | provides p1: AnotherJavaInterface |
||
| 71 | } |
||
| 72 | |||
| 73 | component MyBeautifulComponent { |
||
| 74 | provides portName: AJavaInterface |
||
| 75 | requires anotherPortName: AnotherJavaInterface |
||
| 76 | } |
||
| 77 | } |
||
| 78 | </pre> |
||
| 79 | |||
| 80 | A component is defined using the keyword *component*, has a name and can contains as many port declaration as wanted. |
||
| 81 | |||
| 82 | A port has a name and an interface. |
||
| 83 | The keywords *provides* and *requires* respectfully represents ports that are provided and required by the component and are a mandatory keyword when defining a port inside a component. |
||
| 84 | |||
| 85 | Obviously, having an interface means that there must exist already an interface defined with the same name. |
||
| 86 | Such a definition is done in Java as one would normally do, for example, as follow, in Java files: |
||
| 87 | <pre> |
||
| 88 | package my.interfaces; |
||
| 89 | |||
| 90 | public interface AJavaInterface { |
||
| 91 | public String aMethod(Integer param1); |
||
| 92 | } |
||
| 93 | </pre> |
||
| 94 | |||
| 95 | <pre> |
||
| 96 | package my.interfaces; |
||
| 97 | |||
| 98 | public interface AnotherJavaInterface { |
||
| 99 | public Integer test(); |
||
| 100 | } |
||
| 101 | </pre> |
||
| 102 | |||
| 103 | In SpeADL, one can use completion to complete interface names. |
||
| 104 | Also, the shortcut to organize imports will take interfaces into account. |
||
| 105 | |||
| 106 | h2. Implementations |
||
| 107 | |||
| 108 | To implement a component, one has to extend the abstract class generated automatically by the Eclipse plugin. |
||
| 109 | For example, for the previous example of component, a Java class *simple.stuffs.MyBeautifulComponent* was generated (in the speadl-gen folder, separated from the src folder). |
||
| 110 | |||
| 111 | It is not needed to look at the generated code to use it: when extending the class, some abstract methods must be implemented. |
||
| 112 | It is a good idea to use the errors shown by the Eclipse Java editor and their quick-fixes to quickly generate the skeleton of the implementation itself. |
||
| 113 | |||
| 114 | Each provided port *p* of interface *I* must be implemented by overriding a method called *I make_p()* which returns an implementation for the port used during 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). |
||
| 115 | Usually, one returns in this method an anonymous instance of the interface as the following Java files show: |
||
| 116 | |||
| 117 | <pre> |
||
| 118 | package testpackage; |
||
| 119 | |||
| 120 | import my.interfaces.AnotherJavaInterface; |
||
| 121 | import simple.stuffs.MySimpleComponent; |
||
| 122 | |||
| 123 | public class MySimpleComponentImpl extends MySimpleComponent { |
||
| 124 | |||
| 125 | @Override |
||
| 126 | protected AnotherJavaInterface make_p1() { |
||
| 127 | return new AnotherJavaInterface() { |
||
| 128 | @Override |
||
| 129 | public Integer test() { |
||
| 130 | return 10; |
||
| 131 | } |
||
| 132 | }; |
||
| 133 | } |
||
| 134 | |||
| 135 | } |
||
| 136 | </pre> |
||
| 137 | |||
| 138 | <pre> |
||
| 139 | package testpackage; |
||
| 140 | |||
| 141 | import my.interfaces.AJavaInterface; |
||
| 142 | import simple.stuffs.MyBeautifulComponent; |
||
| 143 | |||
| 144 | public class MyComponentImpl extends MyBeautifulComponent { |
||
| 145 | |||
| 146 | @Override |
||
| 147 | protected AJavaInterface make_portName() { |
||
| 148 | return new AJavaInterface() { |
||
| 149 | @Override |
||
| 150 | public String aMethod(Integer param1) { |
||
| 151 | return "" + param1 + " and " + requires().anotherPortName().test(); |
||
| 152 | } |
||
| 153 | }; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | </pre> |
||
| 157 | |||
| 158 | But the same result can be obtained by implementing the port directly by the component implementation as follow: |
||
| 159 | <pre> |
||
| 160 | public class MyComponentImpl extends MyBeautifulComponent implements AJavaInterface { |
||
| 161 | |||
| 162 | @Override |
||
| 163 | public String aMethod(Integer param1) { |
||
| 164 | return "" + param1 + " and " + requires().anotherPortName().test(); |
||
| 165 | } |
||
| 166 | |||
| 167 | @Override |
||
| 168 | protected AJavaInterface make_portName() { |
||
| 169 | return this; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | </pre> |
||
| 173 | |||
| 174 | We can see that the class extends simple.stuffs.MyBeautifulComponent and that it overrides a method named *make_portName()* after the name of the provided port. |
||
| 175 | |||
| 176 | Finally, the *requires()* method (inherited from the extended generated class, *MyBeautifulComponent* in the example) gives access to each of the required ports (e.g.,*requires().anotherPortName()* in the example). |
||
| 177 | 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().anotherPortName().test()* in the example). |
||
| 178 | |||
| 179 | Of course, nothing else is required than calling it to use it: as we are going to see now, the implementation of a required port is provided by another component composed with the requiring component. |
||
| 180 | |||
| 181 | h2. Composites |
||
| 182 | |||
| 183 | While defining a component class with a name and ports in SpeADL is only a definition of its external interfaces, a composite of many components connected together is already a partial implementation of this definition. |
||
| 184 | |||
| 185 | A composite, on top of provided and required ports, contains parts. A part is structurally similar to a class member in Java. |
||
| 186 | A part is declared using the *part* keyword and the name of the component class of the part. |
||
| 187 | Furthermore, if the component class of the part has required ports, then a part will also contain bindings for these required ports using the keyword *bind*. |
||
| 188 | |||
| 189 | An example follow: |
||
| 190 | <pre> |
||
| 191 | namespace simple.stuffs { |
||
| 192 | component MyComplexComponent { |
||
| 193 | |||
| 194 | provides p1: AnotherJavaInterface |
||
| 195 | provides p2: AnotherJavaInterface = s.p1 |
||
| 196 | requires p3: AnotherJavaInterface |
||
| 197 | |||
| 198 | part b1: MyBeautifulComponent { |
||
| 199 | bind anotherPortName to s.p1 |
||
| 200 | } |
||
| 201 | |||
| 202 | part b2: MyBeautifulComponent { |
||
| 203 | bind anotherPortName to p1 |
||
| 204 | } |
||
| 205 | |||
| 206 | part b3: MyBeautifulComponent { |
||
| 207 | bind anotherPortName to p3 |
||
| 208 | } |
||
| 209 | |||
| 210 | part s: MySimpleComponent |
||
| 211 | |||
| 212 | } |
||
| 213 | } |
||
| 214 | </pre> |
||
| 215 | |||
| 216 | The keyword *bind* is followed by the name of the required port that is to be bound (completion can be used), then by the keyword *to* then either by: |
||
| 217 | * The name of another part, a dot, then a provided port of this part (as for *b1* in the example). |
||
| 218 | * The name of a provided port of the component containing the part (as for *b2* in the example). |
||
| 219 | * The name of a required port of the component containing the part (as for *b3* in the example). |
||
| 220 | |||
| 221 | Furthermore, another type of binding is a delegation of a provided port to another port. |
||
| 222 | It is declared using the = sign after the provided port declaration (as in *p2* in the example) and can be followed either by a reference to the port of a part, a provided or a required port of the component, as with the normal bindings. |
||
| 223 | |||
| 224 | h3. Implementation of Composites |
||
| 225 | |||
| 226 | As with the implementation of non-composite component classes, a Java class is generated from the description and it must be extended in order to specify the implementation of the provided ports and of the parts. |
||
| 227 | |||
| 228 | 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*. |
||
| 229 | |||
| 230 | For example: |
||
| 231 | <pre> |
||
| 232 | public class ComplexCompImpl extends MyComplexComponent { |
||
| 233 | |||
| 234 | @Override |
||
| 235 | protected MySimpleComponent make_s() { |
||
| 236 | return new MySimpleComponentImpl(); |
||
| 237 | } |
||
| 238 | |||
| 239 | @Override |
||
| 240 | protected AnotherJavaInterface make_p1() { |
||
| 241 | return new AnotherJavaInterface() { |
||
| 242 | @Override |
||
| 243 | public Integer test() { |
||
| 244 | return parts().s().p1().test(); |
||
| 245 | } |
||
| 246 | }; |
||
| 247 | } |
||
| 248 | |||
| 249 | @Override |
||
| 250 | protected MyBeautifulComponent make_b1() { |
||
| 251 | return new MyComponentImpl(); |
||
| 252 | } |
||
| 253 | |||
| 254 | @Override |
||
| 255 | protected MyBeautifulComponent make_b2() { |
||
| 256 | return new MyComponentImpl(); |
||
| 257 | } |
||
| 258 | |||
| 259 | @Override |
||
| 260 | protected MyBeautifulComponent make_b3() { |
||
| 261 | return new MyComponentImpl(); |
||
| 262 | } |
||
| 263 | |||
| 264 | } |
||
| 265 | </pre> |
||
| 266 | |||
| 267 | As we can see, 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. |
||
| 268 | It becomes very easy to define new compositions without extra boilerplate code. |
||
| 269 | |||
| 270 | Also, it is possible to access to the provided ports of the part from within the implementation of a composite by using the method *parts()* (as in the implementation of the port *p1* in the example). |
||
| 271 | |||
| 272 | h2. Instantiation |
||
| 273 | |||
| 274 | In order to instantiate a component, 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. |
||
| 275 | Only component without required port can be manually instantiated: if a component has required ports, it must be composed with other components in a composite component. |
||
| 276 | |||
| 277 | For example: |
||
| 278 | <pre> |
||
| 279 | MySimpleComponent.Component c = new MySimpleComponentImpl().newComponent(); |
||
| 280 | System.out.println(c.p1().test()); |
||
| 281 | </pre> |
||
| 282 | |||
| 283 | As we can see, once we have an instance of a component, we can call the methods of its provided ports. |
||
| 284 | |||
| 285 | The same applies for composite components, the instantiation of the part of a composite is done automatically by the generated code. |
||
| 286 | |||
| 287 | h2. Component Initialisation |
||
| 288 | |||
| 289 | 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. |
||
| 290 | |||
| 291 | In order to do some initialisation at the instantiation of a component (during the call to *newComponent()*), one can override the *void start()* method in the implementation as follow: |
||
| 292 | |||
| 293 | <pre> |
||
| 294 | public class MySimpleComponentImpl extends MySimpleComponent { |
||
| 295 | |||
| 296 | @Override |
||
| 297 | protected AnotherJavaInterface make_p1() { |
||
| 298 | return new AnotherJavaInterface() { |
||
| 299 | @Override |
||
| 300 | public Integer test() { |
||
| 301 | return 10; |
||
| 302 | } |
||
| 303 | }; |
||
| 304 | } |
||
| 305 | |||
| 306 | @Override |
||
| 307 | protected void start() { |
||
| 308 | // do some initialisation using the requires() or the parts(), create a GUI, etc... |
||
| 309 | } |
||
| 310 | } |
||
| 311 | </pre> |