<span style>import spikes.Applicant</span><br style><span style>rule "Is of valid age" when</span><br style><span style> $a : Applicant( age < 18 )</span><br style><span style>then</span><br style><span style> modify( $a ) { valid = false };</span><br style>
<span style>end</span><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">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.</font></div>
<div><font color="#222222" face="arial, sans-serif">Read the documentation manual about some attributes you can use in your rules to avoid this behavior: no-loop, lock-on-active.</font></div><div><font color="#222222" face="arial, sans-serif"><br>
</font></div><div><font color="#222222" face="arial, sans-serif">Best Regards,<br clear="all"></font><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br><br>Esteban Aliverti<br>- Developer @ <a href="http://www.plugtree.com" target="_blank">http://www.plugtree.com </a><br>
- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
<br><br><div class="gmail_quote">On Fri, Jan 20, 2012 at 3:12 PM, roland.kofler <span dir="ltr"><<a href="mailto:roland.kofler@gmail.com">roland.kofler@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Simple example loops ad infinitum if executed:<br>
<br>
package spikes;<br>
<br>
import org.springframework.roo.addon.javabean.RooJavaBean;<br>
<br>
@RooJavaBean<br>
public class Applicant {<br>
private final String string;<br>
private final int age;<br>
public boolean valid=true;<br>
<br>
public Applicant(String string, int i) {<br>
this.string = string;<br>
this.age = i;<br>
}<br>
}<br>
DroolsSpikeTest<br>
<br>
package spikes;<br>
<br>
import static org.junit.Assert.*;<br>
<br>
import org.drools.KnowledgeBase;<br>
import org.drools.runtime.StatelessKnowledgeSession;<br>
import org.junit.Test;<br>
import org.junit.runner.RunWith;<br>
import org.springframework.beans.factory.annotation.Autowired;<br>
import org.springframework.test.context.ContextConfiguration;<br>
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;<br>
<br>
@RunWith(SpringJUnit4ClassRunner.class)<br>
@ContextConfiguration(locations={"classpath:**/applicationContext-drools.xml"})<br>
public class DroolsSpikeTest {<br>
<br>
@Autowired<br>
private KnowledgeBase kbase;<br>
<br>
@Test<br>
public void testspikeDroolRule() {<br>
StatelessKnowledgeSession ksession =<br>
kbase.newStatelessKnowledgeSession();<br>
Applicant a = new Applicant("Mr John Smith", 16);<br>
assertTrue(a.isValid());<br>
ksession.execute(a);<br>
assertFalse(a.isValid());<br>
}<br>
<br>
}<br>
applicationContext-drools.xml<br>
<br>
<?xml version="1.0" encoding="UTF-8" standalone="no"?><br>
<br>
<beans xmlns="<a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a>"<br>
xmlns:aop="<a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a>"<br>
xmlns:context="<a href="http://www.springframework.org/schema/context" target="_blank">http://www.springframework.org/schema/context</a>"<br>
xmlns:jee="<a href="http://www.springframework.org/schema/jee" target="_blank">http://www.springframework.org/schema/jee</a>"<br>
xmlns:tx="<a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a>"<br>
xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a>"<br>
xmlns:drools="<a href="http://drools.org/schema/drools-spring" target="_blank">http://drools.org/schema/drools-spring</a>"<br>
xsi:schemaLocation="<a href="http://www.springframework.org/schema/aop" target="_blank">http://www.springframework.org/schema/aop</a><br>
<a href="http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" target="_blank">http://www.springframework.org/schema/aop/spring-aop-3.1.xsd</a><br>
<a href="http://www.springframework.org/schema/beans" target="_blank">http://www.springframework.org/schema/beans</a><br>
<a href="http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" target="_blank">http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</a><br>
<a href="http://www.springframework.org/schema/context" target="_blank">http://www.springframework.org/schema/context</a><br>
<a href="http://www.springframework.org/schema/context/spring-context-3.1.xsd" target="_blank">http://www.springframework.org/schema/context/spring-context-3.1.xsd</a><br>
<a href="http://www.springframework.org/schema/jee" target="_blank">http://www.springframework.org/schema/jee</a><br>
<a href="http://www.springframework.org/schema/jee/spring-jee-3.1.xsd" target="_blank">http://www.springframework.org/schema/jee/spring-jee-3.1.xsd</a><br>
<a href="http://www.springframework.org/schema/tx" target="_blank">http://www.springframework.org/schema/tx</a><br>
<a href="http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" target="_blank">http://www.springframework.org/schema/tx/spring-tx-3.1.xsd</a><br>
<a href="http://drools.org/schema/drools-spring" target="_blank">http://drools.org/schema/drools-spring</a><br>
<a href="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" target="_blank">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</a>"><br>
<br>
<drools:kbase id="kbase1"><br>
<drools:resources><br>
<drools:resource id="licenseRule" type="DRL"<br>
source="classpath:spikes/licenseApplication.drl"/><br>
</drools:resources><br>
<drools:configuration><br>
<drools:mbeans enabled="true" /><br>
</drools:configuration><br>
</drools:kbase><br>
<drools:ksession kbase="kbase1" type="stateless" id="ksessionStateless"<br>
name="stateless1" ><br>
</drools:ksession><br>
</beans><br>
<br>
licenseApplication.drl<br>
<br>
import spikes.Applicant<br>
rule "Is of valid age" when<br>
$a : Applicant( age < 18 )<br>
then<br>
modify( $a ) { valid = false };<br>
end<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/Infinit-loop-in-simple-example-tp3675536p3675536.html" target="_blank">http://drools.46999.n3.nabble.com/Infinit-loop-in-simple-example-tp3675536p3675536.html</a><br>
Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></span></blockquote></div><br></div>