From 86ffe70e7e7a1ea13e2d5f56b1f49e62374840b1 Mon Sep 17 00:00:00 2001 From: command-line Date: Sun, 21 Apr 2024 15:56:31 +0100 Subject: [PATCH] add aot helloworld, change original helloworld to be interpreted --- imperative-helloworld-aot/pom.xml | 32 ++++++++++++++++ .../imperative/helloworld/AotBuilder.java | 3 +- .../imperative/helloworld/BreachNotifier.java | 24 ++++++++++++ .../helloworld/DataSumCalculator.java | 38 +++++++++++++++++++ .../imperative/helloworld/Event_A.java | 4 ++ .../helloworld/Event_A_Handler.java | 28 ++++++++++++++ .../imperative/helloworld/Event_B.java | 4 ++ .../helloworld/Event_B_Handler.java | 28 ++++++++++++++ .../example/imperative/helloworld/Main.java | 30 +++++++++++++++ .../generated/BreachNotifierProcessor.java | 0 imperative-helloworld/pom.xml | 20 +--------- .../example/imperative/helloworld/Main.java | 3 +- pom.xml | 1 + 13 files changed, 193 insertions(+), 22 deletions(-) create mode 100644 imperative-helloworld-aot/pom.xml rename {imperative-helloworld => imperative-helloworld-aot}/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java (84%) create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/BreachNotifier.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/DataSumCalculator.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A_Handler.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B_Handler.java create mode 100644 imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java rename {imperative-helloworld => imperative-helloworld-aot}/src/main/java/com/fluxtion/example/imperative/helloworld/generated/BreachNotifierProcessor.java (100%) diff --git a/imperative-helloworld-aot/pom.xml b/imperative-helloworld-aot/pom.xml new file mode 100644 index 0000000..57441e3 --- /dev/null +++ b/imperative-helloworld-aot/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + com.fluxtion.example + example.master + 1.0.0-SNAPSHOT + + + imperative-helloworld-aot + imperative :: hello world - AOT + + + + + com.fluxtion + fluxtion-maven-plugin + 3.0.14 + + + + scan + + + + + + + + \ No newline at end of file diff --git a/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java similarity index 84% rename from imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java rename to imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java index 09642c5..8faf6d4 100644 --- a/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/AotBuilder.java @@ -3,10 +3,11 @@ import com.fluxtion.compiler.EventProcessorConfig; import com.fluxtion.compiler.FluxtionCompilerConfig; import com.fluxtion.compiler.FluxtionGraphBuilder; +import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor; /** * This class is discovered by the maven plugin to generate an {@link com.fluxtion.runtime.EventProcessor} ahead of time - * during the compilation phase. The output is the {@link com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor} source + * during the compilation phase. The output is the {@link BreachNotifierProcessor} source * file. *

* Nodes are added to the graph using the {{@link #buildGraph(EventProcessorConfig)}} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/BreachNotifier.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/BreachNotifier.java new file mode 100644 index 0000000..91d3f94 --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/BreachNotifier.java @@ -0,0 +1,24 @@ +package com.fluxtion.example.imperative.helloworld; + +import com.fluxtion.runtime.annotations.OnTrigger; + +/** + * The trigger method, printWarning on this class is invoked when a change is propagated from the parent node + */ +public class BreachNotifier { + private final DataSumCalculator dataAddition; + + public BreachNotifier(DataSumCalculator dataAddition) { + this.dataAddition = dataAddition; + } + + public BreachNotifier() { + this(new DataSumCalculator()); + } + + @OnTrigger + public boolean printWarning() { + System.out.println("WARNING DataSumCalculator value is greater than 100 sum = " + dataAddition.getSum()); + return true; + } +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/DataSumCalculator.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/DataSumCalculator.java new file mode 100644 index 0000000..421f0d0 --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/DataSumCalculator.java @@ -0,0 +1,38 @@ +package com.fluxtion.example.imperative.helloworld; + +import com.fluxtion.runtime.annotations.OnTrigger; +import lombok.Getter; + +/** + * Aggregates two event sources and calculates the sum of their values, whenever either changes. The calculate method + * notifies a propagation of a change when the sum is > 100 + */ +public class DataSumCalculator { + + private final Event_A_Handler eventAHandler; + private final Event_B_Handler eventBHandler; + @Getter + private double sum; + + public DataSumCalculator(Event_A_Handler eventAHandler, Event_B_Handler eventBHandler) { + this.eventAHandler = eventAHandler; + this.eventBHandler = eventBHandler; + } + + public DataSumCalculator() { + this(new Event_A_Handler(), new Event_B_Handler()); + } + + /** + * The {@link OnTrigger} annotation marks this method to be called if any parents have changed + * + * @return flag indicating a change and a propagation of the event wave to child dependencies if the sum > 100 + */ + @OnTrigger + public boolean calculate() { + sum = eventAHandler.getValue() + eventBHandler.getValue(); + System.out.println("sum:" + sum); + return sum > 100; + } + +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A.java new file mode 100644 index 0000000..c0369fa --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A.java @@ -0,0 +1,4 @@ +package com.fluxtion.example.imperative.helloworld; + +public record Event_A(double value) { +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A_Handler.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A_Handler.java new file mode 100644 index 0000000..56dded7 --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_A_Handler.java @@ -0,0 +1,28 @@ +package com.fluxtion.example.imperative.helloworld; + +import com.fluxtion.runtime.annotations.OnEventHandler; +import lombok.Getter; + +/** + * An entry point to {@link com.fluxtion.runtime.EventProcessor} for {@link Event_A} events. The {@link OnEventHandler} + * defines a method as an entry point. + * + */ +@Getter +public class Event_A_Handler { + private double value; + + /** + * The {@link OnEventHandler} annotation marks this method as the start of an execution path with a {@link Event_A}. + * Invoked when the {@link com.fluxtion.runtime.EventProcessor} receives a {@link Event_A} event. + * + * @param eventA the input event + * @return flag indicating a change and a propagation of the event wave to child dependencies + */ + @OnEventHandler + public boolean data1Update(Event_A eventA) { + value = eventA.value(); + return true; + } + +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B.java new file mode 100644 index 0000000..65886c7 --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B.java @@ -0,0 +1,4 @@ +package com.fluxtion.example.imperative.helloworld; + +public record Event_B(double value) { +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B_Handler.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B_Handler.java new file mode 100644 index 0000000..d6a6055 --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Event_B_Handler.java @@ -0,0 +1,28 @@ +package com.fluxtion.example.imperative.helloworld; + +import com.fluxtion.runtime.annotations.OnEventHandler; +import lombok.Getter; + +/** + * An entry point to {@link com.fluxtion.runtime.EventProcessor} for {@link Event_B} events. The {@link OnEventHandler} + * defines a method as an entry point. + * + */ +@Getter +public class Event_B_Handler { + private double value; + + /** + * The {@link OnEventHandler} annotation marks this method as the start of an execution path with a {@link Event_B}. + * Invoked when the {@link com.fluxtion.runtime.EventProcessor} receives a {@link Event_B} event. + * + * @param eventB the input event + * @return flag indicating a change and a propagation of the event wave to child dependencies + */ + @OnEventHandler + public boolean data1Update(Event_B eventB) { + value = eventB.value(); + return true; + } + +} diff --git a/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java new file mode 100644 index 0000000..50ca45c --- /dev/null +++ b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java @@ -0,0 +1,30 @@ +package com.fluxtion.example.imperative.helloworld; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor; +import com.fluxtion.runtime.annotations.OnEventHandler; +import com.fluxtion.runtime.annotations.OnTrigger; + + +/** + * creates a processing graph imperatively, extracts double values from events, calculates the sum and prints a + * message to console if the sum is greater than a 100. + *

+ * Uses @{@link OnEventHandler} annotation to declare the entry point of an execution path + * {@link OnTrigger} annotated methods indicate call back methods to be invoked if a parent propagates a change. + * The return flag from the {@link OnTrigger} method indicates if the event should be propagated. In this case + * the event is only propagated if the sum > 100. + */ +public class Main { + private static final boolean USE_AOT = true; + + public static void main(String[] args) { + var eventProcessor = USE_AOT ? new BreachNotifierProcessor() : Fluxtion.interpret(new BreachNotifier()); + eventProcessor.init(); + eventProcessor.onEvent(new Event_A(34.4)); + eventProcessor.onEvent(new Event_B(52.1)); + eventProcessor.onEvent(new Event_A(105));//should create a breach warning + eventProcessor.onEvent(new Event_A(12.4)); + } + +} \ No newline at end of file diff --git a/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/generated/BreachNotifierProcessor.java b/imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/generated/BreachNotifierProcessor.java similarity index 100% rename from imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/generated/BreachNotifierProcessor.java rename to imperative-helloworld-aot/src/main/java/com/fluxtion/example/imperative/helloworld/generated/BreachNotifierProcessor.java diff --git a/imperative-helloworld/pom.xml b/imperative-helloworld/pom.xml index 6c86cd0..71c1469 100644 --- a/imperative-helloworld/pom.xml +++ b/imperative-helloworld/pom.xml @@ -2,31 +2,13 @@ + 4.0.0 example.master com.fluxtion.example 1.0.0-SNAPSHOT - 4.0.0 imperative-helloworld imperative :: hello world - - - - - com.fluxtion - fluxtion-maven-plugin - 3.0.14 - - - - scan - - - - - - - \ No newline at end of file diff --git a/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java b/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java index 50ca45c..62c225b 100644 --- a/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java +++ b/imperative-helloworld/src/main/java/com/fluxtion/example/imperative/helloworld/Main.java @@ -1,7 +1,6 @@ package com.fluxtion.example.imperative.helloworld; import com.fluxtion.compiler.Fluxtion; -import com.fluxtion.example.imperative.helloworld.generated.BreachNotifierProcessor; import com.fluxtion.runtime.annotations.OnEventHandler; import com.fluxtion.runtime.annotations.OnTrigger; @@ -19,7 +18,7 @@ public class Main { private static final boolean USE_AOT = true; public static void main(String[] args) { - var eventProcessor = USE_AOT ? new BreachNotifierProcessor() : Fluxtion.interpret(new BreachNotifier()); + var eventProcessor = Fluxtion.interpret(new BreachNotifier()); eventProcessor.init(); eventProcessor.onEvent(new Event_A(34.4)); eventProcessor.onEvent(new Event_B(52.1)); diff --git a/pom.xml b/pom.xml index c45f7a4..507992f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ along with this program. If not, see unplugged/part1 imperative-helloworld functional-helloworld + imperative-helloworld-aot cookbook cookbook-functional dispatch-performnce