sierpinski-fractales/src/main/sarl/io/sarl/demos/sierpinski/agents/Fractal.sarl

177 lines
4.5 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
*
*/
package io.sarl.demos.sierpinski.agents
import io.sarl.core.Initialize
import io.sarl.core.MemberLeft
import io.sarl.demos.sierpinski.events.Multiply
import io.sarl.core.Lifecycle
import io.sarl.core.InnerContextAccess
import io.sarl.demos.sierpinski.objects.Vector2D
import io.sarl.demos.sierpinski.objects.Square
import io.sarl.demos.sierpinski.gui.FXMLViewerController
import io.sarl.core.DefaultContextInteractions
import io.sarl.util.OpenEventSpaceSpecification
import java.util.UUID
import io.sarl.core.Behaviors
import io.sarl.demos.sierpinski.objects.Positions
import io.sarl.demos.sierpinski.objects.Triangle
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
var triangle: Triangle
var screenSurface: Square
var screenWidth: Double
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)
ctrl.setGUISpace(guiSpace)
guiSpace.register(asEventListener)
}
} else {
screenSurface = new Square
positions = new Positions
}
screenWidth = screenSurface.width
this.generatePoints
}
/*
* Generate triangle points
*/
def generatePoints(){
triangle = new Triangle(
screenSurface.bottomLeft,
screenSurface.bottomRight,
new Vector2D(screenSurface.bottomLeft.x + screenWidth/2, screenSurface.topLeft.y)
)
positions.addTriangle(triangle)
info("Parent : " + parentID)
}
/*
* Multiply itself and create child Fractal
*/
def multiplication(){
var screen1 = new Square(
triangle.bottomLeft,
screenWidth/2
)
var screen2 = new Square(
new Vector2D(screenSurface.bottomLeft.x + screenWidth/4, screenSurface.bottomLeft.y + screenWidth/2),
screenWidth/2
)
var screen3 = new Square(
new Vector2D(triangle.top.x, screenSurface.bottomLeft.y),
screenWidth/2
)
spawnInContext(Fractal, innerContext, screen1, positions)
spawnInContext(Fractal, innerContext, screen2, positions)
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
}
}