-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
510fec4
commit 15fb5fb
Showing
16 changed files
with
637 additions
and
27 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
compiler/src/main/java/com/fluxtion/compiler/builder/dataflow/ColumFlowBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.fluxtion.compiler.builder.dataflow; | ||
|
||
import com.fluxtion.runtime.EventProcessorBuilderService; | ||
import com.fluxtion.runtime.dataflow.FlowSupplier; | ||
import com.fluxtion.runtime.dataflow.TriggeredFlowFunction; | ||
import com.fluxtion.runtime.dataflow.column.Column; | ||
import com.fluxtion.runtime.dataflow.column.ColumnFilterFlowFunction; | ||
import com.fluxtion.runtime.dataflow.column.ColumnMapFlowFunction; | ||
import com.fluxtion.runtime.dataflow.function.MapFlowFunction; | ||
import com.fluxtion.runtime.dataflow.function.NotifyFlowFunction; | ||
import com.fluxtion.runtime.dataflow.function.PeekFlowFunction; | ||
import com.fluxtion.runtime.dataflow.function.PushFlowFunction; | ||
import com.fluxtion.runtime.dataflow.helpers.Peekers; | ||
import com.fluxtion.runtime.output.SinkPublisher; | ||
import com.fluxtion.runtime.partition.LambdaReflection; | ||
|
||
import java.util.List; | ||
|
||
public class ColumFlowBuilder<T> implements FlowDataSupplier<FlowSupplier<Column<T>>> { | ||
|
||
protected TriggeredFlowFunction<Column<T>> eventStream; | ||
|
||
public ColumFlowBuilder(TriggeredFlowFunction<Column<T>> eventStream) { | ||
this.eventStream = eventStream; | ||
EventProcessorBuilderService.service().add(eventStream); | ||
} | ||
|
||
@Override | ||
public FlowSupplier<Column<T>> flowSupplier() { | ||
return eventStream; | ||
} | ||
|
||
public <R> ColumFlowBuilder<R> map(LambdaReflection.SerializableFunction<T, R> mapColumnFunction) { | ||
LambdaReflection.SerializableFunction<Column<T>, Column<R>> function = new ColumnMapFlowFunction<>(mapColumnFunction)::map; | ||
MapFlowFunction.MapRef2RefFlowFunction<Column<T>, Column<R>, TriggeredFlowFunction<Column<T>>> mapRef2RefFlowFunction = new MapFlowFunction.MapRef2RefFlowFunction<>(eventStream, function); | ||
return new ColumFlowBuilder<>(mapRef2RefFlowFunction); | ||
} | ||
|
||
public ColumFlowBuilder<T> filter(LambdaReflection.SerializableFunction<T, Boolean> filterFunction) { | ||
LambdaReflection.SerializableFunction<Column<T>, Column<T>> filter = new ColumnFilterFlowFunction<>(filterFunction)::filter; | ||
MapFlowFunction.MapRef2RefFlowFunction<Column<T>, Column<T>, TriggeredFlowFunction<Column<T>>> mapRef2RefFlowFunction = new MapFlowFunction.MapRef2RefFlowFunction<>(eventStream, filter); | ||
return new ColumFlowBuilder<>(mapRef2RefFlowFunction); | ||
} | ||
|
||
public FlowBuilder<List<T>> asList() { | ||
return new FlowBuilder<>(new MapFlowFunction.MapRef2RefFlowFunction<>(eventStream, Column::values)); | ||
} | ||
|
||
//OUTPUTS - START | ||
public ColumFlowBuilder<T> notify(Object target) { | ||
EventProcessorBuilderService.service().add(target); | ||
return new ColumFlowBuilder<>(new NotifyFlowFunction<>(eventStream, target)); | ||
} | ||
|
||
public ColumFlowBuilder<T> sink(String sinkId) { | ||
return push(new SinkPublisher<>(sinkId)::publish); | ||
} | ||
|
||
public ColumFlowBuilder<T> push(LambdaReflection.SerializableConsumer<Column<T>> pushFunction) { | ||
if (pushFunction.captured().length > 0) { | ||
EventProcessorBuilderService.service().add(pushFunction.captured()[0]); | ||
} | ||
return new ColumFlowBuilder<>(new PushFlowFunction<>(eventStream, pushFunction)); | ||
} | ||
|
||
public ColumFlowBuilder<T> peek(LambdaReflection.SerializableConsumer<Column<T>> peekFunction) { | ||
return new ColumFlowBuilder<>(new PeekFlowFunction<>(eventStream, peekFunction)); | ||
} | ||
|
||
|
||
public <R> ColumFlowBuilder<T> console(String in, LambdaReflection.SerializableFunction<Column<T>, R> peekFunction) { | ||
peek(Peekers.console(in, peekFunction)); | ||
return this; | ||
} | ||
|
||
public ColumFlowBuilder<T> console(String in) { | ||
peek(Peekers.console(in, Peekers::columnToList)); | ||
return this; | ||
} | ||
|
||
public ColumFlowBuilder<T> console() { | ||
return console("{}"); | ||
} | ||
|
||
//META-DATA | ||
public ColumFlowBuilder<T> id(String nodeId) { | ||
EventProcessorBuilderService.service().add(eventStream, nodeId); | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
compiler/src/test/java/com/fluxtion/compiler/builder/dataflow/ColumnTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.fluxtion.compiler.builder.dataflow; | ||
|
||
import com.fluxtion.compiler.generation.util.CompiledAndInterpretedSepTest; | ||
import com.fluxtion.compiler.generation.util.MultipleSepTargetInProcessTest; | ||
import com.fluxtion.runtime.dataflow.column.Column; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import org.junit.Test; | ||
|
||
public class ColumnTest extends MultipleSepTargetInProcessTest { | ||
public ColumnTest(CompiledAndInterpretedSepTest.SepTestConfig testConfig) { | ||
super(testConfig); | ||
} | ||
|
||
@Test | ||
public void buildColumn() { | ||
writeSourceFile = true; | ||
sep(c -> { | ||
DataFlow.buildColumn(Data.class).map(Data::getName).console("column:{}"); | ||
}); | ||
onEvent(new Data(12, "Steve H")); | ||
onEvent(new Data(65, "Steve G")); | ||
onEvent(new Data(7, "Bob")); | ||
} | ||
|
||
@Test | ||
public void buildColumnFromProperty() { | ||
writeSourceFile = true; | ||
sep(c -> { | ||
DataFlow.buildColumn(Data::getName).console("column:{}"); | ||
}); | ||
onEvent(new Data(12, "Steve H")); | ||
onEvent(new Data(65, "Steve G")); | ||
onEvent(new Data(7, "Bob")); | ||
} | ||
|
||
@Test | ||
public void buildColumnFromPropertyAndMap() { | ||
writeSourceFile = true; | ||
sep(c -> { | ||
DataFlow.buildColumn(Data::getAge) | ||
.map(ColumnTest::multiply10x) | ||
.console("column:{}"); | ||
}); | ||
onEvent(new Data(12, "Steve H")); | ||
onEvent(new Data(65, "Steve G")); | ||
onEvent(new Data(7, "Bob")); | ||
} | ||
|
||
@Test | ||
public void buildColumnPublishToSink() { | ||
writeSourceFile = true; | ||
sep(c -> { | ||
DataFlow.buildColumn(Data::getName).sink("xxx"); | ||
}); | ||
addSink("xxx", (Column<Data> o) -> { | ||
System.out.println("sink -> " + o.values()); | ||
}); | ||
onEvent(new Data(12, "Steve H")); | ||
onEvent(new Data(65, "Steve G")); | ||
onEvent(new Data(7, "Bob")); | ||
} | ||
|
||
@Test | ||
public void buildColumnFilterPublishToSink() { | ||
writeSourceFile = true; | ||
sep(c -> { | ||
DataFlow.buildColumn(Data::getName).filter(ColumnTest::startsWithSteve).sink("xxx"); | ||
}); | ||
addSink("xxx", (Column<Data> o) -> { | ||
System.out.println("sink -> " + o.values()); | ||
}); | ||
onEvent(new Data(12, "Steve H")); | ||
onEvent(new Data(7, "Bob")); | ||
onEvent(new Data(65, "Steve G")); | ||
} | ||
|
||
public static boolean startsWithSteve(String in) { | ||
return in.startsWith("Steve"); | ||
} | ||
|
||
public static int multiply10x(int in) { | ||
return in * 10; | ||
} | ||
|
||
@lombok.Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class Data { | ||
private int age; | ||
private String name; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.