Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Building DSLs with Xtext

                                         Eclipse Modeling Day

                                 ...
FOUNDATION
 MEMBER TM
Model-Driven Software Development
            with DSLs
Suppose...
You’d want to core an apple...
... for your kids.
?
Right tool for the job?
Your trusty swiss army knife!
Suppose...
You’d want to core a few more apples...
... for an apple cake.
Still the best tool for the job?
Better use this one.
and this one:
... a DSL is ...
A specific tool
for a specific job
A specific tool
for a specific job
Domain-Specific Language (DSL)
A DSL is a formal, processable language
  targeting at a specific viewpoint or
     aspect of...
Rd2-c2
“ Queen to c7.
               Check.”




  “ Rd2-c2 ,
                      ”
rook at d2 moves to c2.
Moves in Chess:
!ook at a1 moves to a5.
    P
    iece           S   q uare      A
                                   ctio...
Rook     a1    move      a5
              Bishop   c8   capture    h3   Knight
              Knight   b1   capture    c3  ...
"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...
Xtext is a complete environment
for development of textual
 - programming languages and
 - domain-specific languages.
It is...
ar
Model




                                    m
                                   m
                               ra
...
Grammar (similar to EBNF)
 Game:
 !   "White:" whitePlayer=STRING
 !   "Black:" blackPlayer=STRING
 !   (moves+=Move)+;
 !...
Demo
• Model         File within Editor + Custom View
• Xtext        Grammar for writing simple chess games
• Derived     ...
Real World DSLs
Regular Expressions
[-+]?[0-9]*.?[0-9]+
Complex Event Processing
from FlightBookings F, CarBookings C, HotelBookings H
  matching [24 hours: F, C, H ]
  on F.name = C.name = H.name
  sele...
internal DSL
 (with Java)
Mailer.mail()
.to(“you@gmail.com”)
.from(“me@gmail.com”)
.subject(“Writing DSLs in Java”)
.body(“...”)
.send();
ANT
<project name="MyProject" default="dist" basedir=".">
  <property name="src" location="src"/>
  <property name="build" loc...
ANT
      ?
<project name="MyProject" default="dist" basedir=".">
  <property name="src" location="src"/>
  <property name="build" loc...
Entities by Example




        vs.
package templates;

import java.util.*;
import java.io.Serializable;
import javax.persistence.*;

@SuppressWarnings("seria...
package templates;

import java.io.Serializable;
import java.util.*;
import javax.persistence.*;

@SuppressWarnings("seria...
entity Customer {
! property name : String
! property address : Address
! property orders : Order[]
}




   POJOs        ...
The Finder Method
<entity name="Customer">
 <attribute type="String" name="name"/>
 <findMethod name="findFixed">
  <expression>
   <equals>...
Demo

! Implement simple Entity DSL
! Use prepared Generator
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
grammar org.xtext.webinar.Entity
!   with org.eclipse.xtext.common.Terminals
                                             ...
Pimp
 my Write
Whitespace-Aware
  Languages
Incremental
Generation
Conclusion
Abstraction
Use a DSL to develop your tools
Support
Newsgroup
Community Forum
Professional Support
twitter @HBehrens
blog http://HeikoBehrens.net

mail       Heiko.Behrens@itemis.de
xing       http://www.xing.com/profile/H...
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
Upcoming SlideShare
Loading in …5
×

Building DSLs with Xtext - Eclipse Modeling Day 2009

4,106 views

Published on

Slides of Eclipse Modeling Day in New York and Toronto http://wiki.eclipse.org/Eclipse_Modeling_Day

Motivation of specific tools with apple corer analogy, Example of domain-specific language (chess notation), introduction to Xtext with demo plus outlook

Published in: Technology
  • Be the first to comment

Building DSLs with Xtext - Eclipse Modeling Day 2009

  1. 1. Building DSLs with Xtext Eclipse Modeling Day New York, 16 November 2009 Toronto, 18 November 2009 Heiko Behrens (itemis) © itemis AG 2009 – All rights reserved
  2. 2. FOUNDATION MEMBER TM
  3. 3. Model-Driven Software Development with DSLs
  4. 4. Suppose...
  5. 5. You’d want to core an apple...
  6. 6. ... for your kids.
  7. 7. ? Right tool for the job?
  8. 8. Your trusty swiss army knife!
  9. 9. Suppose...
  10. 10. You’d want to core a few more apples...
  11. 11. ... for an apple cake.
  12. 12. Still the best tool for the job?
  13. 13. Better use this one.
  14. 14. and this one:
  15. 15. ... a DSL is ...
  16. 16. A specific tool for a specific job
  17. 17. A specific tool for a specific job
  18. 18. Domain-Specific Language (DSL) A DSL is a formal, processable language targeting at a specific viewpoint or aspect of a software system. Its semantics, flexibility and notation is designed in order to support working with that viewpoint as good as possible.
  19. 19. Rd2-c2
  20. 20. “ Queen to c7. Check.” “ Rd2-c2 , ” rook at d2 moves to c2.
  21. 21. Moves in Chess: !ook at a1 moves to a5. P iece S q uare A ction De stin ation "ishop at c8 captures knight at h3. P iece S q uare ion Action D es tinat # b1 x c3 Piece S qua re ction stination AD e $2 - g4 ation S A D quar e ction e stin
  22. 22. Rook a1 move a5 Bishop c8 capture h3 Knight Knight b1 capture c3 Queen Pawn g2 move g4 Game Move Source «enum» WhitePlayer * Destination Piece BlackPlayer Piece
  23. 23. "ishop at c8 captures knight at h3 " c8 x h3
  24. 24. 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
  25. 25. Xtext is a complete environment for development of textual - programming languages and - domain-specific languages. It is implemented in Java and is based on Eclipse, EMF, and Antlr.
  26. 26. ar Model m m ra G Generator Runtime Superclass Subclass Class LL(*) Parser ecore meta model editor
  27. 27. 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';
  28. 28. Demo • Model File within Editor + Custom View • Xtext Grammar for writing simple chess games • Derived meta model • Java program that works with textual models • Xpand-based generator © itemis AG 2009 – All rights reserved 31
  29. 29. Real World DSLs
  30. 30. Regular Expressions
  31. 31. [-+]?[0-9]*.?[0-9]+
  32. 32. Complex Event Processing
  33. 33. from FlightBookings F, CarBookings C, HotelBookings H matching [24 hours: F, C, H ] on F.name = C.name = H.name select F.name, true bookingWithCar, into TripleBookings from FlightBookings F, CarBookings C, HotelBookings H matching [24 hours: F, !C, H ] on F.name = C.name = H.name select F.name, false bookingWithCar into TripleBookings from TripleBookings where prev(bookingWithCar) and not bookingWithCar select name into LostCustomers
  34. 34. internal DSL (with Java)
  35. 35. Mailer.mail() .to(“you@gmail.com”) .from(“me@gmail.com”) .subject(“Writing DSLs in Java”) .body(“...”) .send();
  36. 36. ANT
  37. 37. <project name="MyProject" default="dist" basedir="."> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <mkdir dir="${build}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile"> <mkdir dir="${dist}/lib"/> <jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/> </target> <target name="clean"> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
  38. 38. ANT ?
  39. 39. <project name="MyProject" default="dist" basedir="."> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <mkdir dir="${build}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile"> <mkdir dir="${dist}/lib"/> <jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/> </target> <target name="clean"> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
  40. 40. Entities by Example vs.
  41. 41. package templates; import java.util.*; import java.io.Serializable; import javax.persistence.*; @SuppressWarnings("serial") @Entity public class Customer implements Serializable { ! private Long id; ! private String name; ! private Address address; ! private Set<Order> orders = new HashSet<Order>(); ! // No-arg constructor ! public Customer() { ! } ! @Id ! public Long getId() { ! ! return id; ! } ! public void setId(Long id) { ! ! this.id = id; ! } ! public String getName() { ! ! return name; ! } ! public void setName(String name) { ! ! this.name = name; ! } ! public Address getAddress() { ! ! return address; ! } ! public void setAddress(Address address) { ! ! this.address = address; ! } ! @OneToMany ! public Collection<Order> getOrders() { ! ! return orders; ! } ! public void setOrders(Set<Order> orders) { ! ! this.orders = orders; ! } }
  42. 42. package templates; import java.io.Serializable; import java.util.*; import javax.persistence.*; @SuppressWarnings("serial") @Entity public class Customer implements Serializable { ! private Long id; ! private String name; ! private Address address; ! private Set<Order> orders = new HashSet<Order>(); ! // No-arg constructor ! public Customer() { ! } ! @Id ! public Long getId() { ! ! return id; ! } ! public void setId(Long id) { ! ! this.id = id; ! } ! public String getName() { ! ! return name; ! } ! public void setName(String name) { ! ! this.name = name; ! } ! public Address getAddress() { ! ! return address; ! } ! public void setAddress(Address address) { ! ! this.address = address; ! } ! @OneToMany ! public Collection<Order> getOrders() { ! ! return orders; ! } ! public void setOrders(Set<Order> orders) { ! ! this.orders = orders; ! } }
  43. 43. entity Customer { ! property name : String ! property address : Address ! property orders : Order[] } POJOs DAOs
  44. 44. The Finder Method
  45. 45. <entity name="Customer"> <attribute type="String" name="name"/> <findMethod name="findFixed"> <expression> <equals> <attributeRef attr="name"/> <constant val="John Doe"/> </equals> </expression> <findMethod> </entity> entity Customer { ! property name : String ! finder findFixed : name = "John Doe" }
  46. 46. Demo ! Implement simple Entity DSL ! Use prepared Generator
  47. 47. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  48. 48. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  49. 49. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  50. 50. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  51. 51. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  52. 52. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  53. 53. grammar org.xtext.webinar.Entity ! with org.eclipse.xtext.common.Terminals entity generate entity "http://www.xtext.org/webinar/Entity" Model Model: types ! (elements+=Type)*; * Type: Type name: EString ! SimpleType | Entity; SimpleType: ! 'type' name=ID; SimpleType Entity Entity: extends ! 'entity' name=ID ('extends' extends=[Entity])? '{' properties ! ! properties+=Property* * ! '}'; Property name: EString type many: EBoolean Property: ! 'property' name=ID ':' type=[Type] (many?='[]')?;
  54. 54. Pimp my Write
  55. 55. Whitespace-Aware Languages
  56. 56. Incremental Generation
  57. 57. Conclusion
  58. 58. Abstraction
  59. 59. Use a DSL to develop your tools
  60. 60. Support Newsgroup Community Forum Professional Support
  61. 61. twitter @HBehrens blog http://HeikoBehrens.net mail Heiko.Behrens@itemis.de xing http://www.xing.com/profile/Heiko_Behrens linkedin http://www.linkedin.com/in/HeikoBehrens www.xtext.org The Committer Team twitter @Xtext Heiko Michael Sven Moritz Peter Dennis Jan Patrick Knut Sebastian Behrens Clay Efftinge Eysholdt Friese Hübner Köhnlein Schönbach Wannheden Zarnekow

×