⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopping.java

📁 这是一个在eclipse里面使用JBoss规则引擎Drools的一个例子
💻 JAVA
字号:
package org.drools.hu.test;

import java.io.InputStreamReader;

import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;

public class Shopping {

	
	public static void main(String[] args){
	
		try {
			PackageBuilder builder=new PackageBuilder();
			builder.addPackageFromDrl(new InputStreamReader(Shopping.class.getResourceAsStream("Shopping.drl")));
			RuleBase ruleBase=RuleBaseFactory.newRuleBase();
			Package pkg=builder.getPackage();
			ruleBase.addPackage(pkg);
			WorkingMemory wm=ruleBase.newStatefulSession(); 
			Customer hyy=new Customer("hyy",0);
			wm.insert(hyy);
			Product shoes=new Product("shoes",60);
			wm.insert(shoes);
			Product hat=new Product("hat",60);
			wm.insert(hat);
			wm.insert(new Purchase(hyy,shoes));
			FactHandle hatPurchaseHandle=wm.insert(new Purchase(hyy,hat));
			wm.fireAllRules();
			wm.retract(hatPurchaseHandle);   //removes an object from working memory
			System.out.println("Customer hyy has returned the hat");
			wm.fireAllRules();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	public static class Customer{
		
		private String name;   //顾客姓名
		private int discount;  //可以享受的折扣
		
		public Customer(String name,int discount) {
			this.discount = discount;
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getDiscount() {
			return discount;
		}

		public void setDiscount(int discount) {
			this.discount = discount;
		}
	}
	
	public static class Discount{
		private Customer customer;   //顾客
		private int amount;          //数量
		
		public Discount(Customer customer,int amount) {
			this.amount = amount;
			this.customer = customer;
		}

		public Customer getCustomer() {
			return customer;
		}

		public void setCustomer(Customer customer) {
			this.customer = customer;
		}

		public int getAmount() {
			return amount;
		}

		public void setAmount(int amount) {
			this.amount = amount;
		}
	}
	
	public static class Product{
		
		private String name;    //商品名称
		private float price;    //价格
		
		public Product(String name, float price) {
			this.name = name;
			this.price = price;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public float getPrice() {
			return price;
		}

		public void setPrice(float price) {
			this.price = price;
		}
	}
	
	public static class Purchase{
		
		private Customer customer;   //顾客
		private Product product;     //产品
		
		public Purchase(Customer customer, Product product) {
			this.customer = customer;
			this.product = product;
		}

		public Customer getCustomer() {
			return customer;
		}

		public void setCustomer(Customer customer) {
			this.customer = customer;
		}

		public Product getProduct() {
			return product;
		}

		public void setProduct(Product product) {
			this.product = product;
		}	
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -