Interface AlgorithmEventListener
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Listener interface for receiving algorithm events.
Implement this interface to receive notifications about all events
that occur during algorithm execution. Use pattern matching on the
sealed AlgorithmEvent hierarchy to handle specific event types.
Example usage:
algorithm.addEventListener(event -> {
switch (event) {
case IterationStarted e -> System.out.println("Starting iteration " + e.iteration());
case JobInserted e -> System.out.println("Job " + e.job().getId() + " inserted into " + e.routeId());
case AcceptanceDecision e -> {
if (e.isNewBest()) {
System.out.println("New best solution: " + e.newCost());
}
}
default -> {} // ignore other events
}
});
For filtering specific event types, you can also use:
algorithm.addEventListener(AlgorithmEventListener.forType(JobInserted.class, e -> {
System.out.println("Job inserted: " + e.job().getId());
}));
-
Method Summary
Modifier and TypeMethodDescriptionstatic AlgorithmEventListenercomposite(AlgorithmEventListener... listeners) Creates a composite listener that forwards events to multiple listeners.static <T extends AlgorithmEvent>
AlgorithmEventListenerCreates a listener that only handles events of a specific type.voidonEvent(AlgorithmEvent event) Called when an algorithm event occurs.
-
Method Details
-
onEvent
Called when an algorithm event occurs.- Parameters:
event- The event that occurred
-
forType
static <T extends AlgorithmEvent> AlgorithmEventListener forType(Class<T> eventType, Consumer<T> handler) Creates a listener that only handles events of a specific type.- Type Parameters:
T- The event type- Parameters:
eventType- The type of events to handlehandler- The handler for events of this type- Returns:
- A listener that filters and handles only the specified event type
-
composite
Creates a composite listener that forwards events to multiple listeners.- Parameters:
listeners- The listeners to forward events to- Returns:
- A composite listener
-