2017-05-12 14:39:13 +00:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
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
|
2017-05-26 15:50:32 +00:00
|
|
|
|
import io.sarl.demos.sierpinski.events.Exit
|
2017-05-12 14:39:13 +00:00
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Fractal agent
|
|
|
|
|
* Can communicate with the GUI if not contained in an other agent
|
|
|
|
|
* Contains other Fractal agent in its inner context
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
agent Fractal {
|
|
|
|
|
uses InnerContextAccess, Lifecycle, DefaultContextInteractions, Behaviors
|
|
|
|
|
uses Logging
|
|
|
|
|
|
|
|
|
|
var triangle: Triangle
|
|
|
|
|
var screenSurface: Square
|
|
|
|
|
var screenWidth: Double
|
|
|
|
|
var positions: Positions
|
|
|
|
|
var guiSpace: OpenEventSpace
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Initialization step
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
on Initialize {
|
|
|
|
|
if (occurrence.parameters.size >= 2){
|
|
|
|
|
screenSurface = occurrence.parameters.get(0) as Square
|
|
|
|
|
positions = occurrence.parameters.get(1) as Positions
|
2017-06-13 13:19:14 +00:00
|
|
|
|
// Detect if created by the GUI or not
|
2017-05-12 14:39:13 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Generate triangle points
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Multiply itself and create child Fractal
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Create and emit Multiply signal
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
def emitMultiply(){
|
|
|
|
|
var m = new Multiply
|
|
|
|
|
m.source = innerContext.defaultSpace.getAddress(ID)
|
|
|
|
|
innerContext.defaultSpace.emit(m)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Create and emit Exit signal
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
def emitExit(){
|
|
|
|
|
var e = new Exit
|
|
|
|
|
e.source = innerContext.defaultSpace.getAddress(ID);
|
|
|
|
|
innerContext.defaultSpace.emit(e)
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Multiply itself if signal comes from the GUI and have no member agent
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
on Multiply[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
|
|
|
|
|
this.multiplication
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Transmit GUI emit signal if connected to GUI and have member agent
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
on Multiply [guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent] {
|
|
|
|
|
this.emitMultiply
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Multiply itself if not connected to GUI and have no member agent
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
on Multiply[occurrence.isInDefaultSpace && !hasMemberAgent] {
|
|
|
|
|
this.multiplication
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Emit multiply signal in inner context if not connected to GUI and have member agents
|
|
|
|
|
*/
|
2017-05-12 14:39:13 +00:00
|
|
|
|
on Multiply[occurrence.isInDefaultSpace && hasMemberAgent] {
|
|
|
|
|
this.emitMultiply
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Kill itself if have no member agent and is connected to the GUI
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
|
|
|
|
|
killMe;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Transmit a new Exit signal if have member agent and is connected to the GUI
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent]{
|
|
|
|
|
this.emitExit
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Kill itself if have no member agent and is not connected to the GUI
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
on Exit[occurrence.isInDefaultSpace && !hasMemberAgent]{
|
|
|
|
|
killMe
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Emit a new Exit signal in inner context if have member agent and is not connected to the GUI
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
on Exit[occurrence.isInDefaultSpace && hasMemberAgent]{
|
|
|
|
|
this.emitExit
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 13:19:14 +00:00
|
|
|
|
/*
|
|
|
|
|
* Kill itself when all member agent are killed
|
|
|
|
|
*/
|
2017-05-26 15:50:32 +00:00
|
|
|
|
on MemberLeft[!hasMemberAgent]{
|
|
|
|
|
killMe
|
|
|
|
|
}
|
2017-05-12 14:39:13 +00:00
|
|
|
|
|
|
|
|
|
}
|