In this project we show how to write complex DSL using groovy powerful
features (closures, command chains, @DelegatesTo
).
Domain-Specific Languages are small languages, focused on a particular aspect of a software system. They allow business experts to read or write code without having to be programming experts.
We provide complex and fully tested DSL for programming finite state machine:
-
Grammar
: immutable class representing DSL sentence:on
transitionEvent
move fromfromState
totoState
which is represented in code as:
new Grammar().on(transitionEvent).from(fromState).to(toState)
or using closures:
Grammar.make { on transitionEvent, { from fromState, { to toState } } }
Remark: note that the word order in sentence is optional, so:
Grammar.make { on transitionEvent, { from fromState, { to toState } } }
is basically equivalent to
Grammar.make { from _fromState, { to _toState, { on _event } } }
and so on.
-
Fsm
- immutable class representing finite state machine (transitions container)- we create
Fsm
objects usingFsmBuilder
- we could load Fsm (
transitions
map) using closure:will load:def fsm2 = Fsm.load { apply { on _event1, { from _state1, { to _state2 } } } apply { on _event2, { from _state2, { to _state3 } } } initialState _state0 }
_state0
as initial state and[_event1:[_state1, _state2], _event2:[_state2, _state3]]
as transitions map - order of functions in closure is optional
- to move from current state to the other state we call
Remark: if we cannot move from current state to the requested state on the given event we will stay in the current state - no exception is thrown.
fsm.fire(event)
- we create
Coverage: 86%