0 Replies Latest reply on Oct 26, 2006 8:41 AM by vivonpereira

    JBoss Rules - Concern creating RuleBase using RuleBaseLoader

    vivonpereira

      I found there are two ways of creating the Rule base from a given drl source. Each of these ways have a particular problem with them:

      1. a) Write the Rules in a DRL File.
      b) Create a Reader for the DRL
      c) Create a PackageBuilder and then add the reader to the builder using the method 'addPackageFromDrl'
      d) Create the rulebase using 'RuleBaseFactory.newRuleBase()'
      e) Get the package from the builder and then add the package to the rulebase.

      When using the above to execute the Rules Program, it throws error at the point where we use the method 'addPackageFromDrl' from the builder.
      This is a peculiar problem because, the error is thrown during the second execution of the program. It works perfectly fine, when i execute it the first time.
      Also I want to bring it to the notice that, i am able to execute the program any number of times without errors, when I am executing it using command prompt and eclipse. The problem occurs when I am creating the jar and using it as a stand-alone and trying to execute the program from another application. The external application calls the rules method in the jar and executes it properly the first time, on second and further execution it throws me error at : builder.addPackageFromDrl(reader).

      My program code:
      Reader source = new InputStreamReader( DroolTest.class.getResourceAsStream( "/Test.drl" ) );
      PackageBuilder builder = new PackageBuilder();
      builder.addPackageFromDrl( source ); // Program throws error here during second execution from external application

      Package pkg = builder.getPackage();
      RuleBase ruleBase = RuleBaseFactory.newRuleBase();
      ruleBase.addPackage( pkg );


      2. a) Write the Rules in a DRL File.
      b) Create a Reader for the DRL
      c) Create the rulebase using RuleBaseLoader.getInstance().loadFromReader, and passing the reader to this method.

      When using this way of creating the rulebase, it works fine even if i execute the program any number of times. The execution of the program is through: command prompt, eclipse and also by External Application. Even by executing the program from an external application any number of times, the program does not throw any error.

      But there is a Functional problem with this way of creating the rulesbase. If I need to have two drl files, which is the usual case, when I use the line: RuleBaseLoader.getInstance().loadFromReader(reader);
      the second drl replaces the first drl in the rulebase.

      ie., I need to have only one rulebase for all working memories.

      Hence I have the following code:

      Reader source1 = new InputStreamReader(
      DroolTest.class.getResourceAsStream(/Test1.drl));
      ruleBase = RuleBaseLoader.getInstance().loadFromReader(source1);

      Reader source2 = new InputStreamReader(
      DroolTest.class.getResourceAsStream(/Test2.drl));
      ruleBase = RuleBaseLoader.getInstance().loadFromReader(source2);

      Here the rulebase created for the first time is replaced by the rulebase created second time. Hence the rules in the first drl file are all lost.


      Can anyone give me any suggestion on the above two problems. Appreciate if the response is quick and related to the problem.

      Thank u in advance