Skip to content

Commit

Permalink
[examples] Fixing the syntax errors due to the recent changes in the …
Browse files Browse the repository at this point in the history
…spawning API.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 11, 2020
1 parent 1b0ed01 commit 5f1fef1
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 24 deletions.
Expand Up @@ -126,7 +126,7 @@ agent HelloChildAgent extends AbstractAgent{
}

on AgentSpawned {
info("AgentSpawned, agentID:" + occurrence.agentIdentifiers + " AgentType: " + occurrence.agentType)
info("AgentSpawned, agentID:" + occurrence.agentID + " AgentType: " + occurrence.agentType)
}

on AgentKilled {
Expand Down
Expand Up @@ -46,8 +46,8 @@ agent HolarchyManager {
uses Lifecycle, Schedules, DefaultContextInteractions, Logging

on Initialize {
/* Spawn a first holon that will in turn spawn a whole hierarchy of holons
*/
/* Spawn a first holon that will in turn spawn a whole hierarchy of holons
*/
spawn(Holon, #[])
}

Expand Down Expand Up @@ -96,12 +96,14 @@ agent Holon {
info("Create the child #" + i + " at level " + count)
/* spawn a new sub-holon (a holon inside his own internal context)
*/
childUIDs.add(spawnInContext(Holon, innerContext, count, i))
val aid = UUID::randomUUID
spawnInContextWithID(Holon, aid, innerContext, count, i)
childUIDs.add(aid)
}

/* check every seconds if he's himself the last remaining holon as member of its internal context event space
* in this case, its means that all of its members are dead, he can thus die peacefully
* TEMPORAL APPROACH TO DESTROY THE HOLARCHY not the best one, it is better to rely on event to manage the holarchy destruction see the Memberleft on clause
* in this case, its means that all of its members are dead, he can thus die peacefully
* TEMPORAL APPROACH TO DESTROY THE HOLARCHY not the best one, it is better to rely on event to manage the holarchy destruction see the Memberleft on clause
* */
// task("toto").every(1000) [
// if (innerContext.defaultSpace.participants.size == 1) {
Expand Down
Expand Up @@ -132,7 +132,8 @@ class BoidsSimulation implements EventListener {

defaultSARLContext = kernel.startWithoutAgent

environment = kernel.startAgent(typeof(Environment), height, width)
environment = UUID::randomUUID
kernel.startAgentWithID(typeof(Environment), environment, height, width)

launchAllBoids
^space = defaultSARLContext.defaultSpace as OpenEventSpace
Expand Down Expand Up @@ -160,7 +161,8 @@ class BoidsSimulation implements EventListener {
var initialPosition = new Vector2d((Math::random - 0.5) * width, (Math::random - 0.5) * height)
var initialVitesse = new Vector2d(Math::random - 0.5, Math::random - 0.5)

var b = kernel.startAgent(typeof(Boid), environment, p, initialPosition, initialVitesse, boidName)
var b = UUID::randomUUID
kernel.startAgentWithID(typeof(Boid), b, environment, p, initialPosition, initialVitesse, boidName)
this.boidBodies.put(b, new PerceivedBoidBody(p, b, initialPosition, initialVitesse))

if (Settings::isLogActivated) {
Expand Down
Expand Up @@ -109,7 +109,8 @@ class FireworksFxViewerController extends FxViewerController {
var ^event = new SetupSettings(this.rocketQuantity, this.fireQuantity,
this.gravity, this.draw_zone.width)
if (!this.launched) {
this.launchedAgent = startAgentApplication(typeof(LaunchingArea)) [
this.launchedAgent = UUID::randomUUID
startAgentApplication(typeof(LaunchingArea), this.launchedAgent) [
^event.emitToAgents
]
this.launch_button.disable = false
Expand Down
Expand Up @@ -21,6 +21,7 @@

package io.sarl.demos.gameoflife.game

import io.sarl.core.DefaultContextInteractions
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import java.util.List
Expand All @@ -39,7 +40,7 @@ import org.arakhne.afc.vmutil.locale.Locale
*/
skill DefaultGridManagerSkill implements GridManager {

uses Lifecycle, Logging
uses Lifecycle, Logging, DefaultContextInteractions

val listeners = <EnvironmentListener>newArrayList

Expand Down Expand Up @@ -73,7 +74,8 @@ skill DefaultGridManagerSkill implements GridManager {
for (j : 0 ..< height) {
// FIXME: agents created 2 times ?
// FIXME: double agent spawning apparently come from: https://github.com/sarl/sarl/issues/525
val uuid = CellAgent.spawn
val uuid = UUID::randomUUID
CellAgent.spawnInContextWithID(uuid, defaultContext)

grid.set(i, j, Pair.of(uuid, false))

Expand Down
Expand Up @@ -20,8 +20,9 @@ class MyAppFxViewerController extends FxViewerController {
def initialize {
// Launching of the agent-side of the application
if (!this.launched.getAndSet(true)) {
// First launch => start the agent framework with an agent of type MyAgent.
this.launchedAgent = startAgentApplication(typeof(MyAgent)) [
// First launch => start the agent framework with an agent of type MyAgent and the computed id.
val id = UUID::randomUUID
startAgentApplication(typeof(MyAgent), id) [
// TODO: Put any action that must be done after the first agent launching
]
}
Expand Down
Expand Up @@ -33,7 +33,7 @@
import static io.sarl.examples.tests.ExamplesTestUtils.isMavenProject;
import static io.sarl.examples.tests.ExamplesTestUtils.readExtensionPointFromXml;
import static io.sarl.examples.tests.ExamplesTestUtils.readXmlNode;
import static io.sarl.examples.tests.ExamplesTestUtils.unpackFiles;
import static io.sarl.examples.tests.ExamplesTestUtils.*;
import static io.sarl.examples.tests.ExamplesTestUtils.assertFile;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.LAUNCH_PROPERTY_FILE;
import static io.sarl.examples.wizard.SarlExampleLaunchConfiguration.readLaunchConfigurationFromXml;
Expand All @@ -43,8 +43,10 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static io.sarl.tests.api.tools.TestUtils.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -55,11 +57,13 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.opentest4j.TestAbortedException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import io.sarl.lang.compiler.batch.SarlBatchCompiler;
import io.sarl.lang.util.OutParameter;
import io.sarl.tests.api.tools.TestUtils;

/** Class for testing the examples.
*
Expand All @@ -72,6 +76,21 @@
@DisplayName("Testing all the SARL examples")
public class ExamplesTest {

private static List<File> installFiles(ExampleDescription example, File projectRoot, boolean skipIfInvalidPom) throws Exception {
// The behavior is different in Eclipse Junit and Maven surefire.
final List<File> installedFiles;
if (isEclipseRuntimeEnvironment()) {
if (skipIfInvalidPom && isMavenProject(example.sourceFolder)) {
// Pom file is not valid because Maven has not yet applied the macro replacements into the file.
throw new TestAbortedException();
}
installedFiles = copySourceFiles(projectRoot, example.sourceFolder);
} else {
installedFiles = unpackFiles(projectRoot, example.archive);
}
return installedFiles;
}

/** Replies the dynamics tests for the examples' paths.
*
* @return the dynamic tests.
Expand All @@ -91,11 +110,11 @@ public Stream<DynamicTest> path() throws Exception {
* @throws Exception in case of error for recovering the example descriptions.
*/
@TestFactory
@DisplayName("SARL Compilation")
@DisplayName("SARL compilation of archives")
public Stream<DynamicTest> compilation() throws Exception {
return dynamicTests(example -> {
final File projectRoot = createProject();
final List<File> installedFiles = unpackFiles(projectRoot, example.archive);
final File projectRoot = createProject();
final List<File> installedFiles = installFiles(example, projectRoot, true);
assertFalse(installedFiles.isEmpty(), () -> "No installed file in " + projectRoot);
if (isMavenProject(example.sourceFolder)) {
// Maven compilation
Expand All @@ -119,7 +138,7 @@ public Stream<DynamicTest> compilation() throws Exception {
public Stream<DynamicTest> launchConfiguration() throws Exception {
return dynamicTests(example -> {
final File projectRoot = createProject();
final List<File> installedFiles = unpackFiles(projectRoot, example.archive);
final List<File> installedFiles = installFiles(example, projectRoot, true);
final File launchConfiguration = new File(projectRoot, LAUNCH_PROPERTY_FILE);
assumeTrue(launchConfiguration.exists());

Expand Down Expand Up @@ -159,7 +178,7 @@ public Stream<DynamicTest> launchConfiguration() throws Exception {
public Stream<DynamicTest> fileToOpenInEditor() throws Exception {
return dynamicTests(example -> {
final File projectRoot = createProject();
final List<File> installedFiles = unpackFiles(projectRoot, example.archive);
final List<File> installedFiles = installFiles(example, projectRoot, false);
final File pluginFile = new File(DEFAULT_RELATIVE_PATH, "plugin.xml");
final Document document = readXmlContent(pluginFile);
Node node = readXmlNode(document, "plugin");
Expand Down
Expand Up @@ -415,6 +415,58 @@ public static List<File> unpackFiles(File root, File exampleZipFile) throws Exce
return installedFiles;
}

/** Copy the files from the given source folder into the root folder.
*
* @param root the root folder.
* @param sourceFolder the folder to copy.
* @return the list of extracted files.
* @throws Exception if cannot unpack.
* @since 0.11
*/
public static List<File> copySourceFiles(File root, File sourceFolder) throws Exception {
List<File> folders = new ArrayList<>();
if (sourceFolder.isDirectory()) {
final File absSourceFolder = sourceFolder.getAbsoluteFile().getCanonicalFile();
folders.add(sourceFolder);
while (!folders.isEmpty()) {
final File folder = folders.remove(0);
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
folders.add(file);
} else if (file.isFile()) {
if (!isIgnorableFile(file)) {
final File absFile = file.getAbsoluteFile().getCanonicalFile();
final File relPathFile = FileSystem.makeRelative(absFile, absSourceFolder);
final File targetFile = FileSystem.join(root, relPathFile);
targetFile.getParentFile().mkdirs();
FileSystem.copy(file, targetFile);
}
}
}
}
}

final List<File> installedFiles = new ArrayList<>();
folders = new ArrayList<>();
folders.add(root);
while (!folders.isEmpty()) {
final File folder = folders.remove(0);
for (final File file : folder.listFiles()) {
if (file.isDirectory()) {
folders.add(file);
} else if (file.isFile()) {
if (!isIgnorableFile(file)) {
final File relPathFile = FileSystem.makeRelative(file, root);
installedFiles.add(relPathFile);
} else {
file.delete();
}
}
}
}
return installedFiles;
}

private static boolean isIgnorableFile(File file) {
final String name = file.getName();
return ".classpath".equals(name) || ".project".equals(name);
Expand Down
Expand Up @@ -101,12 +101,13 @@ abstract class FxViewerController implements EventListener {
}

/** Start the agent application.
*
*
* @param agentType the type of the agent to launch.
* @param agentID is the identifier of the agent.
* @param whenAgentLaunched the lambda that is called when the agent is really launched.
* @since 0.11
*/
def startAgentApplication(agentType : Class<? extends Agent>,
whenAgentLaunched : () => void) {
def startAgentApplication(agentType : Class<? extends Agent>, agentID : UUID = null, whenAgentLaunched : ()=>void) {
var bootstrap = SRE::bootstrap
if (!bootstrap.active) {
throw new IllegalStateException(Messages::FxViewerController_0)
Expand All @@ -115,11 +116,15 @@ abstract class FxViewerController implements EventListener {
this.globalSpace = context.defaultSpace as OpenEventSpace
this.UISpace = context.createSpace(typeof(OpenEventSpaceSpecification), ID)
new LaunchingCallback(ID, this.globalSpace, whenAgentLaunched)
bootstrap.startAgent(agentType, this)
if (agentID !== null) {
bootstrap.startAgentWithID(agentType, agentID, this)
} else {
bootstrap.startAgent(agentType, this)
}
}

/** Catch exit event from JavaFX. Stop the agents and the JavaFX user interface.
*
*
* @param event the JavaFx event that is the source of the exit.
*/
@FXML
Expand Down

0 comments on commit 5f1fef1

Please sign in to comment.