SlideShare a Scribd company logo
www.tothenew.com
Building DSL using
Groovy
www.tothenew.com
What is DSL?
● A computer language specialized to a particular application domain
● Not intended to be broadly applicable across domains
● Syntax is focused on intended domain or problem
● Small and simple
www.tothenew.com
About me
speaker {
name 'Puneet Behl'
organization 'TO THE NEW'
designation 'Associate Lead - Technology'
email 'puneet.behl@tothenew.com'
twitter '@puneetbhl'
github 'http://github.com/puneetbehl'
}
www.tothenew.com
Characteristics of DSL
Highly context sensitive
www.tothenew.com
Characteristics of DSL
www.tothenew.com
Characteristics of DSL
List<String> employees = ["Puneet", "Himanshu"]
employees.with {
add "Manoj"
add "Roni"
}
www.tothenew.com
Examples
take 2.pills of chloroquinine after 6.hours
www.tothenew.com
Characteristics of DSL
3.times { println “Hello” }
www.tothenew.com
Examples - Spock
class AdderSpec extends spock.lang.Specification {
def "adder-test"() {
given: "a new Adder class is created"
def adder = new Adder();
expect: "Adding two numbers to return the sum"
adder.add(3, 4) == 7
}
}
www.tothenew.com
Examples - Gradle
apply plugin: 'eclipse'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.10'
}
www.tothenew.com
Things to note
● Compact code
● Closer to English than programming language code
www.tothenew.com
Let’s start building DSL
www.tothenew.com
please show the square_root of 100
Using Maps
www.tothenew.com
Conference.cfp {
title "Building DSL using Groovy"
description """
Domain Specific Languages (DSLs) have become a valuable part of
the Groovy idiom. DSLs are used in native Groovy builders, Grails and
GORM, and testing frameworks. To a developer, DSLs are consumable and
understandable, which makes implementation more fluid compared to
traditional programming. But how is a DSL implemented? How does it work
behind the scenes?
In this talk, we will talk about DSL and how can we write DSL
using Groovy with various examples and see the significance of DSL
"""
level INTERMEDIATE
owner "Puneet Behl"
duration "50.mins"
}
Closure and Delegates
www.tothenew.com
candidates "John", "Alex", "Hanna"
John 7
Alex 8
Hanna 3
reportWinner
Dynamic Methods
www.tothenew.com
import groovy.time.*
Date time
use(TimeCategory) {
time = 1.week.from.now
}
println time
Categories
www.tothenew.com
String name = "JohnDoe"
String encryptedString = "23434Super secret string23434"
use(StringUtils) {
println name.camelCase
println encryptedString.removeLeadingAndTrailingToken("23434")
}
Custom Categories
www.tothenew.com
String name = "JohnDoe"
String encryptedString = "23434Super secret string23434"
use(StringUtils) {
println name.camelCase
println encryptedString.removeLeadingAndTrailingToken("23434")
}
ExpandoMetaClass
www.tothenew.com
Integrating
Groovy Script Engine
Groovy Shell
www.tothenew.com
def binding = new Binding([
robot: new Robot()
])
Using Binding
www.tothenew.com
abstract class ConferenceBaseScript extends Script {
}
Base Script Class
www.tothenew.com
import org.codehaus.groovy.control.CompilerConfiguration
// create a configuration
def config = new CompilerConfiguration()
// tweak the configuration
config.addCompilationCustomizers(...)
// run your script
def shell = new GroovyShell(config)
shell.evaluate(script)
Compiler Customization
www.tothenew.com
CompilerConfiguration config = new CompilerConfiguration()
def icz = new ImportCustomizer()
config.addCompilationCustomizers(icz)
Import Customizer
www.tothenew.com
Questions???
www.tothenew.com
References
https://github.com/puneetbehl/helloDSL
http://docs.groovy-lang.org/docs/latest/html/documentation/core-domain-specifi
c-languages.html
www.tothenew.com
Thank you

More Related Content

PPTX
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
PDF
Http2.0 Guide 2013-08-14 #http2study
PDF
Docker and .NET Core - Best Friends Forever - Michael Newton - Codemotion Rom...
PDF
Python for IoT, A return of experience
PDF
SPDY / W3C
PDF
HTTP2 is Here!
PDF
Extensible web
PDF
Running At 99%: Mitigating App DoS
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
Http2.0 Guide 2013-08-14 #http2study
Docker and .NET Core - Best Friends Forever - Michael Newton - Codemotion Rom...
Python for IoT, A return of experience
SPDY / W3C
HTTP2 is Here!
Extensible web
Running At 99%: Mitigating App DoS

What's hot (20)

KEY
Compact Web - Remind "web compression" -
PDF
Understanding how concurrency work in os
KEY
WebGL Awesomeness
PDF
Chromium OS Introduction
PPT
Zombilizing The Web Browser Via Flash Player 9
PDF
Chromium: NaCl and Pepper API
PPTX
Basic html5 and javascript
PDF
really really really awesome php application with bdd behat and iterfaces
PDF
PDF
PHP client - Mongo db User Group Pune
PDF
Introduction to NPM and building CLI Tools with Node.js
PDF
Open web mail setup
PDF
From zero to almost rails in about a million slides...
PPTX
GWT videocall: power-up your mobile & web app with WebRTC
PDF
DomDisc and the Domino data api
PDF
Cache in Chromium: Disk Cache
PDF
Grooscript in Action SpringOne2gx 2015
PDF
Salt and pepper — native code in the browser Browser using Google native Client
PDF
Grooscript greach
Compact Web - Remind "web compression" -
Understanding how concurrency work in os
WebGL Awesomeness
Chromium OS Introduction
Zombilizing The Web Browser Via Flash Player 9
Chromium: NaCl and Pepper API
Basic html5 and javascript
really really really awesome php application with bdd behat and iterfaces
PHP client - Mongo db User Group Pune
Introduction to NPM and building CLI Tools with Node.js
Open web mail setup
From zero to almost rails in about a million slides...
GWT videocall: power-up your mobile & web app with WebRTC
DomDisc and the Domino data api
Cache in Chromium: Disk Cache
Grooscript in Action SpringOne2gx 2015
Salt and pepper — native code in the browser Browser using Google native Client
Grooscript greach
Ad

Similar to Building dsl using groovy (20)

PDF
Venkat Subramaniam Building DSLs In Groovy
PDF
(Greach 2015) Dsl'ing your Groovy
PDF
Writing a DSL for the Dense with Scala - JVMCon
PDF
Writing your own DSL
PDF
DSLs in Boo Domain Specific Languages in NET 1st Edition Ayende Rahien
PPTX
Bdd and dsl как способ построения коммуникации на проекте
PPTX
BDD or DSL как способ построения коммуникации на проекте - опыт комплексного ...
PDF
DSLs in Boo Domain Specific Languages in NET 1st Edition Ayende Rahien
PDF
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
PDF
Agile DSL Development in Ruby
PDF
Os Alrubaie Ruby
PDF
Sprache als Werkzeug: DSLs mit Kotlin (JAX 2020)
PDF
Domain Specific Languages
PPT
DSL explained _
PDF
groovy rules
PDF
groovy DSLs from beginner to expert
PDF
DSLs: what, why, how
PPTX
AestasIT - Internal DSLs in Scala
PDF
Viliam Ganz - Domain Specific Languages
PDF
Domain Specific Languages
Venkat Subramaniam Building DSLs In Groovy
(Greach 2015) Dsl'ing your Groovy
Writing a DSL for the Dense with Scala - JVMCon
Writing your own DSL
DSLs in Boo Domain Specific Languages in NET 1st Edition Ayende Rahien
Bdd and dsl как способ построения коммуникации на проекте
BDD or DSL как способ построения коммуникации на проекте - опыт комплексного ...
DSLs in Boo Domain Specific Languages in NET 1st Edition Ayende Rahien
DSL Construction with Ruby - ThoughtWorks Masterclass Series 2009
Agile DSL Development in Ruby
Os Alrubaie Ruby
Sprache als Werkzeug: DSLs mit Kotlin (JAX 2020)
Domain Specific Languages
DSL explained _
groovy rules
groovy DSLs from beginner to expert
DSLs: what, why, how
AestasIT - Internal DSLs in Scala
Viliam Ganz - Domain Specific Languages
Domain Specific Languages
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Spectroscopy.pptx food analysis technology
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
A comparative analysis of optical character recognition models for extracting...
MYSQL Presentation for SQL database connectivity
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25-Week II
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

Building dsl using groovy

Editor's Notes

  • #3: A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains. There is a wide variety of DSLs, ranging from widely used languages for common domains, such as HTML for web pages, down to languages used by only one or a few pieces of software, such as Emacs Lisp for GNU Emacs and XEmacs. DSLs can be further subdivided by the kind of language, and include domain-specific markup languages, domain-specific modelinglanguages (more generally, specification languages), and domain-specific programming languages. Special-purpose computer languages have always existed in the computer age, but the term "domain-specific language" has become more popular due to the rise of domain-specific modeling. Simpler DSLs, particularly ones used by a single application, are sometimes informally called mini-languages. https://softwareengineering.stackexchange.com/questions/80389/can-css-be-considered-a-dsl
  • #6: We all know the English meaning of the word fart, but did you know that fartmeans speed in Danish, Norwegian, and Swedish? If that doesn’t make you snicker enough, the words for speed bump in each language are fartbump, fartshump, and farthinder.
  • #10: The code is written in a behaviour style, which is closer to English than to programming language code The class has clear sections annotated by labels (given: and expect:) that clearly identify the purpose of the code statements they contain Each label has a comment in the same line that provides a brief description of the business logic There are no “assert” methods (like JUnit). The code is much more compact.
  • #12: The code is written in a behaviour style, which is closer to English than to programming language code The class has clear sections annotated by labels (given: and expect:) that clearly identify the purpose of the code statements they contain Each label has a comment in the same line that provides a brief description of the business logic There are no “assert” methods (like JUnit). The code is much more compact.
  • #14: show = { println it } square_root = { Math.sqrt(it) } def please(action) { [the: { what -> [of: { n -> action(what(n)) }] }] }
  • #15: @TypeChecked - type checking @DelegatesTo - goal of this annotation is to solve both the documentation issue, that will let your IDE know about the expected methods in the closure body, and it will also solve the type checking issue, by giving hints to the compiler about what are the potential receivers of method calls in the closure body.
  • #21: The Script class provides base support for integration with your application through the Binding object, as illustrated in this example:
  • #23: Whether you are using groovyc to compile classes or a GroovyShell, for example, to execute scripts, under the hood, a compiler configuration is used. This configuration holds information like the source encoding or the classpath but it can also be used to perform more operations like adding imports by default, applying AST transformations transparently or disabling global AST transformations.