source: ruby/trunk/lib/models/rm/data_types/quantity.rb@ 146

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

ref #50
DV_Quantity finished

File size: 9.3 KB
Line 
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
4require 'assumed_library_types'
5module OpenEHR
6 module RM
7 module Data_Types
8 module Quantity
9
10 autoload :Date_Time, "rm/data_types/quantity/date_time.rb"
11
12 class DV_Ordered < OpenEHR::RM::Data_Types::Basic::Data_Value
13 include Comparable
14 attr_accessor :normal_range, :other_refference_ranges, :normal_status
15
16 def initialize(normal_range=nil, normal_status = nil,
17 other_reference_ranges=nil)
18 self.normal_range = normal_range
19 self.normal_status = normal_status
20 self.other_reference_ranges = other_reference_ranges
21 end
22
23 def is_normal?
24 if @normal_range.nil? and @normal_status.nil?
25 return false
26 elsif !@normal_range.nil?
27 return @normal_range.has(@value)
28 elsif !@normal_status.nil?
29 return @normal_status.code_string == 'N'
30 end
31 end
32
33 def is_simple?
34 normal_status.nil? and other_refference_ranges.nil?
35 end
36
37 def <=>(other)
38 raise NotImplementedError, 'This method should be implemented'
39 end
40
41 def other_reference_ranges=(other_reference_ranges)
42 unless other_reference_ranges.nil? or !other_reference_ranges.is_empty?
43 raise ArgumentError, "Other reference ranges validity error"
44 end
45 @other_reference_ranges = other_reference_ranges
46 end
47
48 def is_strictly_comparable_to?(other)
49 raise NotImplementedError, 'this method should be implemented'
50 end
51 end
52
53 class DV_Interval < OpenEHR::Assumed_Library_Types::Interval
54
55 end
56
57 class DV_Quantified < DV_Ordered
58 attr_reader :magnitude, :magnitude_status
59
60 def initialize(magnitude, magnitude_status=nil,
61 normal_range=nil, normal_status = nil,
62 other_reference_ranges=nil)
63 super(normal_range, normal_status, other_reference_ranges)
64 self.magnitude = magnitude
65 self.magnitude_status = magnitude_status
66 end
67
68 def <=>(others)
69 @value <=> others.value
70 end
71
72 def magnitude=(magnitude)
73 raise ArgumentError, 'magnitude should not be nil' if magnitude.nil?
74 @magnitude = magnitude
75 end
76
77 def magnitude_status=(magnitude_status)
78 if magnitude_status.nil?
79 @magnitude_status = '='
80 elsif DV_Quantified.valid_magnitude_status?(magnitude_status)
81 @magnitude_status = magnitude_status
82 else
83 raise ArgumentError, 'magnitude_status invalid'
84 end
85 end
86
87 def accuracy=(accuracy)
88 raise NotImplementedError, 'subclasses need to implemented'
89 end
90
91 def accuracy_unknown?
92 return accuracy.nil?
93 end
94
95 def self.valid_magnitude_status?(s)
96 if s == '=' || s == '>' || s == '<' || s == '<=' ||
97 s == '>=' || s == '~'
98 return true
99 else
100 return false
101 end
102 end
103 end
104
105 class DV_Ordinal < DV_Ordered
106 attr_reader :value, :symbol, :limits
107
108 def initialize(value, symbol, limits=nil, normal_range=nil,
109 normal_status = nil, other_reference_ranges=nil)
110 self.value = value
111 self.symbol = symbol
112 self.limits = limits
113 super(normal_range, normal_status, other_reference_ranges)
114 end
115
116 def value=(value)
117 raise ArgumentError, 'value should not be nil' if value.nil?
118 @value = value
119 end
120
121 def symbol=(symbol)
122 raise ArgumentError,'symbol should not be nil' if symbol.nil?
123 @symbol = symbol
124 end
125
126 def is_strictly_comparable_to?(others)
127 unless others.instance_of? OpenEHR::RM::Data_Types::Quantity::DV_Ordinal
128 return false
129 end
130 unless others.symbol.defining_code.terminology_id.value ==
131 @symbol.defining_code.terminology_id.value
132 return false
133 end
134 return true
135 end
136
137 def <=>(other)
138 @value <=> other.value
139 end
140
141 def limits=(limits)
142 unless limits.nil? or limits.meaning.value == 'limits'
143 raise ArgumentError, 'invalid limits'
144 else
145 @limits = limits
146 end
147 end
148 end
149
150 class DV_Absolute_Quantity < DV_Quantified
151 attr_reader :accuracy
152
153 def add(a_diff)
154 raise NotImplementError, 'add must be implemented'
155 end
156
157 def diff(other)
158 raise NotImplementError, 'diff must be implemented'
159 end
160
161 def subtract(a_diff)
162 raise NotImplementError, 'subtract must be implemented'
163 end
164 end
165
166 class DV_Amount < DV_Quantified
167 attr_reader :accuracy, :accuracy_percent
168 def initialize(magnitude, magnitude_status=nil, accuracy=nil,
169 accuracy_percent=nil, normal_range=nil,
170 normal_status = nil, other_reference_ranges=nil)
171 super(magnitude, magnitude_status, normal_range,
172 normal_status, other_reference_ranges)
173 unless accuracy.nil?
174 set_accuracy(accuracy, accuracy_percent)
175 else
176 @accuracy, @accuracy_percent = nil, nil
177 end
178 end
179 def +(other)
180 raise NotImplementError, '+ operator must be overloaded'
181 end
182
183 def -(other)
184 raise NotImplementError, '- operator must be overloaded'
185 end
186
187 def set_accuracy(accuracy, accuracy_percent)
188 if accuracy_percent
189 raise ArgumentError, 'accuracy invalid' if accuracy < 0.0 || accuracy > 100.0
190 else
191 raise ArgumentError, 'accuracy invaild' if accuracy < 0.0 || accuracy > 1.0
192 end
193 @accuracy, @accuracy_percent = accuracy, accuracy_percent
194 end
195
196 def accuracy_is_percent?
197 return @accuracy_percent
198 end
199 end
200
201 class DV_Quantity < DV_Amount
202 attr_reader :units, :precision
203 def initialize(magnitude, units, magnitude_status=nil, precision=nil,
204 accuracy=nil, accuracy_percent=nil, normal_range=nil,
205 normal_status = nil, other_reference_ranges=nil)
206 super(magnitude, magnitude_status, accuracy, accuracy_percent,
207 normal_range, normal_status, other_reference_ranges)
208 self.units = units
209 self.precision = precision
210 end
211
212 def units=(units)
213 raise ArgumentError, 'units should not be nil' if units.nil?
214 @units = units
215 end
216
217 def precision=(precision)
218 unless precision.nil? || precision >= -1
219 raise ArgumentError, 'precision invalid'
220 end
221 @precision = precision
222 end
223
224 def is_strictly_comparable_to?(others)
225 return false if others.nil?
226 if others.instance_of?(DV_Quantity) && others.units == @units
227 return true
228 else
229 return false
230 end
231 end
232
233 def is_integral?
234 if @precision.nil? || precision != 0
235 return false
236 else
237 return true
238 end
239 end
240# accuracy???
241 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,
246 @magnitude_status, @precision,
247 @accuracy, @accuracy_percent, @normal_range,
248 @normal_status, @other_reference_ranges)
249 end
250 def -(other)
251 other.magnitude = - other.magnitude
252 self+(other)
253 end
254 end
255
256 class Reference_Range
257 attr_reader :meaning, :range
258
259 def initialize(meaning, range)
260 self.meaning = meaning
261 self.range = range
262 end
263
264 def meaning=(meaning)
265 if meaning.nil?
266 raise ArgumentError, 'meaning should not be nil'
267 end
268 @meaning = meaning
269 end
270
271 def range=(range)
272 if range.nil?
273 raise ArgumentError, 'range should not be nil'
274 end
275 @range = range
276 end
277
278 def is_in_range?(val)
279 return @range.has?(val)
280 end
281 end
282
283 module Proportion_Kind
284 PK_RATIO = 0
285 PK_UNITARY = 1
286 PK_PERCENT = 2
287 PK_FRACTION = 3
288 PK_INTEGER_FRACTION = 4
289
290 def Proportion_Kind.valid_proportion_kind?(kind)
291 return true if kind >= 0 && kind <= 4
292 return false
293 end
294 end # end of Proportion_Kind
295 end # of Quantity
296 end # of Data_Types
297 end # of RM
298end # of OpenEHR
Note: See TracBrowser for help on using the repository browser.