diff --git a/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl b/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl index 8f38c4d58b..7f591e326a 100644 --- a/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl +++ b/main/apiplugins/io.sarl.core/src/main/sarl/io/sarl/core/events.sarl @@ -246,7 +246,7 @@ final event SpaceDestroyed { } -/** Notifies other members the member with agentID has effectively +/** Notifies the other members that the member with the identifier agentID has effectively * integrated the holon context. * The event is fired within the default space of the context in which the new agent entered. * The joining agent does not receive this event. @@ -283,7 +283,7 @@ final event MemberJoined { } -/** Notifies other members the member with agentID was +/** Notifies the other other members that the member with the identifier agentID was * excluded (forced to leave) the holon context parentContextID. * The event is fired within the default space of the context from which the agent was excluded. * The excluded agent does not receive this event. @@ -319,7 +319,7 @@ final event MemberExcluded { } -/** Notifies other members the member with agentID has left the holon context. +/** Notifies the other members that the member with the identifier agentID has left the holon context. * The event is fired within the default space of the context from which the agent has left. * The left agent does not receive this event. * The source of the event is the parent agent. diff --git a/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/skills/bic/DefaultContextInteractionsSkill.sarl b/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/skills/bic/DefaultContextInteractionsSkill.sarl index 65f0162d48..4fb65222c3 100644 --- a/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/skills/bic/DefaultContextInteractionsSkill.sarl +++ b/sre/io.janusproject/io.janusproject.plugin/src/main/sarl/io/sarl/sre/skills/bic/DefaultContextInteractionsSkill.sarl @@ -47,52 +47,93 @@ skill DefaultContextInteractionsSkill extends Skill implements DefaultContextInt def getDefaultContext : AgentContext { - getLife(owner).defaultContext.context + val life = getLife(owner) + assert life !== null + val reference = life.defaultContext + assert reference !== null + val context = reference.context + assert context !== null + return context } def getDefaultSpace : EventSpace { - getLife(owner).defaultContext.defaultSpace + val life = getLife(owner) + assert life !== null + val reference = life.defaultContext + assert reference !== null + val ^space = reference.defaultSpace + assert ^space !== null + return ^space } def getDefaultAddress : Address { - getLife(owner).defaultContext.addressInDefaultSpace + val life = getLife(owner) + assert life !== null + val reference = life.defaultContext + assert reference !== null + val adr = reference.addressInDefaultSpace + assert adr !== null + return adr } def isDefaultContext(context : AgentContext) : boolean { - context.ID.isDefaultContext + assert context !== null + val id = context.ID + assert id !== null + return id.isDefaultContext } def isDefaultContext(contextID : UUID) : boolean { - contextID == this.defaultContext.ID + assert contextID !== null + val context = defaultContext + assert context !== null + return contextID == context.ID } def isDefaultSpace(^space : Space) : boolean { - ^space.spaceID.isDefaultSpace + assert ^space !== null + val id = ^space.spaceID + assert id !== null + return id.isDefaultSpace } def isDefaultSpace(^space : SpaceID) : boolean { - ^space == this.defaultSpace.spaceID + assert ^space !== null + val dspace = defaultSpace + assert dspace !== null + return ^space == dspace.spaceID } def isDefaultSpace(^space : UUID) : boolean { - ^space == this.defaultSpace.spaceID.ID + assert ^space !== null + val dspace = defaultSpace + assert dspace !== null + val spaceid = dspace.spaceID + assert spaceid !== null + return ^space == spaceid.ID } def isInDefaultSpace(^event : Event) : boolean { if (^event !== null) { val adr = ^event.source if (adr !== null) { - return adr.spaceID.isDefaultSpace + val spaceid = adr.spaceID + assert spaceid !== null + return spaceid.isDefaultSpace } } return false } def emit(^event : Event, scope : Scope
= null) { - ^event.source = this.defaultAddress - this.defaultSpace.emit(ID, ^event, scope) + assert ^event !== null + ^event.source = defaultAddress + val dspace = defaultSpace + assert dspace !== null + dspace.emit(ID, ^event, scope) } + @Deprecated def willReceive(receiver : UUID, ^event : Event) { ^event.emit [it.UUID == receiver] } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnMemberLeftTest.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnMemberLeftTest.sarl index e309cc8421..42663261b2 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnMemberLeftTest.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/OnMemberLeftTest.sarl @@ -24,16 +24,22 @@ package io.sarl.sre.tests.runtime.internal import io.sarl.sre.test.framework.context.SreRunContext import io.sarl.sre.test.framework.^extension.PropertyRestoreExtension import io.sarl.sre.test.framework.^extension.SreRunExtension -import io.sarl.sre.tests.runtime.internal.mocks.RootAgent1 +import io.sarl.sre.tests.runtime.internal.mocks.Bye +import io.sarl.sre.tests.runtime.internal.mocks.MemberLeftAgent0 +import io.sarl.sre.tests.runtime.internal.mocks.MemberLeftAgent1 import io.sarl.tests.api.extensions.ContextInitExtension import io.sarl.tests.api.extensions.JavaVersionCheckExtension +import java.util.UUID import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.RepeatedTest import org.junit.jupiter.api.Tag import org.junit.jupiter.api.^extension.ExtendWith -import static io.sarl.tests.api.tools.TestAssertions.* -import org.junit.jupiter.api.Disabled +import static io.sarl.sre.test.framework.Constants.* + +import static extension io.sarl.sre.tests.framework.SreTestUtilities.* +import static extension io.sarl.tests.api.tools.TestAssertions.* +import static extension org.junit.jupiter.api.Assertions.* /** * @author $Author: sgalland$ @@ -51,11 +57,74 @@ import org.junit.jupiter.api.Disabled @Tag("run") class OnMemberLeftTest { - @RepeatedTest(5) - @DisplayName("run") - def run(extension rc : SreRunContext) { - typeof(RootAgent1).runSre - assertContains(allResults, "ROOT:ChildAgent1", "ROOT:Child2Agent1", "CHILD1:Child2Agent1") + @RepeatedTest(10) + @DisplayName("In default context") + def inDefaultContext(extension rc : SreRunContext) { + var kern = setupTheSreKernel(null, null) + + val id0 = UUID::randomUUID + waitForAgentSpawned(id0) [ + kern.startAgentWithID(typeof(MemberLeftAgent0), id0, buildAgentInitializationParameters(1)) + ] + + val id1 = UUID::randomUUID + waitForAgentSpawned(id1) [ + kern.startAgentWithID(typeof(MemberLeftAgent0), id1, buildAgentInitializationParameters(2)) + ] + + val id2 = UUID::randomUUID + waitForAgentSpawned(id2) [ + kern.startAgentWithID(typeof(MemberLeftAgent0), id2, buildAgentInitializationParameters(3)) + ] + + rootContext.defaultSpace.emit(UUID::randomUUID, new Bye) + + waitForTheKernel(STANDARD_TIMEOUT) + + // There is no context joint for the default context. + // Consequently, no ContextJoined event is fired + allResults.assertContains() + } + + @RepeatedTest(10) + @DisplayName("In other context") + def inOtherContext(extension rc : SreRunContext) { + var kern = setupTheSreKernel(null, null) + + val id0 = UUID::randomUUID + waitForAgentSpawned(id0) [ + kern.startAgentWithID(typeof(MemberLeftAgent1), id0, buildAgentInitializationParameters(1, null)) + ] + + val innerContext = rc.getInnerContextForAgent(id0) + innerContext.assertNotNull + + val id1 = UUID::randomUUID + waitForAgentSpawned(id1) [ + kern.startAgentWithID(typeof(MemberLeftAgent1), id1, buildAgentInitializationParameters(2, innerContext)) + ] + + val id2 = UUID::randomUUID + waitForAgentSpawned(id2) [ + kern.startAgentWithID(typeof(MemberLeftAgent1), id2, buildAgentInitializationParameters(3, innerContext)) + ] + + // Kill the inner agents, first + waitForAgentKilled(#[id1, id2]) [ + innerContext.defaultSpace.emit(UUID::randomUUID, new Bye)[it.UUID != id0] + ] + + // Kill the containing agent, second + rootContext.defaultSpace.emit(UUID::randomUUID, new Bye) + + waitForTheKernel(STANDARD_TIMEOUT) + + allResults.assertContains( + "AG1: joined MemberLeftAgent1 " + id1 + " in default space", + "AG1: joined MemberLeftAgent1 " + id1 + " in inner space", + "AG1: joined MemberLeftAgent1 " + id2 + " in default space", + "AG1: joined MemberLeftAgent1 " + id2 + " in inner space" + ) } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent0.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent0.sarl deleted file mode 100644 index 3efd42f27c..0000000000 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent0.sarl +++ /dev/null @@ -1,56 +0,0 @@ -/* - * $Id$ - * - * SARL is an general-purpose agent programming language. - * More details on http://www.sarl.io - * - * Copyright (C) 2014-2020 the original authors or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License") - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sarl.sre.tests.runtime.internal.mocks - -import io.sarl.core.Initialize -import io.sarl.core.MemberJoined -import io.sarl.sre.test.framework.skills.TestingCapacity -import io.sarl.sre.test.framework.skills.TestingSkill - -import static extension io.sarl.tests.api.tools.TestUtils.* -import io.sarl.core.Lifecycle - -/** - * @author $Author: sgalland$ - * @version $FullVersion$ - * @mavengroupid $GroupId$ - * @mavenartifactid $ArtifactId$ - */ -agent ChildAgent0 { - - uses Lifecycle - - uses TestingCapacity - - on Initialize { - setSkill(new TestingSkill(occurrence)) - } - - on MemberJoined { - addResult("CHILD1: joined " + occurrence.agentType.simpleTypeName) - } - - on Bye { - killMe - } - -} diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent2.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent2.sarl deleted file mode 100644 index 91b144174b..0000000000 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent2.sarl +++ /dev/null @@ -1,55 +0,0 @@ -/* - * $Id$ - * - * SARL is an general-purpose agent programming language. - * More details on http://www.sarl.io - * - * Copyright (C) 2014-2020 the original authors or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License") - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sarl.sre.tests.runtime.internal.mocks - -import io.sarl.core.AgentKilled -import io.sarl.core.ContextJoined -import io.sarl.core.Initialize -import io.sarl.core.Lifecycle -import io.sarl.sre.test.framework.skills.TestingCapacity -import io.sarl.sre.test.framework.skills.TestingSkill - -/** - * @author $Author: sgalland$ - * @version $FullVersion$ - * @mavengroupid $GroupId$ - * @mavenartifactid $ArtifactId$ - */ -agent ChildAgent2 { - - uses Lifecycle - - uses TestingCapacity - - on Initialize { - setSkill(new TestingSkill(occurrence)) - } - - on ContextJoined { - addResult("CHILD1") - } - - on AgentKilled { - killMe - } - -} diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberJoinedAgent1.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberJoinedAgent1.sarl index 65de393bd4..8197a16c82 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberJoinedAgent1.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberJoinedAgent1.sarl @@ -21,17 +21,17 @@ package io.sarl.sre.tests.runtime.internal.mocks +import io.sarl.core.Behaviors import io.sarl.core.DefaultContextInteractions import io.sarl.core.Initialize import io.sarl.core.InnerContextAccess import io.sarl.core.Lifecycle import io.sarl.core.MemberJoined +import io.sarl.core.OpenEventSpace import io.sarl.sre.test.framework.skills.TestingCapacity import io.sarl.sre.test.framework.skills.TestingSkill import static extension io.sarl.tests.api.tools.TestUtils.* -import io.sarl.core.OpenEventSpace -import io.sarl.core.Behaviors /** * @author $Author: sgalland$ diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/RootAgent1.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent0.sarl similarity index 69% rename from sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/RootAgent1.sarl rename to sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent0.sarl index 29d33311fe..4f39aceda9 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/RootAgent1.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent0.sarl @@ -21,14 +21,11 @@ package io.sarl.sre.tests.runtime.internal.mocks -import io.sarl.core.AgentKilled -import io.sarl.core.AgentSpawned import io.sarl.core.DefaultContextInteractions import io.sarl.core.Initialize import io.sarl.core.InnerContextAccess import io.sarl.core.Lifecycle import io.sarl.core.MemberLeft -import io.sarl.core.Schedules import io.sarl.sre.test.framework.skills.TestingCapacity import io.sarl.sre.test.framework.skills.TestingSkill @@ -40,29 +37,35 @@ import static extension io.sarl.tests.api.tools.TestUtils.* * @mavengroupid $GroupId$ * @mavenartifactid $ArtifactId$ */ -agent RootAgent1 { +agent MemberLeftAgent0 { - uses Schedules, Lifecycle, InnerContextAccess, DefaultContextInteractions + uses Lifecycle, InnerContextAccess, DefaultContextInteractions uses TestingCapacity + var number : int + on Initialize { setSkill(new TestingSkill(occurrence)) - in(500) [ - typeof(ChildAgent1).spawnInContext(innerContext, buildAgentInitializationParameters) - ] - } - - on AgentSpawned [typeof(ChildAgent1).name == occurrence.agentType] { - typeof(Child2Agent1).spawn(buildAgentInitializationParameters(innerContext)) + this.number = occurrence.parameters.get(1) as Integer } - on MemberLeft [!occurrence.isInDefaultSpace] { - addResult("ROOT:" + occurrence.agentType.simpleTypeName) + on MemberLeft { + var msg : String + if (occurrence.inInnerDefaultSpace) { + msg = "inner" + } else if (occurrence.inDefaultSpace) { + msg = "default" + } else { + msg = "other" + } + addResult( + "AG" + this.number + ": joined " + occurrence.agentType.simpleTypeName + " " + occurrence.agentID + " in " + + msg + " space") } - on AgentKilled { - in(1000) [killMe] + on Bye { + killMe } } diff --git a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent1.sarl b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent1.sarl similarity index 60% rename from sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent1.sarl rename to sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent1.sarl index 954781461d..dd1ce4450f 100644 --- a/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/ChildAgent1.sarl +++ b/sre/io.janusproject/io.janusproject.tests/src/test/sarl/io/sarl/sre/tests/runtime/internal/mocks/MemberLeftAgent1.sarl @@ -21,10 +21,13 @@ package io.sarl.sre.tests.runtime.internal.mocks -import io.sarl.core.AgentKilled +import io.sarl.core.DefaultContextInteractions +import io.sarl.core.ExternalContextAccess import io.sarl.core.Initialize +import io.sarl.core.InnerContextAccess import io.sarl.core.Lifecycle import io.sarl.core.MemberLeft +import io.sarl.sre.services.context.Context import io.sarl.sre.test.framework.skills.TestingCapacity import io.sarl.sre.test.framework.skills.TestingSkill @@ -36,21 +39,40 @@ import static extension io.sarl.tests.api.tools.TestUtils.* * @mavengroupid $GroupId$ * @mavenartifactid $ArtifactId$ */ -agent ChildAgent1 { +agent MemberLeftAgent1 { - uses Lifecycle + uses Lifecycle, InnerContextAccess, DefaultContextInteractions, ExternalContextAccess uses TestingCapacity + var number : int + on Initialize { setSkill(new TestingSkill(occurrence)) + this.number = occurrence.parameters.get(1) as Integer + var octx = occurrence.parameters.get(2) as Context + var ictx = innerContext + assert ictx !== null + if (octx !== null) { + join(octx.ID, octx.defaultSpace.spaceID.ID) + } } on MemberLeft { - addResult("CHILD1:" + occurrence.agentType.simpleTypeName) + var msg : String + if (occurrence.inInnerDefaultSpace) { + msg = "inner" + } else if (occurrence.inDefaultSpace) { + msg = "default" + } else { + msg = "other" + } + addResult( + "AG" + this.number + ": joined " + occurrence.agentType.simpleTypeName + " " + occurrence.agentID + " in " + + msg + " space") } - on AgentKilled { + on Bye { killMe }