mirror of
				https://github.com/klmp200/sierpinski-fractales.git
				synced 2025-11-04 02:33:04 +00:00 
			
		
		
		
	Documentation
This commit is contained in:
		@@ -10,6 +10,9 @@ import javafx.scene.Parent
 | 
				
			|||||||
import javafx.scene.Scene
 | 
					import javafx.scene.Scene
 | 
				
			||||||
import javafx.stage.Stage
 | 
					import javafx.stage.Stage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Sierpinski fractal demo
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
class FractalesFXApplications extends Application {
 | 
					class FractalesFXApplications extends Application {
 | 
				
			||||||
	private var loader : FXMLLoader
 | 
						private var loader : FXMLLoader
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,11 @@ import io.sarl.core.Logging
 | 
				
			|||||||
import io.sarl.util.OpenEventSpace
 | 
					import io.sarl.util.OpenEventSpace
 | 
				
			||||||
import io.sarl.demos.sierpinski.events.Exit
 | 
					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 {
 | 
					agent Fractal {
 | 
				
			||||||
	uses InnerContextAccess, Lifecycle, DefaultContextInteractions, Behaviors
 | 
						uses InnerContextAccess, Lifecycle, DefaultContextInteractions, Behaviors
 | 
				
			||||||
	uses Logging
 | 
						uses Logging
 | 
				
			||||||
@@ -31,10 +36,14 @@ agent Fractal {
 | 
				
			|||||||
	var positions: Positions
 | 
						var positions: Positions
 | 
				
			||||||
	var guiSpace: OpenEventSpace
 | 
						var guiSpace: OpenEventSpace
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Initialization step
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on Initialize {
 | 
						on Initialize {
 | 
				
			||||||
		if (occurrence.parameters.size >= 2){
 | 
							if (occurrence.parameters.size >= 2){
 | 
				
			||||||
			screenSurface = occurrence.parameters.get(0) as Square
 | 
								screenSurface = occurrence.parameters.get(0) as Square
 | 
				
			||||||
			positions = occurrence.parameters.get(1) as Positions
 | 
								positions = occurrence.parameters.get(1) as Positions
 | 
				
			||||||
 | 
								// Detect if created by the GUI or not
 | 
				
			||||||
			if (occurrence.parameters.size.equals(3)){
 | 
								if (occurrence.parameters.size.equals(3)){
 | 
				
			||||||
				var ctrl = occurrence.parameters.get(2) as FXMLViewerController
 | 
									var ctrl = occurrence.parameters.get(2) as FXMLViewerController
 | 
				
			||||||
				guiSpace =  defaultContext.createSpace(OpenEventSpaceSpecification, UUID.randomUUID)
 | 
									guiSpace =  defaultContext.createSpace(OpenEventSpaceSpecification, UUID.randomUUID)
 | 
				
			||||||
@@ -49,6 +58,9 @@ agent Fractal {
 | 
				
			|||||||
		this.generatePoints
 | 
							this.generatePoints
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Generate triangle points
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	def generatePoints(){
 | 
						def generatePoints(){
 | 
				
			||||||
		triangle = new Triangle(
 | 
							triangle = new Triangle(
 | 
				
			||||||
			screenSurface.bottomLeft,
 | 
								screenSurface.bottomLeft,
 | 
				
			||||||
@@ -59,6 +71,9 @@ agent Fractal {
 | 
				
			|||||||
		info("Parent : " + parentID)
 | 
							info("Parent : " + parentID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Multiply itself and create child Fractal
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	def multiplication(){
 | 
						def multiplication(){
 | 
				
			||||||
		var screen1 = new Square(
 | 
							var screen1 = new Square(
 | 
				
			||||||
			triangle.bottomLeft,
 | 
								triangle.bottomLeft,
 | 
				
			||||||
@@ -78,50 +93,83 @@ agent Fractal {
 | 
				
			|||||||
		spawnInContext(Fractal, innerContext, screen3, positions)
 | 
							spawnInContext(Fractal, innerContext, screen3, positions)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Create and emit Multiply signal
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	def emitMultiply(){
 | 
						def emitMultiply(){
 | 
				
			||||||
		var m = new Multiply
 | 
							var m = new Multiply
 | 
				
			||||||
		m.source = innerContext.defaultSpace.getAddress(ID)
 | 
							m.source = innerContext.defaultSpace.getAddress(ID)
 | 
				
			||||||
		innerContext.defaultSpace.emit(m)
 | 
							innerContext.defaultSpace.emit(m)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Create and emit Exit signal
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	def emitExit(){
 | 
						def emitExit(){
 | 
				
			||||||
		var e = new Exit
 | 
							var e = new Exit
 | 
				
			||||||
		e.source = innerContext.defaultSpace.getAddress(ID);
 | 
							e.source = innerContext.defaultSpace.getAddress(ID);
 | 
				
			||||||
		innerContext.defaultSpace.emit(e)
 | 
							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]{
 | 
						on Multiply[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
 | 
				
			||||||
		this.multiplication
 | 
							this.multiplication
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Transmit GUI emit signal if connected to GUI and have member agent
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on Multiply [guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent] {
 | 
						on Multiply [guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent] {
 | 
				
			||||||
		this.emitMultiply
 | 
							this.emitMultiply
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Multiply itself if not connected to GUI and have no member agent
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on Multiply[occurrence.isInDefaultSpace && !hasMemberAgent] {
 | 
						on Multiply[occurrence.isInDefaultSpace && !hasMemberAgent] {
 | 
				
			||||||
		this.multiplication
 | 
							this.multiplication
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Emit multiply signal in inner context if not connected to GUI and have member agents
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on Multiply[occurrence.isInDefaultSpace && hasMemberAgent] {
 | 
						on Multiply[occurrence.isInDefaultSpace && hasMemberAgent] {
 | 
				
			||||||
		this.emitMultiply
 | 
							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]{
 | 
						on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && !hasMemberAgent]{
 | 
				
			||||||
		killMe;
 | 
							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]{
 | 
						on Exit[guiSpace !== null && occurrence.source.spaceId == guiSpace.spaceID && hasMemberAgent]{
 | 
				
			||||||
		this.emitExit
 | 
							this.emitExit
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Kill itself if have no member agent and is not connected to the GUI
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on Exit[occurrence.isInDefaultSpace && !hasMemberAgent]{
 | 
						on Exit[occurrence.isInDefaultSpace && !hasMemberAgent]{
 | 
				
			||||||
		killMe
 | 
							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]{
 | 
						on Exit[occurrence.isInDefaultSpace && hasMemberAgent]{
 | 
				
			||||||
		this.emitExit
 | 
							this.emitExit
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Kill itself when all member agent are killed
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	on MemberLeft[!hasMemberAgent]{
 | 
						on MemberLeft[!hasMemberAgent]{
 | 
				
			||||||
		killMe
 | 
							killMe
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,11 @@
 | 
				
			|||||||
package io.sarl.demos.sierpinski.events
 | 
					package io.sarl.demos.sierpinski.events
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Multiply signal to make Fractal agent multiply
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
event Multiply
 | 
					event Multiply
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Make Fractal agent kill itself and its content
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
event Exit
 | 
					event Exit
 | 
				
			||||||
@@ -20,8 +20,14 @@ import io.sarl.demos.sierpinski.objects.Vector2D
 | 
				
			|||||||
import io.sarl.lang.core.Address
 | 
					import io.sarl.lang.core.Address
 | 
				
			||||||
import io.sarl.demos.sierpinski.events.Exit
 | 
					import io.sarl.demos.sierpinski.events.Exit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Controller of the GUI
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
class FXMLViewerController implements EventListener {
 | 
					class FXMLViewerController implements EventListener {
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Inner class used to launch janus kernel and first Fractal agent
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	static class Launcher {
 | 
						static class Launcher {
 | 
				
			||||||
		static def main(screen: Square, controller: FXMLViewerController){
 | 
							static def main(screen: Square, controller: FXMLViewerController){
 | 
				
			||||||
			Boot::offline = true
 | 
								Boot::offline = true
 | 
				
			||||||
@@ -40,6 +46,9 @@ class FXMLViewerController implements EventListener {
 | 
				
			|||||||
	@FXML private var drawZone: Canvas
 | 
						@FXML private var drawZone: Canvas
 | 
				
			||||||
	@FXML private var multiplyButton: Button
 | 
						@FXML private var multiplyButton: Button
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Exit janus kernel by sending kill signal to agents
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	@FXML public def exitApplication(){
 | 
						@FXML public def exitApplication(){
 | 
				
			||||||
		if (setuped){
 | 
							if (setuped){
 | 
				
			||||||
			var e = new Exit()
 | 
								var e = new Exit()
 | 
				
			||||||
@@ -49,6 +58,9 @@ class FXMLViewerController implements EventListener {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Emit a multiply signal
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	@FXML protected def actionMultiply() {
 | 
						@FXML protected def actionMultiply() {
 | 
				
			||||||
		if (!setuped){
 | 
							if (!setuped){
 | 
				
			||||||
			Launcher.main(
 | 
								Launcher.main(
 | 
				
			||||||
@@ -62,10 +74,16 @@ class FXMLViewerController implements EventListener {
 | 
				
			|||||||
		this.ispace.emit(m)
 | 
							this.ispace.emit(m)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Get position object
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	public def getPositions(){
 | 
						public def getPositions(){
 | 
				
			||||||
		return this.positions
 | 
							return this.positions
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Draw fractals on canvas
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	private def draw(){
 | 
						private def draw(){
 | 
				
			||||||
		var gc: GraphicsContext = drawZone.graphicsContext2D
 | 
							var gc: GraphicsContext = drawZone.graphicsContext2D
 | 
				
			||||||
		var wait: PauseTransition = new PauseTransition(Duration.seconds(0.03))
 | 
							var wait: PauseTransition = new PauseTransition(Duration.seconds(0.03))
 | 
				
			||||||
@@ -87,16 +105,25 @@ class FXMLViewerController implements EventListener {
 | 
				
			|||||||
		wait.play
 | 
							wait.play
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Allow to first agent to register gui on a space
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	public def setGUISpace(ispace: OpenEventSpace){
 | 
						public def setGUISpace(ispace: OpenEventSpace){
 | 
				
			||||||
		this.ispace = ispace
 | 
							this.ispace = ispace
 | 
				
			||||||
		this.ispace.register(this)
 | 
							this.ispace.register(this)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Get object ID on the space
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public def getID(): UUID {
 | 
						public def getID(): UUID {
 | 
				
			||||||
		return this.id
 | 
							return this.id
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Needed for EventListner implementation
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public def receiveEvent(ievent: Event){
 | 
						public def receiveEvent(ievent: Event){
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,10 @@ import java.util.List
 | 
				
			|||||||
import java.util.ArrayList
 | 
					import java.util.ArrayList
 | 
				
			||||||
import java.util.Collections
 | 
					import java.util.Collections
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Object referencing every Fractal position
 | 
				
			||||||
 | 
					 * Used by the GUI for printing in canvas
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class Positions {
 | 
					public class Positions {
 | 
				
			||||||
	private var frac: List<Triangle>
 | 
						private var frac: List<Triangle>
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,8 @@
 | 
				
			|||||||
package io.sarl.demos.sierpinski.objects
 | 
					package io.sarl.demos.sierpinski.objects
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Square object used to simulate a screen to print Triangle on
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class Square {
 | 
					public class Square {
 | 
				
			||||||
	private var bottomLeft : Vector2D
 | 
						private var bottomLeft : Vector2D
 | 
				
			||||||
	private var topLeft : Vector2D
 | 
						private var topLeft : Vector2D
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,8 @@
 | 
				
			|||||||
package io.sarl.demos.sierpinski.objects
 | 
					package io.sarl.demos.sierpinski.objects
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Triangle object created by Fractal
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
class Triangle {
 | 
					class Triangle {
 | 
				
			||||||
	private var bottomLeft: Vector2D
 | 
						private var bottomLeft: Vector2D
 | 
				
			||||||
	private var bottomRight: Vector2D
 | 
						private var bottomRight: Vector2D
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,9 @@
 | 
				
			|||||||
package io.sarl.demos.sierpinski.objects
 | 
					package io.sarl.demos.sierpinski.objects
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Simple 2D vector
 | 
				
			||||||
 | 
					 * Contains x and y position
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
public class Vector2D {
 | 
					public class Vector2D {
 | 
				
			||||||
	private var y: Double
 | 
						private var y: Double
 | 
				
			||||||
	private var x: Double
 | 
						private var x: Double
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user