28
Practical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010

Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Practical DSL Design

Groovy Sydney MeetupMay 4th, 2010

Peter BellCEO/CTO SystemsForge

Wednesday, May 5, 2010

Page 2: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 3: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 4: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Warning!• Jetlagged

• Rewrote this afternoon

• First cut

• Relatively new to Groovy

• Brought to you by . . .

Wednesday, May 5, 2010

Page 5: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 6: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

What is a DSL?• Specific to single domain

• Executable

• Generally not Turing complete

• More expressive than GPL within the domain

Wednesday, May 5, 2010

Page 7: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

• 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

Page 8: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 9: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

What is a DSL? - When Use?

• Frequently changing business rules

• Interactions with stakeholders

• Relatively well understood domain

Wednesday, May 5, 2010

Page 10: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Creating a DSL• Top down:

• Sketch out UL with stakeholders

• Bottom up:

• Evolve from API

Wednesday, May 5, 2010

Page 11: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 12: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Key Concepts• Overview:

• Vertical vs horizontal

• Abstract vs Concrete

• Projections

• Three types of DSL

• Implementing external DSLs

• Internet vs. External

Wednesday, May 5, 2010

Page 13: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Vertical vs. Horizontal• Vertical: business domain

• Insurance, PC configurator . . .

• Horizontal: technical domain

• SQL, CSS, GORM . . .

Wednesday, May 5, 2010

Page 14: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 15: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Projections• Multiple projections

• Textual

• Spreadsheet

• Visual

Wednesday, May 5, 2010

Page 16: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Three Types of DSL• Internal

• External

• Language workbench

Wednesday, May 5, 2010

Page 17: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 18: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 19: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Implementing DSLs in Groovy• Flexible and malleable

syntax

• Metaprogramming

• AST transformations

© Guillaume Laforge

Wednesday, May 5, 2010

Page 20: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 21: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

Metaprogramming• PoGo

• Categories

• Builders

• Custom metaclass

• ExpandoMetaClass

© Guillaume Laforge

Wednesday, May 5, 2010

Page 22: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

AST Transformations

© Guillaume Laforge

• AST traversal

• Local transformations

• Global transformations

• Hooks into ANTLR

Wednesday, May 5, 2010

Page 23: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 24: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

DSL Evolution• The problem: success!

• Approaches:

• Backwards compatibility

• Versioning

• Model migrations

Wednesday, May 5, 2010

Page 25: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 26: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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

Page 27: Practical DSL Design - WordPress.comPractical DSL Design Groovy Sydney Meetup May 4th, 2010 Peter Bell CEO/CTO SystemsForge Wednesday, May 5, 2010. Overview ... Metaprogramming

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