drools6入门例子(二)

产品规则如下:
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

你可能感兴趣的:(drools)