jess学习笔记之三:java中嵌入jess引擎

这是jess7试用版的下载地址: http://herzberg.ca.sandia.gov/jess/download.shtml
java中嵌入jess引擎的部分功能可能是要money的。

加载jess引擎的代码部分:

               public class PricingEngine {

    private Rete engine;

    private WorkingMemoryMarker marker;

    private Database database;

 

    public PricingEngine(Database aDatabase) throws JessException {

        // Create a Jess rule engine

        engine = new Rete();

        engine.reset();

 

        // Load the pricing rules

        engine.batch("pricing.clp");//自动查找pricing.clp,无论它在当前目录下,还是封装在一个jar文件中,或是在web-inf/classes目录下,都能找到

 

        // Load the catalog data into working memory

        database = aDatabase;

        engine.addAll(database.getCatalogItems());

 

       // Mark end of catalog data for later

       marker = engine.mark();

    }

处理一条jess命令,需要四个步骤:重新设置引擎为初始状态,加载命令,执行规则,提取结果。加载命令的代码如下:

    private void loadOrderData(int orderNumber) throws JessException {

        // Retrive the order from the database

        Order order = database.getOrder(orderNumber);

 

        if (order != null) {

           // Add the order and its contents to working memory

           engine.add(order);

           engine.add(order.getCustomer());

           engine.addAll(order.getItems());

        }

    }

    //执行命令并返回一个结果的的iterator,这结果需重新转换为java对象:

    public Iterator run(int orderNumber) throws JessException {

        // Remove any previous order data, leaving only catalog data

        engine.resetToMark(marker);

 

        // Load data for this order

        loadOrderData(orderNumber);

 

        // Fire the rules that apply to this order

        engine.run();

 

        // Return the list of offers created by the rules

        return engine.getObjects(new Filter.ByClass(Offer.class));

}

clp中的规则的内容则类似

Jess> (defrule 10%-volume-discount

    "Give a 10% discount to everybody who spends more than $100."

    (Order {total > 100})

    =>

    (add (new Offer "10% volume discount" (/ ?total 10))))

 

例子代码详见jess/examples/pricing_engine下。

你可能感兴趣的:(java,list,database,iterator,Class,引擎)