Over a million developers have joined DZone.

Create Your Own DSL

DZone's Guide to

Create Your Own DSL

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

In software development I encounter problems with creating user interfaces for manage logical chains. More complex chain of logic cause more difficult to develop, test, and debug resulting interface. That interfaces are hard to understand without help of man, who participated in the design.

Usually, it looks like that: 

Instead of complex interface is to try to use DSL. This require a little more skill from the end user but in return it will eliminate the overloaded ui. It provide a more flexible control over logic at lower labor costs. A much easier debugging, testing, and auto-testing.

Consider the example of pethouse. We have the following classes:

class Food {
    Integer size
class Pet {
    String name
    Long age
    Closure hello
class PetHouse {
    String name
    Pet master
    @DSLTypeHint(Pet) List<Pet> pet
    @DSLTypeHint(Food) Map<String, Food> food

To create pethouse we need one screen to PetHouse and two dialogue to Pet and Food. The difficulties begin with the behavior of hello method for each pet. How many screens needed if we want to pet shouted "Hello!" only when there is enough food for it? And if we want to check whether a particular type of food?

All this logic is easy to describe in the DSL (groovy):

pet_house {
    name "My pet house"
    food {
        cat_food {size 12}
    pet {
        name "Cat"
        hello {
            if (food["cat_food"].size > 0) {
                return "Hello"
            } else {
                return "I need food!!!"

Plain and simple.

There is DSLParser with specification to help write your own DSL. Also, it requires Reflections which can be replaced simply.

DSLParser (github) (include spec)

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook


Published at DZone with permission of Alexey Kutuzov. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}