Skip to content

Commit

Permalink
updated race example
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-higgins committed Mar 8, 2024
1 parent 03fe89b commit e46d2c6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -43,18 +44,20 @@ public String runDuration() {
public static class RaceTimeTracker {

private final transient Map<Long, RunningRecord> 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;
}
}
Expand All @@ -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() + "]"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<data key="vertex_label">
<jGraph:ShapeNode>
<jGraph:Geometry height="70" width="160" x="20" y="20"/>
<jGraph:label text="&lt;&lt;EventHandle&gt;&gt;&#10;id:resultsPublisher&#10;class:ResultsPublisherImpl"/>
<jGraph:Style properties="EVENTHANDLER"/>
<jGraph:label text="id:resultsPublisher&#10;class:ResultsPublisherImpl"/>
<jGraph:Style properties="NODE"/>
</jGraph:ShapeNode>
</data>
</node>
Expand Down Expand Up @@ -56,29 +56,29 @@
</jGraph:ShapeNode>
</data>
</node>
<node id="RunnerFinished">
<node id="ResultsPublisher">
<data key="vertex_label">
<jGraph:ShapeNode>
<jGraph:Geometry height="70" width="160" x="20" y="20"/>
<jGraph:label text="&lt;&lt;Event&gt;&gt;&#10;RunnerFinished"/>
<jGraph:Style properties="EVENT"/>
<jGraph:label text="&lt;&lt;ExportService&gt;&gt;&#10;class:ResultsPublisher&#10;"/>
<jGraph:Style properties="EXPORTSERVICE"/>
</jGraph:ShapeNode>
</data>
</node>
<node id="ResultsPublisher">
<node id="RunnerStarted">
<data key="vertex_label">
<jGraph:ShapeNode>
<jGraph:Geometry height="70" width="160" x="20" y="20"/>
<jGraph:label text="&lt;&lt;ExportService&gt;&gt;&#10;class:ResultsPublisher&#10;"/>
<jGraph:Style properties="EXPORTSERVICE"/>
<jGraph:label text="&lt;&lt;Event&gt;&gt;&#10;RunnerStarted"/>
<jGraph:Style properties="EVENT"/>
</jGraph:ShapeNode>
</data>
</node>
<node id="RunnerStarted">
<node id="RunnerFinished">
<data key="vertex_label">
<jGraph:ShapeNode>
<jGraph:Geometry height="70" width="160" x="20" y="20"/>
<jGraph:label text="&lt;&lt;Event&gt;&gt;&#10;RunnerStarted"/>
<jGraph:label text="&lt;&lt;Event&gt;&gt;&#10;RunnerFinished"/>
<jGraph:Style properties="EVENT"/>
</jGraph:ShapeNode>
</data>
Expand All @@ -103,12 +103,12 @@
<jGraph:ShapeEdge/>
</data>
</edge>
<edge id="5" source="RunnerFinished" target="resultsPublisher">
<edge id="5" source="ResultsPublisher" target="resultsPublisher">
<data key="edge_label">
<jGraph:ShapeEdge/>
</data>
</edge>
<edge id="6" source="ResultsPublisher" target="resultsPublisher">
<edge id="6" source="RunnerStarted" target="raceCalculator">
<data key="edge_label">
<jGraph:ShapeEdge/>
</data>
Expand All @@ -118,10 +118,5 @@
<jGraph:ShapeEdge/>
</data>
</edge>
<edge id="8" source="RunnerStarted" target="raceCalculator">
<data key="edge_label">
<jGraph:ShapeEdge/>
</data>
</edge>
</graph>
</graphml>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e46d2c6

Please sign in to comment.