Sha256: d50cac64bdff1d8345d9f09bec2dbea7f44bb310f742876fef670bf57a3cbec1

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved.
# License::   LGPL
# $Id: WExpr.rb 567 2005-04-13 08:00:06Z polrop $


module TTK

  module Weights

    class WExpr < Weight

      attr_accessor :expr

      def initialize ( aFloat, min=0, max=1, expr=nil )
        super(aFloat, min, max)
        @expr = expr || "#{to_f}[#@min, #@max]"
      end

      def + ( rhs )
        x = super
        x.expr = "(#{@expr} + #{rhs.to_s})[#{x.min}, #{x.max}]"
        x
      end

      def * ( rhs )
        x = super
        x.expr = "(#{to_s} * #{rhs.to_s})[#{x.min}, #{x.max}]"
        x
      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,
                      "((#{to_s} - #{min.to_s}) / (#{max.to_s} - #{min.to_s}))")
        end
      end


      def get
        @expr + " == #{to_f.to_s} == #{(to_f * 100).floor}%"
      end

      def to_s
        @expr
      end

      def to_yaml ( opts={} )
        if opts[:ttk]
          to_f.to_yaml(opts)
        else
          super
        end
      end

    end # class WExpr

  end # module Weights

end # module TTK

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ttk-0.1.576 lib/ttk/weights/WExpr.rb
ttk-0.2.1 lib/ttk/weights/WExpr.rb
ttk-0.1.579 lib/ttk/weights/WExpr.rb
ttk-0.1.580 lib/ttk/weights/WExpr.rb
ttk-0.2.0 lib/ttk/weights/WExpr.rb