Skip to content

Commit

Permalink
added array reference example
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Mar 11, 2024
1 parent 735e16d commit afd46a3
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public boolean handleStringEvent(String stringToProcess) {
System.out.println("MyNode::handleStringEvent received:" + stringToProcess);
return true;
}

}

public static class MyNode2 {
Expand Down

0 comments on commit afd46a3

Please sign in to comment.