By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,583 Members | 617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,583 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi,

I'm not sure what your budget is, but a third party component like ILOG Rules
for .NET may do what you need <http://www.ilog.com/products/rulesnet/>

Unfortunately, ILOG tends to be on the expensive side.

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>

hazz wrote:
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

Nov 17 '05 #2

P: n/a
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" <gr*********@nospamcomcast.net> wrote in message
news:eA**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #3

P: n/a
In message <eA**************@TK2MSFTNGP12.phx.gbl>, hazz
<gr*********@nospamcomcast.net> writes
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.

--
Steve Walker
Nov 17 '05 #4

P: n/a
I do a lot with XSLT for simple rules as well...

I know these projects aim at something like this, but have never used them:

http://www.agilepartner.net/oss/nxbre/
http://sourceforge.net/projects/sdsre/

Good luck,

Wiebe

hazz wrote:
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

Nov 17 '05 #5

P: n/a
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

"Rodger Constandse" <ro*************@effexis.com> wrote in message
news:Ge***************@tornado.socal.rr.com...
Hi,

I'm not sure what your budget is, but a third party component like ILOG
Rules for .NET may do what you need
<http://www.ilog.com/products/rulesnet/>

Unfortunately, ILOG tends to be on the expensive side.

Best regards,

Rodger

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>

hazz wrote:
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
ou. -hazz

Nov 17 '05 #6

P: n/a
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

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:04********************@comcast.com...
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" <gr*********@nospamcomcast.net> wrote in message
news:eA**************@TK2MSFTNGP12.phx.gbl...
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
ou. -hazz


Nov 17 '05 #7

P: n/a

Google 'c# rules engine'

Here's one that uses XML ... looks cool:

http://www.dnzone.com/ShowDetail.asp?NewsId=600

"NxBRE is the first open-source rule engine for the .NET platform and a
lightweight Business Rules Engine (aka Rule-Based Engine)."

hazz wrote:
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

Nov 17 '05 #8

P: n/a
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

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:za**************@otolith.demon.co.uk...
In message <eA**************@TK2MSFTNGP12.phx.gbl>, hazz
<gr*********@nospamcomcast.net> writes
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.

--
Steve Walker

Nov 17 '05 #9

P: n/a
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

<ja*****@texeme.com> wrote in message news:42**************@texeme.com...

Google 'c# rules engine'

Here's one that uses XML ... looks cool:

http://www.dnzone.com/ShowDetail.asp?NewsId=600

"NxBRE is the first open-source rule engine for the .NET platform and a
lightweight Business Rules Engine (aka Rule-Based Engine)."

hazz wrote:
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
ou. -hazz

Nov 17 '05 #10

P: n/a
In message <Op*************@TK2MSFTNGP15.phx.gbl>, hazz
<gr*********@nospamcomcast.net> writes
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.

--
Steve Walker
Nov 17 '05 #11

P: n/a
Thank you..

"Steve Walker" <st***@otolith.demon.co.uk> wrote in message
news:GS**************@otolith.demon.co.uk...
In message <Op*************@TK2MSFTNGP15.phx.gbl>, hazz
<gr*********@nospamcomcast.net> writes
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.

--
Steve Walker

Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.