practical dsl design - wordpress.compractical dsl design groovy sydney meetup may 4th, 2010 peter...
TRANSCRIPT
Practical DSL Design
Groovy Sydney MeetupMay 4th, 2010
Peter BellCEO/CTO SystemsForge
Wednesday, May 5, 2010
Overview• Before DSLs . . .
• What is a DSL?
• Creating a DSL
• Good DSL Design
• Key Concepts
• Implementing DSLs in Groovy
• Testing DSLs
• DSL Evolution
Wednesday, May 5, 2010
About Me• Programmer: 50-80 projects/yr
• Entrepreneur: profitable/practical
• Writer: GroovyMag, InfoQ, IEEE Software, JSMag, Fusion Authority, Flex Authority
• Presenter: ooPSLA, Code Generation, BCS SPA . . .
• Research: Domain Specific Modeling, Software Product Lines
Wednesday, May 5, 2010
Warning!• Jetlagged
• Rewrote this afternoon
• First cut
• Relatively new to Groovy
• Brought to you by . . .
Wednesday, May 5, 2010
Before DSLs (meet the parents!)• CASE -> DSM
• No one language, SME readable
• Requirement engineering -> DDD
• Domain model is key
• Little languages (Eric Raymond)
• Small, discrete, composable
Wednesday, May 5, 2010
What is a DSL?• Specific to single domain
• Executable
• Generally not Turing complete
• More expressive than GPL within the domain
Wednesday, May 5, 2010
• Subjective
• Often facade to API
• Focus on language nature
• “Fluent” interface
What is a DSL? - DSL vs. API
API: new event( startTime: "16:00", endTime: "18:00" )
DSL: new event.from 4.pm, to: 6.pm
Wednesday, May 5, 2010
What is a DSL? - Benefits• Expressive (clear intent, concise)
• Separate business logic from code
• Separate lifecycle
• SME readable (not usually writable)
Wednesday, May 5, 2010
What is a DSL? - When Use?
• Frequently changing business rules
• Interactions with stakeholders
• Relatively well understood domain
Wednesday, May 5, 2010
Creating a DSL• Top down:
• Sketch out UL with stakeholders
• Bottom up:
• Evolve from API
Wednesday, May 5, 2010
Good DSL Design• Learn from DDD/UL:
• Common, rigorous understanding
• Driven by domain model
• Involve to SME’s
• Lots of little languages
• Bounded contexts
• Risk:
• DSL eventually becomes badly designed GPL
• Think: Ant
Wednesday, May 5, 2010
Key Concepts• Overview:
• Vertical vs horizontal
• Abstract vs Concrete
• Projections
• Three types of DSL
• Implementing external DSLs
• Internet vs. External
Wednesday, May 5, 2010
Vertical vs. Horizontal• Vertical: business domain
• Insurance, PC configurator . . .
• Horizontal: technical domain
• SQL, CSS, GORM . . .
Wednesday, May 5, 2010
Abstract vs. Concrete• Abstract grammar
• what say
• Concrete syntax
• how say
<person> <FirstName>Paul</FirstName>
<LastName>King</LastName></person>
new person( firstName: "Paul", lastName: "King" )
person called “Paul “, “ King “
Paul King
Wednesday, May 5, 2010
Projections• Multiple projections
• Textual
• Spreadsheet
• Visual
Wednesday, May 5, 2010
Three Types of DSL• Internal
• External
• Language workbench
Wednesday, May 5, 2010
Implementing External DSLs• XML
• Delimiter directed translation
• Regex or similar
• Doesn’t support nesting
• Only for simplest languages
• Syntax directed translation
• Grammar/parser
• ANTLR
Wednesday, May 5, 2010
Internal vs. External• Internal:
• Easier to implement
• Power of GPL:
• Loops
• Math
• Conditionals . . .
• Refactor to external
• External:
• Easier to evolve(?)
• End user writable (forms)?
• Projectional:
• Forms
• Visual
• Spreadsheet
Wednesday, May 5, 2010
Implementing DSLs in Groovy• Flexible and malleable
syntax
• Metaprogramming
• AST transformations
© Guillaume Laforge
Wednesday, May 5, 2010
Flexible and Malleable Syntax• Scripts
• Optional typing
• Native syntax constructs
• (lists, maps)
• Parentheses/semi omission
• Named arguments
• Operator overloading
• Closures
© Guillaume Laforge
Wednesday, May 5, 2010
Metaprogramming• PoGo
• Categories
• Builders
• Custom metaclass
• ExpandoMetaClass
© Guillaume Laforge
Wednesday, May 5, 2010
AST Transformations
© Guillaume Laforge
• AST traversal
• Local transformations
• Global transformations
• Hooks into ANTLR
Wednesday, May 5, 2010
Testing DSLs• Test domain model
• Test fluent interface (expression builder)
• Scenarios
• Orthogonal test DSLs
• Don’t gen tests unless lots of custom extensions
Wednesday, May 5, 2010
DSL Evolution• The problem: success!
• Approaches:
• Backwards compatibility
• Versioning
• Model migrations
Wednesday, May 5, 2010
Conclusions• Learn from history
• It’s all about the domain
• Evolve to DSLs
• API to internal . . .
• . . . external if required
• Check Guillaume’s preso for syntax
• Consider testing and evolution from day 1
Wednesday, May 5, 2010
Resources
History:http://www.faqs.org/docs/artu/minilanguageschapter.html
DSM:JP Tolvanen: http://www.metacase.com/blogs/jpt/blogViewSteve Kelly: http://www.metacase.com/blogs/stevek/blogViewMarkus Voelter: http://www.voelter.de/
DDD:http://domaindrivendesign.org/
Wednesday, May 5, 2010
Resources (2)DSLs:Martin Fowler: http://martinfowler.com/dslwip/
Groovy DSLs:Google - Laforge, Subramaniam, Ford
DSL Evolution:http://www.infoq.com/articles/dsl-evolution
Books:Domain Specific Modeling - Kelly/TolvanenGenerative Programming - Czarnecki/EiseneckerDomain Driven Design - Evans
Wednesday, May 5, 2010
The End!• http://gettinggroovy.wordpress.com
• Email: [email protected]
• Twitter: peterbell
Wednesday, May 5, 2010