Friday, September 2, 2005

Using Drools, If It Weren't For All That Bloody XML

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 []  trackback []
Crescent City State of Mind

Today was one of those days with too much going on in my head. Tops above all, of course was the Gulf Coast Hurricane aftermath.

Everything just seems so inconsequential compared to it. It feels like such a chore just to keep up with all the technology blogs that I usually am so interested in.

My soul is just aching for a sense of peace, but every thought races down south and manufactures a connection to some human entity that I don't even have consciousness of. It's not a real person, but an embodiment of a relationship I desire to have with a real person in the thick of the suffering just so I can experience a little of the same. All the tragedy that is going on has a way of making the minute, hum-drum aspects of life seem incredibly trite.

Rest well, ye souls of New Orleans past and present.


9:33:12 PM      comment []  trackback []