Skip to content

Commit

Permalink
Fix Event Bus keeping state
Browse files Browse the repository at this point in the history
The static event bus is dangerous since a simulation will reuse listeners still registered from the previous simulation.
  • Loading branch information
franksn90 committed Jun 3, 2024
1 parent aa98c02 commit 86dc301
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/cambio/simulator/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public static <T extends Schedulable> void subscribe(Class<T> targetClass, Consu
//noinspection unchecked
listeners.computeIfAbsent(targetClass, k -> new LinkedList<>()).add((Consumer<Schedulable>) consumer);
}

// TODO: This is a hotfix to avoid having a state from one simulation carry over to the next one. Actually,
// static classes are an anti-pattern that should not be used exactly for this reason!
public static void clear() {
listeners.clear();
}

}
1 change: 1 addition & 0 deletions src/main/java/cambio/simulator/ExperimentCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ExperimentCreator {
* @return a new {@link Experiment} that is configured based on the given config
*/
public Experiment createSimulationExperiment(ExperimentStartupConfig config) {
EventBus.clear();
String archDescLocation = config.getArchitectureDescLoc();
String expDescLocation;

Expand Down

0 comments on commit 86dc301

Please sign in to comment.