The document discusses Domain Specific Languages (DSLs) and how Groovy is well-suited for creating DSLs. It provides examples of DSLs like XML, HTML, and SQL. It then explains how Groovy features like dynamic typing, closures, meta-programming capabilities via metaclasses, and chaining methods allow it to easily define and implement DSLs. Specific Groovy DSL examples discussed include builders for XML/HTML, the GORM for object-relational mapping, and a Korean language DSL.
2. DSL
•DSLR? DSL?
A domain-specific language (DSL) is a computer language specialized to a
particular application domain.
http://en.wikipedia.org/wiki/Domain-specific_language
http://www.aladin.co.kr/shop/wproduct.aspx?ISBN=8991268994
•DSL <-> GPL(General Purpose Language)
•DSL = Mini Languages
•DSL Example => XML, HTML, CSS, SQL
•Groovy로 해석되는 DSL
– 각종 Builder, GORM, gradle, Grails
4. Groovy Builder DSL – XML
writer = new StringWriter()
builder = new groovy.xml.MarkupBuilder(writer)
builder.numbers {
description 'Squares and factors of 10..15'
for (i in 10..15) {
number (value: i, square: i*i) {
//#1
for ( j in 2..<i) {
if (i % j == 0) {
factor (value: j) //#2
}
}
}
}
}
println writer
http://groovyconsole.appspot.com/
5. Groovy Builder DSL - HTML
def writer = new StringWriter()
def html = new groovy.xml.MarkupBuilder(writer)
html.html {
head { title 'Constructed by MarkupBuilder' }
body {
h1 'What can I do with MarkupBuilder?'
form (action:'whatever') {
for (line in [
'Produce HTML',
'Produce XML',
'Have some fun'
]){
input(type:'checkbox',checked:'checked', id:line, '')
label(for:line, line)
br('')
}
}
}
}
println writer
6. GORM – DSL
• ORM DSL
– Mapping
• Table, Column mapping
• Relational mapping
– ORM관련 설정
•
•
•
•
Cache 전략
Custom ID 전략
Composite Primary Keys
Index 설정
– http://grails.org/doc/latest/guide/single.html#ormdsl
• Query DSL
–
Basic Query
• list, get, getAll
– Dynamic Finders
• findByXXX
–
–
Book.findByTitle(„정글만리‟), findByTitleLike(„%정글‟)
http://grails.org/doc/latest/ref/Domain%20Classes/findBy.html
7. Groovy로 만든 한글 DSL
Object.metaClass.을 =
Object.metaClass.를 =
Object.metaClass.의 =
{ clos -> clos(delegate) }
먼저 = { it }
표시하라 = { println it }
제곱근 = { Math.sqrt(it) }
먼저 100 의 제곱근 을 표시하라
먼저(100).의(제곱근).을(표시하라)
출처: http://it.gilbird.com/textyle/8318
8. Groovy는 왜 DSL에 강한가?
• Dynamic Language
– Dynamic Type
num = 10; str = “20”
assert num instanceof Integer
assert str instanceof String
– Closure
• Caller에게 양보하기
– MOP(Meta Class, Expando Class)
• 요술상자, 아메바
– Groovy‟s chain method calls (GEP 3)
• 괄호 빼기
먼저 100 의 제곱근 을 표시하라
먼저(100).의(제곱근).을(표시하라)
•
JVM 기반
– Groovy는 결국 Java Class로 컴파일
– 거의 95%이상 Java와 동일하게 코딩 가능
– Java와 Groovy의 혼용
15. 한국어 DSL로 돌아와서
Object.metaClass.을 =
Object.metaClass.를 =
Object.metaClass.의 =
{ clos -> clos(delegate) }
먼저 = { it }
표시하라 = { println it }
제곱근 = { Math.sqrt(it) }
먼저 100 의 제곱근 을 표시하라
먼저(100).의(제곱근).을(표시하라)
100.제곱근 을 표시하라
출처: http://it.gilbird.com/textyle/8318
16. 관련 자료
• http://notes.3kbo.com/groovy-dsls
• http://en.wikipedia.org/wiki/Domainspecific_language
• DSL 번역본
• 프로그래밍 그루비
• 한국어 DSL
• Groovy for Domain-Specific Languages
• http://en.wikipedia.org/wiki/Metaobject
• http://groovy.codehaus.org/api/overviewsummary.html
• Groovy Grails 의 좋은 점
• ★ Groovy