Re: JESS: [EXTERNAL] nested foreach to return from the inner loop
"Jason Morris" <[email protected]> Thu, 23 May 2013 22:46:35 -0400
| Newsgroups | gmane.comp.java.jess |
|---|---|
| Message-ID | <CALQZ0BHfXnG+2MJ+nKZnZGbZFD0YvDC_fO0rvKR3GRzG1kMgnw@mail.gmail.com> |
--089e0141a0a877f59c04dd6dd015 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=utf-8 Did you try the (break)<http://www.jessrules.com/jess/docs/71/functions.html#break>function? Arguments:NoneReturns:N/ADescription: Immediately exit any enclosing loop or control scope. Can be used inside of for<http://www.jessrules.com/jess/docs/71/functions.html#for>, while <http://www.jessrules.com/jess/docs/71/functions.html#while>, and foreach <http://www.jessrules.com/jess/docs/71/functions.html#foreach>loops, as well as within the body of a deffunction<http://www.jessrules.com/jess/docs/71/constructs.html#deffunction>or the right hand side of a defrule <http://www.jessrules.com/jess/docs/71/constructs.html#defrule>. If called anywhere else, will throw an exception. *Jason C. Morris* President, Principal Consultant Morris Technical Solutions LLC President, Rules Fest Association Chairman, IntelliFest 2012: International Conference on Reasoning Technologies -------------------------------------------- phone: +01.517.376.8314 skype: jcmorris-mts email: [email protected] mybio: http://www.linkedin.com/in/jcmorris www.intellifest.org Invent * Innovate * Implement at IntelliFest! On Thu, May 23, 2013 at 12:43 PM, Przemyslaw Woznowski < [email protected]> wrote: > Hi Jess users, > > I have the following function: > > (deffunction similarity-score (?base ?other) > (bind ?score 0) > (bind ?totalDistance 0) > (bind ?counter 0) > (foreach ?x ?base > (bind ?counter (+ ?counter 1)) > (printout t "counter is:" ?counter) > (bind ?partialDistance 0) > (printout t "partialDist is:" ?partialDistance) > (foreach ?y ?other > (bind ?partialDistance (+ ?partialDistance 1)) > (printout t "foreach partialDist is:" ?partialDistance) > (if (eq ?x ?y) then > (bind ?score (+ ?score 1)) > (printout t "score is:" ?score) > (return) > ) > ) > (bind ?totalDistance (/ ?counter ?partialDistance)) > ) > (return (- (/ ?score (length$ ?base)) (* (- 1 (/ (length$ ?base) > ?totalDistance)) 0.1) )) > ) > > As you can see, the above function has a nested foreach loop, which upon > finding equal values in both list should skip to the next iteration of the > outer loop. In java, instead of (return) one would normally put a break > statement. As far as the documentation of the (foreach) construct reads, it > says: "The return function can be used to break the iteration". However, > what I am finding is that the (return) function terminates the outer loop > too - unless I seriously messed up the code - but this is the result of my > observation of the (printout t) function. Moreover, when I remove the > (return) function, the function iterates over the outer and inner loops > just fine. > > Any advice on how can I break the inner's loop iteration? I know I can use > a variable to flag that the match has been found and add it to the if > statement, but I am hoping that there is an equivalent of break in Jess. > > > Cheers, > Pete > --089e0141a0a877f59c04dd6dd015 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Did you try the <a href=3D"http://www.jessrules.com/jess/d= ocs/71/functions.html#break">(break)</a> function?<br><br><dl><dt>Arguments= :</dt><dd>None</dd><dt>Returns:</dt><dd>N/A</dd><dt>Description:</dt><dd> Immediately exit any enclosing loop or control scope. Can b= e used inside of <a href=3D"http://www.jessrules.com/jess/docs/71/functions= .html#for"><tt>for</tt></a>, <a href=3D"http://www.jessrules.com/jess/docs/= 71/functions.html#while"><tt>while</tt></a>, and <a href=3D"http://www.jessrules.com/jess/docs/71/functi= ons.html#foreach"><tt>foreach</tt></a> loops, as well as within the body of= a <a href=3D"http://www.jessrules.com/jess/docs/71/constructs.html#deffunc= tion"><tt>deffunction</tt></a> or the right hand side of a <a href=3D"http://www.jessrules.co= m/jess/docs/71/constructs.html#defrule"><tt>defrule</tt></a>. If called any= where else, will throw an exception.</dd></dl><br></div><div class=3D"gmail= _extra"> <br clear=3D"all"><div><font><b style=3D"font-family:trebuchet ms,sans-seri= f">Jason C. Morris</b><br style=3D"font-family:trebuchet ms,sans-serif"><sp= an style=3D"font-family:trebuchet ms,sans-serif">President, Principal Consu= ltant</span><br style=3D"font-family:trebuchet ms,sans-serif"> <span style=3D"font-family:trebuchet ms,sans-serif">Morris Technical Soluti= ons LLC<br></span></font><font style=3D"font-family:trebuchet ms,sans-serif= ">President, Rules Fest Association<br>Chairman, IntelliFest 2012: Internat= ional Conference on Reasoning Technologies</font><br style=3D"font-family:t= rebuchet ms,sans-serif"> <font><span style=3D"font-family:trebuchet ms,sans-serif"></span><span styl= e=3D"font-family:trebuchet ms,sans-serif">---------------------------------= -----------</span><br style=3D"font-family:trebuchet ms,sans-serif"></font>= <font style=3D"font-family:trebuchet ms,sans-serif">phone: +01.517.</font><= font style=3D"font-family:trebuchet ms,sans-serif">376.8314</font><br> <font style=3D"font-family:trebuchet ms,sans-serif">skype: jcmorris-mts<br>= email: <a href=3D"mailto:[email protected]" target= =3D"_blank">[email protected]</a><br>mybio: <a href= =3D"http://www.linkedin.com/in/jcmorris" target=3D"_blank">http://www.linke= din.com/in/jcmorris</a><br> </font><font><span style=3D"font-family:trebuchet ms,sans-serif"></span></f= ont><br><font style=3D"font-family:trebuchet ms,sans-serif"><br></font><div= style=3D"text-align:center"><img src=3D"http://intellifest.org/wordpress/w= p-content/uploads/2012/02/intellifest_logo_150px.png"><br> <font><span style=3D"font-family:trebuchet ms,sans-serif"><a href=3D"http:/= /www.intellifest.org" target=3D"_blank">www.intellifest.org</a> </span></fo= nt><br><font><span style=3D"font-family:trebuchet ms,sans-serif"></span></f= ont><font><span style=3D"font-family:trebuchet ms,sans-serif">Invent * Inno= vate * Implement at IntelliFest!</span></font><br> <font><span style=3D"font-family:trebuchet ms,sans-serif"> </span></font></div></div> <br><br><div class=3D"gmail_quote">On Thu, May 23, 2013 at 12:43 PM, Przemy= slaw Woznowski <span dir=3D"ltr"><<a href=3D"mailto:[email protected].= ac.uk" target=3D"_blank">[email protected]</a>></span> wrote:<br= ><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1= px #ccc solid;padding-left:1ex"> <div dir=3D"ltr"><div><div><div><div>Hi Jess users,<br><br></div>I have the= following function:<br><br>(deffunction similarity-score (?base ?other)<br= >=C2=A0=C2=A0=C2=A0 (bind ?score 0)<br>=C2=A0=C2=A0=C2=A0 (bind ?totalDista= nce 0)<br>=C2=A0=C2=A0=C2=A0 (bind ?counter 0)<br> =C2=A0=C2=A0=C2=A0 (foreach ?x ?base<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2= =A0 (bind ?counter (+ ?counter 1))<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0= (printout t "counter is:" ?counter)<br>=C2=A0=C2=A0=C2=A0 =C2=A0= =C2=A0=C2=A0 (bind ?partialDistance 0)<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0= =C2=A0 (printout t "partialDist is:" ?partialDistance)<br> =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (foreach ?y ?other<br>=C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (bind ?partialDistance (+ ?partia= lDistance 1))<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (= printout t "foreach partialDist is:" ?partialDistance)<br>=C2=A0= =C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (if (eq ?x ?y) then <br>= =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0= (bind ?score (+ ?score 1))<br> =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0= (printout t "score is:" ?score)<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2= =A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 (return)<br>=C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 )<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2= =A0=C2=A0 )<br>=C2=A0=C2=A0=C2=A0 (bind ?totalDistance (/ ?counter ?partial= Distance))<br>=C2=A0=C2=A0=C2=A0 )<br>=C2=A0=C2=A0=C2=A0 (return (- (/ ?sco= re (length$ ?base)) (* (- 1 (/ (length$ ?base) ?totalDistance)) 0.1) ))<br> )<br><br></div>As you can see, the above function has a nested foreach loop= , which upon finding equal values in both list should skip to the next iter= ation of the outer loop. In java, instead of (return) one would normally pu= t a break statement. As far as the documentation of the (foreach) construct= reads, it says: "The return function can be used to break the iterati= on". However, what I am finding is that the (return) function terminat= es the outer loop too - unless I seriously messed up the code - but this is= the result of my observation of the (printout t) function. Moreover, when = I remove the (return) function, the function iterates over the outer and in= ner loops just fine.<br> <br></div>Any advice on how can I break the inner's loop iteration? I k= now I can use a variable to flag that the match has been found and add it t= o the if statement, but I am hoping that there is an equivalent of break in= Jess.<br> <br><br clear=3D"all"></div>Cheers,<br>Pete<br></div> </blockquote></div><br></div> --089e0141a0a877f59c04dd6dd015--