SpeADL Dynamic Tutorial » Historique » Version 5
Anonyme, 14/10/2014 15:39
| 1 | 3 | Anonyme | h1. SpeADL for Dynamic Architectures Tutorial |
|---|---|---|---|
| 2 | 2 | Anonyme | |
| 3 | 1 | Anonyme | This is a tutorial for SpeADL: understanding ecosystems and species, defining a simple ecosystem, composing species together with uses. |
| 4 | 3 | Anonyme | |
| 5 | h2. Objectives |
||
| 6 | |||
| 7 | The objective of this tutorial to understand the abstractions of species and uses. |
||
| 8 | |||
| 9 | We will create a Logging ecosystem that contains Logger species. |
||
| 10 | Then we will create a Banking ecosystem that contains Account species that need to log what happens. |
||
| 11 | Finally we will create a Bank ecosystem that compose Account species with Logger species thanks to the use abstraction. |
||
| 12 | |||
| 13 | h2. Prerequisites |
||
| 14 | |||
| 15 | It is needed to understand the content of the [[SpeADL Minus Tutorial|SpeADL⁻ tutorial]] before doing this one. |
||
| 16 | |||
| 17 | h2. Creating a New Project and Organisation |
||
| 18 | |||
| 19 | Create a Java project. |
||
| 20 | We will use again an organisation of the package and namespaces as explained in [[MAY Best Practices#Project Organisation|this best practice]]. |
||
| 21 | |||
| 22 | h2. The Logging Ecosystem |
||
| 23 | |||
| 24 | Logging will be an ecosystem, and its particularity is that it will be able to create Loggers: while logging is the subsystem responsible of all logging, each logger will be responsible of logging one particular aspect of the system identified by a _name_. |
||
| 25 | We will have multiple implementation for the Logging ecosystem: |
||
| 26 | * One with only one file where to log with each line prepended with the name. |
||
| 27 | * One with one file per Logger all in the same directory. |
||
| 28 | |||
| 29 | h3. Organisation |
||
| 30 | |||
| 31 | Create the following hierarchy of packages: |
||
| 32 | * tutorial2.logging |
||
| 33 | ** impl |
||
| 34 | ** interfaces |
||
| 35 | |||
| 36 | h3. Defining the Ecosystem and the Species |
||
| 37 | |||
| 38 | Create a SpeADL file named _filesystem.speadl_ in the package _tutorial2.filesystem_. |
||
| 39 | |||
| 40 | Define in it the ecosystem and the species in the right namespace as well as the needed interface: |
||
| 41 | <pre> |
||
| 42 | import tutorial2.filesystem.interfaces.ILog |
||
| 43 | |||
| 44 | namespace tutorial2.filesystem { |
||
| 45 | |||
| 46 | ecosystem Logging { |
||
| 47 | species Logger(name: String) { |
||
| 48 | provides log: ILog |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | } |
||
| 53 | </pre> |
||
| 54 | |||
| 55 | <pre> |
||
| 56 | package tutorial2.filesystem.interfaces; |
||
| 57 | |||
| 58 | public interface ILog { |
||
| 59 | |||
| 60 | public void addLine(String line); |
||
| 61 | } |
||
| 62 | </pre> |
||
| 63 | |||
| 64 | h3. Implementing the Ecosystem and the Species, First One |
||
| 65 | |||
| 66 | 4 | Anonyme | Create a new Java class in _tutorial2.logging.impl_ named _LoggingImplOne_ that extends _Logging_, that takes the a _File_ as a parameter to the constructor to store the logs, and resolve the error with the Quick Fixes of Eclipse: |
| 67 | 3 | Anonyme | <pre> |
| 68 | package tutorial2.logging.impl; |
||
| 69 | |||
| 70 | import java.io.File; |
||
| 71 | import java.io.FileNotFoundException; |
||
| 72 | 5 | Anonyme | import java.io.FileWriter; |
| 73 | 3 | Anonyme | import java.io.IOException; |
| 74 | import java.io.PrintWriter; |
||
| 75 | |||
| 76 | import tutorial2.logging.Logging; |
||
| 77 | |||
| 78 | public class LoggingImplOne extends Logging { |
||
| 79 | |||
| 80 | private PrintWriter logWriter; |
||
| 81 | |||
| 82 | public LoggingImplOne(File logFile) { |
||
| 83 | // if an exception happens, nothing can be done about it |
||
| 84 | // we just let logStream be null and |
||
| 85 | // the operations of logging won't be done |
||
| 86 | try { |
||
| 87 | 5 | Anonyme | this.logWriter = new PrintWriter(new FileWriter(logFile), true); |
| 88 | 3 | Anonyme | } catch (FileNotFoundException e) { |
| 89 | 5 | Anonyme | System.err.println("An error happened with the file, nothing will be logged."); |
| 90 | 1 | Anonyme | this.logWriter = null; |
| 91 | 3 | Anonyme | } catch (IOException e) { |
| 92 | 5 | Anonyme | System.err.println("An error happened with the file, nothing will be logged."); |
| 93 | 3 | Anonyme | this.logWriter = null; |
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | @Override |
||
| 98 | protected Logger make_Logger(String name) { |
||
| 99 | // TODO Auto-generated method stub |
||
| 100 | return null; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | </pre> |
||
| 104 | |||
| 105 | As we can see, the species we defined needs to be implemented and this implementation needs to be returned by the method _Logger make_Logger(String name)_. |
||
| 106 | |||
| 107 | 4 | Anonyme | Let's define an inner class inside _LoggingImplOne_ to do that, it will take as a parameter to the constructor the _name_ of the Logger and that will exploit the _logWriter_ to do the actual logging. |
| 108 | 3 | Anonyme | <pre> |
| 109 | public class LoggingImplOne extends Logging { |
||
| 110 | |||
| 111 | private PrintWriter logWriter; |
||
| 112 | |||
| 113 | // ... |
||
| 114 | |||
| 115 | @Override |
||
| 116 | protected Logger make_Logger(String name) { |
||
| 117 | return new LoggerImpl(name); |
||
| 118 | } |
||
| 119 | |||
| 120 | private class LoggerImpl extends Logger implements ILog { |
||
| 121 | |||
| 122 | private final String name; |
||
| 123 | |||
| 124 | public LoggerImpl(String name) { |
||
| 125 | this.name = name; |
||
| 126 | } |
||
| 127 | |||
| 128 | @Override |
||
| 129 | protected ILog make_log() { |
||
| 130 | return this; |
||
| 131 | } |
||
| 132 | 1 | Anonyme | |
| 133 | @Override |
||
| 134 | public void addLine(String line) { |
||
| 135 | 5 | Anonyme | if (logWriter != null) { |
| 136 | logWriter.println("["+name+"] "+ line); |
||
| 137 | } |
||
| 138 | 1 | Anonyme | } |
| 139 | } |
||
| 140 | } |
||
| 141 | 5 | Anonyme | </pre> |
| 142 | |||
| 143 | We now have a complete implementation for the Logging ecosystem. |
||
| 144 | Let's test it with a very simple program, for that we will subclass _LoggingImplOne_ in _tutorial2.logging.tests_, create some Logger in its *start()* method and instantiate it: |
||
| 145 | <pre> |
||
| 146 | package tutorial2.logging.tests; |
||
| 147 | |||
| 148 | import java.io.File; |
||
| 149 | |||
| 150 | import tutorial2.logging.impl.LoggingImplOne; |
||
| 151 | |||
| 152 | public class LoggingImplOneTest extends LoggingImplOne { |
||
| 153 | |||
| 154 | public LoggingImplOneTest(File logFile) { |
||
| 155 | super(logFile); |
||
| 156 | } |
||
| 157 | |||
| 158 | @Override |
||
| 159 | protected void start() { |
||
| 160 | // create new Loggers |
||
| 161 | Logger.Component l1 = newLogger("a"); |
||
| 162 | Logger.Component l2 = newLogger("b"); |
||
| 163 | Logger.Component l3 = newLogger("c"); |
||
| 164 | // log things to them |
||
| 165 | l1.log().addLine("1 a says hi"); |
||
| 166 | l1.log().addLine("2 test test"); |
||
| 167 | l2.log().addLine("3 b says hoy"); |
||
| 168 | l1.log().addLine("4 blabla"); |
||
| 169 | l3.log().addLine("5 hop"); |
||
| 170 | } |
||
| 171 | |||
| 172 | public static void main(String[] args) { |
||
| 173 | new LoggingImplOneTest(new File("/tmp/tutorial2-logging-test.txt")).newComponent(); |
||
| 174 | |||
| 175 | } |
||
| 176 | } |
||
| 177 | </pre> |
||
| 178 | |||
| 179 | We can confirm that the file _/tmp/tutorial2-logging-test.txt_ contains the following: |
||
| 180 | <pre> |
||
| 181 | [a] 1 a says hi |
||
| 182 | [a] 2 test test |
||
| 183 | [b] 3 b says hoy |
||
| 184 | [a] 4 blabla |
||
| 185 | [c] 5 hop |
||
| 186 | 3 | Anonyme | </pre> |
| 187 | |||
| 188 | h3. Implementing the Ecosystem and the Species, Second One |