Allow rules to be applied multiple times

This is turning out more complex than I think is necessary.  Perhaps a more elegant solution is possible?  What I thought would be a basic switching the order of a loop turns out to be more.  Basically, I spent most time solving structure such as this one:

    [word1 [links [rel1 [member0 word2 [links [rel5 ...
                        [member1 ...
                        ....
                  [rel2 [member0 ...
                        ....
                  [rel3 [member0 ...
                        ....
                  [rel4 wordN [links ...

If we have two rules:

    rule1: (rel1($a, $b) & rel2($a, $c) & rel3($a, $d) & rel4($a, $e)
    rule2: (rel1($a, $b) & rel2($a, $c) & rel3($a, $d) & rel4($a, $e) & rel5($b, $f)

Then for rule1, we need to generate all possible combination of links/member# of rel1 to rel4, since a rule can be applied multiple times.  In addition, for rule2, in addition to generating all combination, we also need to check deeper into all the path to check if one or more of them link to a rel5.  Additional care also needed to avoid loop.  The solution I have implemented now involves a lot of recursions... and I can in fact still think of one extreme case which my code might not cover.

Perhaps working directly with the Feature Structure is not the best solution for Relex2Logic?  I think this because Feature Structure treat a word node as parent to relation, but R2L works with relation first before checking the word.  It might be easier to crawl the structure once and generate a list of triplets:

    <relation_name <word1, word1uuid> <word2, word2uuid>>

But then it will still be a problem of finding all possible combinations.