<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.apple-style-span
        {mso-style-name:apple-style-span;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>You
need no-loop on the rules because of the call to “modify(s)” . Modify changes
the fact in working memory and </span><span class=apple-style-span><span
style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>the engine is
automatically notified of the changes at the end of the modify block</span></span><span
style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>. After modify the
same rule could hit again as the conditions are still true.<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'>The
first rule doesn’t need the no-loop it because of the check “</span>recommendPurchase
== null”<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Joe<span style='font-size:11.0pt;font-family:"Calibri","sans-serif"'><o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p> </o:p></span></p>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>
<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>
rules-users-bounces@lists.jboss.org
[mailto:rules-users-bounces@lists.jboss.org] <b>On Behalf Of </b>Bryan Hansen<br>
<b>Sent:</b> Sunday, November 02, 2008 8:21 PM<br>
<b>To:</b> rules-users@lists.jboss.org<br>
<b>Subject:</b> [rules-users] no-loop usage question<o:p></o:p></span></p>
</div>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>I have been reading through the documentation and trying
some of the tutorials and have a question about the no-loop attribute. <br>
<br>
Not really relevant to the post, but I figured I would mention it, I have been
following this post and updating it to use the current version of Drools. <a
href="http://www.onjava.com/pub/a/onjava/2005/08/03/drools.html?page=7">http://www.onjava.com/pub/a/onjava/2005/08/03/drools.html?page=7</a>
<br>
<br>
I have written my rules as such:<br>
<br>
package org.drools.examples<br>
<br>
import org.drools.examples.StockOffer;<br>
<br>
rule PriceBelow100<br>
dialect "java" <br>
when<br>
s: StockOffer( stockPrice < 100
&& recommendPurchase == null )<br>
then<br>
modify( s ) {<br>
setRecommendPurchase(StockOffer.YES)<br>
}<br>
end<br>
<br>
rule NegativeStockPrice<br>
dialect "java" <br>
no-loop true<br>
when<br>
s: StockOffer( stockPrice < 0 )<br>
then<br>
modify( s ) {<br>
setRecommendPurchase(StockOffer.NO)<br>
}<br>
end <br>
<br>
rule DontBuyXYZStock<br>
dialect "java" <br>
no-loop true<br>
when<br>
s: StockOffer(stockName ==
"XYZ" && stockPrice < 10 )<br>
then<br>
modify( s ) {<br>
setRecommendPurchase(StockOffer.YES)<br>
}<br>
end <br>
<br>
rule BuyXYZStock<br>
dialect "java" <br>
no-loop true<br>
when<br>
s: StockOffer(stockName ==
"XYZ" && stockPrice > 10 )<br>
then<br>
modify( s ) {<br>
setRecommendPurchase(StockOffer.NO)<br>
}<br>
end <br>
<br>
<br>
<br>
My question is, why do I have to put no-loop on every rule (except for the
first). After the first time through the rules, it shouldn't be modifying to
where the rules would fire it again. Am I doing something wrong with how my
rules are written? My sample data is this:<br>
<br>
package org.drools.examples;<br>
<br>
import org.drools.examples.BusinessLayer;<br>
import org.drools.examples.StockOffer;<br>
<br>
import junit.framework.TestCase;<br>
<br>
/*<br>
* JUnit test for the business rules in the <br>
* application.<br>
* <br>
* This also acts a 'simulator' for the business <br>
* rules - allowing us to specify the inputs,<br>
* examine the outputs and see if they match our <br>
* expectations before letting the code loose in <br>
* the real world.<br>
*/<br>
public class BusinessRuleTest extends TestCase {<br>
/**<br>
* Tests the purchase of a stock<br>
*/<br>
public void testStockBuy() throws Exception {<br>
<br>
// Create a Stock with simulated values<br>
StockOffer testOffer = new StockOffer();<br>
testOffer.setStockName("MEGACORP");<br>
testOffer.setStockPrice(22);<br>
testOffer.setStockQuantity(1000);<br>
<br>
// Run the rules on it<br>
BusinessLayer.evaluateStockPurchase(testOffer);<br>
<br>
// Is it what we expected?<br>
assertTrue(testOffer.getRecommendPurchase() != null);<br>
<br>
assertTrue("YES".equals(testOffer.getRecommendPurchase()));<br>
}<br>
<br>
/**<br>
* Tests the purchase of a stock makes sure the system
will not accept<br>
* negative numbers.<br>
*/<br>
public void testNegativeStockBuy() throws Exception {<br>
<br>
// Create a Stock with our simulated
values<br>
StockOffer testOffer = new StockOffer();<br>
testOffer.setStockName("MEGACORP");<br>
testOffer.setStockPrice(-22);<br>
testOffer.setStockQuantity(1000);<br>
<br>
// Run the rules on it<br>
BusinessLayer.evaluateStockPurchase(testOffer);<br>
<br>
// Is it what we expected?<br>
assertTrue("NO".equals(testOffer.getRecommendPurchase()));<br>
}<br>
<br>
/**<br>
* Makes sure the system will buy stocks of XYZ corp
only if it really cheap<br>
*/<br>
public void testXYZStockBuy() throws Exception {<br>
<br>
// Create a Stock with our simulated
values<br>
StockOffer testOfferLow = new
StockOffer();<br>
StockOffer testOfferHigh = new
StockOffer();<br>
<br>
testOfferLow.setStockName("XYZ");<br>
testOfferLow.setStockPrice(9);<br>
testOfferLow.setStockQuantity(1000);<br>
<br>
testOfferHigh.setStockName("XYZ");<br>
testOfferHigh.setStockPrice(11);<br>
testOfferHigh.setStockQuantity(1000);<br>
<br>
// Run the rules on it and test<br>
BusinessLayer.evaluateStockPurchase(testOfferLow);<br>
assertTrue("YES".equals(testOfferLow.getRecommendPurchase()));<br>
<br>
BusinessLayer.evaluateStockPurchase(testOfferHigh);<br>
assertTrue("NO".equals(testOfferHigh.getRecommendPurchase()));<br>
}<br>
}<br>
<br>
<br>
I can post up all the code if this isn't enough to answer the question.<br>
<br>
Thanks!<o:p></o:p></p>
</div>
</body>
</html>