[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
|
---|
[298] | 4 | #require 'assumed_library_types'
|
---|
[297] | 5 | module OpenEHR
|
---|
[4] | 6 | module RM
|
---|
[167] | 7 | module DataTypes
|
---|
[4] | 8 | module Quantity
|
---|
[297] | 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 |
|
---|
[298] | 13 | def initialize(args = {})
|
---|
| 14 | super(args)
|
---|
| 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?
|
---|
[298] | 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)
|
---|
[298] | 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 |
|
---|
[298] | 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 |
|
---|
[297] | 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 |
|
---|
[298] | 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)
|
---|
[369] | 68 | self.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 |
|
---|
[298] | 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
|
---|
[298] | 130 | def is_strictly_comparable_to?(others)
|
---|
| 131 | unless super(others)
|
---|
| 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 |
|
---|
[298] | 146 | def initialize(args = {})
|
---|
| 147 | super(args)
|
---|
| 148 | self.accuracy = args[:accuracy]
|
---|
[148] | 149 | end
|
---|
| 150 |
|
---|
[88] | 151 | def add(a_diff)
|
---|
[298] | 152 | type_check(a_diff)
|
---|
[369] | 153 | return result_builder(self.class,
|
---|
[298] | 154 | @magnitude+a_diff.magnitude)
|
---|
[88] | 155 | end
|
---|
| 156 |
|
---|
| 157 | def diff(other)
|
---|
[298] | 158 | type_check(other)
|
---|
[369] | 159 | return result_builder(self.class,
|
---|
[298] | 160 | (@magnitude-other.magnitude).abs)
|
---|
[88] | 161 | end
|
---|
| 162 |
|
---|
| 163 | def subtract(a_diff)
|
---|
[298] | 164 | type_check(a_diff)
|
---|
[369] | 165 | return result_builder(self.class,
|
---|
[298] | 166 | @magnitude-a_diff.magnitude)
|
---|
[88] | 167 | end
|
---|
[298] | 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
|
---|
[298] | 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)
|
---|
[298] | 199 | unless self.is_strictly_comparable_to? other
|
---|
[147] | 200 | raise ArgumentError, 'type mismatch'
|
---|
| 201 | end
|
---|
[298] | 202 | result = self.dup
|
---|
| 203 | result.magnitude = @magnitude + other.magnitude
|
---|
| 204 | return result
|
---|
[145] | 205 | end
|
---|
[140] | 206 |
|
---|
[298] | 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
|
---|
[298] | 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)
|
---|
[298] | 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
|
---|
[298] | 268 |
|
---|
[140] | 269 | end
|
---|
| 270 |
|
---|
[167] | 271 | class ReferenceRange
|
---|
[144] | 272 | attr_reader :meaning, :range
|
---|
[141] | 273 |
|
---|
[298] | 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 |
|
---|
[298] | 315 | def initialize(args = {})
|
---|
| 316 | self.type = args[:type]
|
---|
| 317 | self.numerator = args[:numerator]
|
---|
| 318 | self.denominator = args[:denominator]
|
---|
| 319 | self.precision = args[:precision]
|
---|
| 320 | self.magnitude_status =args[:magnitude_status]
|
---|
| 321 | unless args[:accuracy].nil?
|
---|
| 322 | set_accuracy(args[:accuracy], args[:accuracy_percent])
|
---|
[148] | 323 | else
|
---|
| 324 | @accuracy, @accuracy_percent = nil, nil
|
---|
| 325 | end
|
---|
[298] | 326 | self.normal_range = args[:normal_range]
|
---|
| 327 | self.normal_status = args[:normal_status]
|
---|
| 328 | self.other_reference_ranges = args[:other_reference_ranges]
|
---|
[147] | 329 | end
|
---|
| 330 |
|
---|
| 331 | def numerator=(numerator)
|
---|
| 332 | raise ArgumentError, 'numerator should not be nil' if numerator.nil?
|
---|
| 333 | if (@type == PK_FRACTION || @type == PK_INTEGER_FRACTION) &&
|
---|
| 334 | !numerator.integer?
|
---|
| 335 | raise ArgumentError, 'numerator invalid for type'
|
---|
| 336 | end
|
---|
| 337 | @numerator = numerator
|
---|
| 338 | end
|
---|
| 339 |
|
---|
| 340 | def denominator=(denominator)
|
---|
[298] | 341 | case @type
|
---|
| 342 | when PK_UNITARY
|
---|
| 343 | unless denominator == 1
|
---|
| 344 | raise ArgumentError, 'Unitary denominator must be 1'
|
---|
| 345 | end
|
---|
| 346 | when PK_PERCENT
|
---|
| 347 | unless denominator == 100
|
---|
| 348 | raise ArgumentError, 'Percent denominator must be 100'
|
---|
| 349 | end
|
---|
| 350 | when PK_FRACTION, PK_INTEGER_FRACTION
|
---|
| 351 | unless denominator.integer? and @numerator.integer?
|
---|
| 352 | raise ArgumentError, 'Fraction numerator/denominator must be integer'
|
---|
| 353 | end
|
---|
[147] | 354 | end
|
---|
| 355 | @denominator = denominator
|
---|
| 356 | end
|
---|
| 357 |
|
---|
| 358 | def type=(type)
|
---|
[167] | 359 | if ProportionKind.valid_proportion_kind?(type)
|
---|
[147] | 360 | @type = type
|
---|
| 361 | else
|
---|
| 362 | raise ArgumentError, 'type invalid'
|
---|
| 363 | end
|
---|
| 364 | end
|
---|
| 365 |
|
---|
| 366 | def magnitude
|
---|
| 367 | return numerator.to_f/denominator.to_f
|
---|
| 368 | end
|
---|
| 369 |
|
---|
| 370 | def precision=(precision)
|
---|
| 371 | unless precision.nil?
|
---|
[298] | 372 | if (self.is_integral? && precision !=0)
|
---|
[147] | 373 | raise ArgumentError, 'precision invalid'
|
---|
| 374 | end
|
---|
| 375 | end
|
---|
[298] | 376 | @precision = precision
|
---|
[147] | 377 | end
|
---|
| 378 |
|
---|
| 379 | def is_integral?
|
---|
| 380 | return denominator.integer? && numerator.integer?
|
---|
| 381 | end
|
---|
| 382 |
|
---|
| 383 | def is_strictly_comparable_to?(other)
|
---|
[298] | 384 | unless super(other)
|
---|
[147] | 385 | return false
|
---|
| 386 | end
|
---|
| 387 | if other.type == @type
|
---|
| 388 | return true
|
---|
| 389 | else
|
---|
| 390 | return false
|
---|
| 391 | end
|
---|
| 392 | end
|
---|
[167] | 393 | end # end of DvProportion
|
---|
[4] | 394 | end # of Quantity
|
---|
| 395 | end # of Data_Types
|
---|
| 396 | end # of RM
|
---|
| 397 | end # of OpenEHR
|
---|