... | ... | @@ -63,6 +63,79 @@ We create instances of our three stages and connect them correctly. |
|
|
Please remember that TeeTime only supports uni-directed communication.
|
|
|
A sample solution can look like [this](https://build.se.informatik.uni-kiel.de/teetime/teetime/blob/master/src/test/java/teetime/examples/filecounter/PrintResultConfig.java).
|
|
|
|
|
|
# TeeTime Configuration Builder
|
|
|
|
|
|
With the TeeTime configuration builder you can simply connect your stages in a short, type-safe, and fluent way.
|
|
|
|
|
|
## How to use it
|
|
|
|
|
|
Typically, you create your configurations by instantiating all stages and connecting them via lines of `connectPorts(...)` calls.
|
|
|
|
|
|
```java
|
|
|
public class MyConfiguration extends Configuration {
|
|
|
|
|
|
public MyConfiguration(final List<String> strings) {
|
|
|
final InitialElementProducer<String> producer = new InitialElementProducer<>(strings);
|
|
|
final ToLowerCase toLowerCase = new ToLowerCase();
|
|
|
final WordcharacterFilter wordcharacterFilter = new WordcharacterFilter();
|
|
|
final WordCounter wordCounter = new WordCounter();
|
|
|
final Printer<CountingMap<String>> printer = new Printer<>();
|
|
|
|
|
|
this.connectPorts(producer.getOutputPort(), toLowerCase.getInputPort());
|
|
|
this.connectPorts(toLowerCase.getOutputPort(), wordcharacterFilter.getInputPort());
|
|
|
this.connectPorts(wordcharacterFilter.getOutputPort(), wordCounter.getInputPort());
|
|
|
this.connectPorts(wordCounter.getOutputPort(), printer.getInputPort());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
```
|
|
|
With the TeeTime configuration builder you can simply connect the stages by calling `from(...)`, `to(...)`, and `end(...)`.
|
|
|
|
|
|
```java
|
|
|
public MyConfiguration(final List<String> strings) {
|
|
|
final InitialElementProducer<String> producer = new InitialElementProducer<>(strings);
|
|
|
final ToLowerCase toLowerCase = new ToLowerCase();
|
|
|
final WordcharacterFilter wordcharacterFilter = new WordcharacterFilter();
|
|
|
final WordCounter wordCounter = new WordCounter();
|
|
|
final Printer<CountingMap<String>> printer = new Printer<>();
|
|
|
|
|
|
this.from(producer).to(toLowerCase).to(wordcharacterFilter).to(wordCounter).end(printer);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
In addition to shorter and more expressive code, the configuration builder forces you to use every stage port exactly once.
|
|
|
Furthermore, you do not have to declare all your stages as local variables if you do not need access to them.
|
|
|
|
|
|
```java
|
|
|
public MyConfiguration(final List<String> strings) {
|
|
|
this.from(new InitialElementProducer<>(strings))
|
|
|
.to(new ToLowerCase())
|
|
|
.to(new WordcharacterFilter())
|
|
|
.to(new WordCounter())
|
|
|
.end(new Printer<>());
|
|
|
}
|
|
|
```
|
|
|
|
|
|
If you do not need an own class for your configuration, you can create configurations straightforward by using the `ConfigurationBuilder.from(...)` method as shown in the next example.
|
|
|
|
|
|
```java
|
|
|
final Configuration myConfiguration = ConfigurationBuilder
|
|
|
.from(new InitialElementProducer<>(strings))
|
|
|
.to(new ToLowerCase())
|
|
|
.to(new WordcharacterFilter())
|
|
|
.to(new WordCounter())
|
|
|
.end(new Printer<>());
|
|
|
```
|
|
|
|
|
|
You may want to use stages that do implement the `ITransformation` interface, for example, because they have multiple output ports like the `InstanceOfFilter`. For this case you an alternative form of the `to(..)` method, in which you can specify the stage´s input and output port you want use. This is done via a lambda function as shown below.
|
|
|
|
|
|
```java
|
|
|
final Configuration myConfiguration = ConfigurationBuilder
|
|
|
.from(new MyProducerStage())
|
|
|
.to(new MyFilterStage(), s -> s.getMyInputPort(), s -> s.getMyOutputPort())
|
|
|
.end(new MyConsumerStage());
|
|
|
```
|
|
|
|
|
|
### Execute that Configuration
|
|
|
|
|
|
After all the hard work you may be way to excited to go to bed yet.
|
... | ... | |