Skip to content

Commit

Permalink
Tuning demo PingPong in subspace
Browse files Browse the repository at this point in the history
  • Loading branch information
ngaud committed Feb 25, 2020
1 parent 1c6aeb1 commit c5dfdd7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 54 deletions.
@@ -1,41 +1,47 @@
/*
/*
* $Id$
*
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-@RELEASE_YEAR@ the original authors or authors.
*
*
* 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
*
*
* 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.examples.pingpongspace

import io.sarl.core.Initialize
import io.sarl.core.Lifecycle
import java.util.UUID

/**
/**
* Boot agent launching two other agents Ping and Pong
* @author Stephane Galland.
* @author Nicolas GAUD
*/
agent BootAgent {

uses Lifecycle

/**
* Number of iterations before stopping this demo and killing all agents
*/
val MAX_ITERATION = 30000;

on Initialize {
var spaceId = UUID::randomUUID
spawn( PongAgent, spaceId )
spawn( PingAgent, spaceId )
spawn(PingAgent, spaceId, MAX_ITERATION)
spawn(PongAgent, spaceId, MAX_ITERATION)
killMe
}

Expand Down
@@ -1,50 +1,49 @@
/*
/*
* $Id$
*
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-@RELEASE_YEAR@ the original authors or authors.
*
*
* 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
*
*
* 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.examples.pingpongspace

import io.sarl.core.Behaviors
import io.sarl.core.DefaultContextInteractions
import io.sarl.core.ExternalContextAccess
import io.sarl.core.Initialize
import io.sarl.core.Schedules
import io.sarl.core.Behaviors
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import io.sarl.core.OpenEventSpace
import io.sarl.core.OpenEventSpaceSpecification
import io.sarl.core.ExternalContextAccess
import io.sarl.core.ParticipantJoined
import io.sarl.core.Schedules
import java.util.UUID
import io.sarl.core.Logging

/**
/**
* Agent waiting its buddy PongAgent to send him a ping event, and replies to pong event with a ping event
* @author Stephane Galland.
* @author Nicolas GAUD
*/
agent PingAgent {

uses DefaultContextInteractions, Schedules, Behaviors, Logging, ExternalContextAccess
uses DefaultContextInteractions, Schedules, Behaviors, Logging, ExternalContextAccess, Lifecycle

var comspace : OpenEventSpace

on Pong {
info("Receiving Pong #" + occurrence.index)
info("Sending Ping #" + (occurrence.index + 1))
comspace.emit(new Ping(occurrence.index + 1)) [it == occurrence.source]
}
var MAX_ITERATION : Integer

on Initialize {
comspace = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification),
Expand All @@ -53,6 +52,12 @@ agent PingAgent {
comspace.register(asEventListener())

info("Waiting for Pong Agent")

MAX_ITERATION = occurrence.parameters.get(1) as Integer
}

on ParticipantJoined [(!isFromMe) && (occurrence.spaceID == comspace.spaceID)] {

val task = task("waiting_for_partner")
task.every(1000) [
if (comspace.participants.size > 1) {
Expand All @@ -67,4 +72,16 @@ agent PingAgent {
]
}

on Pong [occurrence.index < MAX_ITERATION] {
info("Receiving Pong #" + occurrence.index)
info("Sending Ping #" + (occurrence.index + 1))
comspace.emit(new Ping(occurrence.index + 1))[it == occurrence.source]
}

on Pong [occurrence.index == MAX_ITERATION] {
info("Receiving last Pong #" + occurrence.index)
info("PingAgent dying")
killMe
}

}
@@ -1,55 +1,71 @@
/*
/*
* $Id$
*
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-@RELEASE_YEAR@ the original authors or authors.
*
*
* 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
*
*
* 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.examples.pingpongspace

import io.sarl.core.Behaviors
import io.sarl.core.DefaultContextInteractions
import io.sarl.core.OpenEventSpace
import io.sarl.core.ExternalContextAccess
import io.sarl.core.Initialize
import io.sarl.core.Logging
import io.sarl.core.OpenEventSpace
import io.sarl.core.OpenEventSpaceSpecification
import io.sarl.core.Behaviors
import io.sarl.core.ExternalContextAccess
import java.util.UUID
import io.sarl.core.Logging
import io.sarl.core.Lifecycle

/**
/**
* Agent waiting a ping event and replies with a pong event
* @author Stephane Galland.
* @author Nicolas GAUD
*/
agent PongAgent {
uses DefaultContextInteractions, Behaviors, Logging, ExternalContextAccess

uses DefaultContextInteractions, Behaviors, Logging, ExternalContextAccess, Lifecycle

var comspace : OpenEventSpace

var MAX_ITERATION : Integer

on Initialize {
comspace = defaultContext.getOrCreateSpaceWithSpec(
typeof(OpenEventSpaceSpecification),
comspace = defaultContext.getOrCreateSpaceWithSpec(typeof(OpenEventSpaceSpecification),
occurrence.parameters.get(0) as UUID)
comspace.register(asEventListener())
MAX_ITERATION = occurrence.parameters.get(1) as Integer
}
on Ping {

on Ping [occurrence.index < MAX_ITERATION] {
info("Receiving Ping #" + occurrence.index)
info("Sending Pong #" + occurrence.index)
comspace.emit(new Pong( occurrence.index )) [it == occurrence.source]
comspace.emit(new Pong(occurrence.index)) [
it == occurrence.source
]
}

on Ping [occurrence.index == MAX_ITERATION] {
info("FINAL: Receiving Ping #" + occurrence.index)
info("FINAL: Sending a very last Pong #" + occurrence.index)
comspace.emit(new Pong(occurrence.index)) [
it == occurrence.source
]
info("PongAgent dying")
killMe
}

}

0 comments on commit c5dfdd7

Please sign in to comment.