2006-08-01

My Rete implementation and Pypy

Having had some more time to check out Pypy 0.9.0 I've come to realize that they're actually implementing, in the Logic ObjectSpace, quite a lot of the things that I'm working on at the moment. We seem to be going about it differently but the use cases are the same:

[snippets from pypy-0.9.0 docs/discussion/use_case_of_logic.txt]

Business Logic
- We define Business Logic as expressing consistency (as an example) ona set of objects in a business application. For exeample checking the consistency of a calculation beforecommitting the changes. The domain is quite rich in example of uses of Busines Logic.

User Interfaces
- We use rules to describe the layout and visibility constraints ofelements that are to be displayed on screen. The rule can also helpdescribing how an element is to be displayed depending on its state(for instance, out of bound values can be displayed in a different colour).

and so on...

They're working on a general Constraint Solver with concurrency capabilities where the search algorithm can be swapped out. which makes it sound real interesting.

My approach is to "hide" a forward-chaining Rule Engine in a custom Pypy ObjectSpace. It will automatically add or update instances of Python objects to the Working Memory whenever an instance is created or modified. This would basically let you add, for example, constraint rules "outside" of the normal business logic.

It would probably look a bit like the following:


>>> class Foo(object):
... def __init__(self, n):
... self.n = n

>>> rule keepFooNPositive:
... if ?obj:Foo.n <= 0: ... raise Exception('Not allowed') >>> foo = Foo(42)
>>> foo.n
42
>>> foo.n = -1
Traceback (most recent call last):
File "", line 1, in -toplevel-
raise Exception('Not allowed')
Exception: Not allowed

>>> foo.n
42

Well that's the general idea anyway. I'm currently using a similar setup at work but we're using Java and Drools and it's quite messy. It would be really nice to get a cleaner way of doing the marshalling of instance values.

Inga kommentarer: