# Author:: Nicolas Pouillard . # Copyright:: Copyright (c) 2004, 2005 Uttk team. All rights reserved. # License:: LGPL # $Id: /fey/uttk/trunk/lib/uttk/weights/Weight.rb 8800 2005-10-09T11:12:27.126567Z ertai $ require 'delegate' module Uttk # FIXME: explain here how to write a new weight computation module Weights # FIXME: document me class Weight attr_reader :min, :max def initialize ( aFloat, min=0, max=1 ) if BASE.has_key? aFloat aFloat, min, max = BASE[aFloat] end aFloat = aFloat.to_f @val = aFloat @min, @max = min, max unless @min <= aFloat or aFloat <= @max raise ArgumentError, "#@min <= #{aFloat} <= #@max" end end BASE = { :START => [0, 0, 0], :PASS => [1, 0, 1], :FAIL => [0, 0, 1] }.freeze.each_value { |x| x.freeze } def start? [self, @max, @min].all? { |x| x.zero? } end def min? to_f == @min end def max? to_f == @max end def pass? ! zero? and max? end def fail? to_f != @max end def + ( rhs ) rhs = self.class.new(rhs) unless rhs.is_a? Weight self.class.new(to_f + rhs.to_f, @min + rhs.min, @max + rhs.max) end def * ( rhs ) f = rhs.to_f a, b, c = to_f * f, @min * f, @max * f b, c = c, b if f < 0 self.class.new(a, b, c) end def normalize ( weight ) diff = @max - @min if diff.zero? if @max.zero? and weight.zero? self.class.new(:PASS) else self.class.new(:FAIL) end else self.class.new((self - @min) / diff, 0, 1) end end attr_reader :val alias :get :val def long_get "#{get}[#@min, #@max]" end def == ( rhs ) rhs.class <= Weight and @min == rhs.min and @val == rhs.val and @max == rhs.max end def method_missing ( *a, &b ) @val.send(*a, &b) end end # class Weight end # module Weights end # module Uttk