Xtext at Eclipse DemoCamp London in June 2009

Preview:

DESCRIPTION

Introduction to Xtext via chess notation, some real world examples and a live demonstration of a pizza recipe.

Citation preview

developing domain specific languages

Heiko BehrensEclipse DemoCamp 2009

London 29.07.2009

E-Mail Heiko.Behrens@itemis.de

Blog www.1160pm.net

Xtext?

Xtext is a framework for development ofexternal textual DSLs.

A domain specific language (DSL)in the context of modeling

is a formal, processable languagetargeting at a specific viewpoint or aspect

of a software system.

Its semantics, flexibility and notation is designedin order to support working with that viewpoint

as good as possible.

Rd2-c2

Rd2-c2 ,rook at d2 moves to c2.”

Queen to c7.“”

Check.

Moves in Chess:

!ook at a1 moves to a5.

"ishop at c8 captures knight at h3.

# b1 x c3

$2 - g4

Piece

Piece

Piece

Square

Square

Square

Square

Action

Action

Action

Action

Destination

Destination

Destination

Destination

Rook a1 move a5

Bishop c8 capture h3 Knight

Knight b1 capture c3 Queen

Pawn g2 move g4

SourceDestinationPiece

Move

WhitePlayerBlackPlayer

Game«enum»

Piece*

"ishop at c8 captures knight at h3

" c8 x h3

Model (textfile)

White: "Mayfield"

Black: "Trinks"

pawn at e2 moves to e4

pawn at f7 moves to g5

K b1 - c3

f7 - f5

queen at d1 moves to h5

// 1-0

Grammar (similar to EBNF)Game:

! "White:" whitePlayer=STRING

! "Black:" blackPlayer=STRING

! (moves+=Move)+;!

Move:

! AlgebraicMove | SpokenMove;

AlgebraicMove:

! (piece=Piece)? source=Square (captures?='x'|'-') dest=Square;!

SpokenMove:

! piece=Piece 'at' source=Square

! (captures?='captures' capturedPiece=Piece 'at' | 'moves to')

! dest=Square;!

terminal Square:

! ('a'..'h')('1'..'8'); !

enum Piece:

! pawn = 'P' | pawn = 'pawn' |

! knight = 'N' | knight = 'knight' |

! bishop = 'B' | bishop = 'bishop' |

! rook = 'R' | rook = 'rook' |

! queen = 'Q' | queen = 'queen' |

! king = 'K' | king = 'king';

Xtext framework (customizable with Google Guice)

Grammatik

Superclass

Subclass Class

ecore meta modelLL(*) Parser editor

Modell

© itemis AG 2009 – All rights reserved

Demo

• Model File

• Xtext Grammar for writing simple chess games

• Derived meta model

• Java program that works with textual models

• Xpand-based generator

10

Entities (1/2)@SuppressWarnings("serial")

@Entity@Table(name = "CUSTOMER_INFO")

public class CustomerInfo implements Serializable {

! @Id! @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idSeq")

! @SequenceGenerator(name = "idSeq", sequenceName = "CUST_SEQ", allocationSize = 1)! @Column(name = "CUST_ID", nullable = false)

! private String customerId;

! public void setCustomerId(String customerId) {! ! this.customerId = customerId;

! }

! public String getCustomerId() {! ! return customerId;

! }

! @Column(name = "EMAIL", nullable = false, length = 128)! private String emailAddress;

! public String getEmailAddress() {

! ! return emailAddress;! }

! public void setEmailAddress(String emailAddress) {

! ! String oldValue = emailAddress;! ! this.emailAddress = emailAddress;

! ! firePropertyChangedEvent("emailAddress", oldValue, this.emailAddress);! }

Entities (2/2)

entity CustomerInfo

! (id=CUST_ID, sequenceName=CUST_SEQ) {

!

! String emailAddress (notNull, length = 128)

!

}

DSLs in real world scenarios

modeling datadescribe architectures

configurationbusiness rulesdocumentationdata mappings

navigation on object graphsconstraints

knitting patterns...

Demo! Graphical EMF model

! Enhanced with textual DSL (with Xtext)

! Validation

! Code Generation

! Changes textual DSL

Language development has never been so easy.

amoun%&nit' (am)

ingredient'

*rocedur)

© itemis AG 2009 – All rights reserved

Demo

17

recipe:

"pizza without yeast"

ingredients:

125g curd

1 egg

20ml oil

40ml milk

5g salt

250g flour

1x ! soda

procedure:

"Combine all ingredients. Make it a ball.

Knead 10 times. Put dough on a greased cookie sheet

and roll."

www.xtext.org

twitter: Xtext

xtext.itemis.com

twitter: itemis

me

Heiko Behrens

www.1160pm.net

twitter: HBehrens

Where to go next?

Recommended