Krishnan,<br><br> I always prefer to use specific tools for specific jobs. So, if I were implementing that, I would not put the I/O code inside the DRL.<br> If you want your rules to drive the parsing, I would model it like that:
<br><br>1) Create a class to "represent" your file... that would be your File interface. It would have a method: readLine() that returns a Line object, as well as an "eof" property (true when EOF was reached).
<br><br>2) Create a rule to drive your parsing:<br><br><br><font style="background: rgb(255, 255, 255) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" color="#000000">
rule "R1: Read one more line"<br> when <br> $f : File( eof == false )<br> then <br> System.out.println("Generate new line");<br> assert( $f.readLine() ); <br> modify( $f ); // as readLine "modifies" the state of the File
<br>end</font><br><br>3) Create the rules to effectivelly "analyze" your lines. If you want to read all lines first and then analyze them, just make rule R1 to have a greater salience. Otherwise, if you want each line analyzed as it is parsed, make your other rules have a greater salience as showed bellow:
<br><br>rule "Rx: analyze line"<br> salience 10<br>when<br> Line( content matches "xxx" ) <br>then<br> // do something<br>end<br><br>4. Create the rule to close your file when EOF is reached:<br>
<br>rule "close file'<br>when<br> $f: File( eof == true )<br>then<br> $f.closeFile();<br>end<br><br> Keeping the procedural code in your java classes instead of DRL functions makes all your rules more clear, makes easy to unit test each part, and reduces the overall complexity.
<br><br> That is how I would do it.<br><br> Hope it helps.<br> Edson<br><br><div><span class="gmail_quote">2007/5/17, Krishnan <<a href="mailto:krishiyer@gmail.com">krishiyer@gmail.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><font style="background: rgb(255, 255, 255) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" color="#000000"><br>Hi, <br>
<br>I want to parse a file and match the file for several regular expressions. Based on what I match, I write more <br>rules so that based on all the rules, I can set my results. <br><br>Brute force method : Parse the entire file and add all the lines into the working memory. Write rules that
<br>check each line for some regular expression. This works great. <br><br>I want to optimize the above, as we are checking each line, if we got we are looking for, then I want to get out. <br></font><div>
<font color="#ff6600"><br></font></div><font style="background: rgb(255, 255, 255) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" color="#000000">
So, I wrote a function inside the rule file like the below <br><br>function String getLine(BufferedReader fileReader) {<br> String line;<br> try {<br> if ((line = fileReader.readLine()) != null) {<br> return
line.toLowerCase();<br> } else {<br> System.out.println("This is an empty line.");<br> }<br> } catch (Exception ex) { <br> }<br> return line;<br>}<br><br>Now, I need to write the rules that will use this.
<br><br>rule "Generate New Line"<br><br> when <br> eval (getLine(fileReader) != null)<br> then <br> System.out.println("Generate new line");<br> //assert(getLine(fileReader)); // need to somehow assert the line specified in the eval condition
<br><br>end<br><br>Also, I am confused, how to specify file end. <br><br></font><font style="background: rgb(255, 255, 255) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" color="#000000">
rule "File End"<br>
<br>
when <br>
eval (getLine(fileReader) == null)<br>
then <br>
System.out.println("Reached end of file"); // I am assuming this is not needed since if all rules are executed, then it will get out of fireRules() anyways ?<br><br>
end</font><br><font style="background: rgb(255, 255, 255) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" color="#000000">
<br>Any help is appreciated. <br><br>TIA, <br>Krishnan (newbie)<br><br></font></div>- <br>Sivaramakrishna Iyer Krishnan (Anand) <br><br>Never assume the obvious is true. <br>- William Safire
<br>_______________________________________________<br>rules-users mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">
https://lists.jboss.org/mailman/listinfo/rules-users</a><br><br></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> Software Engineer - JBoss Rules Core Developer<br> Office: +55 11 3529-6000<br> Mobile: +55 11 9287-5646
<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a>