Skip to content

Commit

Permalink
[contribs] Add the firework's example.
Browse files Browse the repository at this point in the history
see #697

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 10, 2017
1 parent 89b158b commit a60f29e
Show file tree
Hide file tree
Showing 28 changed files with 1,451 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ io_sarl_tutorials_pingpong_Name = Ping-Pong event exchanges
io_sarl_tutorials_pingpong_Description = Agents are exchanging events with a ping-pong interaction protocol.

io_sarl_tutorials_pingpongspace_Name = Ping-Pong event exchanges inside a specific space
io_sarl_tutorials_pingpongspace_Description = Agents are exchanging events inside a specific space with a ping-pong interaction protocol.
io_sarl_tutorials_pingpongspace_Description = Agents are exchanging events inside a specific space with a ping-pong interaction protocol.

io_sarl_demos_fireworks_Name = Fireworks with JavaFX
io_sarl_demos_fireworks_Description = The goal of this demo is to bring out some fireworks using SARL agents. The application is composed of 4 types of agents. The whole structure is holonic.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<zip_example name="io-sarl-tutorials-holonicauction" />
<zip_example name="io-sarl-tutorials-pingpong" />
<zip_example name="io-sarl-tutorials-pingpongspace" />
<zip_example name="io-sarl-demos-fireworks" />
<!--zip_example name="name-examples" excludes="**/*._trace,.antlr-generator*.jar"/>-->
</target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@
</link>
</group>

<html src="./javafx_intro.html"/>

<group style-id="quick-links" id="quick-links">
<link style-id="content-link" label="Fireworks in SARL"
url="http://org.eclipse.ui.intro/execute?command=org.eclipse.ui.newWizard%28newWizardId%3Dio.sarl.demos.fireworks%29"
id="JavaFX-Fireworks-Sample">
<text>
A JavaFX application showing fireworks on the screen.
</text>
</link>
</group>

<html src="./tutorials_intro.html"/>

<group style-id="quick-links" id="quick-links">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="content-header">
<h4 style="color: #f59616; line-height: 1.5;">SARL Examples</h4>
<p>
<span style="line-height: 1.3; font-size: 60%;"> Learn about SARL, a collections of examples to illustrate the different aspects of the agent-oriented language.</span>
<span style="line-height: 1.3; font-size: 60%;"> Learn about SARL, a collection of examples to illustrate the different aspects of the agent-oriented language.</span>
</p>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="content-header">
<h4 style="color: #f59616; line-height: 1.5;">SARL Examples with JavaFX</h4>
<p>
<span style="line-height: 1.3; font-size: 60%;">A collection of examples based on the JavaFX user interface.</span>
</p>
</div>



42 changes: 42 additions & 0 deletions contribs/io.sarl.examples/io.sarl.examples.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,37 @@
</example>
</extension>

<!-- ========= io-sarl-demos-fireworks Example ========= -->

<extension point="org.eclipse.ui.newWizards">
<wizard id="io.sarl.demos.fireworks"
name="%io_sarl_demos_fireworks_Name"
class="io.sarl.examples.wizard.SarlExampleInstallerWizard"
category="org.eclipse.ui.Examples/io.sarl.Examples"
icon="platform:/plugin/org.eclipse.xtend.examples/icons/genproject.gif"
project="true">
<description>%io_sarl_demos_fireworks_Description</description>
</wizard>
</extension>

<extension point="org.eclipse.ui.newWizards">
<wizard id="io.sarl.demos.fireworks"
name="%io_sarl_demos_fireworks_Name"
class="io.sarl.examples.wizard.SarlExampleInstallerWizard"
category="io.sarl.eclipse.category.wizards/io.sarl.project.Examples"
icon="platform:/plugin/org.eclipse.xtend.examples/icons/genproject.gif"
project="true">
<description>%io_sarl_demos_fireworks_Description</description>
</wizard>
</extension>

<extension point="org.eclipse.emf.common.ui.examples">
<example wizardID="io.sarl.demos.fireworks" pageImage="platform:/plugin/org.eclipse.xtend.examples/icons/genproject.gif">
<projectDescriptor name="io-sarl-demos-fireworks" contentURI="contents/io-sarl-demos-fireworks.zip" description="%io_sarl_demos_fireworks_Description"/>
<fileToOpen location="io-sarl-demos-fireworks/io/sarl/demos/fireworks/Fireworks.sarl"/>
</example>
</extension>

<!-- ========= Intro (welcome page) Extensions ========= -->

<extension
Expand All @@ -357,5 +388,16 @@
</command>
</extension>

<!-- adding a new quicklink for the fireworks example -->
<extension
point="org.eclipse.ui.intro.quicklinks">
<command
id="org.eclipse.ui.newWizard(newWizardId=io.sarl.demos.fireworks)"
description="%io_sarl_demos_fireworks_Description"
label="%io_sarl_demos_fireworks_Name"
icon="platform:/plugin/org.eclipse.ui.intro.universal/themes/circles/graphics/icons/ctool/sa_onesample48.gif">
</command>
</extension>


</plugin>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# MVC pattern

This application follows a MVC (Model-View-Controller) pattern for the GUI communication.

![MVC pattern applied on this project](MVC_en.png)


# Fireworks demonstration


The goal of this demo is to bring out some fireworks using SARL agents.
The application is composed of 4 agents. The whole structure holonic.

![Agents organization in the fireworks demo](Firework_en.png)

The main agent is the *LaunchingArea* which is linked with the GUI and and contains the other agents inside its inner context.
This agent creates the *RocketLauncher*, one by rocket asked by the user. It also transmits the gravity and other parameters.
This agent also registers the GUI on a dedicated OpenEventSpace for communication.

```Scala
agent LaunchingArea {

...

/*
* Get setup event from the GUI
*/
on SetupSettings {
this.rocketsQuantity = occurrence.rocketsQuantity
this.fireQuantity = occurrence.fireQuatity
this.gravity = occurrence.gravity
this.maxWidth = occurrence.maxWidth
}

/*
* Agent initialisation when spawned by the GUI
* A communication space is opened between
* the GUI and this agent
*/
on Initialize [!occurrence.parameters.empty] {
var ctrl = occurrence.parameters.get(0) as FXMLViewerController
var ispace = defaultContext.createSpace(
OpenEventSpaceSpecification, UUID.randomUUID)
ctrl.setGUISpace(ispace)
ispace.register(asEventListener)

ctrl.listenAndDraw(grid)

info("Finishing initialization of Launching Area")

}

...
}
```

![Application with the GUI](firework_screenshot.png)

Then, each RocketLauncher creates a *Rocket*. When this one is destroyed, the *RocketLauncher* is going to generate another one. This allows to displace the verification of the existence of the *Rocket* out of the LaunchingArea. It also isolates the *Rocket* and avoid that emitted events disrupt the managing of the simulation.

```Scala
agent RocketLauncher {

...

/*
* A new rocket is launched when the previous
* one is destroyed
*/
on MemberLeft [!isFromMe(occurrence) && !exited] {
// wake allows to send the event at the agent itself
wake(new Launch)
}
...
}
```

*Rocket* agents are the heart of this demo. Each one create a task with a fixed delay where they updates their position by writing it in a object shared with the GUI, here named *Positions*. When the *Rocket* reach the end of its lifetime, they create *Fire* agent within their inner context according to the quantity asked by the user and then wait for the destruction of every *Fire* to kill themselves.

```Scala
agent Rocket {

...

on Initialize {

...

/*
* Create a new background task to update
* the agent position at a fixed delay
*/
move = atFixedDelay(
Configuration.RocketLifeCycleSchedulingRate) [
try {
wake(new UpdateRocketPosition);
} catch (e : Exception) {
e.printStackTrace
}
]
}

on UpdateRocketPosition [isFromMe(occurrence) &&
!frozen && !exploded] {
var vect = new Vector(2)
x = x + speedx
y = y + speedy
vect.clear()
vect.add(x)
vect.add(y)
lifetime = lifetime - 10

/* Updates the Position object */
if (grid !== null)
grid.setRocketPosition(id, vect)
if (lifetime <= 0) {
exploded = true
move.cancel(true)
grid.hideHocketPosition(id)
wake(new Explode)
}
}

...
}

```
The *Fire* is the last agent of the demo. It have a list of positions and is subjected to gravity. As *Rocket* agent, the *Fire* launch a task at a fixed delay to update its actual position, add it at the end of the actual list and then update the *Positions* object. When its lifetime is over, the *Fire* is destroyed.

When the GUI is closed, an event *Exit* is sent to the *LaunchingArea* which transmits it to the other agents she owns. It waits for their elimination to destroy the next one, until itself.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollBar?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<Pane fx:id="main_pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="608.0" prefWidth="933.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.sarl.demos.fireworks.gui.FireworksFxViewerController">
<children>
<Canvas fx:id="draw_zone" height="583.0" layoutX="199.0" layoutY="13.0" rotate="180.0" width="715.0" />
<Button fx:id="setup_button" layoutX="26.0" layoutY="45.0" mnemonicParsing="false" onAction="#actionSetup" text="%SetupButtonText">
<tooltip>
<Tooltip text="%SetupButtonTooltip" />
</tooltip></Button>
<Button fx:id="launch_button" disable="true" layoutX="26.0" layoutY="89.0" mnemonicParsing="false" onAction="#actionLaunch" text="%LaunchButtonText">
<tooltip>
<Tooltip text="%LaunchButtonTooltip" />
</tooltip></Button>
<AnchorPane layoutX="30.0" layoutY="145.0">
<children>
<ScrollBar fx:id="gravity_input" max="3.0" onMouseEntered="#actionGravityDisplay" prefHeight="17.0" prefWidth="159.0" unitIncrement="0.1" value="0.5">
<tooltip>
<Tooltip text="%GravityTooltip" />
</tooltip></ScrollBar>
<Label fx:id="gravity_display" layoutX="135.0" layoutY="17.0" prefHeight="17.0" prefWidth="46.0" text="0,5" textAlignment="RIGHT" />
<Label layoutY="17.0" prefHeight="17.0" prefWidth="46.0" text="%GravityLabelText" />
</children>
</AnchorPane>
<AnchorPane layoutX="27.0" layoutY="255.0">
<children>
<Label layoutX="4.0" layoutY="17.0" text="%FireQuantityLabelText" />
<Label fx:id="fire_quantity_display" layoutX="135.0" layoutY="17.0" prefHeight="17.0" prefWidth="46.0" text="30" textAlignment="RIGHT" />
<ScrollBar fx:id="fire_quantity_input" layoutX="4.0" max="50.0" min="5.0" onMouseEntered="#actionFireQuantityDisplay" prefHeight="17.0" prefWidth="159.0" value="30.0">
<tooltip>
<Tooltip text="%FireQuantityTooltip" />
</tooltip></ScrollBar>
</children>
</AnchorPane>
<AnchorPane layoutX="30.0" layoutY="199.0">
<children>
<ScrollBar fx:id="rocket_quantity_input" max="40.0" min="1.0" onMouseEntered="#actionRocketQuantityDisplay" prefHeight="17.0" prefWidth="159.0" value="20.0">
<tooltip>
<Tooltip text="%RocketQuantityTooltip" />
</tooltip></ScrollBar>
<Label layoutY="20.0" text="%RocketQuantityLabelText" />
<Label fx:id="rocket_quantity_display" layoutX="135.0" layoutY="20.0" prefHeight="17.0" prefWidth="46.0" text="20" textAlignment="RIGHT" />
</children>
</AnchorPane>
<Button fx:id="stop_button" disable="true" layoutX="117.0" layoutY="89.0" mnemonicParsing="false" onAction="#actionStop" text="%StopButtonText">
<tooltip>
<Tooltip text="%StopButtonTooltip" />
</tooltip></Button>
</children>
</Pane>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
TITLE=SARL Demo: Fireworks Animation
SetupButton=Configure
LaunchButton=Launch
StopButton=Stop
GravityLabel=Gravity
RocketQuantityLabel=Rocket quantity
FireQuantityLabel=Fire quantity
SetupButtonTooltip=Setup the entire environment according to the data given below
LaunchButtonTooltip=Launch fireworks
StopButtonTooltip=Stop and freeze fireworks
GravityTooltip=Change demo gravity
RocketQuantityTooltip=Change rocket quantity of the demo
FireQuantityTooltip=Change fire quantity of the demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
TITLE=SARL Démo: Animation de feux d'artifice
SetupButtonText=Configurer
LaunchButtonText=Lancer
StopButtonText=Stop
GravityLabelText=Gravité
RocketQuantityLabelText=Quantité de roquettes
FireQuantityLabelText=Quantité de feu
SetupButtonTooltip=Configure l'environement en fonction des données plus bas
LaunchButtonTooltip=Lance l'animation de feux d'artifices
StopButtonTooltip=Stop et gèle les feux d'artifices
GravityTooltip=Change la gravité de la démo
RocketQuantityTooltip=Change la quantité de roquettes de la démo
FireQuantityTooltip=Change la quantité de feu émise par les roquettes
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* @Author: Bartuccio Antoine
* @Date: 2017-05-26 16:41:58
* @Last Modified by: klmp200
* @Last Modified time: 2017-05-26 17:29:05
*/

// Nice white #F9FAFB
// Nice black #2B2B2B

#main_pane {
-fx-background-color: #2B2B2B;
}

.scroll-bar {
-fx-background-color: rgb(57, 57, 57);
}

.scroll-bar .thumb {
-fx-background-color: rgb(169, 169, 169);
-fx-background-radius: 0em;
-fx-background-inserts: 0, 0, 0;
}

.scroll-bar .increment-arrow,
.scroll-bar .decrement-arrow {
-fx-color: rgb(169, 169, 169);
}

.scroll-bar:hover .increment-arrow,
.scroll-bar:hover .decrement-arrow {
-fx-color: #2B2B2B;
-fx-background-color: rgb(169, 169, 169);
}

.label {
-fx-text-fill: #F9FAFB;
}

.button {
-fx-background-radius: 0em;
-fx-background-inserts: 0, 0, 0;
-fx-focus-color: transparent;
-fx-text-fill: #2B2B2B;
-fx-background-color: rgb(169, 169, 169);
}
Loading

0 comments on commit a60f29e

Please sign in to comment.