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"

        eval (getLine(fileReader) != null)
        System.out.println("Generate new line");
        //assert(getLine(fileReader)); // need to somehow assert the line specified in the eval condition


Also, I am confused, how to specify file end.

rule "File End"

        eval (getLine(fileReader) == null)
        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 ?


Any help is appreciated.

Krishnan (newbie)

Sivaramakrishna Iyer Krishnan (Anand)

Never assume the obvious is true.
- William Safire