Hi,
i modified the shopping example from drool 4.0.4 example downloaded from jboss website in
order to have a better understanding of insertlogical.
the rule is as follows
package org.drools.examples
dialect "mvel"
import org.drools.examples.ShoppingExample.Customer
import org.drools.examples.ShoppingExample.Product
import org.drools.examples.ShoppingExample.Purchase
import org.drools.examples.ShoppingExample.Discount
rule "Purchase notification"
salience 10
when
$c : Customer()
$p : Purchase( customer == $c)
then
System.out.println( "Customer " + $c.name + " just purchased " +
$p.product.name );
end
rule "Discount removed notification"
when
$c : Customer()
not Discount( customer == $c )
then
$c.discount = 0 ;
System.out.println( "Customer " + $c.name + " now has a discount of
" + $c.discount );
end
rule "Discount awarded notification"
when
$c : Customer()
$d : Discount( customer == $c )
then
System.out.println( "Customer " + $c.name + " now has a discount of
" + $d.amount );
end
rule "Apply 10% discount if total purcahses is over 100"
no-loop true
dialect "java"
when
$c : Customer()
$i : Double(doubleValue > 100) from accumulate ( Purchase( customer == $c, $price :
product.price ),
sum( $price ) )
then
$c.setDiscount( 10 );
insertLogical( new Discount($c, 10) );
System.out.println( "Customer " + $c.getName() + " now has a shopping
total of " + $i );
end
my java lanucher
package org.drools.examples;
import java.io.InputStreamReader;
import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.compiler.PackageBuilder;
public class ShoppingExample {
public static final void main(String[] args) throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader(
ShoppingExample.class.getResourceAsStream( "Shopping.drl" ) ) );
final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( builder.getPackage() );
final StatefulSession session = ruleBase.newStatefulSession();
final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session );
logger.setFileName( "log/shoppingExample" );
Customer mark = new Customer( "mark",
0 );
session.insert( mark );
Product shoes = new Product( "shoes",
900 );
session.insert( shoes );
Product hat = new Product( "hat",
160 );
session.insert( hat );
session.insert( new Purchase( mark,
shoes ) );
session.insert( new Purchase( mark,
hat ) );
session.fireAllRules();
Product pent = new Product("pent", 100);
session.insert(new Purchase(mark, pent));
System.out.println( "Customer mark also buy the pent" );
session.fireAllRules();
logger.writeToDisk();
}
public static class Customer {
private String name;
private int discount;
public Customer(String name,
int discount) {
this.name = name;
this.discount = discount;
}
public String getName() {
return 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.customer = customer;
this.amount = amount;
}
public Customer getCustomer() {
return customer;
}
public int getAmount() {
return 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 float getPrice() {
return 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 Product getProduct() {
return product;
}
}
}
i got the following output after i ran the java code
Customer mark just purchased hat
Customer mark just purchased shoes
Customer mark now has a shopping total of 1060.0
Customer mark now has a discount of 10
Customer mark also buy the pent
Customer mark just purchased pent
Customer mark now has a discount of 0
Customer mark now has a shopping total of 1160.0
Customer mark now has a discount of 10
i don not know why "Customer mark now has a discount of 0" still get printed.
As my understanding, the insertion of the pent purchase should still keep the "Apply
10% discount if total purcahses is over 100" rule valid, so the logical Discount is
not retracted which means that the engine should not print out the "
Customer mark now has a discount of 0". However i got the opposite answer.
is my understanding wrong or i miss something ?
I am running drools 4.0.4
Thanks for any help
Regards,
Tao
_________________________________________________________________
MSN圣诞礼物火热登场,免费发放中,快来领取吧!
http://im.live.cn/emoticons/?ID=18