Overview
Domain Specific Languages are high-level abstract programming languages used to increase productivity when programming for a very specific problem domain. Drools allows XML elements and attributes to be used to represent business rules, this may be combined with a Schema (XSD) and a good XML authoring tool to provide semi-RAD like environments. Drools provides SMF implementations for java, groovy and python - however these are not true DSLs. DSL Examples are provided in drools-examples; House Example, Conway's Game Of Life Example.
So instead of:
<java:condition> room.getName( ).equals( "lounge" ) <java:condition> <java:condition> convertToCelsius( room.getTemperature() ) > 20 <java:condition>
You can use:
<house:condition> <house:room name="lounge"> <house:temperature> <house:greater-than scale="C">20</house:greater-than> </house:temperature> </house:room> </house:condition>
A good XML authoring tool will, using an XSD, display available elements and attributes for each position within the document allowing them to be dragged and dropped. It is important that you have access to a good XML Schema aware editor: some commercial options are Oxygen, and Altova XMLSpy (both of which can run as plug ins in your IDE). Hand editing XML is never fun !
How to Create
- Create an XSD, which describes the layout and constraints of the DSL.
- Create Factories for Conditions and Consequences.
- Create a drools-<semantic>.conf file that registers the Condition and Consequence factories for a given namespace using the specified local names.
- Add drools-<semantic> to a drools.conf file.
RuleSetReader in drools-io takes care of parsing the DRL and using the namespace and local names for each element instantiates the appropriate factories passing a Configuration object, which is a DOM fragment representing the element, its attributes and all child elemnts.
