Hi Michael,

  Thanks very much  for the instant reply. In my application, I have
number of .drl files for differnt kinds of domain problems and i pass
the .drl file name to the RuleExecuter from the EJB. The RuleExecuter
will fetch and load the rule file into RuleBase and then execute the
rule.

  So, if i rewrite the code to store the RuleBase in a static HashMap
(with synchronized put) when it is created for the first time and then
get it from there for subsequent use,  whether it will be fine? My code
will now look like the below one. Please give your advice on this.

Thanks and Regards,

 -- Abhilash.

  // This HashMap will act as the cache for RuleBase instances of each
rule in .drl file
  private static HashMap ruleBaseCache =  null;

  static {
        ruleBaseCache = new HashMap();
  }


  /**
    * @param ruleFile   - Name of the rule file (.drl file)
    * @param objects    - Fact objects for the rule
    * @return
    * @throws AppException
    */

  private static RuleResultVO execute( String ruleFile, Object [] objects
) throws AppException {

         /* This will contain the execution result of the rule. Every rule
will have reference to
          * this object and will finally store the result in this object
          */
        RuleResultVO ruleResult = new RuleResultVO();

        try {


                //See whether the ruleBase for this rule file is in cache.
Theoretically, this also should be synchrnoized, but will be a
performance overhead.
                RuleBase ruleBase = (RuleBase) ruleBaseCache.get( ruleFile );

                if ( ruleBase == null ) {  // First time access. Create the 
ruleBase
and put it in cache

                        synchronized( RuleExecuter.ruleBaseCache ) {
                                //Read the rule source file
                                InputStream in = 
ResourceLoader.getResourceAsStream(ruleFile);
                                Reader reader = new InputStreamReader(in);
                                DrlParser parser = new DrlParser();
                                PackageDescr packageDescr = parser.parse( 
reader );

                                //pre build the package
                                PackageBuilder builder = new PackageBuilder();
                                builder.addPackage( packageDescr );
                                Package pkg = builder.getPackage();

                                //add the package to a rulebase
                                ruleBase = RuleBaseFactory.newRuleBase();
                                ruleBase.addPackage( pkg );

                                //Add the RuleBase to the cache so that the 
subsequent calls
will use it from there.
                                ruleBaseCache.put( ruleFile, ruleBase );
                        }
                }


                //create the WorkingMemory
                WorkingMemory workingMemory = ruleBase.newWorkingMemory();

                //Supply the fact objects for the rule
                for ( int i = 0; i < objects.length; i++ ) {
                        workingMemory.assertObject( objects[i] );
                }

                workingMemory.assertObject( ruleResult );

                // go !
                workingMemory.fireAllRules();


                }catch( Exception e ) {
                        e.printStackTrace();
                        ExceptionUtil.throwAppException( 
"RuleExecuter.executeRule() :
Exception Occured :[" +e.getMessage()+"]");
                }

          return ruleResult;
  }







_______________________________________________
rules-users mailing list
[EMAIL PROTECTED]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to