Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create Your Own DSL

DZone's Guide to

Create Your Own DSL

· DevOps Zone ·
Free Resource

Read why times series is the fastest growing database category.

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
}
@DSLRoot("pet_house")
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)

Learn how to get 20x more performance than Elastic by moving to a Time Series database.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}