<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">That’s a balancing act I’m struggling with. I’m trying to make it a reasonably full-featued web application, but at the same time, I’m trying to keep it simple enough to use for demonstrating things. Unfortunately, those aims often feel mutually exclusive. But I certainly need to add more explanatory comments. There are some areas of the application where I haven’t done much commenting.</div><div class=""><br class=""></div><div class="">This is the controller class with the @Controller and @RequestMapping annotations in it:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/validation/web/PaymentValidationControllerImpl.java" class="">https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/validation/web/PaymentValidationControllerImpl.java</a></div><div class=""><br class=""></div><div class="">You should see in there that there is a mapping for an IBAN validation request:</div></div><div class=""><br class=""></div><div class=""><pre style="font-family: Consolas, 'Liberation Mono', Courier, monospace; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); line-height: 18px;" class=""><div id="LC56" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="nd">@Override</span></div><div id="LC57" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="nd">@RequestMapping</span><span style="font-weight: bold;" class="o">(</span><span class="n">value</span> <span style="font-weight: bold;" class="o">=</span> <span style="color: rgb(221, 17, 68);" class="s">"/iban/validate/{iban}"</span><span style="font-weight: bold;" class="o">,</span> <span class="n">method</span> <span style="font-weight: bold;" class="o">=</span> <span class="n">RequestMethod</span><span style="font-weight: bold;" class="o">.</span><span style="color: rgb(0, 128, 128);" class="na">GET</span><span style="font-weight: bold;" class="o">,</span> <span class="n">produces</span> <span style="font-weight: bold;" class="o">=</span> <span style="color: rgb(221, 17, 68);" class="s">"application/json"</span><span style="font-weight: bold;" class="o">)</span></div><div id="LC58" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold;" class="kd">public</span> <span class="nd">@ResponseBody</span></div><div id="LC59" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">IbanValidationResult</span> <span style="color: rgb(153, 0, 0); font-weight: bold;" class="nf">validateIban</span><span style="font-weight: bold;" class="o">(</span><span class="nd">@PathVariable</span> <span class="n">String</span> <span class="n">iban</span><span style="font-weight: bold;" class="o">)</span> <span style="font-weight: bold;" class="o">{</span></div><div id="LC60" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">IbanValidationResult</span> <span class="n">result</span> <span style="font-weight: bold;" class="o">=</span> <span class="n">ibanValidator</span><span style="font-weight: bold;" class="o">.</span><span style="color: rgb(0, 128, 128);" class="na">validateIban</span><span style="font-weight: bold;" class="o">(</span><span class="n">iban</span><span style="font-weight: bold;" class="o">);</span></div><div id="LC61" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">log</span><span style="font-weight: bold;" class="o">.</span><span style="color: rgb(0, 128, 128);" class="na">debug</span><span style="font-weight: bold;" class="o">(</span><span style="color: rgb(221, 17, 68);" class="s">"Validated IBAN: "</span> <span style="font-weight: bold;" class="o">+</span> <span class="n">result</span><span style="font-weight: bold;" class="o">.</span><span style="color: rgb(0, 128, 128);" class="na">toString</span><span style="font-weight: bold;" class="o">());</span></div><div id="LC62" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold;" class="k">return</span> <span class="n">result</span><span style="font-weight: bold;" class="o">;</span></div><div id="LC63" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight: bold;" class="o">}</span></div></pre><div class=""><br class=""></div></div><div class="">That invokes an IbanValidator, which in this case has been injected with the “ruleBasedIbanValidator”:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/validation/iban/RuleBasedIbanValidator.java" class="">https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/java/com/sctrcd/payments/validation/iban/RuleBasedIbanValidator.java</a></div><div class=""><br class=""></div><div class="">That particular validator is a Spring @Service. It constructs a knowledge base in its constructor, because Spring will instantiate it on start-up, and ensure that there is only one instance.</div><div class=""><br class=""></div><div class="">The validateIban(iban) method in the RuleBasedIbanValidator creates a new stateless session for the knowledge base. It creates an IbanValidationRequest fact, which it inserts into the session and causes rules to fire.</div><div class=""><br class=""></div><div class="">As you might see in the constructor for the IBAN validator, the rules are in here:</div><div class=""><a href="https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/resources/rules/payments/validation/IbanRules.drl" class="">https://github.com/gratiartis/sctrcd-payment-validation-web/blob/master/src/main/resources/rules/payments/validation/IbanRules.drl</a></div><div class=""><br class=""></div><div class="">That contains rules such as this:</div><div class=""><br class=""></div><div class=""><pre style="font-family: Consolas, 'Liberation Mono', Courier, monospace; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); line-height: 18px;" class=""><div id="LC20" style="padding-left: 10px;" class="line">rule "IBAN failed the Mod-97 checksum test."</div><div id="LC21" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;salience 100</div><div id="LC22" style="padding-left: 10px;" class="line">when</div><div id="LC23" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;$req: IbanValidationRequest($iban:iban)</div><div id="LC24" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;not PaymentValidationAnnotation(</div><div id="LC25" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attribute == PaymentAttribute.iban,</div><div id="LC26" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;level == AnnotationLevel.REJECT, </div><div id="LC27" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruleName == "IBAN failed the Mod-97 checksum test."</div><div id="LC28" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;)</div><div id="LC29" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;eval(!IbanMod97Check.isValid($iban))</div><div id="LC30" style="padding-left: 10px;" class="line">then</div><div id="LC31" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;insert(</div><div id="LC32" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new PaymentValidationAnnotation(</div><div id="LC33" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drools.getRule().getName(),</div><div id="LC34" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnnotationLevel.REJECT, </div><div id="LC35" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"The IBAN is not valid.", </div><div id="LC36" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PaymentAttribute.iban</div><div id="LC37" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)</div><div id="LC38" style="padding-left: 10px;" class="line">&nbsp;&nbsp;&nbsp;&nbsp;);</div><div id="LC39" style="padding-left: 10px;" class="line">end</div></pre><div class=""><br class=""></div></div><div class="">That rule will activate if the IBAN is not valid according to a standard "Mod-97" check. In the case of it activating, it will insert a PaymentValidationAnnotation fact. This is a simple object to note that the request has been rejected, and indicates why.</div><div class=""><br class=""></div><div class="">Whenever an annotation is inserted, the “Derive most severe IBAN annotation” rule might fire. This will update the “mostSevereAnnotation” property of the original IbanValidationRequest. Similarly, the “RejectIBAN request if there is an IBAN rejection annotation” rule will set the “isValid” property to false if the request has has a rejection raised against it.</div><div class=""><br class=""></div><div class="">Going back to the RuleBasedIbanValidator, once the rules have executed, all I do is look at the IbanValdiationRequest that was inserted into the session. I use the contents of that to create an IbanValidationResult which just has a flag to indicate whether it is valid and contains a list of annotations. That gets returned to the controller.</div><div class=""><br class=""></div><div class="">Finally the controller, returns the IbanValidationResult. Because it has a @ResponseBody annotation and the project includes a dependency on Jackson, that result will be converted to JSON automatically. Hence, firing a valid IBAN at the service, I’ll get:</div><div class=""><br></div><div class="">{"annotations":[],"iban":"GB29NWBK60161331926819","valid":true}</div><div class=""><br></div><div class="">… and firing an invalid IBAN gives me:</div><div class=""><br></div><div class="">{"annotations":[],"iban":"GB29NWBK6016133192681","valid”:false}</div><div class=""><br></div><div class="">… which shows that it’s not working right! It’s working out that the IBAN is incorrect, but the response is not including the annotations. Time for me to do a little bit of debugging. :)</div><div class=""><br></div><div class="">Anyway, I hope that’s a useful walkthrough. Feel free to fire more questions at me.</div><div class=""><br class=""></div><div class="">Steve&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div><div class="">On 9 Nov 2013, at 11:28, forsakendoll &lt;<a href="mailto:forsakendoll@hotmail.com">forsakendoll@hotmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">I'm already reading your code while waiting for response here. Actually Its a<br class="">bit big project for me to start and I'm also new to Spring MVC so its really<br class="">hard for me to read this code too. I'm using annotation for controllers and<br class="">I don't see one in yours. Correct me if I'm wrong what I'm expecting is:<br class=""><br class="">1. From the user interaction through the page it will send a request to the<br class="">controller.<br class="">2. From the controller a bean will be send to the .drl file which have<br class="">rules.<br class="">3. Update or send back some data to the controller ( this one is I don't<br class="">really have the idea of how can I do this )<br class="">4. Add to the model attribute the data that has been send by .drl file.<br class=""><br class="">Please correct me if I'm wrong. And please guide me a bit more to this.<br class=""><br class=""><br class="">Stephen Masters wrote<br class=""><blockquote type="cite">Given that you’re using Spring MVC, this might be a reasonable example:<br class=""><br class=""><span style="white-space: pre;" class="Apple-tab-span">        </span><a href="https://github.com/gratiartis/sctrcd-payment-validation-web">https://github.com/gratiartis/sctrcd-payment-validation-web</a><br class=""><br class="">It has examples of doing this for a simple validation, with a stateless<br class="">session.<br class=""><br class="">i.e.<br class="">Request goes to Spring MVC controller.<br class="">Controller invokes a service bean, which wraps a knowledge base.<br class="">Rule based validator inserts a fact and executes rules.<br class="">Rules may or may not set an ‘isValid’ flag on the request. They may (or<br class="">may not) also annotate that request fact to indicate which rules are<br class="">rejecting it and why.<br class="">The rule based validator returns a result object to the service.<br class="">The service returns a result object to the controller.<br class="">The controller returns a JSON object to the client.<br class=""><br class="">Hopefully it’s not too difficult to find your way around the project.<br class=""><br class="">To try it out:<br class="">git clone https://github.com/gratiartis/sctrcd-payment-validation-web<br class="">cd sctrcd-payment-validation-web<br class="">mvn clean install tomcat7:run<br class="">curl http://localhost:9090/iban/validate/GB29NWBK60161331926819<br class="">Change a couple of characters in the IBAN on another curl to see a<br class="">rejection.<br class=""><br class="">btw - If anybody else fancies taking a look at it, please feel free to<br class="">send criticism back to me. Either directly or to discuss on the mailing<br class="">list. There’s so little out there in the way of documented good practice<br class="">for using Drools, that I get the impression that everyone just finds their<br class="">own way. So I would be happy to hear what others feel could be done to<br class="">improve my little Spring MVC/Drools demo project.<br class=""><br class="">Steve<br class=""><br class=""><br class="">On 9 Nov 2013, at 09:24, forsakendoll &amp;lt;<br class=""></blockquote><br class=""><blockquote type="cite">forsakendoll@<br class=""></blockquote><br class=""><blockquote type="cite">&amp;gt; wrote:<br class=""><br class=""><blockquote type="cite">I'm very new to drools. I know this question is really a noob question<br class="">but<br class="">please bear with me. I'm using Spring MVC and I want to integrate drools<br class="">expert to my project. What I've done so far is to integrate the hello<br class="">world<br class="">sample of drools expert. But now what I want to do is:<br class=""><br class="">1. Send a bean to the rules for it to evaluate.<br class="">2. Modify the bean depending on the rules<br class="">3. Send it back to the controller to make a response to the user.<br class=""><br class="">I was able to do the number 1. But for number 2 and 3. I don't know how<br class="">to<br class="">do it. I want to have a nested rule. But now I'm only capable of doing<br class="">this<br class="">rule:<br class=""><br class="">global String $test;<br class=""><br class="">rule "Excellent"<br class=""><br class="">&nbsp;&nbsp;when<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$m: FLTBean ( listeningScore &gt; 85 )<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$p: FLTBean ( listeningScore &lt; 101 )<br class="">&nbsp;&nbsp;then<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$test = "Excellent";<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println( $test );<br class=""><br class="">end<br class=""><br class="">I don't know yet how can I make a nested rule. Please give me a simple<br class="">example that a newbie like me can understand.<br class=""><br class=""><br class=""><br class=""><br class="">--<br class="">View this message in context:<br class=""><a href="http://drools.46999.n3.nabble.com/Drools-in-a-web-application-tp4026704.html" class="">http://drools.46999.n3.nabble.com/Drools-in-a-web-application-tp4026704.html</a><br class="">Sent from the Drools: User forum mailing list archive at<span class="Apple-converted-space">&nbsp;</span><a href="http://nabble.com/" class="">Nabble.com</a>.<br class="">_______________________________________________<br class="">rules-users mailing list<br class=""><br class=""></blockquote></blockquote><br class=""><blockquote type="cite">rules-users@.jboss<br class=""></blockquote><br class=""><blockquote type="cite"><blockquote type="cite"><a href="https://lists.jboss.org/mailman/listinfo/rules-users" class="">https://lists.jboss.org/mailman/listinfo/rules-users</a><br class=""></blockquote><br class=""><br class="">_______________________________________________<br class="">rules-users mailing list<br class=""></blockquote><br class=""><blockquote type="cite">rules-users@.jboss<br class=""></blockquote><br class=""><blockquote type="cite"><a href="https://lists.jboss.org/mailman/listinfo/rules-users" class="">https://lists.jboss.org/mailman/listinfo/rules-users</a><br class=""></blockquote><br class=""><br class=""><br class=""><br class=""><br class="">--<br class="">View this message in context:<span class="Apple-converted-space">&nbsp;</span><a href="http://drools.46999.n3.nabble.com/Drools-in-a-web-application-tp4026704p4026707.html" class="">http://drools.46999.n3.nabble.com/Drools-in-a-web-application-tp4026704p4026707.html</a><br class="">Sent from the Drools: User forum mailing list archive at<span class="Apple-converted-space">&nbsp;</span><a href="http://nabble.com/" class="">Nabble.com</a>.<br class=""><br class="">_______________________________________________<br class="">rules-users mailing list<br class=""><a href="mailto:rules-users@lists.jboss.org" class="">rules-users@lists.jboss.org</a><br class=""><a href="https://lists.jboss.org/mailman/listinfo/rules-users" class="">https://lists.jboss.org/mailman/listinfo/rules-users</a></div></blockquote></div><br class=""></body></html>