SpeADL Dynamic Tutorial » Historique » Version 3
Anonyme, 14/10/2014 15:21
| 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 | 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 | <pre> |
||
| 68 | package tutorial2.logging.impl; |
||
| 69 | |||
| 70 | import java.io.File; |
||
| 71 | import java.io.FileNotFoundException; |
||
| 72 | import java.io.IOException; |
||
| 73 | import java.io.PrintWriter; |
||
| 74 | |||
| 75 | import tutorial2.logging.Logging; |
||
| 76 | |||
| 77 | public class LoggingImplOne extends Logging { |
||
| 78 | |||
| 79 | private PrintWriter logWriter; |
||
| 80 | |||
| 81 | public LoggingImplOne(File logFile) { |
||
| 82 | // if an exception happens, nothing can be done about it |
||
| 83 | // we just let logStream be null and |
||
| 84 | // the operations of logging won't be done |
||
| 85 | try { |
||
| 86 | logFile.createNewFile(); |
||
| 87 | this.logWriter = new PrintWriter(logFile); |
||
| 88 | } catch (FileNotFoundException e) { |
||
| 89 | this.logWriter = null; |
||
| 90 | } catch (IOException e) { |
||
| 91 | this.logWriter = null; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | @Override |
||
| 96 | protected Logger make_Logger(String name) { |
||
| 97 | // TODO Auto-generated method stub |
||
| 98 | return null; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | </pre> |
||
| 102 | |||
| 103 | 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)_. |
||
| 104 | |||
| 105 | Let's define an inner class insed LoggingImplOne to do that, it will take as a parameter to the constructor the name of the Logger and that will exploit the _logWriter_ member to do the actual logging. |
||
| 106 | <pre> |
||
| 107 | public class LoggingImplOne extends Logging { |
||
| 108 | |||
| 109 | private PrintWriter logWriter; |
||
| 110 | |||
| 111 | // ... |
||
| 112 | |||
| 113 | @Override |
||
| 114 | protected Logger make_Logger(String name) { |
||
| 115 | return new LoggerImpl(name); |
||
| 116 | } |
||
| 117 | |||
| 118 | private class LoggerImpl extends Logger implements ILog { |
||
| 119 | |||
| 120 | private final String name; |
||
| 121 | |||
| 122 | public LoggerImpl(String name) { |
||
| 123 | this.name = name; |
||
| 124 | } |
||
| 125 | |||
| 126 | @Override |
||
| 127 | protected ILog make_log() { |
||
| 128 | return this; |
||
| 129 | } |
||
| 130 | |||
| 131 | @Override |
||
| 132 | public void addLine(String line) { |
||
| 133 | logWriter.println("["+name+"] "+ line); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | </pre> |
||
| 138 | |||
| 139 | h3. Implementing the Ecosystem and the Species, Second One |