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
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
Not sure I understand what you are asking?
What about an example of what you would like to have?
Pat.