Skip to content

Commit

Permalink
query and control examples
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Apr 27, 2024
1 parent 8480b6e commit 7439209
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@
import com.fluxtion.runtime.audit.LogRecord;
import com.fluxtion.runtime.time.Clock;

public class AuditEncodeExample {
public class AuditControlExample {
public static void main(String[] args) {
var processor = Fluxtion.interpret(c ->{
c.addNode(new MyAuditingNode());
c.addEventAudit();
});

processor.init();

//AUDIT IS INFO BY DEFAULT
processor.onEvent("detailed message 1");

//CHANGE LOG LEVEL DYNAMICALLY
processor.setAuditLogLevel(EventLogControlEvent.LogLevel.DEBUG);
processor.onEvent("detailed message 2");

//REPLACE LOGRECORD ENCODER
processor.setAuditLogRecordEncoder(new MyLogEncoder(Clock.DEFAULT_CLOCK));
//REPLACE LOGRECORD PROCESSOR
processor.setAuditLogProcessor(logRecord -> {
System.err.println("WARNING -> "+ logRecord.toString());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.annotations.Initialise;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.audit.EventLogControlEvent;
import com.fluxtion.runtime.audit.EventLogNode;

public class AuditExample {
Expand All @@ -12,14 +11,9 @@ public static void main(String[] args) {
c.addNode(new MyAuditingNode());
c.addEventAudit();
});

processor.init();

//AUDIT IS INFO BY DEFAULT
processor.onEvent("detailed message 1");

//change log level dynamically
processor.setAuditLogLevel(EventLogControlEvent.LogLevel.DEBUG);
processor.onEvent("detailed message 2");
}

public static class MyAuditingNode extends EventLogNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fluxtion.example.reference.integration;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.time.Clock;

import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.atomic.LongAdder;

public class ClockExample {

public static void main(String[] args) {
var processor = Fluxtion.interpret(new TimeLogger());
processor.init();
//PRINT CURRENT TIME
processor.onEvent(DateFormat.getDateTimeInstance());

//USE A SYNTHETIC STRATEGY TO SET TIME FOR THE PROCESSOR CLOCK
LongAdder syntheticTime = new LongAdder();
processor.setClockStrategy(syntheticTime::longValue);

//SET A NEW TIME - GOING BACK IN TIME!!
syntheticTime.add(1_000_000_000);
processor.onEvent(DateFormat.getDateTimeInstance());

//SET A NEW TIME - BACK TO THE FUTURE
syntheticTime.add(1_800_000_000_000L);
processor.onEvent(DateFormat.getDateTimeInstance());
}


public static class TimeLogger {
public Clock wallClock = Clock.DEFAULT_CLOCK;

@OnEventHandler
public boolean publishTime(DateFormat dateFormat) {
System.out.println("time " + dateFormat.format(new Date(wallClock.getWallClockTime())));
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
package com.fluxtion.example.reference.integration;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.EventProcessorContext;
import com.fluxtion.runtime.annotations.Start;
import com.fluxtion.runtime.annotations.builder.Inject;

public class ContextParamInput {
public static void main(String[] args) {
var processor = Fluxtion.interpret(new ContextParamReader());
processor.init();

processor.addContextParameter("myContextParam1", "[param1: update 1]");
processor.start();

processor.addContextParameter("myContextParam1", "[param1: update 2]");
processor.addContextParameter("myContextParam2", "[param2: update 1]");
processor.start();
}

public static class ContextParamReader {
@Inject
public EventProcessorContext context;

@Start
public void start() {
System.out.println("myContextParam1 -> " + context.getContextProperty("myContextParam1"));
System.out.println("myContextParam2 -> " + context.getContextProperty("myContextParam2"));
System.out.println();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fluxtion.example.reference.integration;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.compiler.builder.dataflow.DataFlow;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.dataflow.helpers.Mappers;
import com.fluxtion.runtime.node.NamedNode;
import lombok.Data;

public class GetFlowNodeByIdExample {
public static void main(String[] args) throws NoSuchFieldException {
var processor = Fluxtion.interpret(c ->{
DataFlow.subscribe(String.class)
.filter(s -> s.equalsIgnoreCase("monday"))
.mapToInt(Mappers.count())
.id("MondayChecker")//this makes the wrapped value accessible via the id
.console("Monday is triggered");
});
processor.init();

processor.onEvent("Monday");
processor.onEvent("Tuesday");
processor.onEvent("Wednesday");

//ACCESS THE WRAPPED VALUE BY ITS ID
Integer mondayCheckerCount = processor.getStreamed("MondayChecker");
System.out.println("Monday count:" + mondayCheckerCount + "\n");

//ACCESS THE WRAPPED VALUE BY ITS ID
processor.onEvent("Monday");
mondayCheckerCount = processor.getStreamed("MondayChecker");
System.out.println("Monday count:" + mondayCheckerCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fluxtion.example.reference.integration;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.compiler.builder.dataflow.DataFlow;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.node.NamedNode;

public class GetNodeByIdExample {
public static void main(String[] args) throws NoSuchFieldException {
var processor = Fluxtion.interpret(c ->{
DataFlow.subscribeToNode(new DirtyStateNode())
.console("Monday is triggered");
});
processor.init();

processor.onEvent("Monday");
processor.onEvent("Tuesday");
processor.onEvent("Wednesday");

DirtyStateNode dirtyStateNode = processor.getNodeById("MondayChecker");
System.out.println("Monday count:" + dirtyStateNode.getMondayCount() + "\n");


processor.onEvent("Monday");
System.out.println("Monday count:" + dirtyStateNode.getMondayCount());
}

public static class DirtyStateNode implements NamedNode {

private int mondayCounter = 0;

@OnEventHandler
public boolean checkIsMonday(String day){
boolean isMonday = day.equalsIgnoreCase("monday");
mondayCounter += isMonday ? 1 : 0;
return isMonday;
}

public int getMondayCount() {
return mondayCounter;
}

@Override
public String getName() {
return "MondayChecker";
}
}
}

0 comments on commit 7439209

Please sign in to comment.