Browse Source

Implements fractals

master
Antoine Bartuccio 5 years ago
commit
d9b92bb1c4
  1. 23
      .gitignore
  2. 27
      sierpinski-fractales.iml
  3. 13
      src/main/sarl/io/sarl/demos/sierpinski/FractalesFXApplication.fxml
  4. 36
      src/main/sarl/io/sarl/demos/sierpinski/FractalesFXApplication.sarl
  5. 110
      src/main/sarl/io/sarl/demos/sierpinski/agents/Fractal.sarl
  6. 14
      src/main/sarl/io/sarl/demos/sierpinski/events/events.sarl
  7. 104
      src/main/sarl/io/sarl/demos/sierpinski/gui/FXMLViewerController.sarl
  8. 21
      src/main/sarl/io/sarl/demos/sierpinski/objects/Positions.sarl
  9. 63
      src/main/sarl/io/sarl/demos/sierpinski/objects/Square.sarl
  10. 32
      src/main/sarl/io/sarl/demos/sierpinski/objects/Triangle.sarl
  11. 38
      src/main/sarl/io/sarl/demos/sierpinski/objects/Vector2D.sarl

23
.gitignore

@ -0,0 +1,23 @@
# Xtext generated files
src/main/generated-sources/
# NetBean / Eclipse
bin/
**/*.java._trace
**/*.java_trace
**/*.smap
**/.project
**/.classpath
**/.settings
**/.metadata
# Subversion and CVS
**/.svn/
**/.cvs/
# Maven
target/
# Mac
.DS_Store

27
sierpinski-fractales.iml

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="EclipseModuleManager">
<conelement value="io.sarl.eclipse.launching.SARL_SUPPORT" />
<conelement value="io.janusproject.plugin.launching.JANUS_SUPPORT" />
<src_description expected_position="0">
<src_folder value="file://$MODULE_DIR$/src/main/sarl" expected_position="0" />
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="1" />
<src_folder value="file://$MODULE_DIR$/src/main/resources" expected_position="2" />
<src_folder value="file://$MODULE_DIR$/src/main/generated-sources/sarl" expected_position="3" />
</src_description>
</component>
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/target/classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/sarl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/generated-sources/sarl" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="library" exported="" name="io.sarl.eclipse.launching.SARL_SUPPORT" level="application" />
<orderEntry type="library" name="io.janusproject.plugin.launching.JANUS_SUPPORT" level="application" />
</component>
</module>

13
src/main/sarl/io/sarl/demos/sierpinski/FractalesFXApplication.fxml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.Canvas?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="608.0" prefWidth="813.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="io.sarl.demos.sierpinski.gui.FXMLViewerController">
<children>
<Canvas fx:id="drawZone" height="600.0" layoutX="199.0" layoutY="4.0" rotate="180.0" scaleX="-1.0" width="600.0" />
<Button layoutX="26.0" layoutY="45.0" mnemonicParsing="false" onAction="#actionSetup" text="Setup" />
<Button fx:id="multiplyButton" disable="true" layoutX="26.0" layoutY="89.0" mnemonicParsing="false" onAction="#actionMultiply" text="Multiply" />
</children>
</Pane>

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

@ -0,0 +1,36 @@
package io.sarl.demos.sierpinski
import javafx.application.Application
import javafx.stage.Stage
import javafx.fxml.FXMLLoader
import java.net.URL
import javafx.fxml.JavaFXBuilderFactory
import javafx.scene.Parent
import javafx.scene.Scene
class FractalesFXApplications extends Application {
private var loader: FXMLLoader
@Override
public def start(stage: Stage) throws Exception {
var location: URL = getClass().getResource("FractalesFXApplication.fxml")
loader = new FXMLLoader()
loader.setLocation(location)
loader.setBuilderFactory(new JavaFXBuilderFactory)
var root: Parent = loader.load(location.openStream) as Parent
var scene: Scene = new Scene(root)
stage.setTitle("SARL Demo: Sierpinski Fractal Animation")
stage.setScene(scene)
stage.show
}
public static def main(args: String[]){
launch(args)
}
@Override
public def stop() throws Exception {
super.stop()
}
}

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

@ -0,0 +1,110 @@
/**
*
*/
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
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)
}
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 Multiply[hasMemberAgent && memberAgentCount >= 3 && (occurrence.emmiter == parentID || guiSpace !== null)]{
// innerContext.defaultSpace.emit(new Multiply(ID))
// }
// on MemberLeft [!isFromMe(occurrence) && !hasMemberAgent] {
// killMe
// }
}

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

@ -0,0 +1,14 @@
package io.sarl.demos.sierpinski.events
import java.util.UUID
event Multiply {
var emmiter: UUID
new (emmiter: UUID){
this.emmiter = emmiter
}
new (){
emmiter = null
}
}
event Exit

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

@ -0,0 +1,104 @@
package io.sarl.demos.sierpinski.gui
import io.sarl.lang.core.EventListener
import java.util.UUID
import io.sarl.util.OpenEventSpace
import javafx.fxml.FXML
import javafx.scene.canvas.Canvas
import javafx.scene.control.Button
import io.sarl.lang.core.Event
import javafx.^event.ActionEvent
import io.sarl.demos.sierpinski.agents.Fractal
import io.janusproject.Boot
import io.sarl.demos.sierpinski.objects.Square
import io.sarl.demos.sierpinski.events.Multiply
import io.sarl.demos.sierpinski.objects.Positions
import javafx.scene.canvas.GraphicsContext
import javafx.animation.PauseTransition
import javafx.util.Duration
import javafx.scene.paint.Color
import io.sarl.demos.sierpinski.objects.Vector2D
import io.sarl.lang.core.Address
class FXMLViewerController implements EventListener {
static class Launcher {
static def main(screen: Square, controller: FXMLViewerController){
Boot::offline = true
Boot::startJanus(typeof(Fractal), screen, controller.positions, controller)
controller.draw
}
}
private val id: UUID = UUID.randomUUID()
private var positions: Positions = new Positions
private var ispace: OpenEventSpace
private var setuped = false
@FXML private var drawZone: Canvas
@FXML private var multiplyButton: Button
@FXML public def exitApplication(ievent: ActionEvent){
// ispace.emit(new Exit())
// Platform.ext()
}
@FXML protected def actionSetup() {
if (!setuped){
multiplyButton.setDisable(false)
Launcher.main(
new Square(new Vector2D(0.0, 0.0),
600.0
), this )
setuped = true
}
}
@FXML protected def actionMultiply() {
var m = new Multiply
m.source = new Address(ispace.spaceID, id)
this.ispace.emit(m)
}
public def getPositions(){
return this.positions
}
private def draw(){
var gc: GraphicsContext = drawZone.graphicsContext2D
var wait: PauseTransition = new PauseTransition(Duration.seconds(0.03))
gc.setStroke(Color.ORANGERED)
gc.setLineWidth(3)
wait.setOnFinished [ e |
try {
gc.clearRect(0, 0, drawZone.width, drawZone.height)
positions.triangles.forEach[ t |
gc.strokeLine(t.bottomLeft.x, t.bottomLeft.y, t.top.x, t.top.y)
gc.strokeLine(t.top.x, t.top.y, t.bottomRight.x, t.bottomRight.y)
gc.strokeLine(t.bottomRight.x, t.bottomRight.y, t.bottomLeft.x, t.bottomLeft.y)
]
} catch (exception: Exception){}
wait.playFromStart
]
wait.play
}
public def setGUISpace(ispace: OpenEventSpace){
this.ispace = ispace
this.ispace.register(this)
}
@Override
public def getID(): UUID {
return this.id
}
@Override
public def receiveEvent(ievent: Event){
}
}

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

@ -0,0 +1,21 @@
package io.sarl.demos.sierpinski.objects
import java.util.List
import java.util.ArrayList
import java.util.Collections
public class Positions {
private var frac: List<Triangle>
new (){
frac = new ArrayList
}
public def addTriangle(t: Triangle){
synchronized(frac){frac.add(t)}
}
public def getTriangles(): List<Triangle> {
synchronized(frac){return Collections.unmodifiableList(frac)}
}
}

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

@ -0,0 +1,63 @@
package io.sarl.demos.sierpinski.objects
public class Square {
private var bottomLeft : Vector2D
private var topLeft : Vector2D
private var bottomRight : Vector2D
private var topRight : Vector2D
new () {
bottomLeft = new Vector2D
topLeft = new Vector2D
bottomRight = new Vector2D
topRight = new Vector2D
}
new (BottomLeft: Vector2D, length: Double){
this.bottomLeft = BottomLeft
this.bottomRight = new Vector2D(BottomLeft.x + length, bottomLeft.y)
this.topRight = new Vector2D(bottomRight.x, bottomRight.y + length)
this.topLeft = new Vector2D(bottomLeft.x, bottomLeft.y + length)
}
def getBottomLeft() : Vector2D {
return bottomLeft
}
def getBottomRight() : Vector2D {
return bottomRight
}
def getTopLeft() : Vector2D {
return topLeft
}
def getTopRight() : Vector2D {
return topRight
}
def setBottomLeft(v : Vector2D) {
bottomLeft = v
}
def setBottomRight(v : Vector2D) {
bottomRight = v
}
def setTopLeft(v : Vector2D) {
topLeft = v
}
def setTopRight(v : Vector2D) {
topRight = v
}
def getWidth() : Double {
return bottomRight.x - bottomLeft.x
}
def toString(): String {
return bottomLeft + " " + topLeft + " " + topRight + " " + bottomLeft
}
}

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

@ -0,0 +1,32 @@
package io.sarl.demos.sierpinski.objects
class Triangle {
private var bottomLeft: Vector2D
private var bottomRight: Vector2D
private var top: Vector2D
new (){
bottomLeft = new Vector2D
bottomRight = new Vector2D
top = new Vector2D
}
new (bottomLeft: Vector2D, bottomRight: Vector2D, top: Vector2D){
this.bottomLeft = bottomLeft
this.top = top
this.bottomRight = bottomRight
}
public def getBottomLeft(): Vector2D {
return bottomLeft
}
public def getBottomRight(): Vector2D {
return bottomRight
}
public def getTop(): Vector2D {
return top
}
}

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

@ -0,0 +1,38 @@
package io.sarl.demos.sierpinski.objects
public class Vector2D {
private var y: Double
private var x: Double
new () {
x = 0.0
y = 0.0
}
new (x : Double, y : Double) {
this.x = x
this.y = y
}
public def getX() : Double {
return this.x
}
public def getY() : Double {
return this.y
}
public def setX(x : Double) {
this.x = x
}
public def setY(y : Double) {
this.y = y
}
public def toString(): String{
return "(x: " + x + ", y: " + y + ")"
}
}
Loading…
Cancel
Save