public abstract class Activator<A extends AbstractAgent>
extends java.lang.Object
execute(List, Object...)
for defining how a sequential execution of a list of
agents take place. By default, this list corresponds to all the agents in a single core mode or to partial views of
the entire list when the multicore mode is used. The multicore mode is set to false
by default.Scheduler
,
GenericBehaviorActivator
Constructor and Description |
---|
Activator(java.lang.String community,
java.lang.String group,
java.lang.String role)
Builds a new Activator on the given CGR location of the artificial society with multicore mode set to
false . |
Modifier and Type | Method and Description |
---|---|
protected void |
adding(A agent)
This method is automatically called when an agent joins the corresponding group and role.
|
protected void |
adding(java.util.List<A> agents)
Called when a list of agents joins the corresponding group and role.
|
void |
allAgentsLeaveRole()
Makes all the agents leave the corresponding role at once.
|
abstract void |
execute(java.util.List<A> agentsList,
java.lang.Object... args)
This should define what has to be done on the agents for a simulation step.
|
void |
execute(java.lang.Object... args)
Call #execute(List agentsList) on all the agents, i.e. using
Overlooker.getCurrentAgentsList() . |
static <T> java.lang.reflect.Method |
findMethodOn(java.lang.Class<T> agentClass,
java.lang.String methodName,
java.lang.Class<?>... parameterTypes)
Returns the agent's method named
methodName considering a given agentClass. |
static <T> java.lang.reflect.Method |
findMethodOnFromArgsSample(java.lang.Class<T> agentClass,
java.lang.String methodName,
java.lang.Object... argsSample)
Returns the agent's method named
methodName considering a given agentClass and a sample of
the arguments which could be passed to it. |
java.lang.String |
getCommunity()
Gets the community to which this activator/probe is binded to.
|
java.util.List<A> |
getCurrentAgentsList()
Returns a snapshot at moment t of the agents handling the group/role couple
|
java.lang.String |
getGroup()
Gets the group to which this activator/probe is binded to.
|
static java.util.concurrent.ExecutorService |
getMadkitServiceExecutor()
Returns the MDK ExecutorService that executes task in parallel
|
java.lang.String |
getRole()
Gets the role to which this activator/probe is binded to.
|
java.util.List<A> |
getShuffledList()
Returns a ListIterator over the agents which is shuffled
|
Scheduler.SimulationTime |
getSimulationTime()
Get the
Scheduler.SimulationTime associated with the simulation |
void |
initialize()
Called by the MaDKit kernel when the Activator or Probe is first added.
|
boolean |
isMulticoreModeOn() |
void |
killAgents()
Kills all the agents which are monitored.
|
protected void |
multicoreExecute(java.lang.Object... args)
Executes the behavior on all the agents in a concurrent way, using several processor cores if available.
|
int |
nbOfParallelTasks()
Returns the number of tasks that will be created by this activator in order to benefit from multicore platforms.
|
protected void |
removing(A agent)
This method is automatically called when an agent leaves the corresponding group and role.
|
protected void |
removing(java.util.List<A> agents)
This method is automatically called when a list of agents has leaved the corresponding group and role.
|
int |
size()
Returns the number of the agents handling the group/role couple
|
java.lang.String |
toString()
returns a string containing the CGR location and the number of monitored agents.
|
void |
useMulticore(int nbOfParallelTasks)
Sets the number of tasks which will be used on a multicore architecture.
|
public Activator(java.lang.String community, java.lang.String group, java.lang.String role)
false
. This has the same effect as Activator(community, group, role, false)
.community
- group
- role
- Scheduler
public void execute(java.lang.Object... args)
args
- arguments that could be used by the scheduler to pass information to this activator for an activationScheduler.doSimulationStep()
protected void adding(A agent)
agent
- which has been added to this group/rolepublic abstract void execute(java.util.List<A> agentsList, java.lang.Object... args)
Overlooker.getCurrentAgentsList()
is used by
default. When the multicore mode is on, the list is only a portion and this method will automatically be distributed
over several threads. So, one has to take care about how the activator's fields are used here to avoid a
ConcurrentModificationException
for instance.agentsList
- protected void multicoreExecute(java.lang.Object... args)
nbOfParallelTasks()
tasks so that there are independently
performed by the available core of the host.
Default implementation Beware that using this call will produce different outputs for each run unless a concurrent simulation model is used. That is to say, a model supporting concurrent phases in the simulation execution such as the IRM4S model
public java.lang.String toString()
public boolean isMulticoreModeOn()
true
if the multicore mode is on. I.e. nbOfParallelTasks()
> 1. This method could be
used by the default behavior of scheduler agents as they test in which mode each activator has to be used.public void useMulticore(int nbOfParallelTasks)
multicoreExecute(Object...)
on this activator when
execute(Object...)
is called. If set to 1, the agents are sequentially activated. Beware that this
is the only way to do exact replication of simulations, unless you have clear specifications for your model, see
multicoreExecute(Object...)
.nbOfParallelTasks
- the number of simultaneous tasks that this activator will use to make a step. Default is 1 upon creation,
so that isMulticoreModeOn()
returns false
.public int nbOfParallelTasks()
public static java.util.concurrent.ExecutorService getMadkitServiceExecutor()
public static <T> java.lang.reflect.Method findMethodOn(java.lang.Class<T> agentClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes) throws java.lang.NoSuchMethodException
methodName
considering a given agentClass. This also works for private
and inherited methods.agentClass
- the class wherein the search has to be mademethodName
- the name of the methodparameterTypes
- the parameter types of the targeted methodmethodName
java.lang.NoSuchMethodException
public static <T> java.lang.reflect.Method findMethodOnFromArgsSample(java.lang.Class<T> agentClass, java.lang.String methodName, java.lang.Object... argsSample) throws java.lang.NoSuchMethodException
methodName
considering a given agentClass and a sample of
the arguments which could be passed to it. The purpose of this method is restricted to a limited number of
use cases since findMethodOn(Class, String, Class...)
should be preferred is the exact signature of the
searched method is known. A typical use case of this method
is when the only information available is the arguments which are passed, for instance when overriding
the execute(List, Object...)
method and the like in Activator
subclasses. This also works for private
and inherited methods.agentClass
- the class wherein the search has to be mademethodName
- the name of the methodargsSample
- a sample of the args which can be passed to the methodmethodName
java.lang.NoSuchMethodException
- if a matching method cannot be foundpublic Scheduler.SimulationTime getSimulationTime()
Scheduler.SimulationTime
associated with the simulationpublic java.lang.String getCommunity()
public java.lang.String getGroup()
public java.lang.String getRole()
public void initialize()
adding(getCurrentAgentsList());
protected void adding(java.util.List<A> agents)
AbstractAgent.launchAgentBucket(String, int, String...)
. Override this method when you want to do some
initialization on the agents that enter the group/role. Default implementation is:
protected void adding(final List<A> agents) { for (A agent : agents) { adding(agent); } }
agents
- the list of agents which have been added to this group/role at once.protected void removing(java.util.List<A> agents)
protected void removing(final List<A> agents) { for (A agent : agents) { removing(agent); } }
agents
- the list of agents which have been removed from this group/roleprotected void removing(A agent)
agent
- the agent which is being removed from this group/rolepublic int size()
public java.util.List<A> getCurrentAgentsList()
public java.util.List<A> getShuffledList()
public void killAgents()
public void allAgentsLeaveRole()
Fabien Michel, Olivier Gutknecht, Jacques Ferber - September 9 2019