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

129 lines
3.4 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
agent Fractal {
uses InnerContextAccess, Lifecycle, DefaultContextInteractions, Behaviors
uses Logging
var triangle: Triangle
var screenSurface: Square
var screenWidth: Double
var positions: Positions
var guiSpace: OpenEventSpace
on Initialize {
if (occurrence.parameters.size >= 2){
screenSurface = occurrence.parameters.get(0) as Square
positions = occurrence.parameters.get(1) as Positions
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
}
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)
}
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)
}
def emitMultiply(){
var m = new Multiply
m.source = innerContext.defaultSpace.getAddress(ID)
innerContext.defaultSpace.emit(m)
}
def emitExit(){
var e = new Exit
e.source = innerContext.defaultSpace.getAddress(ID);
innerContext.defaultSpace.emit(e)
}
on Multiply[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
this.multiplication
}
on Multiply [guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent] {
this.emitMultiply
}
on Multiply[occurrence.isInDefaultSpace && !hasMemberAgent] {
this.multiplication
}
on Multiply[occurrence.isInDefaultSpace && hasMemberAgent] {
this.emitMultiply
}
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
killMe;
}
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent]{
this.emitExit
}
on Exit[occurrence.isInDefaultSpace && !hasMemberAgent]{
killMe
}
on Exit[occurrence.isInDefaultSpace && hasMemberAgent]{
this.emitExit
}
on MemberLeft[!hasMemberAgent]{
killMe
}
}