Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
54 deletions.
There are no files selected for viewing
30 changes: 18 additions & 12 deletions
30
...ts/io-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/boot.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 38 additions & 22 deletions
60
...-sarl-tutorials-pingpongspace/src/main/sarl/io/sarl/examples/pingpongspace/pongagent.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |