diff --git a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java index f4d2fcf..a95c964 100644 --- a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java +++ b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java @@ -2,6 +2,7 @@ import com.fluxtion.runtime.annotations.ExportService; import com.fluxtion.runtime.annotations.OnEventHandler; +import com.fluxtion.runtime.annotations.OnTrigger; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -28,9 +29,9 @@ public interface ResultsPublisher { void publishAllResults(); } - private record RunningRecord(Instant startTime, Instant finishTime) { - RunningRecord(Instant startTime) { - this(startTime, startTime); + private record RunningRecord(long runnerId, Instant startTime, Instant finishTime) { + RunningRecord(long runnerId, Instant startTime) { + this(runnerId, startTime, startTime); } public String runDuration() { @@ -43,18 +44,20 @@ public String runDuration() { public static class RaceTimeTracker { private final transient Map raceTimeMap = new HashMap<>(); + private RunningRecord latestFinisher; @OnEventHandler(propagate = false) public boolean runnerStarted(RunnerStarted runnerStarted) { - raceTimeMap.put(runnerStarted.runnerId(), new RunningRecord(runnerStarted.startTime())); + long runnerId = runnerStarted.runnerId(); + raceTimeMap.put(runnerId, new RunningRecord(runnerId, runnerStarted.startTime())); return false; } @OnEventHandler public boolean runnerFinished(RunnerFinished runner) { - raceTimeMap.computeIfPresent( + latestFinisher = raceTimeMap.computeIfPresent( runner.runnerId(), - (id, startRecord) -> new RunningRecord(startRecord.startTime(), runner.finishTime())); + (id, startRecord) -> new RunningRecord(id, startRecord.startTime(), runner.finishTime())); return true; } } @@ -64,18 +67,18 @@ public static class ResultsPublisherImpl implements @ExportService ResultsPublis private final RaceTimeTracker raceTimeTracker; - @OnEventHandler(propagate = false) - public boolean runnerFinished(RunnerFinished runner) { - var raceTime = raceTimeTracker.getRaceTimeMap().get(runner.runnerId()); - System.out.format("Crossed the line runner:%d time:%s%n", runner.runnerId(), raceTime.runDuration()); + @OnTrigger + public boolean sendIndividualRunnerResult(){ + var raceRecord = raceTimeTracker.getLatestFinisher(); + System.out.format("Crossed the line runner:%d time [%s]%n", raceRecord.runnerId(), raceRecord.runDuration()); return false; } @Override public void publishAllResults() { - System.out.println("FINAL RESULTS"); + System.out.println("\nFINAL RESULTS"); raceTimeTracker.getRaceTimeMap().forEach((l, r) -> - System.out.println("id:" + l + " final time:" + r.runDuration())); + System.out.println("id:" + l + " time [" + r.runDuration() + "]")); } } } diff --git a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.java b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.java index 9f1a707..b51929d 100644 --- a/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.java +++ b/cookbook/src/main/java/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.java @@ -202,7 +202,9 @@ public void handleEvent(RunnerFinished typedEvent) { auditEvent(typedEvent); // Default, no filter methods isDirty_raceCalculator = raceCalculator.runnerFinished(typedEvent); - resultsPublisher.runnerFinished(typedEvent); + if (guardCheck_resultsPublisher()) { + resultsPublisher.sendIndividualRunnerResult(); + } afterEvent(); } @@ -238,7 +240,6 @@ public void bufferEvent(Object event) { RunnerFinished typedEvent = (RunnerFinished) event; auditEvent(typedEvent); isDirty_raceCalculator = raceCalculator.runnerFinished(typedEvent); - resultsPublisher.runnerFinished(typedEvent); } else if (event instanceof com.fluxtion.example.cookbook.racing.RaceCalculator.RunnerStarted) { RunnerStarted typedEvent = (RunnerStarted) event; auditEvent(typedEvent); @@ -253,6 +254,9 @@ public void bufferEvent(Object event) { public void triggerCalculation() { buffering = false; String typedEvent = "No event information - buffered dispatch"; + if (guardCheck_resultsPublisher()) { + resultsPublisher.sendIndividualRunnerResult(); + } afterEvent(); } diff --git a/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.graphml b/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.graphml index 17d0415..2f724aa 100644 --- a/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.graphml +++ b/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.graphml @@ -6,8 +6,8 @@ - - + + @@ -56,29 +56,29 @@ - + - - + + - + - - + + - + - + @@ -103,12 +103,12 @@ - + - + @@ -118,10 +118,5 @@ - - - - - diff --git a/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.png b/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.png index c1a5a29..09d0e58 100644 Binary files a/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.png and b/cookbook/src/main/resources/com/fluxtion/example/cookbook/racing/generated/RaceCalculatorProcessor.png differ