unitsml 0.6.14 → 0.6.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/unitsml/conversion/register.rb +175 -0
- data/lib/unitsml/conversion.rb +157 -8
- data/lib/unitsml/errors/quantity_errors.rb +12 -0
- data/lib/unitsml/errors.rb +5 -0
- data/lib/unitsml/opal/database_payload.rb +1 -1
- data/lib/unitsml/opal/payload_generator.rb +10 -1
- data/lib/unitsml/opal.rb +1 -0
- data/lib/unitsml/quantity.rb +233 -17
- data/lib/unitsml/quantity_kind.rb +135 -0
- data/lib/unitsml/version.rb +1 -1
- data/lib/unitsml.rb +1 -0
- metadata +4 -2
|
@@ -54,7 +54,16 @@ module Unitsml
|
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
def database_hash
|
|
57
|
-
::Unitsdb::Database.from_db(unitsdb_data_dir).to_hash
|
|
57
|
+
database = ::Unitsdb::Database.from_db(unitsdb_data_dir).to_hash
|
|
58
|
+
return database if database.key?("scales")
|
|
59
|
+
|
|
60
|
+
scales_path = File.join(unitsdb_data_dir, "scales.yaml")
|
|
61
|
+
return database unless File.file?(scales_path)
|
|
62
|
+
|
|
63
|
+
# UnitsDB 2.2.13 ships typed scale models/data before exposing them
|
|
64
|
+
# through Database. Keep generated data stable across that API change.
|
|
65
|
+
scales = ::Unitsdb::Scales.from_yaml(File.read(scales_path)).to_hash
|
|
66
|
+
database.merge("scales" => scales.fetch("scales"))
|
|
58
67
|
end
|
|
59
68
|
|
|
60
69
|
# Custom serializer rather than Hash#inspect so the output is
|
data/lib/unitsml/opal.rb
CHANGED
data/lib/unitsml/quantity.rb
CHANGED
|
@@ -9,7 +9,7 @@ module Unitsml
|
|
|
9
9
|
# and division on non-ratio scales; the classification is pluggable
|
|
10
10
|
# per quantity until the unitsdb scale-legality data lands (#130).
|
|
11
11
|
class Quantity
|
|
12
|
-
attr_reader :value, :formula, :scale
|
|
12
|
+
attr_reader :value, :formula, :scale, :kind
|
|
13
13
|
|
|
14
14
|
# Distinguishes an omitted unit keyword from an explicit nil.
|
|
15
15
|
ABSENT = Object.new.freeze
|
|
@@ -18,8 +18,12 @@ module Unitsml
|
|
|
18
18
|
# unit: accepts a String (parsed, like unit_expression:), a Unit, a
|
|
19
19
|
# Formula (kept as-is, never reparsed) or nil (a pure number).
|
|
20
20
|
# unit_expression: remains the String-only spelling; give only one.
|
|
21
|
+
# kind: an optional data-declared quantity kind (a UnitsDB quantity
|
|
22
|
+
# identifier or name, a QuantityKind, or :primary for the primary
|
|
23
|
+
# quantity of a single unit). Untyped quantities (kind nil) keep the
|
|
24
|
+
# dimension-only gating.
|
|
21
25
|
def initialize(value:, unit: ABSENT, unit_expression: ABSENT,
|
|
22
|
-
scale: nil)
|
|
26
|
+
scale: nil, kind: nil)
|
|
23
27
|
unless unit.equal?(ABSENT) || unit_expression.equal?(ABSENT)
|
|
24
28
|
raise ArgumentError, "give only one of unit: or unit_expression:"
|
|
25
29
|
end
|
|
@@ -30,13 +34,18 @@ module Unitsml
|
|
|
30
34
|
# the expression (degC is continuous_interval); an explicit argument
|
|
31
35
|
# (the :difference typing of subtraction) overrides it.
|
|
32
36
|
@scale = (scale || derived_scale).to_sym
|
|
37
|
+
@kind = resolve_kind(kind)
|
|
33
38
|
end
|
|
34
39
|
|
|
35
40
|
# Canonical dimension identity; "1" (dimensionless) for pure numbers.
|
|
36
41
|
def dimension_id
|
|
37
42
|
@dimension_id ||= begin
|
|
38
43
|
list = dimension_list
|
|
39
|
-
|
|
44
|
+
# A fractional vector (m^(1/2)) has no unitsdb dimension identity;
|
|
45
|
+
# none is asserted for it.
|
|
46
|
+
if list && !fractional_vector?
|
|
47
|
+
Utility.dim_id(list) || "1"
|
|
48
|
+
end
|
|
40
49
|
end
|
|
41
50
|
end
|
|
42
51
|
|
|
@@ -44,6 +53,13 @@ module Unitsml
|
|
|
44
53
|
# Base-dimension exponents summed across the composed expression —
|
|
45
54
|
# V (L²MT⁻³I⁻¹) times A (I¹) merges to the watt vector L²MT⁻³.
|
|
46
55
|
def dimension_vector
|
|
56
|
+
# sqrt subtrees are not admitted: the parser overwrites the leaf
|
|
57
|
+
# exponents under a sqrt with 0.5 (sqrt(m) and sqrt(m^2) yield the
|
|
58
|
+
# same tree) and leaves composite radicands unhalved, so the tree
|
|
59
|
+
# does not determine the vector (#82 roots, deferred).
|
|
60
|
+
return nil if formula && sqrt_tree?(formula.value)
|
|
61
|
+
return fractional_dimension_vector if fractional_leaves?
|
|
62
|
+
|
|
47
63
|
list = raw_dimension_list
|
|
48
64
|
return nil if list.nil?
|
|
49
65
|
|
|
@@ -79,7 +95,10 @@ module Unitsml
|
|
|
79
95
|
if Compose.type?(Numeric, other)
|
|
80
96
|
gate_zero!(other)
|
|
81
97
|
gate_scaling!
|
|
82
|
-
|
|
98
|
+
# scaling keeps the scale typing: a scaled difference stays a
|
|
99
|
+
# difference (gate_scaling! admits only ratio and difference)
|
|
100
|
+
return Quantity.new(value: divide_values(value, other),
|
|
101
|
+
unit: copied_formula, scale: scale, kind: kind)
|
|
83
102
|
end
|
|
84
103
|
|
|
85
104
|
other = Compose.quantity_operand(other)
|
|
@@ -89,16 +108,24 @@ module Unitsml
|
|
|
89
108
|
"division by a zero quantity is indeterminate"
|
|
90
109
|
end
|
|
91
110
|
|
|
92
|
-
Quantity.new(value: value
|
|
111
|
+
Quantity.new(value: divide_values(value, other.value),
|
|
93
112
|
unit: compose(formula, other.formula, "/"))
|
|
94
113
|
end
|
|
95
114
|
|
|
96
115
|
# R-A4: differences are legal on both ratio and interval scales;
|
|
97
|
-
# only the dimension identity must match.
|
|
116
|
+
# only the dimension identity must match. An operand in another unit
|
|
117
|
+
# is first converted into this quantity's unit with convert_to (exact;
|
|
118
|
+
# a difference operand skips the affine addends): 1 km + 1 m is
|
|
119
|
+
# 1001/1000 km, not 2 km.
|
|
98
120
|
def +(other)
|
|
99
121
|
quantity_kind!(other)
|
|
100
122
|
gate_dimension!(other)
|
|
101
|
-
|
|
123
|
+
# differences sum to a difference (5 K + 5 K of difference converts
|
|
124
|
+
# to 10 K, never 283.15 K)
|
|
125
|
+
both_differences = [scale, other.scale].all?(:difference)
|
|
126
|
+
sum_scale = both_differences ? :difference : nil
|
|
127
|
+
Quantity.new(value: value + aligned_value(other), unit: copied_formula,
|
|
128
|
+
scale: sum_scale, kind: kind || other.kind)
|
|
102
129
|
end
|
|
103
130
|
|
|
104
131
|
def -(other)
|
|
@@ -106,9 +133,16 @@ module Unitsml
|
|
|
106
133
|
gate_dimension!(other)
|
|
107
134
|
# A difference of two quantities is difference-typed: its conversion
|
|
108
135
|
# skips the affine addends (20 degC - 15 degC converts to 5 K, not
|
|
109
|
-
# 278.15 K).
|
|
110
|
-
|
|
111
|
-
|
|
136
|
+
# 278.15 K). Subtracting a difference from an absolute value shifts
|
|
137
|
+
# that value and keeps its scale (20 degC - 5 K of difference is
|
|
138
|
+
# 15 degC, which converts to 288.15 K).
|
|
139
|
+
result_scale = if other.scale == :difference && scale != :difference
|
|
140
|
+
scale
|
|
141
|
+
else
|
|
142
|
+
:difference
|
|
143
|
+
end
|
|
144
|
+
Quantity.new(value: value - aligned_value(other), unit: copied_formula,
|
|
145
|
+
scale: result_scale, kind: kind || other.kind)
|
|
112
146
|
end
|
|
113
147
|
|
|
114
148
|
# R-A3: convert this quantity to another (dimension-compatible) unit
|
|
@@ -136,17 +170,109 @@ module Unitsml
|
|
|
136
170
|
((Rational(value) * src.factor) + src.offset - dst.offset) / dst.factor
|
|
137
171
|
end
|
|
138
172
|
|
|
139
|
-
|
|
173
|
+
# a converted difference is still a difference
|
|
174
|
+
Quantity.new(value: converted, unit_expression: target_formula.norm_text,
|
|
175
|
+
scale: scale == :difference ? :difference : nil,
|
|
176
|
+
kind: kind)
|
|
140
177
|
end
|
|
141
178
|
|
|
142
179
|
def ==(other)
|
|
143
180
|
other.is_a?(Quantity) && value == other.value &&
|
|
144
|
-
dimension_vector == other.dimension_vector
|
|
181
|
+
dimension_vector == other.dimension_vector && kinds_compatible?(other)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# The primary quantity of this quantity's unit, selected by the UnitsDB
|
|
185
|
+
# primary flag (never positional order); nil for pure numbers and
|
|
186
|
+
# composite expressions, which name no single unit record.
|
|
187
|
+
def primary_kind
|
|
188
|
+
record = single_unit_record
|
|
189
|
+
record && QuantityKind.primary_for(record)
|
|
145
190
|
end
|
|
146
191
|
# rubocop:enable Metrics/AbcSize, Layout/LineLength
|
|
147
192
|
|
|
148
193
|
private
|
|
149
194
|
|
|
195
|
+
# The other operand's magnitude in this quantity's unit. Same-unit and
|
|
196
|
+
# pure-number operands are taken as-is (no conversion, value types
|
|
197
|
+
# kept); otherwise convert_to applies the exact canonical conversion.
|
|
198
|
+
def aligned_value(other)
|
|
199
|
+
return other.value if formula.nil? || other.formula.nil?
|
|
200
|
+
return other.value if formula.norm_text == other.formula.norm_text
|
|
201
|
+
|
|
202
|
+
other.convert_to(formula).value
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def sqrt_tree?(nodes)
|
|
206
|
+
Array(nodes).any? do |node|
|
|
207
|
+
case node
|
|
208
|
+
when Sqrt then true
|
|
209
|
+
when Formula then sqrt_tree?(node.value)
|
|
210
|
+
when Fenced then sqrt_tree?([node.value])
|
|
211
|
+
else false
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def leaf_units(nodes)
|
|
217
|
+
Array(nodes).flat_map do |node|
|
|
218
|
+
case node
|
|
219
|
+
when Unit then [node]
|
|
220
|
+
when Formula then leaf_units(node.value)
|
|
221
|
+
when Fenced then leaf_units([node.value])
|
|
222
|
+
else []
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# Exact exponent of a parsed power ("1/2", "(1/2)", "-1"): an Integer
|
|
228
|
+
# when integral, otherwise a Rational.
|
|
229
|
+
def exact_exponent(power)
|
|
230
|
+
return 1 if power.nil?
|
|
231
|
+
|
|
232
|
+
power = power.value while power.is_a?(Fenced)
|
|
233
|
+
power = power.raw_value if power.respond_to?(:raw_value)
|
|
234
|
+
exact = case power
|
|
235
|
+
when Integer, Rational then Rational(power)
|
|
236
|
+
when Float then Rational(power.to_s)
|
|
237
|
+
else Rational(power.to_s.delete("() "))
|
|
238
|
+
end
|
|
239
|
+
exact.denominator == 1 ? exact.numerator : exact
|
|
240
|
+
rescue ArgumentError, ZeroDivisionError
|
|
241
|
+
raise Errors::InvalidPowerError.new(value: power, reason: :invalid_number)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def fractional_leaves?
|
|
245
|
+
return false unless formula
|
|
246
|
+
|
|
247
|
+
leaf_units(formula.value).any? do |unit|
|
|
248
|
+
exact_exponent(unit.power_numerator).is_a?(Rational)
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def fractional_vector?
|
|
253
|
+
dimension_vector&.values&.any?(Rational) || false
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Fractional leaf powers (m^(1/2), J^(1/2)) scale each leaf's exact
|
|
257
|
+
# unitsdb decomposition; nil when any leaf has no decomposition path.
|
|
258
|
+
def fractional_dimension_vector
|
|
259
|
+
vector = Hash.new(0)
|
|
260
|
+
leaf_units(formula.value).each do |unit|
|
|
261
|
+
dims = Conversion.unit_dimensions(
|
|
262
|
+
unit.unit_instance, exact_exponent(unit.power_numerator), []
|
|
263
|
+
)
|
|
264
|
+
return nil if dims.nil?
|
|
265
|
+
|
|
266
|
+
dims.each { |entry| vector[entry[:dimension]] += entry[:exponent] }
|
|
267
|
+
end
|
|
268
|
+
vector.transform_values! do |exponent|
|
|
269
|
+
exponent = Rational(exponent)
|
|
270
|
+
exponent.denominator == 1 ? exponent.numerator : exponent
|
|
271
|
+
end
|
|
272
|
+
vector.delete_if { |_, exponent| exponent.zero? }
|
|
273
|
+
vector
|
|
274
|
+
end
|
|
275
|
+
|
|
150
276
|
# The scale of a quantity is the scale of the units it composes: any
|
|
151
277
|
# interval-scaled unit (degC per unitsdb scale_reference) makes the
|
|
152
278
|
# quantity interval-scaled.
|
|
@@ -168,6 +294,8 @@ module Unitsml
|
|
|
168
294
|
# Base dimensions in canonical DIMS_VECTOR order; nil when all
|
|
169
295
|
# exponents cancel (dimensionless).
|
|
170
296
|
def dimension_list
|
|
297
|
+
return nil if dimension_vector.nil?
|
|
298
|
+
|
|
171
299
|
merged = Utility::DIMS_VECTOR.filter_map do |dimension|
|
|
172
300
|
exponent = dimension_vector[dimension]
|
|
173
301
|
next if exponent.zero?
|
|
@@ -235,22 +363,110 @@ module Unitsml
|
|
|
235
363
|
unless other.is_a?(Quantity)
|
|
236
364
|
raise ArgumentError, "expected a Unitsml::Quantity, got #{other.class}"
|
|
237
365
|
end
|
|
366
|
+
return if kinds_compatible?(other)
|
|
367
|
+
|
|
368
|
+
raise Errors::QuantityKindMismatchError,
|
|
369
|
+
"#{kind.id} (#{kind.name}) and #{other.kind.id} " \
|
|
370
|
+
"(#{other.kind.name}) are not interchangeable: no shared " \
|
|
371
|
+
"equivalence class"
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# Both typed: same quantity or same declared equivalence class. An
|
|
375
|
+
# untyped operand keeps the dimension-only gating.
|
|
376
|
+
def kinds_compatible?(other)
|
|
377
|
+
kind.nil? || other.kind.nil? || kind.compatible?(other.kind)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def resolve_kind(source)
|
|
381
|
+
return nil if source.nil?
|
|
382
|
+
|
|
383
|
+
resolved = if source == :primary
|
|
384
|
+
primary_kind || raise(
|
|
385
|
+
Errors::UnknownQuantityKindError,
|
|
386
|
+
"#{norm_text.inspect} names no single unit with a " \
|
|
387
|
+
"primary quantity",
|
|
388
|
+
)
|
|
389
|
+
else
|
|
390
|
+
QuantityKind.resolve(source)
|
|
391
|
+
end
|
|
392
|
+
check_kind_dimension!(resolved)
|
|
393
|
+
resolved
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
def check_kind_dimension!(resolved)
|
|
397
|
+
ours = dimension_id
|
|
398
|
+
theirs = resolved.dimension_id
|
|
399
|
+
return if ours.nil? || theirs.nil? || ours == theirs
|
|
400
|
+
|
|
401
|
+
raise Errors::DimensionMismatchError,
|
|
402
|
+
"quantity kind #{resolved.id} has dimension #{theirs}, the unit " \
|
|
403
|
+
"#{norm_text.inspect} has #{ours}"
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
# The UnitsDB record of a formula that is one unit at power 1 (a prefix
|
|
407
|
+
# is allowed: km is still a length unit).
|
|
408
|
+
def single_unit_record # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
409
|
+
return nil unless formula
|
|
410
|
+
|
|
411
|
+
terms = formula.value.grep_v(Extender)
|
|
412
|
+
return nil unless terms.size == 1 && terms.first.is_a?(Unit)
|
|
413
|
+
|
|
414
|
+
unit = terms.first
|
|
415
|
+
power = unit.power_numerator
|
|
416
|
+
power = power.raw_value if power.respond_to?(:raw_value)
|
|
417
|
+
return nil unless power.nil? || power.to_s == "1"
|
|
418
|
+
|
|
419
|
+
unit.unit_instance
|
|
238
420
|
end
|
|
239
421
|
|
|
240
422
|
def gate_dimension!(other)
|
|
423
|
+
ours = gating_vector
|
|
424
|
+
theirs = other.gating_vector
|
|
241
425
|
# Unknown dimensions (units without a decomposition path) defer the
|
|
242
426
|
# legality decision to the conversion layer.
|
|
243
|
-
return if
|
|
244
|
-
return if
|
|
427
|
+
return if ours.nil? || theirs.nil?
|
|
428
|
+
return if ours == theirs
|
|
245
429
|
|
|
246
430
|
raise Errors::DimensionMismatchError,
|
|
247
|
-
"dimension mismatch: #{
|
|
248
|
-
"vs #{other.dimension_vector.inspect}"
|
|
431
|
+
"dimension mismatch: #{ours.inspect} vs #{theirs.inspect}"
|
|
249
432
|
end
|
|
250
433
|
|
|
434
|
+
protected
|
|
435
|
+
|
|
436
|
+
# The UnitsDB dimension vector, or, for a unit without a decomposition
|
|
437
|
+
# path, the dimensions an active caller register declares for it. A
|
|
438
|
+
# register makes such a unit convertible, so it must also make it
|
|
439
|
+
# dimension-checked: otherwise 2 cwt (US) + 3 m would succeed.
|
|
440
|
+
def gating_vector
|
|
441
|
+
vector = dimension_vector
|
|
442
|
+
return vector unless vector.nil?
|
|
443
|
+
|
|
444
|
+
record = single_unit_record
|
|
445
|
+
register = Conversion.current_register
|
|
446
|
+
entry = record && register&.entry_for(record)
|
|
447
|
+
entry&.vector
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
private
|
|
451
|
+
|
|
251
452
|
def scalar_product(number)
|
|
252
453
|
gate_scaling!
|
|
253
|
-
|
|
454
|
+
# a scaled difference stays a difference: its conversion must keep
|
|
455
|
+
# skipping the affine addends
|
|
456
|
+
Quantity.new(value: value * number, unit: copied_formula, scale: scale,
|
|
457
|
+
kind: kind)
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# Integer / Integer is exact: Ruby's Integer#/ would drop the remainder
|
|
461
|
+
# (7 / 2 => 3), so integers divide by Rational and a whole result comes
|
|
462
|
+
# back as an Integer (6 / 2 => 3, 7 / 2 => (7/2)). Any other Numeric
|
|
463
|
+
# (Float, Rational, BigDecimal) keeps its own division.
|
|
464
|
+
def divide_values(dividend, divisor)
|
|
465
|
+
integers = dividend.is_a?(Integer) && divisor.is_a?(Integer)
|
|
466
|
+
return dividend / divisor unless integers
|
|
467
|
+
|
|
468
|
+
quotient = Rational(dividend, divisor)
|
|
469
|
+
quotient.denominator == 1 ? quotient.numerator : quotient
|
|
254
470
|
end
|
|
255
471
|
|
|
256
472
|
def gate_zero!(other)
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
# A data-declared quantity kind: one UnitsDB quantity record.
|
|
5
|
+
#
|
|
6
|
+
# Kind compatibility comes from the register, never from code: the
|
|
7
|
+
# quantities schema's equivalence_class is "L1 equivalence class:
|
|
8
|
+
# quantities in the same class are interchangeable for
|
|
9
|
+
# addition/subtraction arithmetic (work, energy and heat share the energy
|
|
10
|
+
# class). Absent means identity-only gating." (unitsdb
|
|
11
|
+
# schemas/quantities-schema.yaml:47-49). So work + heat is legal (both
|
|
12
|
+
# in the energy class) while torque + energy is refused although both
|
|
13
|
+
# have dimension L2MT-2.
|
|
14
|
+
class QuantityKind
|
|
15
|
+
attr_reader :record
|
|
16
|
+
|
|
17
|
+
# kind: a QuantityKind, a UnitsDB quantity record, or a String quantity
|
|
18
|
+
# identifier or name ("NISTq18", "work").
|
|
19
|
+
def self.resolve(kind)
|
|
20
|
+
case kind
|
|
21
|
+
when QuantityKind then kind
|
|
22
|
+
when ::Unitsdb::Quantity then new(kind)
|
|
23
|
+
when String
|
|
24
|
+
record = Utility.quantity_instance(kind)
|
|
25
|
+
unless record
|
|
26
|
+
raise Errors::UnknownQuantityKindError,
|
|
27
|
+
"unknown quantity kind #{kind.inspect}"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
new(record)
|
|
31
|
+
else
|
|
32
|
+
raise Errors::UnknownQuantityKindError,
|
|
33
|
+
"a quantity kind is a String identifier or name, " \
|
|
34
|
+
"got #{kind.class}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The primary quantity of a UnitsDB unit record. The units schema
|
|
39
|
+
# marks it explicitly: "True for the primary quantity of this unit ...
|
|
40
|
+
# Consumers select this reference instead of positional order."
|
|
41
|
+
# (unitsdb schemas/units-schema.yaml:147-149). Selection reads the flag
|
|
42
|
+
# only, so the result does not depend on the order of
|
|
43
|
+
# quantity_references in the data.
|
|
44
|
+
#
|
|
45
|
+
# A sole reference with the flag ABSENT is the unit's quantity: the
|
|
46
|
+
# schema leaves the field optional, and the release that introduced it
|
|
47
|
+
# flags only multi-reference units ("96 multi-reference units flagged,
|
|
48
|
+
# exactly one primary each", unitsdb 6f3d156, schema 2.2.0). That
|
|
49
|
+
# inference is not stated in the schema text itself. A reference marked
|
|
50
|
+
# primary: false is never promoted, even when it is the only one.
|
|
51
|
+
# Several references with no true flag, or more than one, is a register
|
|
52
|
+
# defect and fails explicitly.
|
|
53
|
+
def self.primary_for(unit_record) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
54
|
+
refs = unit_record&.quantity_references.to_a
|
|
55
|
+
return nil if refs.empty?
|
|
56
|
+
|
|
57
|
+
flagged = refs.select { |ref| ref.primary == true }
|
|
58
|
+
chosen = case flagged.size
|
|
59
|
+
when 1 then flagged.first
|
|
60
|
+
when 0 then sole_unflagged(unit_record, refs)
|
|
61
|
+
else
|
|
62
|
+
raise Errors::PrimaryQuantityError,
|
|
63
|
+
"unit #{unit_label(unit_record)} marks " \
|
|
64
|
+
"#{flagged.size} quantity references primary"
|
|
65
|
+
end
|
|
66
|
+
resolve(chosen.id)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.sole_unflagged(unit_record, refs)
|
|
70
|
+
if refs.size > 1
|
|
71
|
+
raise Errors::PrimaryQuantityError,
|
|
72
|
+
"unit #{unit_label(unit_record)} has #{refs.size} " \
|
|
73
|
+
"quantity references and none is marked primary"
|
|
74
|
+
end
|
|
75
|
+
return refs.first if refs.first.primary.nil?
|
|
76
|
+
|
|
77
|
+
raise Errors::PrimaryQuantityError,
|
|
78
|
+
"unit #{unit_label(unit_record)} marks its only quantity " \
|
|
79
|
+
"reference primary: false"
|
|
80
|
+
end
|
|
81
|
+
private_class_method :sole_unflagged
|
|
82
|
+
|
|
83
|
+
def self.unit_label(record)
|
|
84
|
+
record.identifiers.to_a.map(&:id).first.inspect
|
|
85
|
+
end
|
|
86
|
+
private_class_method :unit_label
|
|
87
|
+
|
|
88
|
+
def initialize(record)
|
|
89
|
+
@record = record
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def id
|
|
93
|
+
@id ||= record.identifiers.to_a.find { |i| i.type == "nist" }&.id ||
|
|
94
|
+
record.identifiers.to_a.first&.id
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def name
|
|
98
|
+
record.names.to_a.find { |n| n.lang == "en" }&.value ||
|
|
99
|
+
record.names.to_a.first&.value
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def equivalence_class
|
|
103
|
+
record.equivalence_class
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def dimension_id
|
|
107
|
+
record.dimension_reference&.id&.to_s
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Same quantity, or both in the same declared equivalence class. An
|
|
111
|
+
# absent class means identity-only gating.
|
|
112
|
+
def compatible?(other)
|
|
113
|
+
return true if id == other.id
|
|
114
|
+
|
|
115
|
+
!equivalence_class.nil? && equivalence_class == other.equivalence_class
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def ==(other)
|
|
119
|
+
other.is_a?(QuantityKind) && id == other.id
|
|
120
|
+
end
|
|
121
|
+
alias eql? ==
|
|
122
|
+
|
|
123
|
+
def hash
|
|
124
|
+
id.hash
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def to_s
|
|
128
|
+
id.to_s
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def inspect
|
|
132
|
+
"#<Unitsml::QuantityKind #{id} #{name.inspect}>"
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
data/lib/unitsml/version.rb
CHANGED
data/lib/unitsml.rb
CHANGED
|
@@ -29,6 +29,7 @@ module Unitsml
|
|
|
29
29
|
autoload :Utility, "unitsml/utility"
|
|
30
30
|
autoload :VERSION, "unitsml/version"
|
|
31
31
|
autoload :Quantity, "unitsml/quantity"
|
|
32
|
+
autoload :QuantityKind, "unitsml/quantity_kind"
|
|
32
33
|
autoload :Conversion, "unitsml/conversion"
|
|
33
34
|
autoload :Xml, "unitsml/xml"
|
|
34
35
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: unitsml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.6.
|
|
4
|
+
version: 0.6.15
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: asciiunits
|
|
@@ -115,6 +115,7 @@ files:
|
|
|
115
115
|
- lib/unitsml/configuration.rb
|
|
116
116
|
- lib/unitsml/conversion.rb
|
|
117
117
|
- lib/unitsml/conversion/definition.rb
|
|
118
|
+
- lib/unitsml/conversion/register.rb
|
|
118
119
|
- lib/unitsml/dimension.rb
|
|
119
120
|
- lib/unitsml/errors.rb
|
|
120
121
|
- lib/unitsml/errors/base_error.rb
|
|
@@ -181,6 +182,7 @@ files:
|
|
|
181
182
|
- lib/unitsml/prefix.rb
|
|
182
183
|
- lib/unitsml/prefix_adapter.rb
|
|
183
184
|
- lib/unitsml/quantity.rb
|
|
185
|
+
- lib/unitsml/quantity_kind.rb
|
|
184
186
|
- lib/unitsml/sqrt.rb
|
|
185
187
|
- lib/unitsml/unit.rb
|
|
186
188
|
- lib/unitsml/unitsdb.rb
|