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