Rule engines, at the outset, appear to be a nifty concept for
making Java applications more late-bound, dynamic and configurable.
Drools in particular even seems to be getting some traction as I've
seen in mentioned several times over the past few weeks. But why, oh
why do they insist on the angly-bracket, ugly XML syntax?
Maybe I'm just a Ruby-as-platform-for-building-DSLs
convert,
but doesn't this seem a whole lot more readable than that mess
of XML that Drools uses?
require 'drools'
require 'daofactory'
require 'stockoffer'
class StockOffer
def print
p "Name: #{@name} Price: #{@price} BUY: #{@recommend_purchase}"
end
end
class BusinessRulesSample < Drools::RuleSet
application_data DaoFactory
rule "XYZCorp" do |rule|
rule.accepts StockOffer
rule.salience = "-1"
rule.condition {|stock| stock.name == "XYZ" }
rule.condition {|stock| stock.recommend_purchase.nil? }
rule.condition {|stock| stock.price > 10 }
rule.consequence do |stock|
stock.recommend_purchase = :no
stock.print
end
end
rule "Stock Price Not Negative" do |rule|
rule.accepts StockOffer
rule.condition {|stock| stock.price < 0 }
rule.consequence do |stock|
stock.recommend_purchase = :no
stock.print
end
end
rule "Stock Price Low Enough" do |rule|
rule.accepts StockOffer
rule.condition {|stock| @dao_factory.stock_dao.on_stock_list? stock.name }
rule.condition {|stock| stock.recommend_purchase.nil? }
rule.condition {|stock| stock.price < 100 }
rule.consequence do |stock|
stock.recommend_purchase = :yes
stock.print
end
end
end
10:23:31 PM
|