4 Replies Latest reply on Aug 27, 2004 4:47 AM by Bela Ban

    Rules Engine Working Memory in JBoss-Cache

    Kieran Beltran Newbie

      I am placing the working memory of the drools open-source rules engine (http://drools.org/ using fishmonger example) into JBoss-Cache. I am using TreeCacheAOP, and am having problems with a single method call. I am driving this from Beanshell (adapted from runShellDemo):

      The problem line of code is:
      workingMemory.assertObject( cart );

      which is responsible for storing an additional fact into the working memory.

      The exception I receive is:
      target exception: Sourced file:c:\jboss-cache\drools.bsh : Method Invocation workingMemory.assertObject: at Line 33 : in file: c:\jboss-cache\drools.bsh : workingMemory .assertObject ( cart )

      Appreciate any assistance. I am new to JBoss-Cache.

      drools.bsh

      //
      // Beanshell script to run the aop tree cache demo
      //
      import org.jboss.cache.PropertyConfigurator;
      import org.jboss.cache.aop.TreeCacheAop;

      import org.drools.RuleBase;
      import org.drools.WorkingMemory;
      import org.drools.rule.RuleSet;
      import org.drools.io.RuleBaseBuilder;
      import org.drools.examples.fishmonger.FishMonger;
      import org.drools.examples.fishmonger.CartItem;
      import org.drools.examples.fishmonger.ShoppingCart;

      import java.io.IOException;
      import java.net.URL;
      import java.util.List;
      import java.util.Iterator;

      show(); // verbose mode

      tree = new TreeCacheAop();
      config = new PropertyConfigurator();
      config.configure(tree, "META-INF/replSync-service.xml");

      url = FishMonger.class.getResource( "fishmonger.drl" );
      ruleBase = RuleBaseBuilder.buildFromUrl( url );
      workingMemory = ruleBase.newWorkingMemory();

      cart = new ShoppingCart();
      item = new CartItem( "tropical fish",12.99 );
      cart.addItem( item );
      //workingMemory.assertObject( cart );
      workingMemory.fireAllRules();

      tree.start(); // kick start tree cache

      tree.putObject("rules", workingMemory); // add aop sanctioned object

      // tree.stop(); // will stop the cache

        • 1. Re: Rules Engine Working Memory in JBoss-Cache
          Bela Ban Master

          Might be a problem of bsh: Can you create a standalone Java program from your bsh (should be simple) and try again ?

          And did you include all classes you want to be instrumentable in jboss-aop.xml ? You can use wildcards, e.g. org.drools.*

          Bela

          • 2. Re: Rules Engine Working Memory in JBoss-Cache
            Ben Wang Master

            Like Bela suggested, please try it in Java program first. One thing I don't like bsh is the debugging capability is poor.

            Thanks,

            -Ben

            • 3. Re: Rules Engine Working Memory in JBoss-Cache
              Kieran Beltran Newbie

              Bela / Ben,

              At your suggestion, I took the following two steps:
              1. Added POJOs contained within drools rules engine packages into jboss-aop.xml
              2. Wrote a program (attached) to exercise the cache

              This was successful. Thanks.

              **** source code ****

              import org.jboss.cache.aop.TreeCacheAop;
              import org.jboss.cache.PropertyConfigurator;

              import org.drools.RuleBase;
              import org.drools.WorkingMemory;
              import org.drools.io.RuleBaseBuilder;
              import org.drools.examples.fishmonger.FishMonger;
              import org.drools.examples.fishmonger.CartItem;
              import org.drools.examples.fishmonger.ShoppingCart;
              import org.drools.DroolsException;
              import org.drools.AssertionException;

              import java.io.IOException;
              import java.net.URL;

              /**
              * @author kieran
              *
              * TODO To change the template for this generated type comment go to Window -
              * Preferences - Java - Code Style - Code Templates
              */
              public class DroolsTester implements Runnable {
              static TreeCacheAop _cache;

              public DroolsTester(TreeCacheAop cache) {
              _cache = cache;
              }

              public static void main(String[] args) {
              try {
              _cache = new TreeCacheAop();
              PropertyConfigurator config = new PropertyConfigurator();
              config.configure(_cache, "META-INF/replSync-service.xml");
              _cache.start(); // kick start tree cache
              } catch (Exception e) {
              e.printStackTrace();
              }
              DroolsTester tester1 = new DroolsTester(_cache);
              DroolsTester tester2 = new DroolsTester(_cache);

              Thread t1 = new Thread(tester1);
              Thread t2 = new Thread(tester2);
              t1.start();
              t2.start();
              try {
              t1.join();
              t2.join();
              } catch (InterruptedException e) {
              e.printStackTrace();
              }
              }

              public void run() {
              RuleBase ruleBase = null;
              WorkingMemory workingMemory = null;
              ShoppingCart cart = null;

              try {
              synchronized (_cache) {
              URL url = FishMonger.class.getResource("fishmonger.drl");
              ruleBase = RuleBaseBuilder.buildFromUrl(url);
              workingMemory = (WorkingMemory) _cache
              .getObject("/workingMemory/rules");
              cart = (ShoppingCart) _cache.getObject("/shoppingCart/cart");
              }
              if (workingMemory == null) {
              workingMemory = ruleBase.newWorkingMemory();
              cart = new ShoppingCart();
              _cache.putObject("/workingMemory/rules", workingMemory);
              _cache.putObject("/shoppingCart/cart", cart);

              try {
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish", 12.99));
              cart.addItem(new CartItem("tropical fish food", 8.00));
              System.out
              .println("----------------------------------------");
              System.out.println(" PRE");
              System.out
              .println("----------------------------------------");
              System.out.println(cart);
              System.out
              .println("----------------------------------------");
              // Now, simply assert them into the working memory and let
              // the logic engine do the rest.
              workingMemory.assertObject(cart);

              } catch (AssertionException e) {
              e.printStackTrace();
              }
              } else {
              try {
              // now fire the rules engine...
              workingMemory.fireAllRules();
              System.out
              .println("----------------------------------------");
              System.out.println(" POST");
              System.out
              .println("----------------------------------------");
              System.out.println(cart);
              System.out
              .println("----------------------------------------");
              _cache.stop(); // will stop the cache
              } catch (AssertionException e) {
              e.printStackTrace();
              }
              }
              } catch (DroolsException e) {
              e.printStackTrace();
              } catch (IOException e) {
              e.printStackTrace();
              } catch (Exception e) {
              e.printStackTrace();
              }
              }
              }

              • 4. Re: Rules Engine Working Memory in JBoss-Cache
                Bela Ban Master

                So which one caused the problem:
                - missing drools from jboss-aop.xml (my guess) or
                - using a Java program.

                I assume you can just cut&paste your program back into bsh, and it should work now.

                Bela