Version 13

    JBRMS v4.01 &150; create enumerations via database access


    contributed by Steve Barfield:



    The latest release of JBRMS (v4.0.1) allows you to write rules using values from an external database.


    This has 2 advantages :


    1. The values used in the LHS rule statements will be derived using the same tables that the calling application will use.


       EG. in the rule





      &147;CHD&148; will be taken from the CHEESE table.


    In the Favourite Cheese Survey application the list of cheeses to choose from is also derived from the CHEESE table.


    When the data from the Cheese Survey is submitted to the rule engine this ensures the code

      &147;CHD&148; (not &147;CHE&148; or &148;CHED&148;) is submitted and hence will match the rule.


    *2. The values used in the LHS will automatically appear as drop down lists within JBRMS. *


    You can even get a descriptor

       &147;Chedder&148; to appear in the drop down list and the key field &147;CHD&148; will be saved in the rule.



    This example uses a BEA Weblogic webserver and an Oracle database but the principle is the same for any webserver or database.







    To achieve this follow these 4 steps :



    Create helper class

    Add helper class to jbrms war file

    Reference helper class in Technical Rule Asset Data Enumeration

    Use Enumeration in rule


    1.Create helper class


    This helper class ListLoader has 2 methods that return an ArrayList, the first method returns a hardcoded list,


    the second uses an additional class DBHandler to return an ArrayList derived from a database.


    The DBHandler class references an Oracle database but any database could be used.

    import java.util.ArrayList;
    public class ListLoader {
         public ListLoader(){}
         // hardcoded list
    public ArrayList loadList(){
            ArrayList aL=new ArrayList();
            return aL;
         //list derived from db
    public ArrayList loadCheeses(){
            DBHandler db=new DBHandler();
            ArrayList aL=new ArrayList();
            String q="select cheese_id, cheese_name from cheeses";
                java.sql.ResultSet r=db.runQuery(q);
            catch(Exception e){
            return aL;
    import java.sql.*;
    import java.util.*;
    import javax.sql.*;
    import java.sql.Connection;
    import oracle.jdbc.pool.OracleDataSource;
    public class DBHandler {
      //Gets a connection from the Connection pool
      private Connection getConnection (){
      Connection conn=null;
           String serverName="localhost";
           String serverPort="1521";
           String sid="xe";
           String userName="hr";
           String userPswd="hr";
           String connectionString=serverName+":"+serverPort+":"+sid;
           String url="jdbc:oracle:thin:@"+connectionString;
           // Set DataSource properties
           OracleDataSource ods=new OracleDataSource();
           conn = ods.getConnection();
        catch (Exception ex) { }
        return conn;
      //runs select queries.
      public ResultSet runQuery (String query) throws SQLException {
        Connection c = getConnection();
        Statement statement = c.createStatement();
        ResultSet r = statement.executeQuery(query);
        return r;
    // free resources by closing the Statement and Connection that created r.
      public void closeQuery (ResultSet r) throws SQLException {
        Statement s = r.getStatement();
        Connection c = s.getConnection();


    2. Add helper class to jbrms war file


    Put helper classes in jar file :


    D:\beaNew\user_projects\w4WP_workspaces\beaNew\HelperClasses>"C:\Program Files\Java\jdk1.5.0_11\bin\jar" 
    \\-cf c:\myHelperClasses.jar com\mycompany\rules\data\ListLoader.class

    Add jar files to drools-jbrms.war


    Note the path is set as WEB-INF\lib\


    Note the jdbc files included in war.


    Note this deployment of jbrms is for BEA Weblogic so jsf files are included, this is not necessary for JBOSS deployment.




    3. Reference helper class in Technical Rule Asset Data Enumeration



    Within Packages, under Explore, use the last icon on the right &147;Create a new data enumeration (drop down List)&148;





    Enter the fact property that the enumeration applies to and enter your helper class plus method :



    'Attributes.favouriteCheese' : (new






    4. Use Enumeration in rule



    When jbrms started, note output to console showing helper class has been used.





    When rule containing Attributes.favouriteCheese written &150; drop down list appears for value !!