From: Alex McHale Date: 2004-05-26T23:15:56+09:00 Subject: Re: defining condititions This really isn't particularly what you're after, but here's a nibble that will evaluate an array that is assumed to be flip-flopping between ands and ors. The simple case will be DNF or CNF. def or_rule conditions if conditions.is_a? Array conditions.each do |element| return true if and_rule(element) end false else true && conditions end end def and_rule conditions if conditions.is_a? Array result = true conditions.each do |element| result &&= or_rule(element) end result else true && conditions end end and_rule([true, [false, false]]) ==> (true AND (false OR false)) ==> false and_rule([true, [true, false]]) ==> (true AND (true OR false)) ==> true and_rule([true, [[true, false], false]]) ==> (true AND ((true AND false) OR false)) ==> false and_rule([true, [[true, true], false]]) ==> (true AND ((true AND true) OR false)) ==> true Anyway, it's a similar topic, so I thought I would post it. The only abnormality is that and_rule([]) is true, or_rule([]) is false, and and_rule([[]]) is false. Alex On Wed, 26 May 2004 19:38:44 +0900, Sascha Doerdelmann wrote: > > Florian Weber wrote: > > my_set.rules = !name("orange") && hasProperty("color") && > > (hasProperty("size") || hasProperty("weight")) > > Apply the composite pattern, see > http://c2.com/cgi/wiki?CompositePattern > or > http://unicoi.kennesaw.edu/~jbamford/csis4650/uml/GoF_Patterns/composite.htm > > Forget about the addComponent, removeComponent messages. You need a > different way to build composite rules. As in the pattern you need an > abstract class for all components. Call it AbstractRule and build > subclasses for composed rules and concrete rules. Implement messages > like "and", "or", etc. in AbstractRule, each of them should return a > composed rule. Depending on your implementation of the composed rules > you might overwrite these messages in composed rules. > > Cheers > Sascha > >