itterations in the conditions - is this a use case

1 view
Skip to first unread message

jason

unread,
Oct 3, 2009, 8:27:42 AM10/3/09
to Ruleby
dear groupies,

I have the following case (in pseudo code):

for each i

if i meets some condition

if a
then b
else c

if b then
then d
else e

end if i

end each i

In the loop above the current i needs to achieve some condition before
all other conditions are checked through.
Effectively only one i needs to be considered at a time.

what I would love to do is:

for each i assert i as a fact if it meets some condition
assert a as a fact
assert b as a fact
assert d as a fact
assert e as a fact

now the question is can I define a rule book that works for this case?

The problem that I have I have a number of conditions that all need i
- so none the conditions can throw i out of the facts - so I am asking
myself if this actually is a use case or not?

or do I should I assert and match against the rulebook for each
itteration?

any help would be very much appreciated.

Thanks a million Jason.







Joe Kutner

unread,
Oct 3, 2009, 3:34:13 PM10/3/09
to Ruleby
You could even get rid of the "if it meets some condition" when
asserting "i"

I believe some rules like this accomplish what you want:

rule [I, :i, m.some_method = "some condition"], [A, :a] do |v|
# then b
end

rule [I, :i, m.some_method = "some condition"], [:not, A] do |v|
# else c
end

rule [I, :i, m.some_method = "some condition"], [B, :b] do |v|
# then d
end

rule [I, :i, m.some_method = "some condition"], [:not, B] do |v|
# else e
end

The engine will take care of the redundant conditions - so they won't
be evaluate more than once. If you are really concerned about
repeating yourself, then you can take advantage of some patterns
consolidate repeated logic (like using a context object). But in
general I find that chaining rules like that defeats the purpose of
rulebook. When possible, I prefer each business rule to "mean what it
means." That way it is independent from the other rules. This makes
the rulebook easier to comprehend and maintain - in my opinion.

Joe

jason

unread,
Oct 3, 2009, 10:06:05 PM10/3/09
to Ruleby
Joe,
This is extremely helpful. thanks.

I have have been torturing myself all day as to how much i use the
database sql and how much data i load from the database as facts for
ruleby.

I have come to the conclusion that it is better to load to load the
objects as facts. It might be a little slower and take up a little
more memory, but it gets rid of a ton of conditionals and makes the
code more readable and concise - so I think it well worth the extra
cost.

So I will be getting rid of the itteration and just dump everything i
need as facts for ruleby.
Reply all
Reply to author
Forward
0 new messages