Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1260396
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类: Java

2007-03-19 09:19:45

这是jess7试用版的下载地址:
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下。

阅读(3776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~