Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[contribs] Reimplement the Sierpinski fractals example for using the …
…new SARL-JavaFX API and the SARL bootstrap. close #745 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
fcaa4d0
commit 4477333
Showing
12 changed files
with
225 additions
and
420 deletions.
There are no files selected for viewing
30 changes: 5 additions & 25 deletions
30
...sierpinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/SierpinskiFractals.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,34 +1,14 @@ | ||
package io.sarl.demos.sierpinskiFractals | ||
|
||
import io.sarl.demos.sierpinskiFractals.agents.Fractal | ||
import javafx.application.Application | ||
import io.sarl.demos.sierpinskiFractals.gui.SierpinskiFractalsFxApplication | ||
import io.sarl.demos.sierpinskiFractals.gui.SierpinskiFractalsFxViewerController | ||
import io.sarl.demos.sierpinskiFractals.gui.fx.FxApplication | ||
import io.sarl.demos.sierpinskiFractals.gui.fx.FxBootAgent | ||
import io.sarl.demos.sierpinskiFractals.gui.fx.FxViewerController | ||
import io.sarl.lang.core.Agent | ||
import java.util.List | ||
import java.util.UUID | ||
|
||
/** Agent that is responsible of the application launching. | ||
/** Application launching. | ||
*/ | ||
agent SierpinskiFractals extends FxBootAgent { | ||
|
||
def getFxApplicationType : Class<? extends FxApplication> { | ||
typeof(SierpinskiFractalsFxApplication) | ||
} | ||
|
||
def getApplicationBootAgentType : Class<? extends Agent> { | ||
typeof(Fractal) | ||
} | ||
class SierpinskiFractals { | ||
|
||
override buildApplicationBootAgentParameters( | ||
launcherID : UUID, | ||
controller : FxViewerController, | ||
parameters : List<Object>) { | ||
var ctrl = controller as SierpinskiFractalsFxViewerController | ||
parameters += Configuration::GLOBAL_AREA | ||
parameters += ctrl.positions | ||
static def main(args : String*) { | ||
Application::launch(typeof(SierpinskiFractalsFxApplication)) | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...pinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/agents/AbstractFractal.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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* | ||
*/ | ||
package io.sarl.demos.sierpinskiFractals.agents | ||
|
||
import io.sarl.core.InnerContextAccess | ||
import io.sarl.core.Lifecycle | ||
import io.sarl.demos.sierpinskiFractals.events.Multiply | ||
import io.sarl.demos.sierpinskiFractals.events.Refresh | ||
import io.sarl.demos.sierpinskiFractals.objects.Positions | ||
import io.sarl.demos.sierpinskiFractals.objects.Square | ||
import io.sarl.demos.sierpinskiFractals.objects.Triangle | ||
import io.sarl.demos.sierpinskiFractals.objects.Vector2D | ||
import io.sarl.core.ExternalContextAccess | ||
|
||
/* | ||
* Abstract implementation of a fractal agent | ||
*/ | ||
abstract agent AbstractFractal { | ||
|
||
uses InnerContextAccess, Lifecycle, ExternalContextAccess | ||
|
||
protected var triangle : Triangle | ||
protected var screenSurface : Square | ||
protected var screenWidth : double | ||
protected var positions : Positions | ||
|
||
abstract def doRefresh | ||
|
||
/** | ||
* Generate triangle points | ||
*/ | ||
def generatePoints { | ||
triangle = new Triangle( | ||
screenSurface.bottomLeft, | ||
screenSurface.bottomRight, | ||
new Vector2D(screenSurface.bottomLeft.x + screenWidth / 2, screenSurface.topLeft.y) | ||
) | ||
synchronized (positions) { | ||
positions.addTriangle(triangle) | ||
} | ||
} | ||
/** | ||
* Multiply itself and create child Fractal | ||
*/ | ||
def multiplication { | ||
val screen1 = new Square(triangle.bottomLeft, screenWidth / 2) | ||
spawnInContext(typeof(ChildFractal), innerContext, screen1, positions) | ||
|
||
val screen2 = new Square( | ||
new Vector2D(screenSurface.bottomLeft.x + screenWidth / 4, screenSurface.bottomLeft.y + screenWidth / 2), | ||
screenWidth / 2) | ||
spawnInContext(typeof(ChildFractal), innerContext, screen2, positions) | ||
|
||
val screen3 = new Square(new Vector2D(triangle.top.x, screenSurface.bottomLeft.y), screenWidth / 2) | ||
spawnInContext(typeof(ChildFractal), innerContext, screen3, positions) | ||
} | ||
/* | ||
* Create and emit Multiply signal | ||
*/ | ||
def emitMultiply { | ||
var m = new Multiply | ||
innerContext.defaultSpace.emit(m) | ||
} | ||
|
||
on Refresh [!isFromMe] { | ||
doRefresh | ||
} | ||
|
||
/* | ||
* Multiply itself if signal comes from the GUI and have no member agent | ||
*/ | ||
on Multiply [!isFromMe] { | ||
if (hasMemberAgent) { | ||
emitMultiply | ||
} else { | ||
multiplication | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...ierpinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/agents/ChildFractal.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* | ||
*/ | ||
package io.sarl.demos.sierpinskiFractals.agents | ||
|
||
import io.sarl.core.Behaviors | ||
import io.sarl.core.DefaultContextInteractions | ||
import io.sarl.core.Initialize | ||
import io.sarl.demos.sierpinskiFractals.events.Refresh | ||
import io.sarl.demos.sierpinskiFractals.objects.Positions | ||
import io.sarl.demos.sierpinskiFractals.objects.Square | ||
import io.sarl.javafx.FXKillSupportBehavior | ||
|
||
/* | ||
* Child fractal agent. | ||
* Contains other Fractal agent in its inner context. | ||
*/ | ||
agent ChildFractal extends AbstractFractal { | ||
|
||
uses Behaviors, DefaultContextInteractions | ||
|
||
/* | ||
* Initialization step for smaller fractals | ||
*/ | ||
on Initialize { | ||
var javafxBehavior = new FXKillSupportBehavior(this) | ||
javafxBehavior.registerBehavior | ||
|
||
screenSurface = occurrence.parameters.get(0) as Square | ||
positions = occurrence.parameters.get(1) as Positions | ||
screenWidth = screenSurface.width | ||
|
||
generatePoints | ||
doRefresh | ||
} | ||
|
||
def doRefresh { | ||
new Refresh().emit[it.UUID == parentID] | ||
} | ||
|
||
} |
155 changes: 0 additions & 155 deletions
155
...mos-sierpinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/agents/Fractal.sarl
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
...sierpinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/agents/RootFractal.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* | ||
*/ | ||
package io.sarl.demos.sierpinskiFractals.agents | ||
|
||
import io.sarl.core.Behaviors | ||
import io.sarl.core.Initialize | ||
import io.sarl.demos.sierpinskiFractals.events.Refresh | ||
import io.sarl.demos.sierpinskiFractals.gui.SierpinskiFractalsFxViewerController | ||
import io.sarl.javafx.FXBehavior | ||
import io.sarl.core.ExternalContextAccess | ||
|
||
/* | ||
* Root fractal agent. | ||
* Can communicate with the GUI if not contained in an other agent. | ||
* Contains other Fractal agent in its inner context. | ||
*/ | ||
agent RootFractal extends AbstractFractal { | ||
|
||
uses Behaviors, ExternalContextAccess | ||
|
||
var javafxBehavior : FXBehavior | ||
|
||
/* | ||
* Initialization step for the root fractal | ||
*/ | ||
on Initialize { | ||
var fxcontroller = occurrence.parameters.get(0) as SierpinskiFractalsFxViewerController | ||
|
||
positions = fxcontroller.positions | ||
screenSurface = fxcontroller.square | ||
screenWidth = screenSurface.width | ||
|
||
// The agent learns how to use the UI with a new behavior. | ||
// This new behavior needs to have a reference to the JavaFX controller | ||
this.javafxBehavior = new FXBehavior(this, fxcontroller) | ||
this.javafxBehavior.registerBehavior | ||
|
||
// You could use the JavaFX controller | ||
// fxcontroller.doSomething | ||
generatePoints | ||
doRefresh | ||
} | ||
|
||
def doRefresh { | ||
this.javafxBehavior.UISpace.emit(new Refresh()) | ||
} | ||
|
||
} |
6 changes: 5 additions & 1 deletion
6
...emos-sierpinskifractals/src/main/sarl/io/sarl/demos/sierpinskiFractals/events/events.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,6 +1,10 @@ | ||
package io.sarl.demos.sierpinskiFractals.events | ||
|
||
/* | ||
/* | ||
* Multiply signal to make Fractal agent multiply | ||
*/ | ||
event Multiply | ||
/* | ||
* Multiply signal to make Fractal agent multiply | ||
*/ | ||
event Refresh |
2 changes: 1 addition & 1 deletion
2
...s/src/main/sarl/io/sarl/demos/sierpinskiFractals/gui/SierpinskiFractalsFxApplication.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
Oops, something went wrong.