public class EventBus extends Object
Constructor and Description |
---|
EventBus(taskSchedulerProvider : Supplier<InternalSchedules>,
dispatcher : BehaviorGuardEvaluatorRegistry)
Instantiates a dispatcher.
|
Modifier and Type | Method and Description |
---|---|
void |
asyncDispatch(event : Event,
[logger : Logger])
Posts an event to all registered
BehaviorGuardEvaluator . |
protected Collection<Runnable> |
evaluateGuards(event : Event,
behaviorGuardEvaluators : ConcurrentLinkedDeque<BehaviorGuardEvaluator>,
logger : Logger)
Evaluate the guard associated to the specified
event and returns the list of behaviors methods that must be
executed. |
protected void |
executeAsynchronouslyBehaviorMethods(behaviorsMethodsToExecute : Collection<Runnable>)
Execute every single Behaviors runnable, a dedicated thread will created by the executor local to this class and be used to
execute each runnable in parallel.
|
protected void |
executeBehaviorMethodsInParalellWithSynchroAtTheEnd(behaviorsMethodsToExecute : Collection<Runnable>,
thrownExceptions : boolean,
logger : Logger)
Execute every single Behaviors runnable, a dedicated thread will created by the executor local to this class and be used to
execute each runnable in parallel, and this method waits until its future has been completed before leaving.
|
InternalSchedules |
getExecutor()
Replies the agent's task scheduler that must be used by the event bus.
|
<T> ConcurrentLinkedDeque<T> |
getRegisteredEventListeners(type : Class<T>)
Extract the registered listeners with the given type.
|
boolean |
hasRegisteredEventListener(type : Class<?>)
Replies if a listener with the given type is registered.
|
void |
immediateDispatch(event : Event,
thrownExceptions : boolean,
[logger : Logger])
Posts an event to all registered
BehaviorGuardEvaluator . |
void |
immediateDispatchTo(listener : Object,
event : Event,
thrownExceptions : boolean,
[logger : Logger])
Posts an event to the registered
BehaviorGuardEvaluator of the given listener only. |
void |
register(object : Object,
filter : (Event)=>Boolean,
callback : (Object)=>void)
Registers all
PerceptGuardEvaluator methods on object to receive events. |
void |
unregister(type : Class<?>,
callback : (Object)=>Boolean)
Unregisters all
PerceptGuardEvaluator methods on the objects of the given type. |
void |
unregister(object : Object,
callback : (Object)=>void)
Unregisters all
PerceptGuardEvaluator methods on a registered object . |
void |
unregisterAll(callback : (Object)=>Boolean)
Unregisters all
PerceptGuardEvaluator methods on all registered objects. |
public new
(taskSchedulerProvider : Supplier<InternalSchedules>,
dispatcher : BehaviorGuardEvaluatorRegistry)
taskScheduler
- a provider of scheduler for the agent tasks that may be invoked on demand.dispatcher
- the event dispatcher.@Pure
public def
getExecutor() : InternalSchedules
EventBus
@Pure
public def
hasRegisteredEventListener(type : Class<?>) : boolean
EventBus
type
- the type of listener.true
if a listener of the given type is registered.@Pure publicdef
getRegisteredEventListeners(type : Class<T>) : ConcurrentLinkedDeque<T>with
<T>
EventBus
T
- the type of the listeners.type
- the type of the listeners.collection
- the collection of listeners that is filled by this function.public def
register(object : Object,
filter : (Event)=>Boolean,
callback : (Object)=>void) : void
EventBus
PerceptGuardEvaluator
methods on object
to receive events.
If the filter is provided, it will be used for determining if the given behavior accepts a specific event.
If the filter function replies true
for a specific event as argument, the event is fired in the
behavior context. If the filter function replies false
, the event is not fired in the behavior context.
object
- object whose PerceptGuardEvaluator
methods should be registered.filter
- the filter function. It could be null
.callback
- function which is invoked just after the first registration of the object. It could be null
.public def
unregister(object : Object,
callback : (Object)=>void) : void
EventBus
PerceptGuardEvaluator
methods on a registered object
.object
- object whose PerceptGuardEvaluator
methods should be unregistered.callback
- function which is invoked just before the object is unregistered.IllegalArgumentException
- if the object was not previously registered.public def
unregister(type : Class<?>,
callback : (Object)=>Boolean) : void
EventBus
PerceptGuardEvaluator
methods on the objects of the given type.type
- type of the objects whose PerceptGuardEvaluator
methods should be unregistered.callback
- function which is invoked just before the object is unregistered.
The callback functions replies true
if it should be called on the next removed object.
If it replies false
, it will be not called anymore.IllegalArgumentException
- if the object was not previously registered.public def
unregisterAll(callback : (Object)=>Boolean) : void
EventBus
PerceptGuardEvaluator
methods on all registered objects.callback
- function which is invoked just before the object is unregistered.
The callback functions replies true
if it should be called on the next removed object.
If it replies false
, it will be not called anymore.IllegalArgumentException
- if the object was not previously registered.public def
immediateDispatch(event : Event,
thrownExceptions : boolean,
[logger : Logger]) : void
EventBus
BehaviorGuardEvaluator
.
The dispatch of the events within the agent is asynchronous.
A rendez-vous point is set up in order to wait for all the event handlers to be finished.
This method will return successfully after the event has been posted to all BehaviorGuardEvaluator
, and regardless
of any exceptions thrown by BehaviorGuardEvaluator
.event
- an event to dispatch synchronously.thrownExceptions
- indicates if the exceptions in the event handlers should be thrown from this function,
or logged out to the agent's log.logger
- the logger to use for notifying the errors.public def
immediateDispatchTo(listener : Object,
event : Event,
thrownExceptions : boolean,
[logger : Logger]) : void
EventBus
BehaviorGuardEvaluator
of the given listener only.
The dispatch of this event will be done synchronously.
This method will return successfully after the event has been posted to all BehaviorGuardEvaluator
, and regardless
of any exceptions thrown by BehaviorGuardEvaluator
.listener
- the listener to dispatch to.event
- an event to dispatch synchronously.thrownExceptions
- indicates if the exceptions in the event handlers should be thrown from this function,
or logged out to the agent's log.logger
- the logger to use for notifying the errors.public def
asyncDispatch(event : Event,
[logger : Logger]) : void
EventBus
BehaviorGuardEvaluator
.
The dispatch of this event will be done asynchronously.
This method will return successfully after the event has been posted to all BehaviorGuardEvaluator
, and regardless
of any exceptions thrown by BehaviorGuardEvaluator
.event
- an event to dispatch asynchronously.logger
- the logger to use for notifying the errors.protected def
evaluateGuards(event : Event,
behaviorGuardEvaluators : ConcurrentLinkedDeque<BehaviorGuardEvaluator>,
logger : Logger) : Collection<Runnable>
EventBus
event
and returns the list of behaviors methods that must be
executed.
Errors are logger by the executor service. But they are not stopping the call to this function.
event
- the event triggering behaviors.behaviorGuardEvaluators
- the list of class containing a PerceptGuardEvaluator
method.logger
- the logger to be used.InvocationTargetException
- - exception when you try to execute a method by reflection and this method doesn't exist.protected def
executeBehaviorMethodsInParalellWithSynchroAtTheEnd(behaviorsMethodsToExecute : Collection<Runnable>,
thrownExceptions : boolean,
logger : Logger) : void
EventBus
Errors are logged by the executor service, and are thrown by this function.
behaviorsMethodsToExecute
- the collection of Behaviors runnable that must be executed.thrownExceptions
- indicates if the exceptions in the event handlers should be thrown from this function,
or logged out to the agent's log.logger
- the logger to use for notifying the errors.InterruptedException
- - something interrupt the waiting of the event handler terminations.ExecutionException
- - when the event handlers cannot be called; or when one of the event handler has failed during
its run.protected def
executeAsynchronouslyBehaviorMethods(behaviorsMethodsToExecute : Collection<Runnable>) : void
EventBus
Errors are logged by the executor service. They are not thrown by this function.
behaviorsMethodsToExecute
- the collection of Behaviors runnable that must be executed.Copyright © 2020 the original authors or authors.