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