Changeset 147 for ruby/trunk
- Timestamp:
- Jun 29, 2009, 1:59:14 AM (15 years ago)
- Location:
- ruby/trunk/lib/models
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ruby/trunk/lib/models/rm/data_types/quantity.rb
r146 r147 177 177 end 178 178 end 179 179 180 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) 181 187 end 182 188 183 189 def -(other) 184 raise NotImplementError, '- operator must be overloaded' 190 other.magnitude = - other.magnitude 191 self+(other) 185 192 end 186 193 … … 240 247 # accuracy??? 241 248 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, 246 251 @magnitude_status, @precision, 247 252 @accuracy, @accuracy_percent, @normal_range, 248 253 @normal_status, @other_reference_ranges) 249 254 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 253 265 end 254 266 end … … 293 305 end 294 306 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 295 386 end # of Quantity 296 387 end # of Data_Types -
ruby/trunk/lib/models/tests/rm/test_data_types.rb
r146 r147 183 183 assert_nothing_raised(Exception){ 184 184 @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)} 185 189 end 186 190 … … 195 199 assert_instance_of OpenEHR::RM::Data_Types::Quantity::DV_Amount, @dv_amount 196 200 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 197 203 end 198 204 … … 216 222 217 223 def test_dv_interval 218 assert @dv_interval.lower < @dv_interval.upper 224 assert @dv_interval.lower < @dv_interval.upper 219 225 end 220 226 … … 298 304 end 299 305 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 300 315 def test_proportion_kind 301 316 assert_equal 0, OpenEHR::RM::Data_Types::Quantity::Proportion_Kind::PK_RATIO … … 309 324 assert !OpenEHR::RM::Data_Types::Quantity::Proportion_Kind.valid_proportion_kind?(5) 310 325 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 311 349 end 312 350
Note:
See TracChangeset
for help on using the changeset viewer.