Krishnan,
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.
If you want your rules to drive the parsing, I would model it like that:
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).
2) Create a rule to drive your parsing:
rule "R1: Read one more line"
when
$f : File( eof == false )
then
System.out.println("Generate new line");
assert( $f.readLine() );
modify( $f ); // as readLine "modifies" the state of the File
end
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:
rule "Rx: analyze line"
salience 10
when
Line( content matches "xxx" )
then
// do something
end
4. Create the rule to close your file when EOF is reached:
rule "close file'
when
$f: File( eof == true )
then
$f.closeFile();
end
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.
That is how I would do it.
Hope it helps.
Edson
2007/5/17, Krishnan <krishiyer(a)gmail.com>:
Hi,
I want to parse a file and match the file for several regular expressions.
Based on what I match, I write more
rules so that based on all the rules, I can set my results.
Brute force method : Parse the entire file and add all the lines into the
working memory. Write rules that
check each line for some regular expression. This works great.
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.
So, I wrote a function inside the rule file like the below
function String getLine(BufferedReader fileReader) {
String line;
try {
if ((line = fileReader.readLine()) != null) {
return line.toLowerCase();
} else {
System.out.println("This is an empty line.");
}
} catch (Exception ex) {
}
return line;
}
Now, I need to write the rules that will use this.
rule "Generate New Line"
when
eval (getLine(fileReader) != null)
then
System.out.println("Generate new line");
//assert(getLine(fileReader)); // need to somehow assert the line
specified in the eval condition
end
Also, I am confused, how to specify file end.
rule "File End"
when
eval (getLine(fileReader) == null)
then
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 ?
end
Any help is appreciated.
TIA,
Krishnan (newbie)
-
Sivaramakrishna Iyer Krishnan (Anand)
Never assume the obvious is true.
- William Safire
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users
--
Edson Tirelli
Software Engineer - JBoss Rules Core Developer
Office: +55 11 3529-6000
Mobile: +55 11 9287-5646
JBoss, a division of Red Hat @
www.jboss.com