JBoss Rules Engine

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

See Also

JBoss Rules Reference

JBoss Rules Manual - v3.0.6

Quick JBoss Rules DRL Compiler - via Junit

JBoss Rules in a Maven Project

Ensure the Jboss repository is listed in the main (and perhaps core) pom.xml

<repository>
            <id>JBoss</id>
            <url>http://repository.jboss.com/maven2/</url>
        </repository>


Add the following to the main (and perhaps core) pom.xml

<dependency>
   <groupId>org.drools</groupId>
           <artifactId>drools-core</artifactId>
           <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>org.drools</groupId>
           <artifactId>drools-decisiontables</artifactId>
           <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>org.drools</groupId>
          <artifactId>drools-jsr94</artifactId>
          <version>3.0.3</version>
</dependency>

Java Code to load the rules from the classpath

/**
     * Where we store the temporary drl file
     */
    private String drlFileLocation = null;
 
    /**
     * the name of the excel file that is our decision table
     */
    private String excelFileLocation = null;
 
    // Handle to the ExcelDecisionTable
    private RuleBase excelRules = null;
 
    /**
     * Logger for this class and subclasses
     */
    protected final Logger log = Logger.getLogger(getClass());
 
    // Where serialised files are stored
    private String serialFileLocation = null;
 
    // The working Memory for a rule
    private WorkingMemory sharedWorkingMemory = null;
 
    /**
     * executeDecisionTable
     *
     * @param javaBean            -
     *                            this should be same as the JavaBean expected by the Decision
     *                            Table or an RuleCompileException will be thrown.
     * @param useNewWorkingMemory -
     *                            whether or not to setup a new working memory , otherwise use
     *                            shared.Get a new working memory if requested. We do this as
     *                            Spring by default makes the bean act as a singleton. Using the
     *                            Shared working memory allows us to re-use previous results
     *                            (faster) and preserves state
     * @return javaBean - same Bean (Type and object reference) as the input,
     *         but with values updated as per the decision Table
     */
    public Object executeDecisionTable(Object javaBean,
                                       boolean useNewWorkingMemory,boolean useCachedRules) throws SearchException {
 
        // Local Variables
        WorkingMemory localWorkingMemory = null;
 
        // Make sure our ruleset is loaded
        initialize(useCachedRules);
 
        // Get a new working memory if requested. We do this as Spring by
        // default
        // makes the bean act as a singleton. Using the Shared working memory
        // allows us to
        // re-use previous results (faster) and preserves state
        if (useNewWorkingMemory) {
            localWorkingMemory = getRuleBase().newWorkingMemory();
        } else {
            localWorkingMemory = this.getSharedWorkingMemory();
        }
 
        // Place the Java Bean in the working memory , keep a handle to it
        FactHandle currentHandle = localWorkingMemory.assertObject(javaBean);
 
        // use Drools *without* any Java intervention
        localWorkingMemory.fireAllRules();
 
        // Remove the reference to this as part of the cleanup for later
        localWorkingMemory.retractObject(currentHandle);
 
        return javaBean;
    }
 
    /**
     * Get the filename of the DRL Rules file , if any (optional)
     *
     * @return
     */
    public String getDrlFileLocation() {
        return drlFileLocation;
    }
 
    /**
     * Set the filename of the DRL Rules file , if any (optional)
     *
     * @return
     */
    public String getExcelFileLocation() {
        return excelFileLocation;
    }
Personal tools