GraphTransformation case study
In this lab session, we'll transform Turtles choreographies into real pictures. From a Turtle representation of a drawing, we'll produce the associated SVG file, viewable in a web browser. The lab session is then divided in four parts:
Turtle to a Figure)Turtle → SVG
We consider in this lab session a very simple Cartesian figure meta-model. In this meta-model, a given Figure contains a set of Shapes. A Shape holds a position, and can be a simple Line, a Polyline (i.e, a line following several points) or a Text. A Figure defines its width and height, and its colours (background and foreground).
We'll assume you're working in a new Kermeta Project. The following archive kermeta_material.zip contains several resources associated to this lab session:
metamodel directory)Turtle.ecore, an Ecore implementation of the Turtle meta-modelFigure.ecore (and its associated Figure.ecorediag diagram), an Ecore implementation of a very restricted figure meta-model.data directory (to be dropped in the model directory)aSquare.xmi: a serialized figure model, representing a squareMystery_figure.xmi: a serialized figure model, representing a nice drawingMystery_turtle.xmi: a serialized turtle model, representing the same drawing as aboveOctogon_turtle.xmi: a serialized turtle model, representing an octagonViolation.xmi: a serialized figure model, which is invalid regarding business constraintsmetamodel/register.kmt) Turtle enhancement: (in a file src/helpers/EnhancedTurtle.kmt)Stage concept: operation load(file: String): Stage is do … endoperation save(file: String): Void is do … endFarms in your resource to avoid DanglingRef troublesFigure enhancement: (in a file src/helpers/EnhancedFigure.kmt)Shape concept: Shape knows its associated Figure as a fig reference.Figure concept: operation load(file: String): Figure is do … endoperation save(file: String): Void is do … endShape concept: position of a Shape must be contained by the FigureCoordinates …Java Project named TrigoWrapper in your workspaceplugins/fr.irisa.triskell.kermeta.interpreter_1.3.0.jarTrigo class, and implement : cos(α) & sin(α),radians ↔ degrees converter (tip: rad = (π * deg) / 180)src/helpers/Trigo.kmt:cos(α) & sin(α) (α expressed in degrees)The SVG standard allows one to define pictures as XML code. Here is an example1) of such a code:
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="0" x2="300" y2="300" style="stroke:rgb(99,99,99);stroke-width:2"/> </svg>
src/transfo/Figure2SVG.kmt)Figure transformable into SVG Code
Here is another example, based on the aSquare model.
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="200" height="200" version="1.1" xmlns="http://www.w3.org/2000/svg" > <rect height="200" width="200" style="fill:#D1D1D1;stroke-width:2; stroke:rgba(0,0,0,1)" /> <text x="15.0" y="15.0" style="fill: rgba(143,34,255,1);">A Simple Square</text> <line x1="50.0" y1="50.0" x2="150.0" y2="50.0" style="stroke:rgba(0,0,0,1); stroke-width:2" /> <line x1="150.0" y1="50.0" x2="150.0" y2="150.0" style="stroke:rgba(255,255,153,1); stroke-width:2" /> <line x1="150.0" y1="150.0" x2="50.0" y2="150.0" style="stroke:rgba(0,0,0,1); stroke-width:2" /> <line x1="50.0" y1="150.0" x2="50.0" y2="50.0" style="stroke:rgba(0,0,0,1); stroke-width:2" /> </svg>
In this section, we'll transform a Turtle choreography into a Figure. In other words, it means to transform a Polar representation into a Cartesian coordinates representation.
In a file src/transfo/Turtle2Figure.kmt:
Turtle concept with this Cartesian concerns ((x,y) real coordinates, α angle)move(r: Real): Void is do … end which make the turtle walkx' = x + r * cos(α)y' = y - r * sin(α)Stage concept (and the others …) to perform the Turtle → Figure transformationattributes … (symptom of a forgotten clone: an existing value vanished, replaced by a void).
In a file src/transfo/Turtle2SVG.kmt:
Here is an execution (simplified) trace when drawing a simple square:
#### Drawing polygons ... ## -> drawPolygon(4,100) -> square.xmi t.move(100.0): (x: 300.0, y: 300.0, theta: 90.0) --> (x: 300.0, y: 200.0, theta: 90.0) t.turn(90.0): (x: 300.0, y: 200.0, theta: 90.0) --> (x: 300.0, y: 200.0, theta: 0.0) t.move(100.0): (x: 300.0, y: 200.0, theta: 0.0) --> (x: 400.0, y: 200.0, theta: 0.0) t.turn(90.0): (x: 400.0, y: 200.0, theta: 0.0) --> (x: 400.0, y: 200.0, theta: -90.0) t.move(100.0): (x: 400.0, y: 200.0, theta: -90.0) --> (x: 400.0, y: 300.0, theta: -90.0) t.turn(90.0): (x: 400.0, y: 300.0, theta: -90.0) --> (x: 400.0, y: 300.0, theta: -180.0) t.move(100.0): (x: 400.0, y: 300.0, theta: -180.0) --> (x: 300.0, y: 300.0, theta: -180.0) t.turn(90.0): (x: 300.0, y: 300.0, theta: -180.0) --> (x: 300.0, y: 300.0, theta: -270.0) #### End of drawing polygon
In the following exercises, you'll define a program in turtle terms, and then use model transformation to produce the assocated SVG file.
Polygon class to draw polygons drawPolygon(8,100,”octagon”) will produce a octagon.svg file containing an octagon of length 100px.EMF implementation of the same problem …Spirograph class to draw Spirographied polygonsFractal class to draw fractals curves