码迷,mamicode.com
首页 > 其他好文 > 详细

drools6入门样例(二)

时间:2017-06-16 11:25:24      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:eal   mave   结果   integer   out   oid   private   count()   system   

产品规则例如以下:
1:单个产品数量超过2个,该产品打9折

2:总价格超过1000,立减50


新建maven项目。增加drools的依赖

<dependency>
	<groupId>org.drools</groupId>
	<artifactId>drools-core</artifactId>
	<version>6.2.0.Final</version>
</dependency>
<dependency>
	<groupId>org.drools</groupId>
	<artifactId>drools-compiler</artifactId>
	<version>6.2.0.Final</version>
</dependency>


创建Fact

package com.lala.bean;

/**
 * 产品
 */
public class Product 
{
	private String name;
	private Integer count = 1; //数量
	private double price; //价格
	private double discount = 1;	//折扣
	public Product(String name, double price)
	{
		this.name = name;
		this.price = price;
	}
	public Product(String name, double price, Integer count)
	{
		this.name = name;
		this.price = price;
		this.count = count;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Integer getCount()
	{
		return count;
	}
	public void setCount(Integer count) 
	{
		this.count = count;
	}
	public double getPrice() 
	{
		return price;
	}
	public void setPrice(double price) 
	{
		this.price = price;
	}
	public double getDiscount() 
	{
		return discount;
	}
	public void setDiscount(double discount)
	{
		this.discount = discount;
	}
	/**
	 * 金额
	 */
	public double getPay()
	{
		return price * count * discount;
	}
	public String toString()
	{
		return "[name="+name+",price="+price+",count="+count+",discount="+discount+",getPay()="+getPay()+"]";
	}
}

package com.lala.bean;

import java.util.List;

/**
 * 订单
 */
public class Order 
{
	private Integer id;
	private List<Product> products;
	private double cash = 0;	//返现
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public List<Product> getProducts() 
	{
		return products;
	}
	public void setProducts(List<Product> products)
	{
		this.products = products;
	}
	/**
	 * 总金额
	 */
	public double getTotal()
	{
		double total = 0;
		for(Product product : products)
		{
			total += product.getPay();
		}
		return total;
	}
	/**
	 * 实际支付金额
	 */
	public double getPay()
	{
		return getTotal() - cash;
	}
	public double getCash() 
	{
		return cash;
	}
	public void setCash(double cash) 
	{
		this.cash = cash;
	}
}


drools规则:

package com.order;

import com.lala.bean.Product;
import com.lala.bean.Order;

rule "products"
	salience 2
    when
    	$order : Order();
    	$product : Product(count >= 3) from $order.products;//从list中取出每一个product
    then
    	$product.setDiscount(0.9);//产品数量超过2个,则该产品打9折
end

rule "order"
	salience 1
    when
    	order:Order(getTotal() > 1000);
    then
    	order.setCash(50);//总价钱超过1000,立减50
end


測试:

package com.lala.mydrools;

import java.util.ArrayList;
import java.util.List;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.lala.bean.Order;
import com.lala.bean.Product;

public class OrderTest 
{
	static KieSession getSession()
    {
        KieServices ks = KieServices.Factory.get();
         
        KieContainer kc = ks.getKieClasspathContainer();
 
        return kc.newKieSession("simpleRuleKSession");
    }
    public static void main(String[] args)
    {
        KieSession ks = getSession();

        Order order = new Order();
        
        List<Product> products = new ArrayList<>();
        products.add(new Product("MP3", 120, 2));
        products.add(new Product("MP4", 200));
        products.add(new Product("TCL电视", 3000));
        products.add(new Product("红米note2", 799, 5));
        
        order.setProducts(products);
       
        ks.insert(order);
        int count = ks.fireAllRules();
        System.out.println("总运行了"+count+"条规则");
        System.out.println("实际须要支付:" + order.getPay());
        ks.dispose();
    }
}


最后输出结果为:

总运行了2条规则
实际须要支付:6985.5

drools6入门样例(二)

标签:eal   mave   结果   integer   out   oid   private   count()   system   

原文地址:http://www.cnblogs.com/jzdwajue/p/7026395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!