Skip to content

Commit

Permalink
library examples
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Apr 28, 2024
1 parent 86f7a0b commit bff7711
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fluxtion.example.reference.libraryfunction;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.annotations.Initialise;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.audit.EventLogNode;

public class AuditExample {
public static class MyAuditingNode extends EventLogNode {

@Initialise
public void init(){
auditLog.info("MyAuditingNode", "init");
auditLog.info("MyAuditingNode_debug", "some debug message");
}

@OnEventHandler
public boolean stringEvent(String event) {
auditLog.info("event", event);
auditLog.debug("charCount", event.length());
return true;
}
}
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");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fluxtion.example.reference.execution;
package com.fluxtion.example.reference.libraryfunction;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.annotations.OnEventHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fluxtion.example.reference.libraryfunction;

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

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

public class ClockExample {

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;
}
}

public static void main(String[] args) throws InterruptedException {
var processor = Fluxtion.interpret(new TimeLogger());
processor.init();
//PRINT CURRENT TIME
processor.onEvent(new SimpleDateFormat("HH:mm:ss.SSS"));

//SLEEP AND PRINT TIME
Thread.sleep(100);
processor.onEvent(new SimpleDateFormat("HH:mm:ss.SSS"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fluxtion.example.reference.libraryfunction;

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 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();
}
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fluxtion.example.reference.libraryfunction;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.compiler.builder.dataflow.DataFlow;
import com.fluxtion.runtime.annotations.OnTrigger;
import com.fluxtion.runtime.annotations.builder.Inject;
import com.fluxtion.runtime.callback.DirtyStateMonitor;
import com.fluxtion.runtime.dataflow.FlowSupplier;
import com.fluxtion.runtime.node.NamedNode;

public class DirtyStateMonitorExample {

public static class TriggeredChild implements NamedNode {
@Inject
public DirtyStateMonitor dirtyStateMonitor;
private final FlowSupplier<Integer> intDataFlow;

public TriggeredChild(FlowSupplier<Integer> intDataFlow) {
this.intDataFlow = intDataFlow;
}

@OnTrigger
public boolean triggeredChild() {
System.out.println("TriggeredChild -> " + intDataFlow.get());
return true;
}

public void printDirtyStat() {
System.out.println("\nintDataFlow dirtyState:" + dirtyStateMonitor.isDirty(intDataFlow));
}

public void markDirty() {
dirtyStateMonitor.markDirty(intDataFlow);
System.out.println("\nmark dirty intDataFlow dirtyState:" + dirtyStateMonitor.isDirty(intDataFlow));
}

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

public static void main(String[] args) throws NoSuchFieldException {
var processor = Fluxtion.interpret(new TriggeredChild(DataFlow.subscribe(Integer.class).flowSupplier()));
processor.init();
TriggeredChild triggeredChild = processor.getNodeById("triggeredChild");

processor.onEvent(2);
processor.onEvent(4);

//NOTHING HAPPENS
triggeredChild.printDirtyStat();
processor.triggerCalculation();

//MARK DIRTY
triggeredChild.markDirty();
processor.triggerCalculation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fluxtion.example.reference.libraryfunction;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.runtime.EventProcessor;
import com.fluxtion.runtime.annotations.OnTrigger;
import com.fluxtion.runtime.time.FixedRateTrigger;

public class FixedRateTriggerExample {
public static class RegularTrigger {

private final FixedRateTrigger fixedRateTrigger;

public RegularTrigger(FixedRateTrigger fixedRateTrigger) {
this.fixedRateTrigger = fixedRateTrigger;
}

public RegularTrigger(int sleepMilliseconds) {
fixedRateTrigger = new FixedRateTrigger(sleepMilliseconds);
}

@OnTrigger
public boolean trigger() {
System.out.println("RegularTrigger::triggered");
return true;
}
}

public static void main(String... args) throws InterruptedException {
var processor = Fluxtion.interpret(new RegularTrigger(100));
processor.init();

//NO TRIGGER - 10MS NEEDS TO ELAPSE
processor.onEvent(new Object());
processor.onEvent("xxx");

//WILL TRIGGER - 10MS HAS ELAPSED
Thread.sleep(100);
processor.onEvent("xxx");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fluxtion.example.reference.execution;
package com.fluxtion.example.reference.libraryfunction;

import com.fluxtion.compiler.Fluxtion;
import com.fluxtion.compiler.builder.dataflow.DataFlow;
Expand Down

0 comments on commit bff7711

Please sign in to comment.