The no loop goes inside each rule
- Ing. Mauricio Salatino -
On Nov 29, 2009, at 6:00 AM, Sinboy Zhang <iamsinboy(a)gmail.com> wrote:
i expect the result is 530,and define the "no-loop=true" in
the
rules ,but result is 460,why?
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
---
--------------------------------------------------------------------
my java test code:
package org.sample;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class SalesTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger =
KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Sales message = new Sales();
message.setName("sinboy");
message.setSales(600);
ksession.insert(message);
ksession.fireAllRules();
//ksession.dispose();
System.out.println("updated:"+message.getName()
+","+message.getSales());
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
//kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
ResourceType.DRL);
kbuilder.add(ResourceFactory.newUrlResource("
http://127.0.0.1:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/org....
"), ResourceType.PKG);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
}
the print info is:
fired1:discount,550
fired0:discount,530
fired1:discount,480
fired0:discount,460
updated:discount,460
---------------------------------------------------------------------
my java model is :
package org.sample;
import java.util.Date;
public class Sales {
private String name;
private long sales;
private Date dateOfSale;
private boolean chocolateOnlyCustomer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSales() {
return sales;
}
public void setSales(long sales) {
this.sales = sales;
}
public Date getDateOfSale() {
return dateOfSale;
}
public void setDateOfSale(Date dateOfSale) {
this.dateOfSale = dateOfSale;
}
public boolean isChocolateOnlyCustomer() {
return chocolateOnlyCustomer;
}
public void setChocolateOnlyCustomer(boolean choclateOnlyCustomer) {
this.chocolateOnlyCustomer = chocolateOnlyCustomer;
}
}
rules:
dialect "mvel"
no-loop true
rule "DiscountRule"
salience 9
when
$fact0:Sales(s:sales>100)
then
#Sales fact0=new Sales();
#fact0.setSales(s.getSales()-10);
#fact0.setName("discount");
#insert(fact0);
modify($fact0){setName("discount")}
modify($fact0){setSales(s-20)}
System.out.println("fired0:"+$fact0.getName()+
","+$fact0.getSales());
end
rule "DiscountRule2"
salience 18
when
$fact1:Sales(s1:sales>500)
then
modify($fact1){setName("discount")}
modify($fact1){setSales(s1-50)}
System.out.println("fired1:"+$fact1.getName()+
","+$fact1.getSales());
end
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users