The simplist solution is just to change your condition to be $a : Applicant(age < 18, valid != false)

 

Thomas

 

From: rules-users-bounces@lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On Behalf Of Esteban Aliverti
Sent: 20 January 2012 14:20
To: Rules Users List
Subject: Re: [rules-users] Infinit loop in simple example

 

import spikes.Applicant
rule "Is of valid age" when
   $a : Applicant( age < 18 )
then
   modify( $a ) { valid = false };
end

 

By using modify() you are telling the engine that $a was modified in some way. This causes the re-evaluation of your rule, and since $a.age is still < 18, then the rule is activated and fired infinite times.

Read the documentation manual about some attributes you can use in your rules to avoid this behavior: no-loop, lock-on-active.

 

Best Regards,

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Esteban Aliverti
- Developer @ http://www.plugtree.com
- Blog @ http://ilesteban.wordpress.com

On Fri, Jan 20, 2012 at 3:12 PM, roland.kofler <roland.kofler@gmail.com> wrote:

Simple example loops ad infinitum if executed:

package spikes;

import org.springframework.roo.addon.javabean.RooJavaBean;

@RooJavaBean
public class Applicant {
private final String string;
private final int age;
public boolean valid=true;

public Applicant(String string, int i) {
   this.string = string;
   this.age = i;
}
}
DroolsSpikeTest

package spikes;

import static org.junit.Assert.*;

import org.drools.KnowledgeBase;
import org.drools.runtime.StatelessKnowledgeSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:**/applicationContext-drools.xml"})
public class DroolsSpikeTest {

   @Autowired
   private KnowledgeBase kbase;

   @Test
   public void testspikeDroolRule() {
       StatelessKnowledgeSession ksession =
kbase.newStatelessKnowledgeSession();
       Applicant a = new Applicant("Mr John Smith", 16);
       assertTrue(a.isValid());
       ksession.execute(a);
       assertFalse(a.isValid());
   }

}
applicationContext-drools.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:drools="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://drools.org/schema/drools-spring
http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd">

<drools:kbase id="kbase1">
  <drools:resources>
   <drools:resource id="licenseRule" type="DRL"
source="classpath:spikes/licenseApplication.drl"/>
  </drools:resources>
  <drools:configuration>
      <drools:mbeans enabled="true" />
  </drools:configuration>
</drools:kbase>
<drools:ksession kbase="kbase1" type="stateless" id="ksessionStateless"
name="stateless1" >
</drools:ksession>
</beans>

licenseApplication.drl

import spikes.Applicant
rule "Is of valid age" when
   $a : Applicant( age < 18 )
then
   modify( $a ) { valid = false };
end

--
View this message in context: http://drools.46999.n3.nabble.com/Infinit-loop-in-simple-example-tp3675536p3675536.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users

 




**************************************************************************************
This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster@nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary.

NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00
**************************************************************************************