?? shopping.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 + -