Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Implementing a Rules DSL


[ 308 ]

Changing the onConsume reward script fixes this anomaly. Running our tests again
will show them all passing as expected. So now we have a rewards DSL script with
a service class that implements it. We've hooked the rewards service into the domain
service so that it is triggered on the important events within the backend services.
We've tested out the rewards by using some test cases. We should now have a good
degree of confidence that, if BroadbandPlus was a real application service, our
reward programs would be getting called at the appropriate times.


Summary


We covered a lot of ground in this chapter. We took a look at Groovy bindings to
see how they can be used in our DSL scripts. By placing closures strategically in
the binding, we can emulate named blocks of code. We can also provide built-in
methods and other shorthand by including closures and named Boolean values in
the binding. These techniques can be used to great effect to write DSL scripts that can
be read and understood by stakeholders outside of the programming audience.


We've worked through a full implementation of a DSL for customer rewards by
using these techniques, and we've seen how such a DSL can be integrated into an
existing application. The reader should now have the confidence to start generating
their own domain-specific DSLs that implement features in a similar way and
integrate them into their own applications.


In the next and final chapter we will cover another fully worked DSL. The DSL
we will implement in Chapter 12, Integrating It All, will make full use of AST
transformations and will build on the techniques we covered in Chapter 8, AST
Transformations.


http://www.ebook3000.com
Free download pdf