Jython and Drools
Download: drools-py.zip
Jython and Drools
http://dobbse.net/2005/03/drools-py/
Use Jython to declare and run Drools rulesets.
I wanted to play with drools. But I wanted to be able to use Jython
for my objects, 'cos its much easier to experiment in Jython. And I
take offense to the XML rules configuration even with the python
semantic module -- it looks too much like JSP without JSTL. (Bob,
aren't you the one who said "I hate programming in angle brackets"?)
Requires*:
Java 1.4.2 http://java.sun.com/
Jython 2.1 http://jython.org/
drools-core 2.0 beta http://drools.codehaus.org/
drools-base 2.0 beta http://drools.codehaus.org/
(* other versions may work, these are the ones I've tested)
I'll assume that you have installed jython and that it is in your path,
and that you have installed the drools jar files in a directory called
<installdir>/lib
Run the tests to confirm your installation
% chdir <installdir>
% export CLASSPATH=lib/drools-base-2.0-beta-21.jar:lib/drools-core-2.0-beta-21.jar
% jython testdrools.py
Then try out the examples:
% jython examples/fibonacci.py
% jython examples/state.py
Specifying Rules:
See Drools documentation for more details about the what an the why of
these things. The following details just explain the grammar of
defining rules. The basic idea is to use python's light object syntax
to declare rules.
A RuleSet is defined in a python module.
A Rule is an object instance which declares Parameters, Conditions,
and a single Consequence through naming conventions.
Parameters are declared in a tuple of name-type pairs:
(('name1', SomeClass), ('name2', AnotherClass))
Conditions are functions whose names start with 'condition'. They
can't be object methods and must be unbounded functions. (Object
methods must have 'self' as the first parameter which interferes with
their use in defining the behavior for rules.) The functions must
accept the same number and type of arguments as defined in the
Parameters. (see the tests and examples)
The Consequence is function named 'consequence'. Same story as with
conditions -- it must be an unbounded function and not a object
method. The function must take a workingMemory as its first argument
and then the same number and type of arguments as defined n the
Parameters (again, see the tests examples).
Copyright (c) 2005 Eric Dobbs Some rights reserved.
This work is licensed under the Creative Commons Attribution
License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
USA.