0 Replies Latest reply on Mar 31, 2008 4:54 AM by mukeshanand

    Drools problem

    mukeshanand

      Hi All,
      I am new in drools engine.
      I am facing one error. I am using drools 4.0.4. I am also attaching the files for your easy reference.
      Error is :

      org.drools.RuntimeDroolsException: java.lang.NullPointerException
      at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:76)
      at org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:145)
      at org.drools.reteoo.CompositeTupleSinkAdapter.createAndPropagateAssertTuple(CompositeTupleSinkAdapter.java:73)
      at org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
      at org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(SingleObjectSinkAdapter.java:22)
      at org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:153)
      at org.drools.reteoo.Rete.assertObject(Rete.java:175)
      at org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
      at org.drools.reteoo.ReteooWorkingMemory$WorkingMemoryReteAssertAction.execute(ReteooWorkingMemory.java:179)
      at org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:1315)
      at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:914)
      at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:881)
      at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:682)
      at com.sample.BusinessLayer.evaluateStockPurchase(BusinessLayer.java:111)
      at com.sample.BusinessRuleTest.testStockBuy(BusinessRuleTest.java:30)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at junit.framework.TestCase.runTest(TestCase.java:154)
      at junit.framework.TestCase.runBare(TestCase.java:127)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:118)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
      Caused by: java.lang.NullPointerException
      at com.sample.Rule_XYZCorp_0.eval0(Rule_XYZCorp_0.java:12)
      at com.sample.Rule_XYZCorp_0Eval0Invoker.evaluate(Rule_XYZCorp_0Eval0Invoker.java:16)
      at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:72)
      ... 32 more

      The files are:
      #created on: Mar 27, 2008

      package com.sample


      #list any import classes here.

      import com.sample.StockOffer;

      import com.sample.DaoFactory;

      import com.sample.StockNameDao;

      import java.lang.Object;

      import java.lang.String;


      #declare any global variables here

      global DaoFactory daoFactory;

      global StockOffer stockOffer;






      rule "XYZCorp"

      salience -1


      when

      #conditions

      //eval(stockOffer.getStockName().equals("XYZ") )and

      //eval(stockOffer.getRecommendPurchase() == null )and

      //eval(stockOffer.getStockPrice() > 10)

      eval((stockOffer.getStockName().equals("XYZ")) && (stockOffer.getRecommendPurchase() == null) && (stockOffer.getStockPrice() > 10))

      then

      #actions

      stockOffer.setRecommendPurchase(StockOffer.NO);

      System.out.println("Name:"+stockOffer.getStockName()+" Price: "+stockOffer.getStockPrice() +" BUY:"+stockOffer.getRecommendPurchase());


      end


      rule "Stock Price Not Negative"

      #include attributes such as "salience" here...

      when

      #conditions

      eval (stockOffer.getStockPrice() < 0 )

      then

      #actions

      stockOffer.setRecommendPurchase(StockOffer.NO);

      System.out.println("Name:"+stockOffer.getStockName()+" Price: "+stockOffer.getStockPrice() +" BUY:"+stockOffer.getRecommendPurchase());

      end


      rule "Stock Price Low Enough"

      #include attributes such as "salience" here...

      when

      #conditions

      //eval(daoFactory.getStockDao().isOnStockList(stockOffer.getStockName())) and

      //eval(stockOffer.getRecommendPurchase() == null )and

      //eval(stockOffer.getStockPrice() < 100)

      eval((daoFactory.getStockDao().isOnStockList(stockOffer.getStockName())) && (stockOffer.getRecommendPurchase() == null) && (stockOffer.getStockPrice() < 100))

      then

      #actions

      stockOffer.setRecommendPurchase(StockOffer.YES);

      System.out.println("Name:"+stockOffer.getStockName()+" Price: "+stockOffer.getStockPrice() +" BUY:"+stockOffer.getRecommendPurchase());


      end

      and BusinessLayer.java:

      package com.sample;


      import java.io.FileInputStream;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.io.Reader;


      //import org.drools.DroolsException;

      import org.drools.RuleBase;

      import org.drools.RuleBaseFactory;

      import org.drools.WorkingMemory;

      import org.drools.event.DebugWorkingMemoryEventListener;

      //import org.drools.io.RuleBaseLoader;

      import org.xml.sax.SAXException;

      import org.drools.rule.Package;

      import org.drools.spi.ConflictResolver;

      import org.drools.compiler.PackageBuilder;

      import org.drools.conflict.SalienceConflictResolver;

      import org.drools.conflict.FifoConflictResolver;

      import org.drools.conflict.CompositeConflictResolver;

      import org.drools.conflict.SimplicityConflictResolver;

      import org.drools.conflict.RecencyConflictResolver;

      import org.drools.conflict.LoadOrderConflictResolver;


      /**

      * Facade for the Business Logic in our example.

      *

      * In this simple example , all our business logic is contained in this class

      * but in reality it would delegate to other classes as required.

      * @author Paul Browne - www.firstpartners.net

      */

      public class BusinessLayer {


      //Name of the file containing the rules

      //private static final String BUSINESS_RULE_FILE="BusinessRules.drl";

      //private static final String BUSINESS_RULE_FILE="C:/MyEclipse_JRE/DroolsTest2/src/main/rules/BusinessRules.drl";


      //Internal handle to rule base

      private static RuleBase businessRules = null;


      /**

      * Load the business rules if we have not already done so

      * @throws Exception - normally we try to recover from these

      */

      private static void loadRules() throws Exception{

      if (businessRules==null){

      System.out.println("Mukesh 5");

      //Generate our list of conflict resolvers

      /* ConflictResolver[] conflictResolvers =

      new ConflictResolver[] { SalienceConflictResolver.getInstance( ),

      RecencyConflictResolver.getInstance(),

      SimplicityConflictResolver.getInstance( ),

      LoadOrderConflictResolver.getInstance( )};



      //Wrap this up into one composite resolver

      CompositeConflictResolver resolver = new CompositeConflictResolver(conflictResolvers);

      */

      //Specify this resolver when we load are rules

      //businessRules = RuleBaseLoader.loadFromUrl(

      // BusinessLayer.class.getResource( BUSINESS_RULE_FILE )/*,resolver*/ );

      Reader source = new InputStreamReader( BusinessLayer.class.getResourceAsStream( "/BusinessRules.drl") );

      final PackageBuilder builder = new PackageBuilder();

      System.out.println("Mukesh 6 ");

      //builder.addPackageFromDrl(new InputStreamReader(new FileInputStream(BUSINESS_RULE_FILE)));

      //builder.addPackageFromXml(new InputStreamReader(new FileInputStream(BUSINESS_RULE_FILE)));

      builder.addPackageFromDrl(source);

      System.out.println("Mukesh 7");

      Package pkg = builder.getPackage();

      System.out.println("Mukesh 8");

      businessRules = RuleBaseFactory.newRuleBase();

      businessRules.addPackage( pkg );

      System.out.println("Mukesh 9");



      }


      }


      /**

      * Evaluate whether or not it is a good idea to purchase this stock.

      * @param stockToBuy

      * @return true if the recommendation is to buy the stock , false if otherwise

      * @throws Exception - normally we try to recover from these

      */

      public static void evaluateStockPurchase(StockOffer stockToBuy) throws Exception{

      System.out.println("Mukesh 3");

      //Ensure that the business rules have been loaded

      loadRules();

      System.out.println("Mukesh 4");

      //Some logging of what is going on

      System.out.println( "\n\n\n\nFIRE RULES" );

      System.out.println( "----------" );


      //Clear any state from previous runs of the business engine

      // WorkingMemory workingMemory = businessRules.newWorkingMemory( );

      WorkingMemory workingMemory = businessRules.newStatefulSession();

      System.out.println("Mukesh 10");

      DaoFactory daoFactory = new DaoFactory();

      System.out.println("Mukesh 11");

      StockOffer stockOffer = new StockOffer();

      System.out.println("Mukesh 12");

      workingMemory.setGlobal("daoFactory", daoFactory);

      System.out.println("Mukesh 13");

      workingMemory.setGlobal("stockOffer", stockOffer);

      System.out.println("Mukesh 14");



      //This is a small ruleset , so we can add a debug listener to see what is going on

      workingMemory.insert(stockToBuy);

      //workingMemory.insert(stockOffer);

      System.out.println("Mukesh 15");

      workingMemory.addEventListener(

      new DebugWorkingMemoryEventListener( ) );

      System.out.println("Mukesh 16");

      //Let the rule engine know about the facts

      // workingMemory.assertObject(stockToBuy);

      //workingMemory.insert(stockToBuy);

      System.out.println("Mukesh 17");

      //Let the rule engine do it's stuff!!

      workingMemory.fireAllRules( );

      System.out.println("Mukesh 18");





      }


      }

      and

      BusinessRuleTest:

      package com.sample;


      import junit.framework.TestCase;




      public class BusinessRuleTest extends TestCase {


      /**

      * Tests the purchase of a stock

      */

      public void testStockBuy() throws Exception{



      //Create a Stock with our simulated values

      StockOffer testOffer = new StockOffer();

      testOffer.setStockName("MEGACORP");

      testOffer.setStockPrice(22);

      testOffer.setStockQuantity(1000);

      System.out.println("Mukesh 1");

      //Run the rules on it

      BusinessLayer.evaluateStockPurchase(testOffer);

      System.out.println("Mukesh 2");

      //Is it what we expected?

      assertTrue(testOffer.getRecommendPurchase()!=null);

      assertTrue("YES".equals(testOffer.getRecommendPurchase()));




      }


      /**

      * Tests the purchase of a stock - makes sure the system

      * will not accept negative numbers

      */

      public void xtestNegativeStockBuy() throws Exception{


      //Create a Stock with our simulated values

      StockOffer testOffer = new StockOffer();

      testOffer.setStockName("MEGACORP");

      testOffer.setStockPrice(-22);

      testOffer.setStockQuantity(1000);


      //Run the rules on it

      BusinessLayer.evaluateStockPurchase(testOffer);


      //Is it what we expected?

      assertTrue("NO".equals(testOffer.getRecommendPurchase()));



      }



      /**

      * Makes sure the system will buy stocks of XYZ corp only if it really cheap

      */

      public void xtestXYZStockBuy() throws Exception{


      //Create a Stock with our simulated values

      StockOffer testOfferLow = new StockOffer();

      StockOffer testOfferHigh = new StockOffer();


      testOfferLow.setStockName("XYZ");

      testOfferLow.setStockPrice(9);

      testOfferLow.setStockQuantity(1000);


      testOfferHigh.setStockName("XYZ");

      testOfferHigh.setStockPrice(11);

      testOfferHigh.setStockQuantity(1000);


      //Run the rules on it and test

      BusinessLayer.evaluateStockPurchase(testOfferLow);

      assertTrue("YES".equals(testOfferLow.getRecommendPurchase()));


      BusinessLayer.evaluateStockPurchase(testOfferHigh);

      assertTrue("NO".equals(testOfferHigh.getRecommendPurchase()));




      }

      }

      and
      DaoFactory.java

      package com.sample;




      public class DaoFactory {




      public DaoFactory(){}

      public static StockNameDao getStockDao(){


      return new DaoImplementation();

      }

      }

      and DaoImplementation.java

      package com.sample;




      public class DaoImplementation implements StockNameDao {


      DaoImplementation(){


      }



      public String[] getStockNames() {


      String[] stockNames={"XYZ","ABC","MEGACORP","SOMEOTHERCOMPANY"};


      return stockNames;

      }



      public boolean isOnStockList(String stockName){


      //Get our list of stocks

      String stockList[] = getStockNames();


      //Loop and see if our stock is on it

      // done this way for clarity . not speed!

      for (int a=0; a<stockList.length;a++){

      if(stockList[a].equals(stockName)){

      return true;

      }

      }


      //Default returh value

      return false;

      }


      }

      and
      StockDaoTest.java

      package com.sample;


      import junit.framework.TestCase;


      public class StockDaoTest extends TestCase {


      public void testIsOnStockList(){


      StockNameDao myDao = DaoFactory.getStockDao();

      assertTrue(myDao.isOnStockList("XYZ"));


      }

      }

      StockNameDao.java

      package com.sample;




      public interface StockNameDao {




      public String [] getStockNames();



      public boolean isOnStockList(String stockName);



      }

      and

      StockOffer.java

      package com.sample;




      public class StockOffer {


      //constants

      public final static String YES="YES";

      public final static String NO="NO";


      //Internal Variables

      private String stockName =null;

      private int stockPrice=0;

      private int stockQuantity=0;

      private String recommendPurchase = null;


      public StockOffer (){}

      /**

      * @return Returns the stockName.

      */

      public String getStockName() {

      return stockName;

      }

      /**

      * @param stockName The stockName to set.

      */

      public void setStockName(String stockName) {

      this.stockName = stockName;

      }

      /**

      * @return Returns the stockPrice.

      */

      public int getStockPrice() {

      return stockPrice;

      }

      /**

      * @param stockPrice The stockPrice to set.

      */

      public void setStockPrice(int stockPrice) {

      this.stockPrice = stockPrice;

      }

      /**

      * @return Returns the stockQuantity.

      */

      public int getStockQuantity() {

      return stockQuantity;

      }

      /**

      * @param stockQuantity The stockQuantity to set.

      */

      public void setStockQuantity(int stockQuantity) {

      this.stockQuantity = stockQuantity;

      }

      /**

      * @return Returns the recommendPurchase.

      */

      public String getRecommendPurchase() {

      return recommendPurchase;

      }

      /**

      * @param recommendPurchase The recommendPurchase to set.

      */

      public void setRecommendPurchase(String recommendPurchase) {

      this.recommendPurchase = recommendPurchase;

      }

      }

      As per my understanding, problem is in BusinessRules.drl file itself.

      Please help me if anyone has faced this issue.

      Thanks and Regards,
      Mukesh Anand Chaurasia
      9811908986