Changeset 147 for ruby/trunk


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.

Location:
ruby/trunk/lib/models
Files:
2 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
  • ruby/trunk/lib/models/tests/rm/test_data_types.rb

    r146 r147  
    183183    assert_nothing_raised(Exception){
    184184      @dv_quantity = OpenEHR::RM::Data_Types::Quantity::DV_Quantity.new(3, 'mg', '~')}
     185    assert_nothing_raised(Exception){
     186      @dv_count = OpenEHR::RM::Data_Types::Quantity::DV_Count.new(1)}
     187    assert_nothing_raised(Exception){
     188      @dv_proportion = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(2,3,0)}
    185189   end
    186190
     
    195199     assert_instance_of OpenEHR::RM::Data_Types::Quantity::DV_Amount, @dv_amount
    196200     assert_instance_of OpenEHR::RM::Data_Types::Quantity::DV_Quantity, @dv_quantity
     201     assert_instance_of OpenEHR::RM::Data_Types::Quantity::DV_Count, @dv_count
     202     assert_instance_of OpenEHR::RM::Data_Types::Quantity::DV_Proportion, @dv_proportion
    197203  end
    198204
     
    216222
    217223  def test_dv_interval
    218     assert @dv_interval.lower < @dv_interval.upper 
     224    assert @dv_interval.lower < @dv_interval.upper
    219225  end
    220226
     
    298304  end
    299305
     306  def test_dv_count
     307    assert_equal 1, @dv_count.magnitude
     308    dv_count2 = OpenEHR::RM::Data_Types::Quantity::DV_Count.new(2)
     309    dv_count3 = @dv_count + dv_count2
     310    assert 3, dv_count3.magnitude
     311    dv_count3 = dv_count2 - @dv_count
     312    assert 1, dv_count3.magnitude
     313  end
     314
    300315  def test_proportion_kind
    301316    assert_equal 0, OpenEHR::RM::Data_Types::Quantity::Proportion_Kind::PK_RATIO
     
    309324    assert !OpenEHR::RM::Data_Types::Quantity::Proportion_Kind.valid_proportion_kind?(5)
    310325  end
     326
     327  def test_dv_proportion
     328    assert_equal 2.0, @dv_proportion.numerator
     329    assert_equal 3.0, @dv_proportion.denominator
     330    assert_equal 0, @dv_proportion.type
     331    assert_equal 2.0/3.0, @dv_proportion.magnitude
     332    assert @dv_proportion.is_integral?
     333    dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(1,3,0)
     334    assert @dv_proportion.is_strictly_comparable_to?(dv_proportion2)
     335    dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(1,3,4)
     336    assert !@dv_proportion.is_strictly_comparable_to?(dv_proportion2)
     337    assert_raise(ArgumentError){
     338      dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(1.5,2.3,3)}
     339    assert_raise(ArgumentError){
     340      dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(10,10,1)}
     341    assert_nothing_raised(Exception){
     342      dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(10,1,1)}
     343    assert_raise(ArgumentError){
     344      dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(10,10,2)}
     345    assert_nothing_raised(Exception){
     346      dv_proportion2 = OpenEHR::RM::Data_Types::Quantity::DV_Proportion.new(10,100,2)}
     347  end
     348
    311349end
    312350
Note: See TracChangeset for help on using the changeset viewer.