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

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

move DvAmount, DvCount 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)
131 super
132 unless others.instance_of? self.class
133 return false
134 end
135 unless others.symbol.defining_code.terminology_id.value ==
136 @symbol.defining_code.terminology_id.value
137 return false
138 else
139 return true
140 end
141 end
[88]142 end
143
[167]144 class DvAbsoluteQuantity < DvQuantified
[148]145 attr_accessor :accuracy
[88]146
[148]147 def initialize(magnitude, magnitude_status=nil, accuracy=nil,
148 normal_range=nil, normal_status = nil,
149 other_reference_ranges=nil)
[149]150 super(magnitude, magnitude_status, normal_range,
[148]151 normal_status, other_reference_ranges)
152 self.accuracy = accuracy
153 end
154
[88]155 def add(a_diff)
[149]156 raise NotImplementedError, 'add must be implemented'
[88]157 end
158
159 def diff(other)
[149]160 raise NotImplementedError, 'diff must be implemented'
[88]161 end
162
163 def subtract(a_diff)
[149]164 raise NotImplementedError, 'subtract must be implemented'
[88]165 end
166 end
[120]167
[167]168 class DvAmount < DvQuantified
[146]169 attr_reader :accuracy, :accuracy_percent
[248]170
171 def initialize(args = {})
172 super(args)
173 unless args[:accuracy].nil?
174 set_accuracy(args[:accuracy], args[:accuracy_percent])
[146]175 else
176 @accuracy, @accuracy_percent = nil, nil
177 end
[145]178 end
[147]179
[145]180 def +(other)
[248]181 unless self.is_strictly_comparable_to? other
[147]182 raise ArgumentError, 'type mismatch'
183 end
[248]184 result = self.dup
185 result.magnitude = @magnitude + other.magnitude
186 return result
[145]187 end
[140]188
[248]189 def -(other)
[147]190 other.magnitude = - other.magnitude
191 self+(other)
[122]192 end
[145]193
194 def set_accuracy(accuracy, accuracy_percent)
195 if accuracy_percent
196 raise ArgumentError, 'accuracy invalid' if accuracy < 0.0 || accuracy > 100.0
197 else
198 raise ArgumentError, 'accuracy invaild' if accuracy < 0.0 || accuracy > 1.0
199 end
200 @accuracy, @accuracy_percent = accuracy, accuracy_percent
[122]201 end
[145]202
203 def accuracy_is_percent?
204 return @accuracy_percent
205 end
[122]206 end
207
[167]208 class DvQuantity < DvAmount
[146]209 attr_reader :units, :precision
210 def initialize(magnitude, units, magnitude_status=nil, precision=nil,
211 accuracy=nil, accuracy_percent=nil, normal_range=nil,
212 normal_status = nil, other_reference_ranges=nil)
213 super(magnitude, magnitude_status, accuracy, accuracy_percent,
214 normal_range, normal_status, other_reference_ranges)
215 self.units = units
216 self.precision = precision
217 end
[140]218
[146]219 def units=(units)
220 raise ArgumentError, 'units should not be nil' if units.nil?
221 @units = units
222 end
223
224 def precision=(precision)
225 unless precision.nil? || precision >= -1
226 raise ArgumentError, 'precision invalid'
227 end
228 @precision = precision
229 end
230
231 def is_strictly_comparable_to?(others)
232 return false if others.nil?
[167]233 if others.instance_of?(DvQuantity) && others.units == @units
[146]234 return true
235 else
236 return false
237 end
238 end
239
240 def is_integral?
241 if @precision.nil? || precision != 0
242 return false
243 else
244 return true
245 end
246 end
247# accuracy???
248 def +(other)
[147]249 dv_amount = super(other)
[167]250 return DvQuantity.new(dv_amount.magnitude, @units,
[146]251 @magnitude_status, @precision,
252 @accuracy, @accuracy_percent, @normal_range,
253 @normal_status, @other_reference_ranges)
254 end
[147]255 end
256
[167]257 class DvCount < DvAmount
[248]258
[140]259 end
260
[167]261 class ReferenceRange
[144]262 attr_reader :meaning, :range
[141]263
[245]264 def initialize(args = {})
265 self.meaning = args[:meaning]
266 self.range = args[:range]
[141]267 end
268
269 def meaning=(meaning)
270 if meaning.nil?
271 raise ArgumentError, 'meaning should not be nil'
272 end
273 @meaning = meaning
274 end
[144]275
276 def range=(range)
277 if range.nil?
278 raise ArgumentError, 'range should not be nil'
279 end
280 @range = range
281 end
282
283 def is_in_range?(val)
284 return @range.has?(val)
285 end
[124]286 end
[139]287
[167]288 module ProportionKind
[139]289 PK_RATIO = 0
290 PK_UNITARY = 1
291 PK_PERCENT = 2
292 PK_FRACTION = 3
293 PK_INTEGER_FRACTION = 4
294
[167]295 def ProportionKind.valid_proportion_kind?(kind)
[139]296 return true if kind >= 0 && kind <= 4
297 return false
298 end
[167]299 end # end of ProportionKind
[147]300
[167]301 class DvProportion < DvAmount
302 include ProportionKind
[147]303 attr_reader :numerator, :denominator, :type, :precision
304
305 def initialize(numerator, denominator, type, precision=nil,
306 magnitude_status=nil, accuracy=nil,
307 accuracy_percent=nil, normal_range=nil,
308 normal_status = nil, other_reference_ranges=nil)
309 self.type = type
310 self.numerator = numerator
311 self.denominator = denominator
312 self.precision = precision
[148]313 self.magnitude_status = magnitude_status
314 unless accuracy.nil?
315 set_accuracy(accuracy, accuracy_percent)
316 else
317 @accuracy, @accuracy_percent = nil, nil
318 end
319 self.normal_range = normal_range
320 self.normal_status = normal_status
321 self.other_reference_ranges = other_reference_ranges
[147]322 end
323
324 def numerator=(numerator)
325 raise ArgumentError, 'numerator should not be nil' if numerator.nil?
326 if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
327 !numerator.integer?
328 raise ArgumentError, 'numerator invalid for type'
329 end
330 @numerator = numerator
331 end
332
333 def denominator=(denominator)
334 if denominator.nil? or denominator == PK_RATIO
335 raise ArgumentError, 'denominator invalid'
336 end
337 if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
338 !denominator.integer?
339 raise ArgumentError, 'denominator invalid for type'
340 end
341 if @type == PK_UNITARY && denominator != 1
342 raise ArgumentError, 'denominator invalid for type'
343 end
344 if @type == PK_PERCENT && denominator != 100
345 raise ArgumentError, 'denominator invaild for type'
346 end
347 @denominator = denominator
348 end
349
350 def type=(type)
[167]351 if ProportionKind.valid_proportion_kind?(type)
[147]352 @type = type
353 else
354 raise ArgumentError, 'type invalid'
355 end
356 end
357
358 def magnitude
359 return numerator.to_f/denominator.to_f
360 end
361
362 def precision=(precision)
363 unless precision.nil?
364 unless precision == 0 || self.is_integral?
365 @precision = precision
366 else
367 raise ArgumentError, 'precision invalid'
368 end
369 end
370 end
371
372 def is_integral?
373 return denominator.integer? && numerator.integer?
374 end
375
376 def is_strictly_comparable_to?(other)
[167]377 unless other.instance_of?(DvProportion)
[147]378 return false
379 end
380 if other.type == @type
381 return true
382 else
383 return false
384 end
385 end
[167]386 end # end of DvProportion
[4]387 end # of Quantity
388 end # of Data_Types
389 end # of RM
390end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.