Domain Specific Language With Groovy


18 August 2018

Your Own Language in Your Own World - Essense of Groovy



Contents

  1. Introduction
  2. Simple Implementation in Java
  3. Converting as DSL
  4. Build as DSL
  5. Sample DSL Codes
  6. Advantages

DSL - An Introduction


Normally, we are looking for a language which reflects our working domain. But the support and enhancement of that domain language is difficult to get. Programming languages like Java, has a big support and almost all libraries. So wrapping up a normal programming language for a specif domain with our own derived keywords will be considered as a DSL.

Examples

Gradle is a good example for DSL which is used for build jobs.

Jenkins pipeline also a build automation DSL used as a plugin.

Ready Up ..!

If you are new to Groovy, First go through basics such as,

Simple Implementation in Java


Let us start with a task for collecting animals list in a zoo. How would be the code in Java for this task.

Java

import java.util.ArrayList;
class Animal
{
    String name;
    Integer age;
    public Animal(String name, Integer age)
    {
        this.name = name;       
        this.age = age;       
    }
}
public class Animals{
    public static ArrayList<Animal> register = new ArrayList<Animal>();
    public static void addAnimal(String name, Integer age)
    {
        Animal newAni = new Animal(name, age);
        register.add(newAni);
    }
    public static void showRegister()
    {
        System.out.println ("Animals Details : ");
        System.out.println ("--------------------------");
        for (Animal s : register)
        {
            System.out.println ("Animal Name    : "+s.name);
            System.out.println ("Animal Age     : "+s.age);
            System.out.println ("--------------------------");
        }
    }
    public static void main(String[] args)
    {
        addAnimal ("Richard Parker", 17);
        addAnimal ("Wolverine", 50);
        showRegister();
    }
}


Converting as DSL


What if we have a ‘Main ()’ method like shown below DSL code,

Groovy

    def groupA = animals {
        addAnimal ("Richard Parker", 17)
        addAnimal ("Wolverine", 50)
    }
    groupA.showRegister()

This DSL code seems more intuition and understandable by a non developer too. This may seems as spec/config file like xml or yaml which we normally have. It can replace those spec files with groovy features which is highly recommended for future changes with reduced conflicts.

This can be obtained from below shown Groovy code,

Groovy

    Animals animals(Closure cl)
    {
        Animals als = new Animals()
        cl.delegate = als 
        cl()
        als
    }
    class Animals
    {
        ArrayList<Animal> register = new ArrayList<Animal>()
        void addAnimal (String name, Integer age)
        {
            Animal newAni = new Animal(name, age)
            register << newAni 
        }
        void showRegister()
        {
            println "Animals Details :"
            println "--------------------------"                
            register.each { s ->
                println "Animal Name    : $s.name"
                println "Animal Age     : $s.age"
                println "--------------------------"
            }
        }
    }
    class Animal
    {
        String name
        Integer age   
        Animal(String name, Integer age)
        {
            this.name = name 
            this.age = age 
        }
    }

Above shown implementation is really have more advantages than a normal Java code.

Build as DSL


Starting with the basic needs of our day to day work will really helps to progress on developing. That will be the base of your Dream DSL. So normally we deals with various file formats like xml, json, yaml, csv, xlsx and etc., Groovy has all supports to parse these various types of files such as like,

  • XmlSlurper / XmlParser.
  • JsonSlurper.
  • POI - Parser for xls/xlsx.

Other facilities like,

  • Javax.mail for EMail.
  • JDBC for Dabatabases.

We can create a language layer with our own keywords over default methods from the libraries. So I have created simple template DSL build for these general activities and hope that it will let you understand the DSL Development in high level.

Sample DSL Codes


These are my sample codes of my own DSL Language from shown above repo. Such as for,

  • Email
  • Excel
  • Json
  • Files
EMail Support
mailserver{
    sethost 'smtp.gmail.com'
    login 'bhanuchander210@gmail.com','*******'
    sendmsg{
        to 'example@gmail.com'
        cc 'any@gmail.com'
        bcc 'any@gmail.com'
        subject 'DSL-EMAIL UTIL'
        body'''
                    ....content ..
            '''
        attach 'dsl.groovy'
    }
}
Excel Support
excelstudy{
    excelfile 'filename.xls'
    sheetlist() // returns sheetName list
    getheader('sheetName') // returns header list
    getdata('sheetName') // return 2D data with header
    getdata('sheetName',header=true) // return 2D data with header selection
    getdata('sheetName',hader=true,[0,1,2]) // column filter
    getdata('sheetName',hader=true,[0,1]) // column filter
    getdata('sheetName',["col_1","col_2"]) // column selection
    getdata ('sheetName', ["col_1",1,2]) // support for both string and integer
    getdatamap('sheetName') // return map
}
Json Support
jsonstudy{
    jsonfile 'fileName'
    row 'Xpath.to.Node'
    column 'Xpath.to.col1','Xpath.to.col2'
    result{
        printraw()
    }
}
File Support
filestudy {
    inputFile fileName
    filterLine 'Richard Parker'
    result{
        println line
        println getBetweenString ('Name = ')
        println getBetween ('Name = "',1,'"',2)
    }
}

We know that, it can be created with our own debug, log trace methods and the distribution can be attached with any Java Package.

Advantages

  • We can always stay closer to the Domain even while coding than our traditional way.

  • Groovy is dynamic, that means we can compile it in runtime. This is a big plus that makes us to keep groovy files as a configuration file. In shown above code, we can use animals{...} method is a configuration file for keeping the animal information.

  • Even a non-developer can understand the code and can develop by their own with a simple documentation.

  • The Distribution of this DSL builds can be integrated with any java packages which is a Java Advantage, makes this DSL a independent build, very much reliable and worth for developing.

Most of the Java developers feels these kind of implementation really helps to easily understand the complicated code.

If you find any typos, inaccurate stuffs and doubtful contents in my post, feel free to comment it out.

Relevant and useful comments are always welcome. Lets make this tech community wonderful ...!

Share and Thanks

Related Posts