Ignore:
Timestamp:
Jun 29, 2009, 1:59:14 AM (15 years ago)
Author:
KOBAYASHI, Shinji
Message:

ref #50

Most of the quantity type pacage was finished.
Jobs have been accumulated for more than a year
Rest of the work on the quantity package
Is not so much. Tomorrow, I will finish this
Package, perfectly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ruby/trunk/lib/models/rm/data_types/quantity.rb

    r146 r147  
    177177            end
    178178          end
     179
    179180          def +(other)
    180             raise NotImplementError, '+ operator must be overloaded'
     181            unless self.is_strictly_comparable_to?(other)
     182              raise ArgumentError, 'type mismatch'
     183            end
     184            return DV_Amount.new(@magnitude+other.magnitude, @magnitude_status,
     185                                 @accuracy, @accuracy_percent, @normal_range,
     186                                 @normal_status, @other_reference_ranges)
    181187          end
    182188
    183189          def -(other)
    184             raise NotImplementError, '- operator must be overloaded'
     190            other.magnitude = - other.magnitude
     191            self+(other)
    185192          end
    186193
     
    240247# accuracy???
    241248          def +(other)
    242             unless self.is_strictly_comparable_to?(other)
    243               raise ArgumentError, 'type mismatch'
    244             end
    245             return DV_Quantity.new(@magnitude+other.magnitude, @units,
     249            dv_amount = super(other)
     250            return DV_Quantity.new(dv_amount.magnitude, @units,
    246251                                   @magnitude_status, @precision,
    247252                                   @accuracy, @accuracy_percent, @normal_range,
    248253                                   @normal_status, @other_reference_ranges)
    249254          end
    250           def -(other)
    251             other.magnitude = - other.magnitude
    252             self+(other)
     255        end
     256
     257        class DV_Count < DV_Amount
     258          def is_strictly_comparable_to?(others)
     259            return false if others.nil?
     260            if others.instance_of?(DV_Count)
     261              return true
     262            else
     263              return false
     264            end
    253265          end
    254266        end
     
    293305          end
    294306        end # end of Proportion_Kind
     307
     308        class DV_Proportion < DV_Amount
     309          include Proportion_Kind
     310          attr_reader :numerator, :denominator, :type, :precision
     311
     312          def initialize(numerator, denominator, type, precision=nil,
     313                         magnitude_status=nil, accuracy=nil,
     314                         accuracy_percent=nil, normal_range=nil,
     315                         normal_status = nil, other_reference_ranges=nil)
     316            self.type = type
     317            self.numerator = numerator
     318            self.denominator = denominator
     319            self.precision = precision
     320          end
     321
     322          def numerator=(numerator)
     323            raise ArgumentError, 'numerator should not be nil' if numerator.nil?
     324            if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
     325                !numerator.integer?
     326              raise ArgumentError, 'numerator invalid for type'
     327            end
     328            @numerator = numerator
     329          end
     330
     331          def denominator=(denominator)
     332            if denominator.nil? or denominator == PK_RATIO
     333              raise ArgumentError, 'denominator invalid'
     334            end
     335            if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
     336                !denominator.integer?
     337              raise ArgumentError, 'denominator invalid for type'
     338            end
     339            if @type == PK_UNITARY && denominator != 1
     340              raise ArgumentError, 'denominator invalid for type'
     341            end
     342            if @type == PK_PERCENT && denominator != 100
     343              raise ArgumentError, 'denominator invaild for type'
     344            end
     345            @denominator = denominator
     346          end
     347
     348          def type=(type)
     349            if Proportion_Kind.valid_proportion_kind?(type)
     350              @type = type
     351            else
     352              raise ArgumentError, 'type invalid'
     353            end
     354          end
     355
     356          def magnitude
     357            return numerator.to_f/denominator.to_f
     358          end
     359
     360          def precision=(precision)
     361            unless precision.nil?
     362              unless precision == 0 || self.is_integral?
     363                @precision = precision
     364              else
     365                raise ArgumentError, 'precision invalid'
     366              end
     367            end
     368          end
     369
     370          def is_integral?
     371            return denominator.integer? && numerator.integer?
     372          end
     373
     374          def is_strictly_comparable_to?(other)
     375            unless other.instance_of?(DV_Proportion)
     376              return false
     377            end
     378            if other.type == @type
     379              return true
     380            else
     381              return false
     382            end
     383          end
     384        end # end of DV_Proportion
     385
    295386      end # of Quantity
    296387    end # of Data_Types
Note: See TracChangeset for help on using the changeset viewer.