The first step is to make an instance of org.drools.RuleBase available in a Seam context variable. For testing purposes, Seam provides a built-in component that compiles a static set of rules from the classpath. You can install this component via components.xml:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
This component compiles rules from a set of DRL (.drl) or decision table (.xls) files and caches an instance of org.drools.RuleBase in the Seam APPLICATION context. Note that you will likely need to install multiple rule bases in a rule-driven application.
If you want to use a Drools DSL, you must also specify the DSL definition:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If you want to register a custom consequence exception handler through the RuleBaseConfiguration, you need to write the handler. This is demonstrated in the following example:
@Scope(ScopeType.APPLICATION)
@Startup
@Name("myConsequenceExceptionHandler")
public class MyConsequenceExceptionHandler
implements ConsequenceExceptionHandler, Externalizable {
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException { }
public void writeExternal(ObjectOutput out) throws IOException { }
public void handleException(Activation activation,
WorkingMemory workingMemory,
Exception exception) {
throw new ConsequenceException( exception, activation.getRule() );
}
}
@Scope(ScopeType.APPLICATION)
@Startup
@Name("myConsequenceExceptionHandler")
public class MyConsequenceExceptionHandler
implements ConsequenceExceptionHandler, Externalizable {
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException { }
public void writeExternal(ObjectOutput out) throws IOException { }
public void handleException(Activation activation,
WorkingMemory workingMemory,
Exception exception) {
throw new ConsequenceException( exception, activation.getRule() );
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Copy to ClipboardCopied!Toggle word wrapToggle overflow
In most rules-driven applications, rules must be dynamically deployable. A Drools RuleAgent is useful to manage the RuleBase. The RuleAgent can connect to a Drools rule server (BRMS), or hot-deploy rules packages from a local file repository. The RulesAgent-managed RuleBase is also configurable via components.xml:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The properties file contains properties specific to the RulesAgent. The following is an example configuration file from the Drools example distribution:
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Next, make an instance of org.drools.WorkingMemory available to each conversation. (Each WorkingMemory accumulates facts relating to the current conversation.)
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Notice that we referred the policyPricingWorkingMemory back to our rule base via the ruleBase configuration property.
We can also add means to be notified of rule engine events, including, for example, rules firing or objects being asserted by adding event listeners to WorkingMemory.