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
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
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
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.
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
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
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...