Fork me on GitHub

Lifecycle Capacity

The built-in capacity Lifecycle provides actions for spawning new agents on different external contexts and the inner context, as well as the killMe action to stop the execution of an agent.

1. Stopping the Agent Execution

Because of the autonomy property of an agent, it can be stopped only by committing a suicide. It means that it is impossible to stop an agent from another agent: the agent to stop must be able to accept or reject this query.

The Lifecycle capacity provides the following function for committing a suicide:

def killMe(abnormalTerminationCause : Throwable = null)

This action automatically unregisters the calling agent from the default context, and therefore all its spaces including the default space.

Very Important Note If the killed agent was a composed agent, it must not have members any more before calling this action, otherwise a RuntimeException is thrown.

This action fires two events in case of success, and one event in case of failure:

Example:

agent A {
	uses Lifecycle
	def myaction {
		killMe
	}
}

The optional argument of the killMe function enables to provide the cause of an abnormal termination of the agent. This cause must be a throwable object, e.g. an exception. If the argument abnormalTerminationCause is provided, its value is put into the field abnormalTerminationCause of the fired AgentKilled event.

2. Spawning in the default context

Many time, it is useful for agent to create a new agent into the default context. The following functions are provided for this task:

def spawn(agentType : Class<? extends Agent>, parameters : Object*)
def spawn(nbAgents: int, agentType : Class<? extends Agent>, parameters : Object*)

This action creates one to nbAgents instance(s) of the given agent type, and launches the agent(s) into the default context. The first spawn function above is spawning a single agent. The second spawn function is spawning the given number of agents. The parameters are passed to the spawned agent inside the Initialize event: the parameters field.

This action fires two events:

Example:

agent A {
	uses Lifecycle
	def myaction {
		var type : Class<? extends Agent>
		var p1 : Object
		var p2 : Object
		type = typeof(A)
		p1 = new Object
		p2 = new Object
		spawn(type, p1, p2)
		spawn(5, type, p1, p2)
	}
}

3. Spawning with a specific agent identifier in the default context

Some time, it is useful to create an agent with a specific identifier. The following function permits to spawn an agent with a given identifier in the default context:

def spawnWithID(agentType : Class<? extends Agent>,
agentId : UUID,
      parameters : Object*)

This action creates an instance of the given agent type, with the given identifier, and launches the agent into the default context. The parameters are passed to the spawned agent inside the Initialize event: the parameters field.

This action fires two events:

Example:

agent A {
	uses Lifecycle
	def myaction {
		var aid : UUID
		var type : Class<? extends Agent>
		var p1 : Object
		var p2 : Object
		type = typeof(A)
		p1 = new Object
		p2 = new Object
		spawnWithID(type, aid, #[p1, p2])
	}
}

4. Spawning in a specific context

When one or more agents should be spawned into a specific agent context, the two following functions could be used for launching the agents:

def spawnInContext(agentType : Class<? extends Agent>,
                   context : AgentContext,
                   parameters : Object*)
def spawnInContext(nbAgents : int,
                   agentType : Class<? extends Agent>,
                   context : AgentContext,
                   parameters : Object*)

This action creates one to nbAgents instance(s) of the given agent type, and launches the agent(s) into the given context. The first spawn function is spawning a single agent. The second spawn function is spawning the given number of agents. The parameters are passed to the spawned agent inside the Initialize event: the parameters field.

This action fires two events:

Example:

agent A {
	uses Lifecycle
	def myaction {
		var c : AgentContext
		var type : Class<? extends Agent>
		var p1 : Object
		var p2 : Object
		type = typeof(A)
		p1 = new Object
		p2 = new Object
		spawnInContext(type, c, p1, p2)
		spawnInContext(5, type, c, p1, p2)
	}
}

5. Spawning with a specific agent identifier in a specific context

Some time, it is useful to create an agent with a specific identifier. The following function permits to spawn an agent with a given identifier in a specific context:

def spawnInContextWithID(agentType : Class<? extends Agent>,
agentId : UUID,
context : AgentContext,
parameters : Object*)

This action creates an instance of the given agent type, with the given identifier, and launches the agent into the given context. The parameters are passed to the spawned agent inside the Initialize event: the parameters field.

This action fires two events:

Example:

agent A {
	uses Lifecycle
	def myaction {
		var c : AgentContext
		var aid : UUID
		var type : Class<? extends Agent>
		var p1 : Object
		var p2 : Object
		type = typeof(A)
		p1 = new Object
		p2 = new Object
		spawnInContextWithID(type, aid, c, #[p1, p2])
	}
}

Copyright © 2014-2023 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.14.0-SNAPSHOT.