rules engine ideas? Trying to prevent tons of conditional branches in a logic filter.

  • Thread starter Thread starter hazz
  • Start date Start date
H

hazz

before I start filling up the first page of perhaps many pages of code with
if/then or switch:case buckets, I wanted to step back and see if there is a
better way...
I will have a table with up to 300 rules in it. Maybe more... In each Score
table there is a column which will refer to a domain specific table and
another table column that contains the property of that domain specific
object. IceCream is a domain and scoops is a property I want to compare
against. There are two other columns which have an operator (=,>,<) and a
value to compare against. And a score assigned to that rule. If
object.value > 100,000 scoops of ice cream, assign a score of 100.
Basically a rule will compare a value for an object which has already been
hydrated from the database and goes through the rule filter, which I have
yet to build. If object.value > target.value then assign score. Keep a
cumulative score as this object, with its various properties and property
values, as it drops through the logic filter.

I am trying to prevent an unwieldy control flow maze. Does anyone have
ideas? I have heard about control structures like the HybridDictionary but I
am having a difficult time imagineering that. I know what to do with an ice
cream cone, especially chocolate chip, but I don't know what to do with a
HybridDictionary object.

Maintanability of this engine would be important as well as the possibilty
of caching all the rules so that no database calls are required after the
service or executable is running. Thank you. -hazz
 
One thing you could do is to place your rules in a database. Each rule
consists of an XPath expression for the operand, and operator, the value you
compare against, and the score. Bring your data into your system and
serialize it into an XML document. Then, apply each of the Xpath queries to
the object, one at a time. If the query produces a result, you have a value
you can compare against. Apply the comparison and, if true, add the score
to your accumulated score.

This will work regardless of the data coming in, and your storage mechanism
can work for anything. Xpath queries are very powerful and you can
differentiate easily depending on the structure of the inbound document, so
that a query may match a value only if it is at a particular place in the
document, or anywhere in the document... up to you.

Total lines of code < 200. Highly flexible since you can add and delete
rules in the db at any time.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
hazz said:
before I start filling up the first page of perhaps many pages of code with
if/then or switch:case buckets, I wanted to step back and see if there is a
better way...

Can the logic be expressed as a flowchart? I wrote a workflow/rules
engine for a previous employer based on that premise. The engine manages
a branching and joining structure of abstract rules with a single entry
point and a single termination point. There's an abstract Evaluate()
method on Rule like this:

public abstract bool Evaluate(object subject);

Developers subclass Rule and implement their own test, casting subject
to whatever it is the rule needs to evaluate. The subclass names and
configuration information for each subclass instance are stored in a
database and instantiated using reflection. Rule subclasses also know
(or more usually know how to calculate) the URL for the webpage which
can affect the outcome of the evaluation (i.e. if the rule is that a
project must have a budget defined, the ProjectBudgetDefinedRule knows
how to calculate the URL to the project budget entry page). Each rule
graph defined in the database is created as a stateless singleton. The
subject to be evaluated is run through the graph generating a memento of
the graph structure containing the evaluated state of the rules.

The memento is built of State classes and uses a voting mechanism to
determine whether a rule has been reached. Subclasses of Rule and State
are used to handle joining (AND) situations, so that JoinState evaluates
to completed if all directly preceding steps are completed. You can then
evaluate whether the entire process has completed and if not, which
steps are available to continue.

I used a web graphics package (http://www.nwoods.com/) to put a
graphical flow chart representation on the front end so that users could
be walked through business processes by clicking on shapes on a flow
chart. I wrote a Windows Forms app to allow the rule graphs to be set up
using drag and drop.

It's not difficult, and it isn't a huge amount of code, but it does take
a fair bit of designing, and you need to be sure that what you are
trying to model rules for can be expressed in that way.
 
Thank you for the idea Rodger! This looks like an interesting Microsoft
tools ecosystem solution. An immediate value of this info is to better frame
how my solution should be built; ie. user features, code integration, ease
of use, etc. thanks!
-hazz
 
Thank you Nick!
Without needing to get complicated, do you have a simple example of link
someplace that would help me frame the XPath query solution....ie. from the
db tables into the object, for which the accumulated score will be
generated?
I also need to visualize how this XML document will look and be used for
this rules engine solution.Any articles that come to mind?
Thanks for offering the XPath/XML way of thinking about this!
-hazz
 
Thank you for helping me think about this Steve. I like your object heiarchy
decomposition of the process. I also like how you attached the web graphics
package to this underlying model!
What is a memento? I think I get in a real basic sense what you are doing,
even if I can't (at the moment) connect all the dots to the implementation
level that I need.
Thanks again.
-hazz
 
Thank you. I think that is the same rules engine that Wiebe just mentioned
in the preceding response....and I am looking at that now. Thanks for
replying! -hazz
 
hazz said:
What is a memento?

It's a pattern usually used where you want to persist or serialize the
internal state of an object without breaking the object's encapsulation.
In this case it's the same idea but to a different end. Because the
individual rules are stateless with respect to the method call, it
represents the structure of the object graph and the state generated
within the scope of the call.
 
Thank you..

Steve Walker said:
It's a pattern usually used where you want to persist or serialize the
internal state of an object without breaking the object's encapsulation.
In this case it's the same idea but to a different end. Because the
individual rules are stateless with respect to the method call, it
represents the structure of the object graph and the state generated
within the scope of the call.
 
Back
Top