diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/BatchCallbacks.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/BatchCallbacks.java new file mode 100644 index 0000000..c396e35 --- /dev/null +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/BatchCallbacks.java @@ -0,0 +1,40 @@ +package com.fluxtion.example.reference.execution; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.runtime.annotations.OnBatchEnd; +import com.fluxtion.runtime.annotations.OnBatchPause; +import com.fluxtion.runtime.annotations.OnEventHandler; +import com.fluxtion.runtime.lifecycle.BatchHandler; + +public class BatchCallbacks { + + public static class MyNode { + @OnEventHandler + public boolean handleStringEvent(String stringToProcess) { + System.out.println("MyNode event received:" + stringToProcess); + return true; + } + + @OnBatchPause + public void batchPause(){ + System.out.println("MyNode::batchPause"); + } + + @OnBatchEnd + public void batchEnd(){ + System.out.println("MyNode::batchEnd"); + } + } + + public static void main(String[] args) { + var processor = Fluxtion.interpret(new MyNode()); + processor.init(); + + processor.onEvent("test"); + + //use BatchHandler service + BatchHandler batchHandler = (BatchHandler)processor; + batchHandler.batchPause(); + batchHandler.batchEnd(); + } +} diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ExportingService.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ExportingService.java new file mode 100644 index 0000000..6988ffc --- /dev/null +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ExportingService.java @@ -0,0 +1,52 @@ +package com.fluxtion.example.reference.execution; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.runtime.annotations.ExportService; +import com.fluxtion.runtime.annotations.OnTrigger; + +import java.util.function.IntSupplier; + +public class ExportingService { + public interface MyService { + void addNumbers(int a, int b); + } + + public static class MyServiceImpl implements @ExportService MyService, IntSupplier { + + private int sum; + + @Override + public void addNumbers(int a, int b) { + System.out.printf("adding %d + %d %n", a, b); + sum = a + b; + } + + @Override + public int getAsInt() { + return sum; + } + } + + public static class ResultPublisher { + private final IntSupplier intSupplier; + + public ResultPublisher(IntSupplier intSupplier) { + this.intSupplier = intSupplier; + } + + @OnTrigger + public boolean printResult() { + System.out.println("result - " + intSupplier.getAsInt()); + return true; + } + } + + public static void main(String[] args) { + var processor = Fluxtion.interpret(new ResultPublisher(new MyServiceImpl())); + processor.init(); + + //get the exported service + MyService myService = processor.getExportedService(); + myService.addNumbers(30, 12); + } +} diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/FilterVariable.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/FilterVariable.java new file mode 100644 index 0000000..bc7b71e --- /dev/null +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/FilterVariable.java @@ -0,0 +1,35 @@ +package com.fluxtion.example.reference.execution; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.runtime.annotations.FilterId; +import com.fluxtion.runtime.annotations.OnEventHandler; +import com.fluxtion.runtime.annotations.OnParentUpdate; +import com.fluxtion.runtime.annotations.OnTrigger; +import com.fluxtion.runtime.event.Signal; + +public class FilterVariable { + + public static class MyNode { + private final String name; + + public MyNode(String name) { + this.name = name; + } + + + @OnEventHandler(filterVariable = "name") + public boolean handleIntSignal(Signal.IntSignal intSignal) { + System.out.printf("MyNode-%s::handleIntSignal - %s%n", name, intSignal.getValue()); + return true; + } + } + + public static void main(String[] args) { + var processor = Fluxtion.interpret(new MyNode("A"), new MyNode("B")); + processor.init(); + + processor.publishIntSignal("A", 22); + processor.publishIntSignal("B", 45); + processor.publishIntSignal("C", 100); + } +} diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ForkingTrigger.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ForkingTrigger.java new file mode 100644 index 0000000..47fe1ba --- /dev/null +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/ForkingTrigger.java @@ -0,0 +1,76 @@ +package com.fluxtion.example.reference.execution; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.runtime.annotations.OnEventHandler; +import com.fluxtion.runtime.annotations.OnTrigger; + +import java.time.Duration; +import java.time.Instant; +import java.util.Random; + +public class ForkingTrigger { + + public static class MyNode { + @OnEventHandler + public boolean handleStringEvent(String stringToProcess) { + System.out.printf("%s MyNode::handleStringEvent %n", Thread.currentThread().getName()); + return true; + } + } + + public static class ForkedChild { + private final MyNode myNode; + private final int id; + + public ForkedChild(MyNode myNode, int id) { + this.myNode = myNode; + this.id = id; + } + + @OnTrigger(parallelExecution = true) + public boolean triggered() { + int millisSleep = new Random(id).nextInt(25, 200); + String threadName = Thread.currentThread().getName(); + System.out.printf("%s ForkedChild[%d]::triggered - sleep:%d %n", threadName, id, millisSleep); + try { + Thread.sleep(millisSleep); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.printf("%s ForkedChild[%d]::complete %n", threadName, id); + return true; + } + } + + public static class ResultJoiner { + private final ForkedChild[] forkedTasks; + + public ResultJoiner(ForkedChild[] forkedTasks) { + this.forkedTasks = forkedTasks; + } + + public ResultJoiner(int forkTaskNumber){ + MyNode myNode = new MyNode(); + forkedTasks = new ForkedChild[forkTaskNumber]; + for (int i = 0; i < forkTaskNumber; i++) { + forkedTasks[i] = new ForkedChild(myNode, i); + } + } + + @OnTrigger + public boolean complete(){ + System.out.printf("%s ResultJoiner:complete %n%n", Thread.currentThread().getName()); + return true; + } + } + + public static void main(String[] args) { + var processor = Fluxtion.interpret(new ResultJoiner(5)); + processor.init(); + + Instant start = Instant.now(); + processor.onEvent("test"); + + System.out.printf("duration: %d milliseconds%n", Duration.between(start, Instant.now()).toMillis()); + } +} diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/NopPropagateServiceMethod.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/NopPropagateServiceMethod.java new file mode 100644 index 0000000..0cc4010 --- /dev/null +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/NopPropagateServiceMethod.java @@ -0,0 +1,65 @@ +package com.fluxtion.example.reference.execution; + +import com.fluxtion.compiler.Fluxtion; +import com.fluxtion.runtime.annotations.ExportService; +import com.fluxtion.runtime.annotations.NoPropagateFunction; +import com.fluxtion.runtime.annotations.OnTrigger; + +import java.util.function.IntSupplier; + +public class NopPropagateServiceMethod { + public interface MyService { + void cumulativeSum(int a); + void reset(); + } + + public static class MyServiceImpl implements @ExportService MyService, IntSupplier { + + private int sum; + + @Override + public void cumulativeSum(int a) { + sum += a; + System.out.printf("MyServiceImpl::adding %d cumSum: %d %n", a, sum); + } + + @Override + @NoPropagateFunction + public void reset() { + sum = 0; + System.out.printf("MyServiceImpl::reset cumSum: %d %n", sum); + } + + @Override + public int getAsInt() { + return sum; + } + } + + public static class ResultPublisher { + private final IntSupplier intSupplier; + + public ResultPublisher(IntSupplier intSupplier) { + this.intSupplier = intSupplier; + } + + @OnTrigger + public boolean printResult() { + System.out.println("ResultPublisher::result - " + intSupplier.getAsInt()); + return true; + } + } + + public static void main(String[] args) { + var processor = Fluxtion.interpret(new ResultPublisher(new MyServiceImpl())); + processor.init(); + + //get the exported service + MyService myService = processor.getExportedService(); + myService.cumulativeSum(11); + myService.cumulativeSum(31); + System.out.println(); + + myService.reset(); + } +} diff --git a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/OverrideTriggerReference.java b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/OverrideTriggerReference.java index 7a1fea1..b119a16 100644 --- a/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/OverrideTriggerReference.java +++ b/reference-examples/runtime-execution/src/main/java/com/fluxtion/example/reference/execution/OverrideTriggerReference.java @@ -13,7 +13,6 @@ public boolean handleStringEvent(String stringToProcess) { System.out.println("MyNode::handleStringEvent received:" + stringToProcess); return true; } - } public static class MyNode2 {