Use DSLs to be more concise

2010-07-08 by mira

Models can be expressed more concise with domain specific languages (DSL). This is an example using Xtext.

Creating a textual DSL with Xtext was surprisingly easy.

As an exercise, I created a DSL to describe business processes. My goal was to generate a jPDL file for jBPM as end result. I didn’t aim at complete coverage of jPDL and rather just modeled a small fragment of it.

Briefly, I wanted to be able to describe that process from the jBPM User Guide.

When creating a DSL, one starts with writing down how the language should look like in the end (concurrency.bpm):

process ConcurrencyGraphBased {

  start
    -> initial_fork

  fork initial_fork
    -> send_invoice
    -> load_truck
    -> print_shipping_documents

  state send_invoice
    -> final_join

  state load_truck
    -> shipping_join

  state print_shipping_documents
    -> shipping_join

  join shipping_join
    -> drive_truck_to_destination

  state drive_truck_to_destination
    -> ok        ? final_join
    -> breakdown ? end_failed

  join final_join
    -> end_ok

  end end_ok

  end end_failed 

}

In contrast to the example from the user guide, I also declared state choice:

  state drive_truck_to_destination
    -> ok        ? final_join
    -> breakdown ? end_failed

The Xtext grammar looks like this:


grammar ch.miranet.bpm.dsl.BpmDsl with org.eclipse.xtext.common.Terminals

generate bpmDsl "http://www.miranet.ch/bpm/dsl/BpmDsl"

Process:
	'process' name=ID '{'
	start=Start
	(states+=State)*
	'}';

Start:
	'start' '->' target=[State];

End:
	'end' name=ID;

State:
	SimpleState | Fork | Join | End;

Fork:
	'fork' name=ID ('->' targets+=[State])+;

Join:
	'join' name=ID '->' target=[State];

SimpleState:
	'state' name=ID
	('->' target=[State] | conditionalTargets+=ConditionalTransition+);

ConditionalTransition:
	'->' choice=Choice '?' target=[State];

Choice:
	name=ID;

To make it complete, here the links to the Xpand template and the resulting jPDL file.

On top of better readability, one also gets a nice Eclipse based text editor, including code completion and outline. In contrast to the resulting jPDL file, the concurrency.bpm input file has typesafe references.

Open Declaration and Find References work the way I’m used to from writing Java code - very nice, indeed!

Archive

architecture