Skip to content

Commit

Permalink
[contribs] Reimplement the Sierpinski fractals example for using the …
Browse files Browse the repository at this point in the history
…new SARL-JavaFX API and the SARL bootstrap.

close #745

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jan 30, 2018
1 parent fcaa4d0 commit 4477333
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 420 deletions.
@@ -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))
}

}
@@ -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
}
}

}
@@ -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]
}

}

This file was deleted.

@@ -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())
}

}
@@ -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
@@ -1,6 +1,6 @@
package io.sarl.demos.sierpinskiFractals.gui

import io.sarl.demos.sierpinskiFractals.gui.fx.FxApplication
import io.sarl.javafx.FxApplication
import java.util.ResourceBundle
import javafx.fxml.FXMLLoader
import javafx.fxml.JavaFXBuilderFactory
Expand Down

0 comments on commit 4477333

Please sign in to comment.