JBoss DSL
From Kb
Expanders and DSL allow you to write your rules in a natural language, then translate to JBoss rules language (think find and replace).
Samples below are from JBoss Rules Trouble Ticket Sample
Contents |
Related Links
Rules
To be able to use the following natural language rules
package org.drools.examples
expander ticketing.dsl
rule "New Ticket"
salience 10
when
There is a customer ticket with status of "New"
then
Log "New"
end
rule "Silver Priorty"
#if after a certain time, we still have an issue, then escalate
duration 3000
when
There is a "Silver" customer with a ticket status of "New"
then
Escalate the ticket
end
rule "Gold Priority"
duration 1000
when
There is a "Gold" customer with a ticket status of "New"
then
Escalate the ticket
end
rule "Platinum Priority"
#we don't want to make the high rollers wait !
when
There is a "Platinum" customer with a ticket status of "New"
then
Escalate the ticket
end
rule "Escalate"
when
There is a customer ticket with status of "Escalate"
then
Send escalation email
end
rule "Done"
when
There is a customer ticket with status of "Done"
then
Log "Done"
end
# This is the same as the non DSL version. This could be kept in another file of course.
function void sendEscalationEmail( Customer customer, Ticket ticket ) {
System.out.println( "Email : " + ticket );
}
#hey, the imports can appear anywhere !
import org.drools.examples.TroubleTicketExampleWithDSL.Customer;
import org.drools.examples.TroubleTicketExampleWithDSL.Ticket;
Domain Specific Language (DSL)
You need a DSL to map this to more technical language.
#place your comments here - this is just a description for your own purposes.
[when]There is a customer ticket with status of "{status}"=customer : Customer( ) ticket : Ticket( customer == customer, status == "{status}" )
[when]There is a "{subscription}" customer with a ticket status of "{status}"=customer : Customer(subscription == "{subscription}") ticket : Ticket( customer == customer, status == "{status}")
[then]Log "{message}"=System.out.println("{message} " + ticket);
[then]Escalate the ticket=ticket.setStatus("Escalate"); modify(ticket);
[then]Send escalation email=sendEscalationEmail( customer, ticket );
Java Code to run this
The addPackageFromDrl( getSource(), getDSL()) loads not only our rules , but the mapping to carry out the translation.
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( getSource(),
getDSL() );
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage() );
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( workingMemory );
logger.setFileName( "log/state" );
... assert objects into working memory ...
workingMemory.fireAllRules();
Java Code to load rules from classpath
Allows our rules to be deployed to a Tomcat instance
/**
* Searches for a file via the local filesystem , then via the Classpath
*
* @param fileName to search for
* @return InputStream containing the opened file
*/
private InputStream getFileIfExists(String fileName) throws SearchException {
// Local Variables
InputStream rIStream = null;
log.debug("Searching for File name:" + fileName);
// The next line creates one execution of the process definition.
// After construction the process execution has one main path
// of execution (the root token) that is positioned in the
// startstate.
try {
rIStream = new FileInputStream(fileName);
} catch (FileNotFoundException fnfe1) {
log
.debug("Could not find resource as file - attempting the classpath");
try {
ClassPathResource myClassPathReader = new ClassPathResource(
fileName);
rIStream = myClassPathReader.getInputStream();
log.debug("Found File via Classpath at path:" + myClassPathReader.getPath() + " file:" + myClassPathReader.getFile());
} catch (Exception fnfe2) {
log.debug("Returning Null as could not find file" + fileName + "locally or on classpath");
//log.info("Could not find resource as file", fnfe1);
//log.info("Could not find resource on classpath", fnfe2);
// we will return null
}
}
return rIStream;
}

