software engineering
Software engineering, patterns, programming.







Subscribe to "software engineering" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Friday, September 2, 2005
 

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    comment []


Click here to visit the Radio UserLand website. © Copyright 2005 Nick Sieger.
Last update: 9/13/05; 8:45:14 PM.
This theme is based on the SoundWaves (blue) Manila theme.
September 2005
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
Aug   Oct