Skip to content

Commit

Permalink
[core][sre] Add spawn() in Lifecycle.
Browse files Browse the repository at this point in the history
see #76

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 26, 2016
1 parent 5146113 commit 50d3cf6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
13 changes: 13 additions & 0 deletions eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl
Expand Up @@ -267,6 +267,19 @@ capacity Behaviors {
*/
capacity Lifecycle {

/**
* Spawns a new Agent inside the default context of this agent.
* This action must automatically register the newly created agent
* within the default space of the context.
*
* @param agentType the type of the agent to spawn.
* @param params the arguments to pass in the initialization event to the spawned agent.
* @return the identifier of the spawned agent.
* @fires AgentSpawned in DefaultSpace
* @since 0.5
*/
def spawn(agentType : Class<? extends Agent>, params : Object*) : UUID fires AgentSpawned

/**
* Spawns a new member agent in the parent's context (parentID).
*
Expand Down
Expand Up @@ -28,6 +28,7 @@
import io.janusproject.services.executor.ChuckNorrisException;
import io.janusproject.services.spawn.SpawnService;

import io.sarl.core.DefaultContextInteractions;
import io.sarl.core.Lifecycle;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.AgentContext;
Expand Down Expand Up @@ -66,6 +67,12 @@ public int getInstallationOrder() {
return installationOrder;
}

@Override
public UUID spawn(Class<? extends Agent> agentType, Object... params) {
return this.spawnService.spawn(getSkill(DefaultContextInteractions.class).getDefaultContext(),
null, agentType, params);
}

@Override
public UUID spawnInContext(Class<? extends Agent> agentType, AgentContext context, Object... params) {
return this.spawnService.spawn(context, null, agentType, params);
Expand Down
Expand Up @@ -38,12 +38,16 @@
import org.mockito.Mockito;

import io.janusproject.kernel.bic.AsynchronousAgentKillingEvent;
import io.janusproject.kernel.bic.DefaultContextInteractionsSkill;
import io.janusproject.kernel.bic.InternalEventBusCapacity;
import io.janusproject.kernel.bic.InternalEventBusSkill;
import io.janusproject.kernel.bic.LifecycleSkill;
import io.janusproject.services.executor.ChuckNorrisException;
import io.janusproject.services.spawn.SpawnService;
import io.janusproject.tests.testutils.AbstractJanusTest;

import io.sarl.core.DefaultContextInteractions;
import io.sarl.core.Lifecycle;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.AgentContext;
import io.sarl.lang.core.BuiltinCapacitiesProvider;
Expand Down Expand Up @@ -72,17 +76,30 @@ public class LifecycleSkillTest extends AbstractJanusTest {
@Mock
private InternalEventBusSkill eventBus;

@Mock
private AgentContext context;

@Mock
private DefaultContextInteractionsSkill defaultContextInteractions;

@InjectMocks
private LifecycleSkill skill;

@Before
public void setUp() throws Exception {
this.agentId = UUID.randomUUID();
Mockito.doReturn(this.context).when(this.defaultContextInteractions).getDefaultContext();
Agent agent = new Agent(Mockito.mock(BuiltinCapacitiesProvider.class), UUID.randomUUID(), null) {
@SuppressWarnings("synthetic-access")
@Override
protected ClearableReference<Skill> $getSkill(Class<? extends Capacity> capacity) {
return new ClearableReference<>(LifecycleSkillTest.this.eventBus);
if (InternalEventBusCapacity.class.equals(capacity)) {
return new ClearableReference<>(LifecycleSkillTest.this.eventBus);
}
if (DefaultContextInteractions.class.equals(capacity)) {
return new ClearableReference<>(LifecycleSkillTest.this.defaultContextInteractions);
}
return null;
}

@SuppressWarnings("synthetic-access")
Expand All @@ -94,18 +111,33 @@ public UUID getID() {
this.reflect.invoke(this.skill, "setOwner", agent);
}

@Test
public void spawn() throws Exception {
Class type = Agent.class;
this.skill.spawn(type, 1, "String"); //$NON-NLS-1$
ArgumentCaptor<AgentContext> argument1 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
verify(this.spawnService, times(1)).spawn(argument1.capture(), argument2.capture(), argument3.capture(),
argument4.capture());
assertSame(this.context, argument1.getValue());
assertNull(argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertArrayEquals(new Object[] { 1, "String" }, argument4.getAllValues().toArray()); //$NON-NLS-1$
}

@Test
public void spawnInContext() {
Class type = Agent.class;
AgentContext context = mock(AgentContext.class);
this.skill.spawnInContext(type, context, 1, "String"); //$NON-NLS-1$
this.skill.spawnInContext(type, this.context, 1, "String"); //$NON-NLS-1$
ArgumentCaptor<AgentContext> argument1 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
verify(this.spawnService, times(1)).spawn(argument1.capture(), argument2.capture(), argument3.capture(),
argument4.capture());
assertSame(context, argument1.getValue());
assertSame(this.context, argument1.getValue());
assertNull(argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertArrayEquals(new Object[] { 1, "String" }, argument4.getAllValues().toArray()); //$NON-NLS-1$
Expand All @@ -114,15 +146,14 @@ public void spawnInContext() {
@Test
public void spawnInContextWithID() {
Class type = Agent.class;
AgentContext context = mock(AgentContext.class);
this.skill.spawnInContextWithID(type, this.agentId, context, 1, "String"); //$NON-NLS-1$
this.skill.spawnInContextWithID(type, this.agentId, this.context, 1, "String"); //$NON-NLS-1$
ArgumentCaptor<AgentContext> argument1 = ArgumentCaptor.forClass(AgentContext.class);
ArgumentCaptor<UUID> argument2 = ArgumentCaptor.forClass(UUID.class);
ArgumentCaptor<Class> argument3 = ArgumentCaptor.forClass(Class.class);
ArgumentCaptor<Object> argument4 = ArgumentCaptor.forClass(Object.class);
verify(this.spawnService, times(1)).spawn(argument1.capture(), argument2.capture(), argument3.capture(),
argument4.capture());
assertSame(context, argument1.getValue());
assertSame(this.context, argument1.getValue());
assertSame(this.agentId, argument2.getValue());
assertEquals(Agent.class, argument3.getValue());
assertArrayEquals(new Object[] { 1, "String" }, argument4.getAllValues().toArray()); //$NON-NLS-1$
Expand Down

0 comments on commit 50d3cf6

Please sign in to comment.