Package net.firstpartners.rp

Source Code of net.firstpartners.rp.BusinessLayer

package net.firstpartners.rp;

import java.io.IOException;

import org.drools.DroolsException;
import org.drools.RuleBase;
import org.drools.WorkingMemory;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.io.RuleBaseLoader;
import org.xml.sax.SAXException;
import org.drools.spi.ConflictResolver;
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";
 
  //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){
     
      //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*/ );
    }
  }
 
  /**
   * 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{
   
    //Ensure that the business rules have been loaded
    loadRules();

        //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( );
       
        //This is a small ruleset , so we can add a debug listener to see what is going on
        workingMemory.addEventListener(
            new DebugWorkingMemoryEventListener( ) );
       
        //Let the rule engine know about the facts
        workingMemory.assertObject(stockToBuy);
       
        //Let the rule engine do it's stuff!!
        workingMemory.fireAllRules( );
       
  
   
   
  }

}
TOP

Related Classes of net.firstpartners.rp.BusinessLayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.