2008-05-20

NotNode woes

Since the rewrite of the RuleCompiler I've been making good progress at good speed with PyRete. It might appear different to the casual observer but the reason it's taking so long to get even an alpha release out is because I've only got about an hour or two a week to spend.

Anyway, last week I spent my hours trying to add support for the NotNode. And, instead of thinking ahead, I implemented it, more or less, exactly in the same faulty manner as I did last time. Which didn't work, I might add. I knew it! But somehow, I kept hoping that changes I had made somewhere else would automagically clean up things here as well. Stupid! Stupid! Stupid!

So, now I've got a RuleCompiler that works as long as the Not-CE contains no more than one Pattern-CE. So Not(Foo(bar == var)) works fine but Not(Foo(bar == var) and Not(Bar(foo == var))) won't compile. Well. It compiles. It's just that it won't result in a correct, usable structure.

The problem is that my approach tries to tack on some state-flags to bits and pieces in the compilation process. But since the Python AST visitor uses a callback interface it's difficult (not impossible) to store state between calls. The more complex the expression being parsed the closer to insanity I get.

The easier solution, and probably the more correct one as well, is to treat whatever is within the Not-CE as a Rete Network in itself. Once compilation is done I rip out the ProductionNode from it and replace it with a NotNode (where the sub-network feeds from the right) and join it to the main network (feeding from the left).

That requires some small changes in my current implementation, but once I get it to support recursive use it's going to work with both Exists-CE and Forall-CE without me having to add an increasingly complex structure for storing state. But, then again. I knew that already! I even knew that before I started working on it. I just didn't believe it would turn out so bad this time. So, I'll spend the rest of the day kicking myself and then it's back to work. Doing it over, but right this time.

Inga kommentarer: