Browse Source

Documentation

master
Antoine Bartuccio 5 years ago
parent
commit
8de385c556
  1. 3
      src/main/sarl/io/sarl/demos/sierpinski/FractalesFXApplication.sarl
  2. 48
      src/main/sarl/io/sarl/demos/sierpinski/agents/Fractal.sarl
  3. 7
      src/main/sarl/io/sarl/demos/sierpinski/events/events.sarl
  4. 27
      src/main/sarl/io/sarl/demos/sierpinski/gui/FXMLViewerController.sarl
  5. 4
      src/main/sarl/io/sarl/demos/sierpinski/objects/Positions.sarl
  6. 3
      src/main/sarl/io/sarl/demos/sierpinski/objects/Square.sarl
  7. 3
      src/main/sarl/io/sarl/demos/sierpinski/objects/Triangle.sarl
  8. 4
      src/main/sarl/io/sarl/demos/sierpinski/objects/Vector2D.sarl

3
src/main/sarl/io/sarl/demos/sierpinski/FractalesFXApplication.sarl

@ -10,6 +10,9 @@ import javafx.scene.Parent
import javafx.scene.Scene
import javafx.stage.Stage
/*
* Sierpinski fractal demo
*/
class FractalesFXApplications extends Application {
private var loader : FXMLLoader

48
src/main/sarl/io/sarl/demos/sierpinski/agents/Fractal.sarl

@ -21,6 +21,11 @@ import io.sarl.core.Logging
import io.sarl.util.OpenEventSpace
import io.sarl.demos.sierpinski.events.Exit
/*
* Fractal agent
* Can communicate with the GUI if not contained in an other agent
* Contains other Fractal agent in its inner context
*/
agent Fractal {
uses InnerContextAccess, Lifecycle, DefaultContextInteractions, Behaviors
uses Logging
@ -31,10 +36,14 @@ agent Fractal {
var positions: Positions
var guiSpace: OpenEventSpace
/*
* Initialization step
*/
on Initialize {
if (occurrence.parameters.size >= 2){
screenSurface = occurrence.parameters.get(0) as Square
positions = occurrence.parameters.get(1) as Positions
// Detect if created by the GUI or not
if (occurrence.parameters.size.equals(3)){
var ctrl = occurrence.parameters.get(2) as FXMLViewerController
guiSpace = defaultContext.createSpace(OpenEventSpaceSpecification, UUID.randomUUID)
@ -49,6 +58,9 @@ agent Fractal {
this.generatePoints
}
/*
* Generate triangle points
*/
def generatePoints(){
triangle = new Triangle(
screenSurface.bottomLeft,
@ -59,6 +71,9 @@ agent Fractal {
info("Parent : " + parentID)
}
/*
* Multiply itself and create child Fractal
*/
def multiplication(){
var screen1 = new Square(
triangle.bottomLeft,
@ -78,50 +93,83 @@ agent Fractal {
spawnInContext(Fractal, innerContext, screen3, positions)
}
/*
* Create and emit Multiply signal
*/
def emitMultiply(){
var m = new Multiply
m.source = innerContext.defaultSpace.getAddress(ID)
innerContext.defaultSpace.emit(m)
}
/*
* Create and emit Exit signal
*/
def emitExit(){
var e = new Exit
e.source = innerContext.defaultSpace.getAddress(ID);
innerContext.defaultSpace.emit(e)
}
/*
* Multiply itself if signal comes from the GUI and have no member agent
*/
on Multiply[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
this.multiplication
}
/*
* Transmit GUI emit signal if connected to GUI and have member agent
*/
on Multiply [guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent] {
this.emitMultiply
}
/*
* Multiply itself if not connected to GUI and have no member agent
*/
on Multiply[occurrence.isInDefaultSpace && !hasMemberAgent] {
this.multiplication
}
/*
* Emit multiply signal in inner context if not connected to GUI and have member agents
*/
on Multiply[occurrence.isInDefaultSpace && hasMemberAgent] {
this.emitMultiply
}
/*
* Kill itself if have no member agent and is connected to the GUI
*/
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
killMe;
}
/*
* Transmit a new Exit signal if have member agent and is connected to the GUI
*/
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent]{
this.emitExit
}
/*
* Kill itself if have no member agent and is not connected to the GUI
*/
on Exit[occurrence.isInDefaultSpace && !hasMemberAgent]{
killMe
}
/*
* Emit a new Exit signal in inner context if have member agent and is not connected to the GUI
*/
on Exit[occurrence.isInDefaultSpace && hasMemberAgent]{
this.emitExit
}
/*
* Kill itself when all member agent are killed
*/
on MemberLeft[!hasMemberAgent]{
killMe
}

7
src/main/sarl/io/sarl/demos/sierpinski/events/events.sarl

@ -1,4 +1,11 @@
package io.sarl.demos.sierpinski.events
/*
* Multiply signal to make Fractal agent multiply
*/
event Multiply
/*
* Make Fractal agent kill itself and its content
*/
event Exit

27
src/main/sarl/io/sarl/demos/sierpinski/gui/FXMLViewerController.sarl

@ -20,8 +20,14 @@ import io.sarl.demos.sierpinski.objects.Vector2D
import io.sarl.lang.core.Address
import io.sarl.demos.sierpinski.events.Exit
/*
* Controller of the GUI
*/
class FXMLViewerController implements EventListener {
/*
* Inner class used to launch janus kernel and first Fractal agent
*/
static class Launcher {
static def main(screen: Square, controller: FXMLViewerController){
Boot::offline = true
@ -40,6 +46,9 @@ class FXMLViewerController implements EventListener {
@FXML private var drawZone: Canvas
@FXML private var multiplyButton: Button
/*
* Exit janus kernel by sending kill signal to agents
*/
@FXML public def exitApplication(){
if (setuped){
var e = new Exit()
@ -49,6 +58,9 @@ class FXMLViewerController implements EventListener {
}
}
/*
* Emit a multiply signal
*/
@FXML protected def actionMultiply() {
if (!setuped){
Launcher.main(
@ -62,10 +74,16 @@ class FXMLViewerController implements EventListener {
this.ispace.emit(m)
}
/*
* Get position object
*/
public def getPositions(){
return this.positions
}
/*
* Draw fractals on canvas
*/
private def draw(){
var gc: GraphicsContext = drawZone.graphicsContext2D
var wait: PauseTransition = new PauseTransition(Duration.seconds(0.03))
@ -87,16 +105,25 @@ class FXMLViewerController implements EventListener {
wait.play
}
/*
* Allow to first agent to register gui on a space
*/
public def setGUISpace(ispace: OpenEventSpace){
this.ispace = ispace
this.ispace.register(this)
}
/*
* Get object ID on the space
*/
@Override
public def getID(): UUID {
return this.id
}
/*
* Needed for EventListner implementation
*/
@Override
public def receiveEvent(ievent: Event){

4
src/main/sarl/io/sarl/demos/sierpinski/objects/Positions.sarl

@ -4,6 +4,10 @@ import java.util.List
import java.util.ArrayList
import java.util.Collections
/*
* Object referencing every Fractal position
* Used by the GUI for printing in canvas
*/
public class Positions {
private var frac: List<Triangle>

3
src/main/sarl/io/sarl/demos/sierpinski/objects/Square.sarl

@ -1,5 +1,8 @@
package io.sarl.demos.sierpinski.objects
/*
* Square object used to simulate a screen to print Triangle on
*/
public class Square {
private var bottomLeft : Vector2D
private var topLeft : Vector2D

3
src/main/sarl/io/sarl/demos/sierpinski/objects/Triangle.sarl

@ -1,5 +1,8 @@
package io.sarl.demos.sierpinski.objects
/*
* Triangle object created by Fractal
*/
class Triangle {
private var bottomLeft: Vector2D
private var bottomRight: Vector2D

4
src/main/sarl/io/sarl/demos/sierpinski/objects/Vector2D.sarl

@ -1,5 +1,9 @@
package io.sarl.demos.sierpinski.objects
/*
* Simple 2D vector
* Contains x and y position
*/
public class Vector2D {
private var y: Double
private var x: Double

Loading…
Cancel
Save