Coffee DSL in Groovy

I thought I’d follow up with my previous post with the Coffee Domain Specific Language in the Groovy Language.

This is really one of my first forays into Groovy, so it’s pretty rough. It’s really just a direct translation of the Ruby code and not what I would expect to be ‘idiomatic Groovy’. I’ll try and update this once I learn some more Groovy.


// CoffeeDSL.groovy
// This is the input from the user, likely read from a file
// or input through a user interface of some sort

CoffeeInput = "venti nonfat decaf whip latte"

class Coffee
{
  def size
  def whip
  def caf
  def type
  def milk

  public invokeMethod(name) 
  {
    if (['venti', 'grande'].contains(name))
      size = name
    else if (['whip', 'nowhip'].contains(name))
      whip = 'whip'.equals(name)
    else if (['caf', 'decaf', 'halfcaf'].contains(name))
      caf = name
    else if (['regular', 'latte', 'cappachino'].contains(name))
      type = name
    else if (['milk', 'nonfat'].contains(name))
      milk = name
    else
    throw new Exception("Unknown coffee informantion: ${name}.")
  }

  public order() {
    def params = ''
    if (milk)
      params += milk + ' '
    if (caf)
      params += caf + ' '
    if (whip)
      params +=  'whip '
    println("Ordering coffee: ${size} ${params}${type}\n")
  }

  public load(input) {
    // turn one line into multi-line "method calls"
    def cleaned = input.split(/\s+/)
    instance_eval(cleaned)
  }

  public instance_eval(methods) {
    for (method in methods) {
      this.invokeMethod(method)
    }
  }
}

// this is your code which loads the DSL input and executes it
coffee = new Coffee()
coffee.load(CoffeeInput)    // load the user input
coffee.order()                 // submit the order

This isn’t even metaprogramming. You could do this in any language, Java, C#, whatever. Everyone talks about metaprogramming in Groovy, but I have not yet found a lot of information on it. Does anyone have any pointers?

About Geoff Lane

I’m Geoff Lane and I write Zorched.net as I figure things out about software development in the hopes that it can help other people facing similar situations. Also as a thanks to the larger web community for all of the information and knowledge that they have shared. I’ve been a professional software developer since 1999 working with a variety of different technologies. I’ve worked for startups in the Silicon Valley and Chicago, IL and now work as a consultant building custom applications for clients.
This entry was posted in Code, Groovy and tagged , . Bookmark the permalink.

2 Responses to Coffee DSL in Groovy

  1. Pingback: The Disco Blog » Blog Archive » Builders are Groovy’s bag

  2. PaulF says:

    Well, actually this is metaprogramming. Or rather the ‘invokeMethod’ call is metaprogramming.

    Both Java and that other language both support reflection. Reflection is metaprogramming, allowing run time access to the program in order to alter execution.

    There are many flavours of metaprogramming, and this is certainly one of them.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang="" line="" escaped="" highlight=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>