RuleSet template?

6 views
Skip to first unread message

SP

unread,
Sep 11, 2007, 6:02:26 PM9/11/07
to Rools Users
Hi,

I was trying to learn RoolZ without the RDoc (since it is down online)
and built on an example I found in a previous thread on the mailing
list. Here it is:
[code]
require 'rubygems'
require 'rools'

def generate_rules
Rools::RuleSet.new do
facts 'PermanentCountries' do
["China", "Russia", "France", "Great Britain", "USA"]
end

facts 'TemporaryCountries' do
[
'Belgium', 'Italy', 'Panama', 'South Africa', 'Indonesia',
'Qatar', 'Congo', 'Slovakia', 'Ghana', 'Peru',
]
end

rule 'Is it a temporary member of the Security Council?' do
parameter String
condition { temporarycountries.include?(string) }
consequence { puts "#{string} is a temporary member of the
security council" }
end

rule 'Is it is permanent member of the Security Council?' do
parameter String
condition { permanentcountries.include?(string) }
consequence { puts "#{string} is a permanent member of the
security council" }
end

rule 'Is not a member of the Security Council?' do
parameter String
condition { !(permanentcountries.include?(string) ||
temporarycountries.include?(string)) }
consequence { puts "#{string} is not a member of the security
council" }
end
end
end

def security_council_member?(country)
rules = generate_rules
rules.assert country
end

security_council_member? 'France'
security_council_member? 'Qatar'
security_council_member? 'Germany'
security_council_member? 'Great Britain'
security_council_member? 'USA'
security_council_member? 'Panama'
security_council_member? 'Canada'
[/code]

I receive the following output (which is exactly what I expect):
[output]
France is a permanent member of the security council
Qatar is a temporary member of the security council
Germany is not a member of the security council
Great Britain is a permanent member of the security council
USA is a permanent member of the security council
Panama is a temporary member of the security council
Canada is not a member of the security council
[/output]

This is all great, but I was wondering if there is a way to have a
"template" of the rule set without having to subclass RuleSet to
define the facts within it? This would eliminate the need to create a
new RuleSet object each time I want to rule the assertion.

Subclassing just doesn't seem like a clean way to do it. Anyone?

Thanks,
SP

SP

unread,
Sep 11, 2007, 6:06:52 PM9/11/07
to Rools Users
Obviously English (my native language) isn't my strong point, the code
should read:

[code]
require 'rubygems'
require 'rools'

def generate_rule_set


Rools::RuleSet.new do
facts 'PermanentCountries' do
["China", "Russia", "France", "Great Britain", "USA"]
end

facts 'TemporaryCountries' do
[
'Belgium', 'Italy', 'Panama', 'South Africa', 'Indonesia',
'Qatar', 'Congo', 'Slovakia', 'Ghana', 'Peru',
]
end

rule 'Is it a temporary member of the Security Council?' do
parameter String
condition { temporarycountries.include?(string) }
consequence { puts "#{string} is a temporary member of the
security council" }
end

rule 'Is it a permanent member of the Security Council?' do


parameter String
condition { permanentcountries.include?(string) }
consequence { puts "#{string} is a permanent member of the
security council" }
end

rule 'Is it not a member of the Security Council?' do


parameter String
condition { !(permanentcountries.include?(string) ||
temporarycountries.include?(string)) }
consequence { puts "#{string} is not a member of the security
council" }
end
end
end

def security_council_member?(country)
rules = generate_rule_set
rules.assert country
end

security_council_member? 'France'
security_council_member? 'Qatar'
security_council_member? 'Germany'
security_council_member? 'Great Britain'
security_council_member? 'USA'
security_council_member? 'Panama'
security_council_member? 'Canada'
[/code]

Linda Derezinski

unread,
Sep 11, 2007, 7:32:26 PM9/11/07
to rools...@googlegroups.com
You can build the rdoc locally with
rake rdoc
I will get Pat to look at the site later on.

-Linda

Pat Cappelaere

unread,
Sep 12, 2007, 11:31:18 AM9/12/07
to rools...@googlegroups.com
SP,

Not sure I understand what you are asking?
What about an example of what you would like to have?
Pat.

Reply all
Reply to author
Forward
0 new messages