rules not getting fired

4 views
Skip to first unread message

nitin

unread,
Mar 25, 2010, 1:43:08 PM3/25/10
to Ruleby
hey Guys,

I am very new to Ruleby and couldn't find much documentation on the
web for this library.
I have written a very simple ruby module using the code here
http://github.com/codeaspects/ruleby/blob/master/examples/diagnosis.rb
The code resides in the lib directory of my Rails app.

module RuleEngine
require 'ruleby'
include Ruleby

class Patient
def initialize(age, bmi)
@age = age
@bmi = bmi
end
attr:age, true
attr:bmi, true

def tips
engine :engine do |e|
DiagnosisRulebook.new e do |r|
r.rules
end
e.assert self
e.match
end
return "done"
end
end

class DiagnosisRulebook < Ruleby::Rulebook
def rules
rule :age_greater_than_or_equal_to_50, [Patient, :p, m.age >=
50] do |v|
puts "age_greater_than_or_equal_to_50"
end

rule :age_greater_than_or_equal_to_65, [Patient, :p, m.age >=
65] do |v|
puts "age_greater_than_or_equal_to_65"
end

rule :bmi_less_than_19, [Patient, :p, m.bmi < 19] do |v|
puts "bmi_less_than_19"
end

rule :bmi_between_25_and_30, [Patient, :p, m.bmi(&c{|f| f >= 25
&& f < 30})] do |v|
puts "bmi_between_25_and_30"
end
end
end

end

Now on script console I do this:

include RuleEngine

p = Patient.new(21, 17)
p.tips

and the output is:

bmi_less_than_19
"done"

which is good

Now when i remove the first parameter which is age the bmi rule
doesn't get fired


p = Patient.new(nil, 17)
p.tips

and the output is:

"done"


I think there is something very basic that I am not doing in a right
manner.

Can anybody help ?

Joe Kutner

unread,
Mar 25, 2010, 7:37:17 PM3/25/10
to rul...@googlegroups.com
Thats bug.  Thanks for point it out.  

I created a fix, but its causing some funky things in a particularly fickle test I have.  In the meantime, you can rewrite your rules like this:

     rule :age_greater_than_or_equal_to_50, [Patient, :p, m.age(&c{|f| f and f >= 50})] do |v|
         puts "age_greater_than_or_equal_to_50"
     end

     rule :age_greater_than_or_equal_to_65, [Patient, :p, m.age(&c{|f| f and f <= 65})] do |v|
         puts "age_greater_than_or_equal_to_65"
     end

     rule :bmi_less_than_19, [Patient, :p, m.bmi(&c{|f| f and f < 18})] do |v|
         puts "bmi_less_than_19"
     end

I'll post back to this thread when i commit the fix.

Thanks,


--
You received this message because you are subscribed to the Google Groups "Ruleby" group.
To post to this group, send email to rul...@googlegroups.com.
To unsubscribe from this group, send email to ruleby+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruleby?hl=en.


Joe Kutner

unread,
Mar 26, 2010, 9:35:21 PM3/26/10
to Ruleby
I have committed a fix that prevents the NoMethodError from being
swallowed. However, I failed consider that you will still need to use
the condition blocks I described in my last post. I can't assume that
all Ruleby users will want to treat Nil as a valid value - some may
desire the error condition. But at least now everyone will be able to
see it :)

Thanks again,

Joe

> > ruleby+un...@googlegroups.com<ruleby%2Bunsu...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages