2007-02-19

Pattern matching

One of the things I've "just" started missing in pyRete is an easy way of doing pattern matching. At the moment you've got to test each property in a separate expression, like so:

>>> @pyRete.Rule
... def some_rule(obj = SomeObject):
... if obj.foo == 1 and obj.bar == 2 and obj.baz == 3:
... pass
I'd like to introduce some way of doing those three tests as one pattern match, similar to how it's done in Clips. I've been been thinking about this for some time and I've tried to stay away for as long as I've could because there's no "natural" way of expressing a pattern to match in Python.

The only thing that I've been able to come up with is:
>>> @pyRete.Rule
... def some_rule(obj = SomeObject):
... if obj(foo = 1, bar = 2, baz = 3):
... pass
which seems, sort of, OK. But from a Python syntax point of view it's not so good. It might even be weird. Maybe I should just ignore Python syntax and use { and } to disambiguate:
>>> @pyRete.Rule
... def some_rule(obj = SomeObject):
... if obj{foo = 1, bar = 2, baz = 3}:
... pass
I don't know. I'll have to think abput this some more I guess.

Inga kommentarer: