views:

388

answers:

3

I am trying to construct a rule-based system for interpreting data. However, I am having issues deciding on a way to construct the logic for storing and interpreting rules.

Currently, there is a database structure that quite complex, but will deal with all aspects of storing the rule data. The idea is that the system will be able to mimic the way that MS Outlook allows a user to add rules.

My problems are as follows:

  1. What pattern should I be using to store these rules inside objects?
  2. Should I use eval() or proper object orientation to execute the rules?

And example rule might be:

Dog must have collar in area park

Where each element (dog, must have, collar, in area, park) is a separate piece of logic to be interpreted.

Any general advice to the above questions is much appreciated!

+1  A: 

I've never implemented a system like you describe in a "real world" context, but I have played with them a considerable amount as hobby projects. My preferred approach is to use some kind of logic language like Prolog to make assertions and check them. You'd have assertions for where the park is, what it means to have something, what a dog is, and then you'd make a rule pretty much exactly like your example in parentheses at the bottom of your post. I'm sorry my Prolog is too rusty to give you a useful example... I've been playing with home-grown inference languages too long.

There are Prolog interpreters available for embedding in most languages, though I'm not sure about PHP5. You could throw together something simple that does forward-chaining inference on rule data structures of your own creation in fairly short order, if you can't find a Prolog interpreter. You may be interested in these notes on automated inference.

rmeador
Prolog would probably be taking it too far from PHP, but this is a good idea. Could you perhaps suggest an implementation for First Order Logic?
tombazza
What do you mean by an "implementation for FOL"? The last link in my post is basically a how-to on implementing your own FOL inference engine. You may also want to check out the Cyc project: http://www.opencyc.org/
rmeador
Yes, I was hoping perhaps someone may have already made something that implements this logic process. At least so I can review how they constructed the code base of it!
tombazza
+1  A: 

Probably this answer is too trivial / obvious for you but I just thought on how I would solve something like that in my current existing project which is a Zend Framework application. I thought of the filter- and validatorChains ZF uses. I assume you have a finit number of possible input objects, a finit number of conditions/constraints and a finit number of actions. In that case you would first instantiate an object and run it against a chain of conditions(validators), if all the conditions are satisfied you run the object against the actionChain. Here I would probably have to implement some kind of action priority system since some actions have to be carried out before others. Like 'sending a notification' and then 'delete' the object in question. So in ZF I would build a custom validator for every condition/constraint. I don't believe the Outlook system is very intelligent meaning that I don't think the validators are very generic.

In the db there could be a table for actual rules, one for the conditions and one for actions. Then there could be two many-to-many tables linking the rule with all needed conditions and actions.

tharkun
The idea of using chains is very interesting actually, containing the chains inside one object which could then be chained into another chain and so on
tombazza
A: 

Try looking at http://codaserver.com/.

Sandy Taylor