[rules-users] Beginner question about Dynamic Beans
fmarchioni
f.marchioni at pride.it
Wed Jul 2 08:38:03 EDT 2008
> that's strange. From my experience, only those rules that depend on
> the cash property should be fired if the new value triggers an
> activation that wouldn't have been triggered for the old value. Could
> you post the code of your PropertyChangeSupport implementation?
>
Hi Marcus,
thanks for your reply, well my Bean implementation is:
public class Buyer {
int age;
double yearlyIncome;
double cash;
private final PropertyChangeSupport changes = new PropertyChangeSupport(
this );
public void addPropertyChangeListener(final PropertyChangeListener l) {
this.changes.addPropertyChangeListener( l );
}
public void removePropertyChangeListener(final PropertyChangeListener l) {
this.changes.removePropertyChangeListener( l );
}
public int getAge() {
return age;
}
public void setAge(int newAge) {
int oldAge = this.age;
this.age = newAge;
this.changes.firePropertyChange("age", oldAge, newAge);
}
public double getCash() {
return cash;
}
public void setCash(double newCash) {
double oldCash = this.cash;
this.cash = newCash;
System.out.println("[Buyer ] cash is " + this.cash);
this.changes.firePropertyChange("cash", oldCash, newCash);
}
public double getYearlyIncome() {
return yearlyIncome;
}
public void setYearlyIncome(double newYearlyIncome) {
double oldYearlyIncome = this.yearlyIncome;
this.yearlyIncome = newYearlyIncome;
this.changes.firePropertyChange("yearlyIncome", oldYearlyIncome,
newYearlyIncome);
}
}
And my complete Rule file is:
global drools.BankApplication bankApp;
rule "Check Age"
when
b : Buyer (age <18)
then
System.out.println("[Rule.drl] Checking age");
bankApp.addMessage(Messages.AGE_UNDER_18);
b.setCash(40000);
end
rule "Check Cash"
when
b : Buyer (cash <5000)
then
System.out.println("[Rule.drl] Checking cash");
System.out.println("[Rule.drl] cash is " + b.getCash());
bankApp.addMessage(Messages.CASH_LOW );
end
rule "Check Income"
when
b : Buyer (yearlyIncome <50000)
then
System.out.println("[Rule.drl] Checking income");
bankApp.addMessage(Messages.INCOME_LOW );
end
And facts are passed to the Working memory this way:
Buyer buyer = new Buyer();
BankApplication bankApp = new BankApplication();
buyer.setAge(15);
buyer.setCash(1000);
buyer.setYearlyIncome(30000);
workingMemory.setGlobal("bankApp", bankApp);
workingMemory.insert(buyer,true);
workingMemory.fireAllRules();
Looking at the log file:
14:23:47,109 INFO [STDOUT] [Buyer ] Setted cash with 1000.0
14:23:47,109 INFO [STDOUT] [Rule.drl] Checking income
14:23:47,125 INFO [STDOUT] [Rule.drl] Checking cash
14:23:47,125 INFO [STDOUT] [Rule.drl] cash is 1000.0
14:23:47,125 INFO [STDOUT] [Rule.drl] Checking age
14:23:47,125 INFO [STDOUT] [Buyer ] Setted cash with 40000.0
14:23:47,125 INFO [STDOUT] [Rule.drl] Checking income
14:23:47,125 INFO [STDOUT] [Rule.drl] Checking age
14:23:47,125 INFO [STDOUT] [Buyer ]Setted cash with 40000.0
I can see that the Rule "income" has been invoked twice: why if it doesn't
use the cash attribute in it ?
Thanks a lot for your time
Francesco
--
View this message in context: http://www.nabble.com/Beginner-question-about-Dynamic-Beans-tp18232282p18236520.html
Sent from the drools - user mailing list archive at Nabble.com.
More information about the rules-users
mailing list