SlideShare a Scribd company logo
1 of 32
Download to read offline
DSL’s With Groovy
Paul Bowler
Senior Consultant, OpenCredo
“Domain-Specific”
Languages?
Domain?
–noun
a field of action, thought,
influence, etc.
Domains, domains, domains...!
Domain Language?
• Narrow, specialised focus
• Contains syntax and grammar to interact
with the domain
• Few, if any, control structures (not ‘Turing
Complete’)
• Communicates instructions and
configuration
So, like these?
...or these?
yes, ... and no!
What then?
“...most DSL’s are merely a thin facade over a library or
framework.”
Martin Fowler,ThoughtWorks
OS
Platform
Framework
DSL
What’s wrong with code?
Developer
Domain
Expert
Cognitive Gap
How do DSL’s help?
• Expressive - Well-designed abstractions
• Simple - Reduce cognitive overhead
• Focussed - Deliver clarity of intent
• Inclusive - Empower the domain expert
What’s wrong with code?
Developer
Domain
Expert
Cognitive Gap
Close the Cognitive Gap
Developer
Domain
Expert
How?
• External DSL - Separate from the ‘main’ language
e.g. SQL, XML, JSON
• Internal DSL -Valid code in the ‘main’ language
• Language Workbench - Specialised ‘IDE’ as editing
environment with ‘human-centric’ graphical
representation
Why Groovy?
Less noise
More sugar
How?
• Builders
• Categories
• Meta-Object Protocol (MOP)
• Abstract Syntax Tree (AST) Transformations
Builders
• Great for hierarchical structures, such as
trees, collections of collections, etc.
• Built in support for XML, HTML, Swing, etc
• Roll your own by extending
‘BuilderSupport’ class
Using builders
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.team(nickname:'DreamTeam') {
member(name:'John Smith', age:'32') {
nationality('British')
job(type:'full-time', 'Java Developer')
}
member(name:'Sue Perb', age:'25') {
nationality('Australian')
job(type:'full-time', 'Tester')
}
member(name:'Fred Bloggs', age:'30') {
nationality('Irish')
job(type:'contract', 'Build Guru')
}
}
Builder Methods
class MyBuilder extends BuilderSupport {
	

 public void setParent(Object parent, Object child) {
	

 }
	

 public createNode(Object name) {
	

 }
	

	

 public createNode(Object name, Object value) {
	

 }
	

	

 public createNode(Object name, Map attributes) {
	

 }
	

	

 public createNode(Object name, Map attributes, Object value) {
	

 }
}
Categories
• Allow syntax extensions, e.g:
def nextWeek = today + 7.days
• Any public static method is a candidate to be a category
method
• The type of the first parameter is the target type that may
have the new methods ‘injected’
• No need to extend a particular class or implement an
interface
• Gotcha! Methods added to classes for limited time using
ThreadLocal.Also requires wrapping with use() method.
Category Example
import org.apache.commons.lang.StringUtils
class StringCategory {
static capitalize( String self ){
StringUtils.capitalize(self)
}
static normalize( String self ){
self.split("_").collect { word ->
word.toLowerCase().capitalize()
}.join("")
}
}
use( StringCategory ) {
assert "Groovy" == "groovy".capitalize()
assert "CamelCase" == "CAMEL_CASE".normalize()
} 
MOP
• Every object inherits from
groovy.lang.GroovyObject which
contains MetaClass
• ExpandoMetaClass extensions and hooks:
invokeMethod(),
getProperty(), methodMissing()
and propertyMissing()
• Used extensively in Grails - Dynamic Finders (e.g.
book.findAllByAuthor)
• Less code and cleaner implementation than categories
• Uses closures and delegates
• Can add or update/overload existing methods
• No need for Use()
• Can overload static methods
• Has longer lifespan than categories
• Inheritance not enabled by default for performance
reasons
MOP vs Categories
MOP Example
import org.apache.commons.lang.StringUtils
String.metaClass.capitalize = {
StringUtils.capitalize(delegate)
}
String.metaClass.normalize = {
delegate.split("_").collect { word ->
word.toLowerCase().capitalize()
}.join("")
}
assert "Groovy" == "groovy".capitalize()
assert "CamelCase" == "CAMEL_CASE".normalize()
Abstract Syntax Tree
• Compile-time Meta Programming allowing
developers to hook into the compilation process
• No runtime performance penalty
• Global transformations using service locator file
• Local transformations using annotations
• Built using... builders!
• Transformations must be performed in one of the
nine defined compiler phases
Compilation Phases
1. Initialization: source files are opened and environment
configured
2. Parsing: the grammar is used to to produce tree of tokens
representing the source code
3. Conversion:An abstract syntax tree (AST) is created from
token trees.
4. Semantic Analysis: Performs consistency and validity checks that
the grammar can't check for, and resolves classes.
5. Canonicalization: Complete building the AST
6. Instruction Selection: instruction set is chosen, for example java5
or pre java5
7. Class Generation: creates the binary output in memory
8. Output: write the binary output to the file system
9. Finalisation: Perform any last cleanup
Popular Transformations
• Bindable andVetoable transformation
• Category and Mixin transformations
• Delegate transformation
• Immutable transformation
• Lazy transformation
• Newify transformation
• PackageScope transformation
• Singleton transformation
It all looks so complicated!
Case Study - Grint
‘Groovier Integration!’
Grint
• Groovy DSL for Integration Patterns
• Deployable as a Grails plugin
• Baked for our DSL course
• Already in production...
A Groovier Approach to Integration
• Events cause messages
• EDA 
• See Event-driven IO to get an idea
• Node.js
• Akka
• Even Spring Integration...
Thank you

More Related Content

What's hot

Practical Domain-Specific Languages in Groovy
Practical Domain-Specific Languages in GroovyPractical Domain-Specific Languages in Groovy
Practical Domain-Specific Languages in GroovyGuillaume Laforge
 
Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)Vitaly Baum
 
Groovy overview, DSLs and ecosystem - Mars JUG - 2010
Groovy overview, DSLs and ecosystem - Mars JUG - 2010Groovy overview, DSLs and ecosystem - Mars JUG - 2010
Groovy overview, DSLs and ecosystem - Mars JUG - 2010Guillaume Laforge
 
Groovy and Grails intro
Groovy and Grails introGroovy and Grails intro
Groovy and Grails introMiguel Pastor
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
javascript teach
javascript teachjavascript teach
javascript teachguest3732fa
 
Greach 2014 - Metaprogramming with groovy
Greach 2014 - Metaprogramming with groovyGreach 2014 - Metaprogramming with groovy
Greach 2014 - Metaprogramming with groovyIván López Martín
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails IntroductionEric Weimer
 
Embedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsEmbedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsLukas Renggli
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemKostas Saidis
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Johnny Sung
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailszenMonkey
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?daoswald
 
Javascript The Good Parts v2
Javascript The Good Parts v2Javascript The Good Parts v2
Javascript The Good Parts v2Federico Galassi
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Heiko Behrens
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)Ary Borenszweig
 

What's hot (19)

Practical Domain-Specific Languages in Groovy
Practical Domain-Specific Languages in GroovyPractical Domain-Specific Languages in Groovy
Practical Domain-Specific Languages in Groovy
 
Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)Building DSLs On CLR and DLR (Microsoft.NET)
Building DSLs On CLR and DLR (Microsoft.NET)
 
Groovy overview, DSLs and ecosystem - Mars JUG - 2010
Groovy overview, DSLs and ecosystem - Mars JUG - 2010Groovy overview, DSLs and ecosystem - Mars JUG - 2010
Groovy overview, DSLs and ecosystem - Mars JUG - 2010
 
Groovy and Grails intro
Groovy and Grails introGroovy and Grails intro
Groovy and Grails intro
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
javascript teach
javascript teachjavascript teach
javascript teach
 
Greach 2014 - Metaprogramming with groovy
Greach 2014 - Metaprogramming with groovyGreach 2014 - Metaprogramming with groovy
Greach 2014 - Metaprogramming with groovy
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
Embedding Languages Without Breaking Tools
Embedding Languages Without Breaking ToolsEmbedding Languages Without Breaking Tools
Embedding Languages Without Breaking Tools
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystem
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
 
Javascript The Good Parts v2
Javascript The Good Parts v2Javascript The Good Parts v2
Javascript The Good Parts v2
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Cfphp Zce 01 Basics
Cfphp Zce 01 BasicsCfphp Zce 01 Basics
Cfphp Zce 01 Basics
 
Crystal internals (part 1)
Crystal internals (part 1)Crystal internals (part 1)
Crystal internals (part 1)
 

Similar to DSL's with Groovy

Mcknight well built extensions
Mcknight well built extensionsMcknight well built extensions
Mcknight well built extensionsRichard McKnight
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell ModulesJune Blender
 
Java jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mJava jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mSteve Elliott
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmodwalkmod
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptBill Buchan
 
Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOLMicro Focus
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionDave Diehl
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumNgoc Dao
 
Refactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_WorkshopRefactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_WorkshopMax Kleiner
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James NelsonGWTcon
 
Java for XPages Development
Java for XPages DevelopmentJava for XPages Development
Java for XPages DevelopmentTeamstudio
 
Building DSLs with Scala
Building DSLs with ScalaBuilding DSLs with Scala
Building DSLs with ScalaMohit Jaggi
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3Sisir Ghosh
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!Alessandro Giorgetti
 
CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)Dilawar Khan
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introductionjyoti_lakhani
 

Similar to DSL's with Groovy (20)

Mcknight well built extensions
Mcknight well built extensionsMcknight well built extensions
Mcknight well built extensions
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell Modules
 
Java jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3mJava jdk-update-nov10-sde-v3m
Java jdk-update-nov10-sde-v3m
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod
 
The View object orientated programming in Lotuscript
The View object orientated programming in LotuscriptThe View object orientated programming in Lotuscript
The View object orientated programming in Lotuscript
 
Object Oriented Programming with COBOL
Object Oriented Programming with COBOLObject Oriented Programming with COBOL
Object Oriented Programming with COBOL
 
Holy PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood editionHoly PowerShell, BATman! - dogfood edition
Holy PowerShell, BATman! - dogfood edition
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Refactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_WorkshopRefactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_Workshop
 
Scala-Ls1
Scala-Ls1Scala-Ls1
Scala-Ls1
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
 
Java for XPages Development
Java for XPages DevelopmentJava for XPages Development
Java for XPages Development
 
Building DSLs with Scala
Building DSLs with ScalaBuilding DSLs with Scala
Building DSLs with Scala
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)CS4443 - Modern Programming Language - I Lecture (1)
CS4443 - Modern Programming Language - I Lecture (1)
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 

Recently uploaded

Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 

Recently uploaded (20)

Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 

DSL's with Groovy

  • 1. DSL’s With Groovy Paul Bowler Senior Consultant, OpenCredo
  • 3. Domain? –noun a field of action, thought, influence, etc.
  • 5. Domain Language? • Narrow, specialised focus • Contains syntax and grammar to interact with the domain • Few, if any, control structures (not ‘Turing Complete’) • Communicates instructions and configuration
  • 9. What then? “...most DSL’s are merely a thin facade over a library or framework.” Martin Fowler,ThoughtWorks OS Platform Framework DSL
  • 10. What’s wrong with code? Developer Domain Expert Cognitive Gap
  • 11. How do DSL’s help? • Expressive - Well-designed abstractions • Simple - Reduce cognitive overhead • Focussed - Deliver clarity of intent • Inclusive - Empower the domain expert
  • 12. What’s wrong with code? Developer Domain Expert Cognitive Gap
  • 13. Close the Cognitive Gap Developer Domain Expert
  • 14. How? • External DSL - Separate from the ‘main’ language e.g. SQL, XML, JSON • Internal DSL -Valid code in the ‘main’ language • Language Workbench - Specialised ‘IDE’ as editing environment with ‘human-centric’ graphical representation
  • 16. How? • Builders • Categories • Meta-Object Protocol (MOP) • Abstract Syntax Tree (AST) Transformations
  • 17. Builders • Great for hierarchical structures, such as trees, collections of collections, etc. • Built in support for XML, HTML, Swing, etc • Roll your own by extending ‘BuilderSupport’ class
  • 18. Using builders def writer = new StringWriter() def xml = new MarkupBuilder(writer) xml.team(nickname:'DreamTeam') { member(name:'John Smith', age:'32') { nationality('British') job(type:'full-time', 'Java Developer') } member(name:'Sue Perb', age:'25') { nationality('Australian') job(type:'full-time', 'Tester') } member(name:'Fred Bloggs', age:'30') { nationality('Irish') job(type:'contract', 'Build Guru') } }
  • 19. Builder Methods class MyBuilder extends BuilderSupport { public void setParent(Object parent, Object child) { } public createNode(Object name) { } public createNode(Object name, Object value) { } public createNode(Object name, Map attributes) { } public createNode(Object name, Map attributes, Object value) { } }
  • 20. Categories • Allow syntax extensions, e.g: def nextWeek = today + 7.days • Any public static method is a candidate to be a category method • The type of the first parameter is the target type that may have the new methods ‘injected’ • No need to extend a particular class or implement an interface • Gotcha! Methods added to classes for limited time using ThreadLocal.Also requires wrapping with use() method.
  • 21. Category Example import org.apache.commons.lang.StringUtils class StringCategory { static capitalize( String self ){ StringUtils.capitalize(self) } static normalize( String self ){ self.split("_").collect { word -> word.toLowerCase().capitalize() }.join("") } } use( StringCategory ) { assert "Groovy" == "groovy".capitalize() assert "CamelCase" == "CAMEL_CASE".normalize() } 
  • 22. MOP • Every object inherits from groovy.lang.GroovyObject which contains MetaClass • ExpandoMetaClass extensions and hooks: invokeMethod(), getProperty(), methodMissing() and propertyMissing() • Used extensively in Grails - Dynamic Finders (e.g. book.findAllByAuthor)
  • 23. • Less code and cleaner implementation than categories • Uses closures and delegates • Can add or update/overload existing methods • No need for Use() • Can overload static methods • Has longer lifespan than categories • Inheritance not enabled by default for performance reasons MOP vs Categories
  • 24. MOP Example import org.apache.commons.lang.StringUtils String.metaClass.capitalize = { StringUtils.capitalize(delegate) } String.metaClass.normalize = { delegate.split("_").collect { word -> word.toLowerCase().capitalize() }.join("") } assert "Groovy" == "groovy".capitalize() assert "CamelCase" == "CAMEL_CASE".normalize()
  • 25. Abstract Syntax Tree • Compile-time Meta Programming allowing developers to hook into the compilation process • No runtime performance penalty • Global transformations using service locator file • Local transformations using annotations • Built using... builders! • Transformations must be performed in one of the nine defined compiler phases
  • 26. Compilation Phases 1. Initialization: source files are opened and environment configured 2. Parsing: the grammar is used to to produce tree of tokens representing the source code 3. Conversion:An abstract syntax tree (AST) is created from token trees. 4. Semantic Analysis: Performs consistency and validity checks that the grammar can't check for, and resolves classes. 5. Canonicalization: Complete building the AST 6. Instruction Selection: instruction set is chosen, for example java5 or pre java5 7. Class Generation: creates the binary output in memory 8. Output: write the binary output to the file system 9. Finalisation: Perform any last cleanup
  • 27. Popular Transformations • Bindable andVetoable transformation • Category and Mixin transformations • Delegate transformation • Immutable transformation • Lazy transformation • Newify transformation • PackageScope transformation • Singleton transformation
  • 28. It all looks so complicated!
  • 29. Case Study - Grint ‘Groovier Integration!’
  • 30. Grint • Groovy DSL for Integration Patterns • Deployable as a Grails plugin • Baked for our DSL course • Already in production...
  • 31. A Groovier Approach to Integration • Events cause messages • EDA  • See Event-driven IO to get an idea • Node.js • Akka • Even Spring Integration...