Trouble with insertlogical
by XIAOTAO
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
17 years, 10 months
Can only reason over sub-objects if you use the From Conditional Element
by Aaron Dixon
It appears that you MUST use the From Condition Element to reason over
sub-objects.
I have a Person class. Person::getDetails() returns a Details
instance, which has the name and age of the person. (This is a
contrived example to demonstrate the issue.)
My rules are:
rule "30 is the new 20"
when
person : Person( details.age == 30 )
then
person.getDetails().setAge(20);
System.out.println( "Now 20 : " + person );
update( person );
end
rule "Older than 20 - Good"
salience -100
when
person : Person( )
Details( age > 20 ) from person.details
then
System.out.println( "Older than 20 (good) : " + person );
end
rule "Older than 20 - Bad"
salience -100
when
person : Person( details.age > 20 )
then
System.out.println( "Older than 20 (bad) : " + person );
end
I assert Abe, Bob, Cat, Don, and Eve with ages of 10, 20, 30, 40, and
50, respectively. The output is as follows.
Now 20 : Person(Details(Cat,20))
Older than 20 (good) : Person(Details(Eve,50))
Older than 20 (bad) : Person(Details(Eve,50))
Older than 20 (good) : Person(Details(Don,40))
Older than 20 (bad) : Person(Details(Don,40))
Older than 20 (bad) : Person(Details(Cat,20))
You can see that the "Bad" rule is more concise but it does not use
the From Conditional Element and therefore it doesn't work properly
(Cat is determined to be older than 20 when she is not.)
Why does Drools allow the "Bad" rule to be written and compiled when
it does not behave properly?
Thanks,
Aaron
17 years, 10 months
Re: drools 4.x branch build problems
by jack wu
i was on maven 2.0.7
all passed after upgrading to maven 2.0.8. many thanks.
jack.
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
17 years, 10 months
Re: drools 4.x branch build problems
by jack wu
tested again with 1.5.0_03. failed.
D:\download\drools4x\4.0.x>java -version
java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Client VM (build 1.5.0_03-b07, mixed mode)
Running org.drools.integrationtests.DynamicRulesTest
org.drools.RuntimeDroolsException: Unable to load dialect 'org.drools.rule.builder.dialect.java.Java
DialectConfiguration:java'
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:160)
at org.drools.compiler.PackageBuilderConfiguration.buildDialectConfigurationMap(PackageBuild
erConfiguration.java:146)
at org.drools.compiler.PackageBuilderConfiguration.init(PackageBuilderConfiguration.java:121
)
at org.drools.compiler.PackageBuilderConfiguration.<init>(PackageBuilderConfiguration.java:9
8)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:124)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:86)
at org.drools.integrationtests.DynamicRulesTest.testDynamicRulesAddRemove(DynamicRulesTest.j
ava:540)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirecto
ryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestS
uite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:33
8)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: java.lang.ClassCastException: org.drools.rule.builder.dialect.java.JavaDialectConfigurati
on
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:155)
... 31 more
org.drools.RuntimeDroolsException: Unable to load dialect 'org.drools.rule.builder.dialect.java.Java
DialectConfiguration:java'
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:160)
at org.drools.compiler.PackageBuilderConfiguration.buildDialectConfigurationMap(PackageBuild
erConfiguration.java:146)
at org.drools.compiler.PackageBuilderConfiguration.init(PackageBuilderConfiguration.java:121
)
at org.drools.compiler.PackageBuilderConfiguration.<init>(PackageBuilderConfiguration.java:9
8)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:124)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:86)
at org.drools.integrationtests.DynamicRulesTest.testRuleBaseAddRemoveSubNetworks(DynamicRule
sTest.java:768)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirecto
ryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestS
uite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Tests run: 16, Failures: 2, Errors: 7, Skipped: 0, Time elapsed: 1.344 sec <<< FAILURE!
Running org.drools.brms.modeldriven.ActionFieldValueTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:33
8)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: java.lang.ClassCastException: org.drools.rule.builder.dialect.java.JavaDialectConfigurati
on
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:155)
... 31 more
Running org.drools.brms.server.util.SuggestionCompletionEngineBuilderTest
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
17 years, 10 months
Uploading the fact model to the DRools BRMS
by Felbecker, Tobias
Hi ,
I got the same problem and wonder if there is a solution for that yet.
I'am using
JBOSS: AS 4.2.2 GA
DBMR: 4.0.4
JAVA: 1.5.0_15
After uploading the jar, editing the import in the package configuration
and finally validating the package I get the following exception:
java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at
org.jboss.seam.util.Reflections.invoke(Reflections.java:21) at.....
The import statement should and the packaging of the jar file seems to
be correct. Otherwise there would be a Class not found error but no
exception.
I tried all kinds of packaging: binaries, source and binaries, creating
the jar through eclipse and from cmd.
This is my very simple java class:
package test;
public class MyBean{
public MyBean(){
}
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
Cheers Tobi
17 years, 10 months
Re: drools 4.x branch build problems
by jack wu
i was using 1.5.0_14.
D:\download\drools4x\4.0.x>java -version
java version "1.5.0_14"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_14-b03)
Java HotSpot(TM) Client VM (build 1.5.0_14-b03, mixed mode)
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.
17 years, 10 months
drools 4.x branch build problems
by jack wu
i checked out the 4.x branch this afternoon but was not able to build it.
Failed tests:
testDynamicRulesAddRemove(org.drools.integrationtests.DynamicRulesTest)
testRuleBaseAddRemoveSubNetworks(org.drools.integrationtests.DynamicRulesTest)
Tests run: 621, Failures: 2, Errors: 33, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
Please refer to D:\download\drools4x\4.0.x\drools-compiler\target\surefire-reports for the individual test results.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 minutes 7 seconds
[INFO] Finished at: Mon Mar 17 17:12:13 PDT 2008
[INFO] Final Memory: 7M/38M
[INFO] ------------------------------------------------------------------------
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
17 years, 10 months
Missing Values for a Dymanic Rule......
by Nikhil_dev
rule "Rule_decimal_1"
dialect "mvel"
when
$a : ApplyRuleBean()
m : ApplyRuleBean ( eval ( ( adultFare + childFare ) < 500 ) )
then
$a.GrossFare = ( $a.NettPayable * 0.10 )
end
Consider the above Rule it has been dynamically generated.....
when i supply values to the above Rule, proper calculated Gross Fare is
given as output
if i dont provide any one of the variable values.... then no error is given
from drool......
the process gets terminated..... is there any ways to identify which values
are not provided in the Rule....
I hope my query is understood.... and waiting for reply...
-----
Regards,
:working:Nikhil
--
View this message in context: http://www.nabble.com/Missing-Values-for-a-Dymanic-Rule......-tp16091335p...
Sent from the drools - user mailing list archive at Nabble.com.
17 years, 10 months
Using memberOf in Decision Tables
by Mike Love
Hi,
I have the following rules that I am trying to implement in a decision table
rule "Payment greater 5m"
agenda-group "txnPricing"
salience 5
when
payment : Collection() from bc.getPaymentTransactionTypes()
t : Transaction(customer == c,
transactionType memberOf payment,
transactionAmount >= 5000000)
then
t.setBaseFee(36.00);
t.setFeeDiscount(0.00);
end
rule "Payment less 5m"
agenda-group "txnPricing"
salience 5
when
payment : Collection() from bc.getPaymentTransactionTypes()
t : Transaction(customer == c,
transactionType memberOf payment,
transactionAmount < 5000000)
then
t.setBaseFee(60.00);
t.setFeeDiscount(0.00);
end
I have attached the Decision Table for reference. Basically, I have two
condition columns representing the 2 conditions respectively. When the
xls is processed the generated DSL has the two statements reversed as
indicated below, and hence the exceptions.
package za.co.fnb.billing;
#generated from Decision Table
import java.util.Collection;
import za.co.fnb.billing.Customer;
import za.co.fnb.billing.Transaction;
import za.co.fnb.billing.BillingConstraints;
global Customer c;
global BillingConstraints bc;
#From row number: 12
rule "Payments_12"
when
t: Transaction(customer == c, t.transactionAmount >= 0,
t.transactionAmount <5000000, transactionType memberOf $payments)
$payment : Collection() from
bc.getTransactionTypesForGroup("Payments")
then
t.setBaseFee(60.00);
t.setFeeDiscount(0.00);
end
#From row number: 13
rule "Payments_13"
when
t: Transaction(customer == c, t.transactionAmount >= 5000000,
t.transactionAmount <9999999999, transactionType memberOf $payments)
$payment : Collection() from
bc.getTransactionTypesForGroup("Payments")
then
t.setBaseFee(36.00);
t.setFeeDiscount(0.00);
end
org.drools.rule.InvalidRulePackage: Unable to return Declaration for
identifier '$payments' : [Rule name=Payments_12, agendaGroup=MAIN,
salience=0, no-loop=false]
Unable to create restriction '[VariableRestriction: memberOf $payments
]' for field 'transactionType' in the rule 'Payments_12' : [Rule
name=Payments_12, agendaGroup=MAIN, salience=0, no-loop=false]
Unable to return Declaration for identifier '$payments' : [Rule
name=Payments_13, agendaGroup=MAIN, salience=0, no-loop=false]
Unable to create restriction '[VariableRestriction: memberOf $payments
]' for field 'transactionType' in the rule 'Payments_13' : [Rule
name=Payments_13, agendaGroup=MAIN, salience=0, no-loop=false]
at org.drools.rule.Package.checkValidity(Package.java:424)
at
org.drools.common.AbstractRuleBase.addPackage(AbstractRuleBase.java:384)
at
za.co.fnb.billing.TxnVolDecisionTableTest.readDecisionTable(TxnVolDecisionTableTest.java:164)
at
za.co.fnb.billing.TxnVolDecisionTableTest.executeExample(TxnVolDecisionTableTest.java:62)
at
za.co.fnb.billing.TxnVolDecisionTableTest.main(TxnVolDecisionTableTest.java:33)
Thanks,
Mike
17 years, 10 months
Interesting null pointer exception when inserting a fact.
by Jason Partyka
Hi,
This is in relation to drools 4.0.4
I have an interesting problem. I am getting a null pointer exception when I am inserting a fact into a StatefulSession object. What is odd about this NPE is that, as far as I can tell (and I have inserted a breakpoint right before I insert the fact) that all the properties in the object are initialized, and there are no rules accessing any thing that could be null.
So here's my exception trace (just first few lines to get context):
Exception in thread "AWT-EventQueue-0" org.drools.RuntimeDroolsException: java.lang.NullPointerException
at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:76)
at org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:145)
at org.drools.reteoo.SingleTupleSinkAdapter.createAndPropagateAssertTuple(SingleTupleSinkAdapter.java:55)
at org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
at org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(SingleObjectSinkAdapter.java:22)
at org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:153)
at org.drools.reteoo.Rete.assertObject(Rete.java:177)
at org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
at org.drools.reteoo.ReteooWorkingMemory.doInsert(ReteooWorkingMemory.java:71)
at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:886)
at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:858)
at org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:659)
at com.hightower.drools.executablerules.Rules.setTemplate(Rules.java:112)
....
Caused by: java.lang.NullPointerException
at org.drools.base.mvel.MVELEvalExpression.evaluate(MVELEvalExpression.java:39)
at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:72)
... 39 more
(that setTemplate method is not a drools template)
Any ideas?
Thanks,
Jason
17 years, 10 months