source: ruby/branches/0.5/lib/open_ehr/rm/data_types/quantity.rb@ 250

Last change on this file since 250 was 250, checked in by KOBAYASHI, Shinji, 15 years ago

move DvQuantity test to spec

File size: 11.9 KB
RevLine 
[88]1# This modules are implemented from the UML shown bellow
2# http://www.openehr.org/uml/release-1.0.1/Browsable/_9_0_76d0249_1109599337877_94556_1510Report.html
3# Ticket refs #50
[175]4#require 'assumed_library_types'
[216]5module OpenEHR
[4]6 module RM
[167]7 module DataTypes
[4]8 module Quantity
[216]9 class DvOrdered < OpenEHR::RM::DataTypes::Basic::DataValue
[4]10 include Comparable
[138]11 attr_accessor :normal_range, :other_refference_ranges, :normal_status
[122]12
[216]13 def initialize(args = {})
[246]14 super(args)
[216]15 self.normal_range = args[:normal_range]
16 self.normal_status = args[:normal_status]
17 self.other_reference_ranges = args[:other_reference_ranges]
[4]18 end
[122]19
[4]20 def is_normal?
[124]21 if @normal_range.nil? and @normal_status.nil?
22 return false
23 elsif !@normal_range.nil?
24 return @normal_range.has(@value)
25 elsif !@normal_status.nil?
26 return @normal_status.code_string == 'N'
27 end
[4]28 end
[122]29
[4]30 def is_simple?
[243]31 return @other_reference_ranges.nil?
[4]32 end
[122]33
34 def <=>(other)
[124]35 raise NotImplementedError, 'This method should be implemented'
[122]36 end
37
38 def other_reference_ranges=(other_reference_ranges)
[243]39 if !other_reference_ranges.nil? && other_reference_ranges.empty?
[4]40 raise ArgumentError, "Other reference ranges validity error"
41 end
[123]42 @other_reference_ranges = other_reference_ranges
[4]43 end
[123]44
[248]45 def is_strictly_comparable_to?(others)
46 if others.instance_of? self.class
47 return true
48 else
49 return false
50 end
[137]51 end
[4]52 end
[88]53
[216]54 class DvInterval < OpenEHR::AssumedLibraryTypes::Interval
[141]55
56 end
[142]57
[167]58 class DvQuantified < DvOrdered
[146]59 attr_reader :magnitude, :magnitude_status
[137]60
[247]61 def initialize(args = {})
62 super(args)
63 self.magnitude = args[:magnitude]
64 self.magnitude_status = args[:magnitude_status]
[137]65 end
66
67 def <=>(others)
[247]68 @magnitude <=> others.magnitude
[137]69 end
70
[144]71 def magnitude=(magnitude)
72 raise ArgumentError, 'magnitude should not be nil' if magnitude.nil?
73 @magnitude = magnitude
74 end
[140]75
[144]76 def magnitude_status=(magnitude_status)
77 if magnitude_status.nil?
78 @magnitude_status = '='
[167]79 elsif DvQuantified.valid_magnitude_status?(magnitude_status)
[144]80 @magnitude_status = magnitude_status
81 else
82 raise ArgumentError, 'magnitude_status invalid'
83 end
[4]84 end
[144]85
86 def accuracy_unknown?
[148]87 return @accuracy.nil?
[144]88 end
89
90 def self.valid_magnitude_status?(s)
91 if s == '=' || s == '>' || s == '<' || s == '<=' ||
92 s == '>=' || s == '~'
93 return true
94 else
95 return false
96 end
97 end
[4]98 end
[88]99
[167]100 class DvOrdinal < DvOrdered
[142]101 attr_reader :value, :symbol, :limits
102
[246]103 def initialize(args = {})
104 super(args)
105 self.symbol = args[:symbol]
106 self.limits = args[:limits]
[140]107 end
[137]108
[142]109 def value=(value)
110 raise ArgumentError, 'value should not be nil' if value.nil?
111 @value = value
112 end
[140]113
[142]114 def symbol=(symbol)
115 raise ArgumentError,'symbol should not be nil' if symbol.nil?
116 @symbol = symbol
[88]117 end
[137]118
[139]119 def <=>(other)
[142]120 @value <=> other.value
[139]121 end
[140]122
[142]123 def limits=(limits)
124 unless limits.nil? or limits.meaning.value == 'limits'
125 raise ArgumentError, 'invalid limits'
126 else
127 @limits = limits
128 end
[88]129 end
[248]130 def is_strictly_comparable_to?(others)
[249]131 unless super(others)
[248]132 return false
133 end
134 unless others.symbol.defining_code.terminology_id.value ==
135 @symbol.defining_code.terminology_id.value
136 return false
137 else
138 return true
139 end
140 end
[88]141 end
142
[167]143 class DvAbsoluteQuantity < DvQuantified
[148]144 attr_accessor :accuracy
[88]145
[249]146 def initialize(args = {})
147 super(args)
[250]148 self.accuracy = args[:accuracy]
[148]149 end
150
[88]151 def add(a_diff)
[249]152 type_check(a_diff)
153 return result_builder(DvAbsoluteQuantity,
154 @magnitude+a_diff.magnitude)
[88]155 end
156
157 def diff(other)
[249]158 type_check(other)
159 return result_builder(DvAmount,
160 (@magnitude-other.magnitude).abs)
[88]161 end
162
163 def subtract(a_diff)
[249]164 type_check(a_diff)
165 return result_builder(DvAbsoluteQuantity,
166 @magnitude-a_diff.magnitude)
[88]167 end
[249]168 private
169 def type_check(other)
170 unless self.is_strictly_comparable_to? other
171 raise ArgumentError, 'type mismatch'
172 end
173 end
174
175 def result_builder(klass, magnitude)
176 return klass.new(:magnitude => magnitude,
177 :magnitude_status => @magnitude_status,
178 :accuracy => @accuracy,
179 :accuracy_percent => @accuracy_percent,
180 :normal_range => @normal_range,
181 :normal_status => @normal_status,
182 :other_reference_ranges => @other_reference_ranges)
183 end
[88]184 end
[120]185
[167]186 class DvAmount < DvQuantified
[146]187 attr_reader :accuracy, :accuracy_percent
[248]188
189 def initialize(args = {})
190 super(args)
191 unless args[:accuracy].nil?
192 set_accuracy(args[:accuracy], args[:accuracy_percent])
[146]193 else
194 @accuracy, @accuracy_percent = nil, nil
195 end
[145]196 end
[147]197
[145]198 def +(other)
[248]199 unless self.is_strictly_comparable_to? other
[147]200 raise ArgumentError, 'type mismatch'
201 end
[248]202 result = self.dup
203 result.magnitude = @magnitude + other.magnitude
204 return result
[145]205 end
[140]206
[248]207 def -(other)
[147]208 other.magnitude = - other.magnitude
209 self+(other)
[122]210 end
[145]211
212 def set_accuracy(accuracy, accuracy_percent)
213 if accuracy_percent
214 raise ArgumentError, 'accuracy invalid' if accuracy < 0.0 || accuracy > 100.0
215 else
216 raise ArgumentError, 'accuracy invaild' if accuracy < 0.0 || accuracy > 1.0
217 end
218 @accuracy, @accuracy_percent = accuracy, accuracy_percent
[122]219 end
[145]220
221 def accuracy_is_percent?
222 return @accuracy_percent
223 end
[122]224 end
225
[167]226 class DvQuantity < DvAmount
[146]227 attr_reader :units, :precision
[250]228
229 def initialize(args = {})
230 super(args)
231 self.units = args[:units]
232 self.precision = args[:precision]
[146]233 end
[140]234
[146]235 def units=(units)
236 raise ArgumentError, 'units should not be nil' if units.nil?
237 @units = units
238 end
239
240 def precision=(precision)
241 unless precision.nil? || precision >= -1
242 raise ArgumentError, 'precision invalid'
243 end
244 @precision = precision
245 end
246
247 def is_strictly_comparable_to?(others)
[250]248 unless super(others)
249 return false
250 end
251 if others.units == @units
[146]252 return true
253 else
254 return false
255 end
256 end
257
258 def is_integral?
259 if @precision.nil? || precision != 0
260 return false
261 else
262 return true
263 end
264 end
[147]265 end
266
[167]267 class DvCount < DvAmount
[248]268
[140]269 end
270
[167]271 class ReferenceRange
[144]272 attr_reader :meaning, :range
[141]273
[245]274 def initialize(args = {})
275 self.meaning = args[:meaning]
276 self.range = args[:range]
[141]277 end
278
279 def meaning=(meaning)
280 if meaning.nil?
281 raise ArgumentError, 'meaning should not be nil'
282 end
283 @meaning = meaning
284 end
[144]285
286 def range=(range)
287 if range.nil?
288 raise ArgumentError, 'range should not be nil'
289 end
290 @range = range
291 end
292
293 def is_in_range?(val)
294 return @range.has?(val)
295 end
[124]296 end
[139]297
[167]298 module ProportionKind
[139]299 PK_RATIO = 0
300 PK_UNITARY = 1
301 PK_PERCENT = 2
302 PK_FRACTION = 3
303 PK_INTEGER_FRACTION = 4
304
[167]305 def ProportionKind.valid_proportion_kind?(kind)
[139]306 return true if kind >= 0 && kind <= 4
307 return false
308 end
[167]309 end # end of ProportionKind
[147]310
[167]311 class DvProportion < DvAmount
312 include ProportionKind
[147]313 attr_reader :numerator, :denominator, :type, :precision
314
315 def initialize(numerator, denominator, type, precision=nil,
316 magnitude_status=nil, accuracy=nil,
317 accuracy_percent=nil, normal_range=nil,
318 normal_status = nil, other_reference_ranges=nil)
319 self.type = type
320 self.numerator = numerator
321 self.denominator = denominator
322 self.precision = precision
[148]323 self.magnitude_status = magnitude_status
324 unless accuracy.nil?
325 set_accuracy(accuracy, accuracy_percent)
326 else
327 @accuracy, @accuracy_percent = nil, nil
328 end
329 self.normal_range = normal_range
330 self.normal_status = normal_status
331 self.other_reference_ranges = other_reference_ranges
[147]332 end
333
334 def numerator=(numerator)
335 raise ArgumentError, 'numerator should not be nil' if numerator.nil?
336 if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
337 !numerator.integer?
338 raise ArgumentError, 'numerator invalid for type'
339 end
340 @numerator = numerator
341 end
342
343 def denominator=(denominator)
344 if denominator.nil? or denominator == PK_RATIO
345 raise ArgumentError, 'denominator invalid'
346 end
347 if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
348 !denominator.integer?
349 raise ArgumentError, 'denominator invalid for type'
350 end
351 if @type == PK_UNITARY && denominator != 1
352 raise ArgumentError, 'denominator invalid for type'
353 end
354 if @type == PK_PERCENT && denominator != 100
355 raise ArgumentError, 'denominator invaild for type'
356 end
357 @denominator = denominator
358 end
359
360 def type=(type)
[167]361 if ProportionKind.valid_proportion_kind?(type)
[147]362 @type = type
363 else
364 raise ArgumentError, 'type invalid'
365 end
366 end
367
368 def magnitude
369 return numerator.to_f/denominator.to_f
370 end
371
372 def precision=(precision)
373 unless precision.nil?
374 unless precision == 0 || self.is_integral?
375 @precision = precision
376 else
377 raise ArgumentError, 'precision invalid'
378 end
379 end
380 end
381
382 def is_integral?
383 return denominator.integer? && numerator.integer?
384 end
385
386 def is_strictly_comparable_to?(other)
[167]387 unless other.instance_of?(DvProportion)
[147]388 return false
389 end
390 if other.type == @type
391 return true
392 else
393 return false
394 end
395 end
[167]396 end # end of DvProportion
[4]397 end # of Quantity
398 end # of Data_Types
399 end # of RM
400end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.