unitsml 0.6.12 → 0.6.13
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/.github/workflows/rake.yml +3 -0
- data/Gemfile +0 -1
- data/docs/README.adoc +221 -1
- data/lib/unitsml/compose/builder.rb +347 -0
- data/lib/unitsml/compose/composable.rb +118 -0
- data/lib/unitsml/compose/composite.rb +145 -0
- data/lib/unitsml/compose/term_tree.rb +48 -0
- data/lib/unitsml/compose.rb +151 -0
- data/lib/unitsml/conversion/definition.rb +117 -0
- data/lib/unitsml/conversion.rb +97 -41
- data/lib/unitsml/dimension.rb +30 -3
- data/lib/unitsml/errors/base_error.rb +11 -0
- data/lib/unitsml/errors/empty_composition_error.rb +14 -0
- data/lib/unitsml/errors/invalid_power_error.rb +37 -0
- data/lib/unitsml/errors/invalid_unit_entry_error.rb +44 -0
- data/lib/unitsml/errors/misplaced_extender_error.rb +18 -0
- data/lib/unitsml/errors/mixed_terms_error.rb +15 -0
- data/lib/unitsml/errors/parse_error.rb +18 -0
- data/lib/unitsml/errors/unknown_dimension_error.rb +18 -0
- data/lib/unitsml/errors/unknown_prefix_error.rb +15 -0
- data/lib/unitsml/errors/unknown_unit_error.rb +16 -0
- data/lib/unitsml/errors.rb +11 -0
- data/lib/unitsml/formula.rb +65 -4
- data/lib/unitsml/model/unit.rb +2 -0
- data/lib/unitsml/model/units/conversion_note.rb +19 -0
- data/lib/unitsml/model/units/conversions.rb +25 -0
- data/lib/unitsml/model/units/float64_conversion_from.rb +62 -0
- data/lib/unitsml/model/units/special_conversion_from.rb +27 -0
- data/lib/unitsml/model/units/wsdl_conversion_from.rb +26 -0
- data/lib/unitsml/model/units.rb +5 -0
- data/lib/unitsml/number.rb +5 -1
- data/lib/unitsml/opal/database_payload.rb +1 -1
- data/lib/unitsml/opal.rb +22 -0
- data/lib/unitsml/parser.rb +12 -1
- data/lib/unitsml/power_numerator.rb +61 -0
- data/lib/unitsml/quantity.rb +95 -23
- data/lib/unitsml/unit.rb +60 -4
- data/lib/unitsml/unitsdb/quantities.rb +21 -0
- data/lib/unitsml/unitsdb/units.rb +6 -0
- data/lib/unitsml/utility.rb +61 -10
- data/lib/unitsml/version.rb +1 -1
- data/lib/unitsml.rb +10 -0
- metadata +23 -2
data/lib/unitsml/opal.rb
CHANGED
|
@@ -33,11 +33,20 @@ require "unitsml"
|
|
|
33
33
|
require "unitsml/version"
|
|
34
34
|
require "unitsml/errors"
|
|
35
35
|
require "unitsml/errors/base_error"
|
|
36
|
+
require "unitsml/errors/empty_composition_error"
|
|
36
37
|
require "unitsml/errors/invalid_model_error"
|
|
38
|
+
require "unitsml/errors/invalid_power_error"
|
|
39
|
+
require "unitsml/errors/invalid_unit_entry_error"
|
|
40
|
+
require "unitsml/errors/misplaced_extender_error"
|
|
41
|
+
require "unitsml/errors/mixed_terms_error"
|
|
37
42
|
require "unitsml/errors/opal_payload_not_bundled_error"
|
|
38
43
|
require "unitsml/errors/plurimath_load_error"
|
|
39
44
|
require "unitsml/errors/parg_load_error"
|
|
45
|
+
require "unitsml/errors/parse_error"
|
|
40
46
|
require "unitsml/errors/quantity_errors"
|
|
47
|
+
require "unitsml/errors/unknown_dimension_error"
|
|
48
|
+
require "unitsml/errors/unknown_prefix_error"
|
|
49
|
+
require "unitsml/errors/unknown_unit_error"
|
|
41
50
|
require "unitsml/errors/unsupported_payload_type_error"
|
|
42
51
|
|
|
43
52
|
require "unitsml/opal/database_payload"
|
|
@@ -78,12 +87,25 @@ require "unitsml/model/quantities/name"
|
|
|
78
87
|
require "unitsml/model/quantity"
|
|
79
88
|
require "unitsml/model/unit"
|
|
80
89
|
require "unitsml/model/units"
|
|
90
|
+
require "unitsml/model/units/conversion_note"
|
|
91
|
+
require "unitsml/model/units/conversions"
|
|
81
92
|
require "unitsml/model/units/enumerated_root_unit"
|
|
93
|
+
require "unitsml/model/units/float64_conversion_from"
|
|
82
94
|
require "unitsml/model/units/name"
|
|
83
95
|
require "unitsml/model/units/root_units"
|
|
96
|
+
require "unitsml/model/units/special_conversion_from"
|
|
84
97
|
require "unitsml/model/units/symbol"
|
|
85
98
|
require "unitsml/model/units/system"
|
|
99
|
+
require "unitsml/model/units/wsdl_conversion_from"
|
|
86
100
|
|
|
101
|
+
require "unitsml/compose"
|
|
102
|
+
require "unitsml/compose/term_tree"
|
|
103
|
+
require "unitsml/compose/builder"
|
|
104
|
+
require "unitsml/compose/composable"
|
|
105
|
+
require "unitsml/compose/composite"
|
|
106
|
+
# PowerNumerator must be defined before Unit/Dimension include it (no lazy
|
|
107
|
+
# autoload under Opal).
|
|
108
|
+
require "unitsml/power_numerator"
|
|
87
109
|
require "unitsml/dimension"
|
|
88
110
|
require "unitsml/extender"
|
|
89
111
|
require "unitsml/fenced"
|
data/lib/unitsml/parser.rb
CHANGED
|
@@ -15,7 +15,7 @@ module Unitsml
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def parse
|
|
18
|
-
tree =
|
|
18
|
+
tree = parse_tree
|
|
19
19
|
mapped = Parg::TreeMapper.new.apply(tree)
|
|
20
20
|
formula = build_formula(mapped)
|
|
21
21
|
update_units_exponents(formula.value, false)
|
|
@@ -51,6 +51,17 @@ module Unitsml
|
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# The artifact is loaded first, so Parsanol is defined by the time the
|
|
55
|
+
# rescue clause names it.
|
|
56
|
+
def parse_tree
|
|
57
|
+
artifact = self.class.artifact
|
|
58
|
+
begin
|
|
59
|
+
artifact.parse("expression", text)
|
|
60
|
+
rescue ::Parsanol::ParseFailed
|
|
61
|
+
raise Errors::ParseError.new(text)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
54
65
|
def update_units_exponents(array, inverse, sqrt = false)
|
|
55
66
|
array.each do |object|
|
|
56
67
|
if object.is_a?(Sqrt)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
# Storage for the power_numerator exponent shared by Unit and Dimension.
|
|
5
|
+
# Values assigned through #power_numerator= are always a Unitsml::Number (or a
|
|
6
|
+
# Fenced exponent for the parser's m^((1/2)) form, or nil for none) -- never a
|
|
7
|
+
# bare Numeric: public Numeric inputs like Unit.new("m", 2) are coerced into a
|
|
8
|
+
# Number so render/compare paths can rely on raw_value/to_latex/to_i/to_f.
|
|
9
|
+
# Internal dimension-vector throwaway units may bypass this setter and keep a
|
|
10
|
+
# raw Numeric in @power_numerator; those objects are never rendered.
|
|
11
|
+
# Anything else (a String, a Hash, a BasicObject) raises instead of being
|
|
12
|
+
# stored and crashing later at render.
|
|
13
|
+
module PowerNumerator
|
|
14
|
+
attr_reader :power_numerator
|
|
15
|
+
|
|
16
|
+
def power_numerator=(power)
|
|
17
|
+
@power_numerator = coerce_power_type(power)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
# The exponent lexemes the library itself stores: an integer, an n/m or
|
|
23
|
+
# n//m fraction (parser), or a decimal ("0.5" for sqrt, "2.0" from unit
|
|
24
|
+
# decomposition). Anything else ("abc") has no numeric meaning and would
|
|
25
|
+
# render and serialize as garbage.
|
|
26
|
+
NUMBER_LEXEME = %r{\A-?\d+(\.\d+)?(//?-?\d+)?\z}
|
|
27
|
+
private_constant :NUMBER_LEXEME
|
|
28
|
+
|
|
29
|
+
def coerce_power_type(power)
|
|
30
|
+
return power if Compose.type?(NilClass, power)
|
|
31
|
+
return validate_number_power(power) if Compose.type?(Number, power)
|
|
32
|
+
return validate_fenced_power(power) if Compose.type?(Fenced, power)
|
|
33
|
+
# Only the numeric types the parser can express (Integer, Rational, whole
|
|
34
|
+
# Float) coerce; other Numerics (BigDecimal, Complex, ...) are rejected
|
|
35
|
+
# rather than stringified into a non-parser exponent.
|
|
36
|
+
if Compose.type_any?([Integer, Rational, Float], power)
|
|
37
|
+
return Number.new(Compose.numeric_exponent_string(power))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
raise Errors::InvalidPowerError.new(value: power,
|
|
41
|
+
reason: :unsupported_storage)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def validate_number_power(power)
|
|
45
|
+
lexeme = Compose.safe_string(power.raw_value)
|
|
46
|
+
return power if lexeme&.match?(NUMBER_LEXEME)
|
|
47
|
+
|
|
48
|
+
raise Errors::InvalidPowerError.new(value: power,
|
|
49
|
+
reason: :invalid_number)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# A Fenced exponent is checked at its innermost Number; other parser
|
|
53
|
+
# payloads inside the fence are kept as-is.
|
|
54
|
+
def validate_fenced_power(power)
|
|
55
|
+
inner = power.value
|
|
56
|
+
inner = inner.value while Compose.type?(Fenced, inner)
|
|
57
|
+
validate_number_power(inner) if Compose.type?(Number, inner)
|
|
58
|
+
power
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/unitsml/quantity.rb
CHANGED
|
@@ -11,10 +11,25 @@ module Unitsml
|
|
|
11
11
|
class Quantity
|
|
12
12
|
attr_reader :value, :formula, :scale
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
# Distinguishes an omitted unit keyword from an explicit nil.
|
|
15
|
+
ABSENT = Object.new.freeze
|
|
16
|
+
private_constant :ABSENT
|
|
17
|
+
|
|
18
|
+
# unit: accepts a String (parsed, like unit_expression:), a Unit, a
|
|
19
|
+
# Formula (kept as-is, never reparsed) or nil (a pure number).
|
|
20
|
+
# unit_expression: remains the String-only spelling; give only one.
|
|
21
|
+
def initialize(value:, unit: ABSENT, unit_expression: ABSENT,
|
|
22
|
+
scale: nil)
|
|
23
|
+
unless unit.equal?(ABSENT) || unit_expression.equal?(ABSENT)
|
|
24
|
+
raise ArgumentError, "give only one of unit: or unit_expression:"
|
|
25
|
+
end
|
|
26
|
+
|
|
15
27
|
@value = value
|
|
16
|
-
@formula = unit_expression
|
|
17
|
-
|
|
28
|
+
@formula = build_formula(unit.equal?(ABSENT) ? unit_expression : unit)
|
|
29
|
+
# The scale derives from the unitsdb scale_reference of the units in
|
|
30
|
+
# the expression (degC is continuous_interval); an explicit argument
|
|
31
|
+
# (the :difference typing of subtraction) overrides it.
|
|
32
|
+
@scale = (scale || derived_scale).to_sym
|
|
18
33
|
end
|
|
19
34
|
|
|
20
35
|
# Canonical dimension identity; "1" (dimensionless) for pure numbers.
|
|
@@ -46,25 +61,28 @@ module Unitsml
|
|
|
46
61
|
formula&.norm_text || value.to_s
|
|
47
62
|
end
|
|
48
63
|
|
|
64
|
+
# Units compose structurally through Compose::Builder (the same engine
|
|
65
|
+
# as Unit#* and Unit#/); nothing is reparsed from text. A Numeric is a
|
|
66
|
+
# scalar; a bare Unit, Dimension or Formula operand is a magnitude-1
|
|
67
|
+
# Quantity (Compose.quantity_operand).
|
|
49
68
|
def *(other)
|
|
50
|
-
if
|
|
51
|
-
gate_scaling!
|
|
52
|
-
return Quantity.new(value: value * other, unit_expression: norm_text)
|
|
53
|
-
end
|
|
69
|
+
return scalar_product(other) if Compose.type?(Numeric, other)
|
|
54
70
|
|
|
71
|
+
other = Compose.quantity_operand(other)
|
|
55
72
|
scale_ratio_gate!(other, "multiplication")
|
|
56
73
|
Quantity.new(value: value * other.value,
|
|
57
|
-
|
|
74
|
+
unit: compose(formula, other.formula, "*"))
|
|
58
75
|
end
|
|
59
76
|
|
|
60
77
|
# rubocop:disable-next Metrics/AbcSize
|
|
61
78
|
def /(other)
|
|
62
|
-
if
|
|
79
|
+
if Compose.type?(Numeric, other)
|
|
63
80
|
gate_zero!(other)
|
|
64
81
|
gate_scaling!
|
|
65
|
-
return Quantity.new(value: value / other,
|
|
82
|
+
return Quantity.new(value: value / other, unit: copied_formula)
|
|
66
83
|
end
|
|
67
84
|
|
|
85
|
+
other = Compose.quantity_operand(other)
|
|
68
86
|
scale_ratio_gate!(other, "division")
|
|
69
87
|
if other.value.zero?
|
|
70
88
|
raise Errors::IndeterminateResultError,
|
|
@@ -72,7 +90,7 @@ module Unitsml
|
|
|
72
90
|
end
|
|
73
91
|
|
|
74
92
|
Quantity.new(value: value / other.value,
|
|
75
|
-
|
|
93
|
+
unit: compose(formula, other.formula, "/"))
|
|
76
94
|
end
|
|
77
95
|
|
|
78
96
|
# R-A4: differences are legal on both ratio and interval scales;
|
|
@@ -80,20 +98,26 @@ module Unitsml
|
|
|
80
98
|
def +(other)
|
|
81
99
|
quantity_kind!(other)
|
|
82
100
|
gate_dimension!(other)
|
|
83
|
-
Quantity.new(value: value + other.value,
|
|
101
|
+
Quantity.new(value: value + other.value, unit: copied_formula)
|
|
84
102
|
end
|
|
85
103
|
|
|
86
104
|
def -(other)
|
|
87
105
|
quantity_kind!(other)
|
|
88
106
|
gate_dimension!(other)
|
|
89
|
-
|
|
107
|
+
# A difference of two quantities is difference-typed: its conversion
|
|
108
|
+
# skips the affine addends (20 degC - 15 degC converts to 5 K, not
|
|
109
|
+
# 278.15 K).
|
|
110
|
+
Quantity.new(value: value - other.value, unit: copied_formula,
|
|
111
|
+
scale: :difference)
|
|
90
112
|
end
|
|
91
113
|
|
|
92
114
|
# R-A3: convert this quantity to another (dimension-compatible) unit
|
|
93
|
-
# using
|
|
94
|
-
#
|
|
95
|
-
#
|
|
96
|
-
|
|
115
|
+
# using unitsdb canonical_conversion data, or conversion definitions
|
|
116
|
+
# supplied by the caller (definitions: a hash of unit ids/symbols to
|
|
117
|
+
# Conversion::Definition or Float64ConversionFrom instances) where
|
|
118
|
+
# given. Exact rational arithmetic throughout; the returned value is
|
|
119
|
+
# a Rational when a conversion was applied.
|
|
120
|
+
def convert_to(target_expression, definitions: nil)
|
|
97
121
|
target_formula = if target_expression.is_a?(Formula)
|
|
98
122
|
target_expression
|
|
99
123
|
else
|
|
@@ -103,9 +127,14 @@ module Unitsml
|
|
|
103
127
|
unit_expression: target_formula.norm_text)
|
|
104
128
|
gate_dimension!(gate_target)
|
|
105
129
|
|
|
106
|
-
src = Conversion.to_canonical(formula)
|
|
107
|
-
dst = Conversion.to_canonical(target_formula)
|
|
108
|
-
converted =
|
|
130
|
+
src = Conversion.to_canonical(formula, definitions: definitions)
|
|
131
|
+
dst = Conversion.to_canonical(target_formula, definitions: definitions)
|
|
132
|
+
converted = if scale == :difference
|
|
133
|
+
# differences carry no affine addends
|
|
134
|
+
(Rational(value) * src.factor) / dst.factor
|
|
135
|
+
else
|
|
136
|
+
((Rational(value) * src.factor) + src.offset - dst.offset) / dst.factor
|
|
137
|
+
end
|
|
109
138
|
|
|
110
139
|
Quantity.new(value: converted, unit_expression: target_formula.norm_text)
|
|
111
140
|
end
|
|
@@ -118,6 +147,24 @@ module Unitsml
|
|
|
118
147
|
|
|
119
148
|
private
|
|
120
149
|
|
|
150
|
+
# The scale of a quantity is the scale of the units it composes: any
|
|
151
|
+
# interval-scaled unit (degC per unitsdb scale_reference) makes the
|
|
152
|
+
# quantity interval-scaled.
|
|
153
|
+
def derived_scale
|
|
154
|
+
interval_units? ? :interval : :ratio
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def interval_units?
|
|
158
|
+
return false unless formula
|
|
159
|
+
|
|
160
|
+
extract_units(formula.value).any? { |unit| unit_interval?(unit) }
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def unit_interval?(unit)
|
|
164
|
+
record = unit.unit_instance
|
|
165
|
+
record&.scale_reference&.id.to_s.include?("interval")
|
|
166
|
+
end
|
|
167
|
+
|
|
121
168
|
# Base dimensions in canonical DIMS_VECTOR order; nil when all
|
|
122
169
|
# exponents cancel (dimensionless).
|
|
123
170
|
def dimension_list
|
|
@@ -164,14 +211,20 @@ module Unitsml
|
|
|
164
211
|
# Scalar multiplication/division scales the magnitude: legal on
|
|
165
212
|
# ratio scales, a specified error on non-ratio ones.
|
|
166
213
|
def gate_scaling!
|
|
167
|
-
|
|
214
|
+
# A difference of interval-scaled values is ratio-scaled: scaling a
|
|
215
|
+
# difference is meaningful (2 x 5 K = 10 K of difference); scaling an
|
|
216
|
+
# absolute interval value is not.
|
|
217
|
+
return if %i[ratio difference].include?(scale)
|
|
168
218
|
|
|
169
219
|
raise Errors::ScaleOperationError,
|
|
170
220
|
"scaling is not legal on a #{scale} scale quantity"
|
|
171
221
|
end
|
|
172
222
|
|
|
173
223
|
def scale_ratio_gate!(other, operation)
|
|
174
|
-
|
|
224
|
+
# Only absolute interval-scaled values are barred from * and /: a
|
|
225
|
+
# difference is ratio-scaled (scaling or dividing it is meaningful),
|
|
226
|
+
# so difference/ratio combinations compose freely.
|
|
227
|
+
return if scale != :interval && other.scale != :interval
|
|
175
228
|
|
|
176
229
|
raise Errors::ScaleOperationError,
|
|
177
230
|
"#{operation} is not legal between #{scale} and " \
|
|
@@ -195,6 +248,11 @@ module Unitsml
|
|
|
195
248
|
"vs #{other.dimension_vector.inspect}"
|
|
196
249
|
end
|
|
197
250
|
|
|
251
|
+
def scalar_product(number)
|
|
252
|
+
gate_scaling!
|
|
253
|
+
Quantity.new(value: value * number, unit: copied_formula)
|
|
254
|
+
end
|
|
255
|
+
|
|
198
256
|
def gate_zero!(other)
|
|
199
257
|
return unless other.zero?
|
|
200
258
|
|
|
@@ -202,8 +260,22 @@ module Unitsml
|
|
|
202
260
|
"division by a zero quantity is indeterminate"
|
|
203
261
|
end
|
|
204
262
|
|
|
263
|
+
def build_formula(source)
|
|
264
|
+
case source
|
|
265
|
+
when ABSENT, nil then nil
|
|
266
|
+
when String then Unitsml.parse(source)
|
|
267
|
+
when Unit then Compose::Builder.from_terms([source])
|
|
268
|
+
when Formula then source
|
|
269
|
+
else raise Errors::InvalidUnitEntryError.new(value: source, field: :unit)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def copied_formula
|
|
274
|
+
Compose::Builder.copy_formula(formula)
|
|
275
|
+
end
|
|
276
|
+
|
|
205
277
|
def compose(left, right, operator)
|
|
206
|
-
|
|
278
|
+
Compose::Builder.quantity_product(left, right, operator)
|
|
207
279
|
end
|
|
208
280
|
end
|
|
209
281
|
end
|
data/lib/unitsml/unit.rb
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Unit
|
|
5
5
|
include MathmlHelper
|
|
6
|
+
include Compose::Composable
|
|
7
|
+
include PowerNumerator
|
|
6
8
|
|
|
7
|
-
attr_accessor :unit_name, :
|
|
9
|
+
attr_accessor :unit_name, :prefix
|
|
8
10
|
|
|
9
11
|
SI_UNIT_SYSTEM = %w[si_base si_derived_special
|
|
10
12
|
si_derived_non_special].freeze
|
|
@@ -12,9 +14,9 @@ module Unitsml
|
|
|
12
14
|
def initialize(unit_name,
|
|
13
15
|
power_numerator = nil,
|
|
14
16
|
prefix: nil)
|
|
15
|
-
@prefix = prefix
|
|
16
|
-
@unit_name = unit_name
|
|
17
|
-
|
|
17
|
+
@prefix = coerce_prefix(prefix)
|
|
18
|
+
@unit_name = resolve_ref(unit_name)
|
|
19
|
+
self.power_numerator = power_numerator
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
def ==(other)
|
|
@@ -122,6 +124,60 @@ module Unitsml
|
|
|
122
124
|
|
|
123
125
|
private
|
|
124
126
|
|
|
127
|
+
# Resolve a unit reference to a canonical symbol id, exactly like the parser
|
|
128
|
+
# (symbol ids only — no short-name resolution). The empty string and the
|
|
129
|
+
# UNKNOWN sentinel are passed through untouched (internal callers rely on
|
|
130
|
+
# them); nil is not a sentinel and fails fast rather than silently building
|
|
131
|
+
# a broken unit. Raises for anything unresolvable.
|
|
132
|
+
def resolve_ref(ref)
|
|
133
|
+
raise Errors::UnknownUnitError.new(value: ref) if Compose.type?(NilClass, ref)
|
|
134
|
+
|
|
135
|
+
string = Compose.safe_string(ref)
|
|
136
|
+
raise Errors::UnknownUnitError.new(value: ref) if string.nil?
|
|
137
|
+
return string if string.empty? || string == Utility::UNKNOWN
|
|
138
|
+
return string if Unitsdb.units.find_by_symbol_id(string)
|
|
139
|
+
|
|
140
|
+
raise Errors::UnknownUnitError.new(value: ref)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# A pre-built Prefix (the parse path) is passed through untouched, so the
|
|
144
|
+
# parser keeps its lazy resolution. A string/symbol prefix (the builder) is
|
|
145
|
+
# validated eagerly and wrapped.
|
|
146
|
+
def coerce_prefix(prefix)
|
|
147
|
+
return prefix if Compose.type?(NilClass, prefix)
|
|
148
|
+
return validate_prefix_object(prefix) if Compose.type?(Prefix, prefix)
|
|
149
|
+
|
|
150
|
+
coerce_prefix_string(prefix)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def coerce_prefix_string(prefix)
|
|
154
|
+
name = Compose.safe_string(prefix)
|
|
155
|
+
raise Errors::UnknownPrefixError.new(value: prefix) if name.nil?
|
|
156
|
+
|
|
157
|
+
# A blank prefix means "no prefix", and the UNKNOWN sentinel from internal
|
|
158
|
+
# decomposition (combine_prefixes for da-/h-prefixed derived units, whose
|
|
159
|
+
# unit is dropped before rendering) both resolve to nil rather than an
|
|
160
|
+
# unvalidated bare string that would crash at render time.
|
|
161
|
+
return if name.strip.empty? || name == Utility::UNKNOWN
|
|
162
|
+
unless Unitsdb.prefixes.find_by_symbol_name(name)
|
|
163
|
+
raise Errors::UnknownPrefixError.new(value: prefix)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
Prefix.new(name)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# A pre-built Prefix (the parse path) is kept as-is so the parser retains
|
|
170
|
+
# its lazy resolution; a directly-constructed one carrying an unresolvable
|
|
171
|
+
# name is rejected here rather than crashing at render.
|
|
172
|
+
def validate_prefix_object(prefix)
|
|
173
|
+
name = Compose.safe_string(prefix.prefix_name)
|
|
174
|
+
raise Errors::UnknownPrefixError.new(value: prefix) if name.nil?
|
|
175
|
+
return prefix if name.strip.empty? || name == Utility::UNKNOWN
|
|
176
|
+
return prefix if Unitsdb.prefixes.find_by_symbol_name(name)
|
|
177
|
+
|
|
178
|
+
raise Errors::UnknownPrefixError.new(value: prefix)
|
|
179
|
+
end
|
|
180
|
+
|
|
125
181
|
def display_exp
|
|
126
182
|
return unless power_numerator
|
|
127
183
|
|
|
@@ -8,6 +8,27 @@ module Unitsml
|
|
|
8
8
|
quantity.identifiers.find { |id| id.id == q_id }
|
|
9
9
|
end
|
|
10
10
|
end
|
|
11
|
+
|
|
12
|
+
# Resolve a quantity by its human name: English names + short slug,
|
|
13
|
+
# case-insensitive. Non-English synonyms are skipped (some contain
|
|
14
|
+
# commas the parser's comma-metadata handling would truncate).
|
|
15
|
+
def find_by_name(name)
|
|
16
|
+
string = Compose.safe_string(name)
|
|
17
|
+
return if string.nil? || string.strip.empty?
|
|
18
|
+
|
|
19
|
+
key = string.downcase
|
|
20
|
+
quantities.find { |quantity| name_matches?(quantity, key) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def name_matches?(quantity, key)
|
|
26
|
+
return true if quantity.short.to_s.downcase == key
|
|
27
|
+
|
|
28
|
+
quantity.names.any? do |name|
|
|
29
|
+
name.lang == "en" && name.value.to_s.downcase == key
|
|
30
|
+
end
|
|
31
|
+
end
|
|
11
32
|
end
|
|
12
33
|
|
|
13
34
|
Configuration.register_model(Quantities, id: :quantities)
|
|
@@ -9,6 +9,12 @@ module Unitsml
|
|
|
9
9
|
find_first_through(units, via: :identifiers, field: :id, value: u_id)
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
+
def find_by_reference_uri(uri)
|
|
13
|
+
units.find do |unit|
|
|
14
|
+
unit.references&.any? { |ref| ref.uri == uri }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
12
18
|
def find_by_name(u_name)
|
|
13
19
|
find_first_through(units, via: :symbols, field: :id, value: u_name)
|
|
14
20
|
end
|
data/lib/unitsml/utility.rb
CHANGED
|
@@ -55,7 +55,15 @@ module Unitsml
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
def quantity_instance(id)
|
|
58
|
-
|
|
58
|
+
# A quantity is resolved and silently dropped when unresolvable, so a
|
|
59
|
+
# pathological id (BasicObject, or a #to_s that raises/returns nil or a
|
|
60
|
+
# non-String) normalizes to nil here rather than leaking a raw
|
|
61
|
+
# exception through the render path.
|
|
62
|
+
string = Compose.safe_string(id)
|
|
63
|
+
return if string.nil? || string.strip.empty?
|
|
64
|
+
|
|
65
|
+
Unitsdb.quantities.find_by_id(string) ||
|
|
66
|
+
Unitsdb.quantities.find_by_name(string)
|
|
59
67
|
end
|
|
60
68
|
|
|
61
69
|
def units2dimensions(units)
|
|
@@ -116,11 +124,20 @@ module Unitsml
|
|
|
116
124
|
unit_name = Unitsdb.units.find_by_id(k.unit_reference.id).symbols.first.id
|
|
117
125
|
exponent = (k.power&.to_i || 1) * (u.power_numerator&.to_f || 1)
|
|
118
126
|
object << { prefix: prefix,
|
|
119
|
-
unit:
|
|
127
|
+
unit: dimension_base_unit(unit_name, exponent, prefix) }
|
|
120
128
|
end
|
|
121
129
|
end
|
|
122
130
|
end
|
|
123
131
|
|
|
132
|
+
# A throwaway unit used only to compute the dimension vector. Its exponent
|
|
133
|
+
# is kept as the raw Numeric (bypassing Number coercion) so the vector
|
|
134
|
+
# still stringifies to a Unitsdb-matchable value; it is never rendered.
|
|
135
|
+
def dimension_base_unit(unit_name, exponent, prefix)
|
|
136
|
+
unit = Unit.new(unit_name, prefix: prefix)
|
|
137
|
+
unit.instance_variable_set(:@power_numerator, exponent)
|
|
138
|
+
unit
|
|
139
|
+
end
|
|
140
|
+
|
|
124
141
|
def gather_units(units)
|
|
125
142
|
units.sort_by { |a| a[:unit]&.unit_name }.each_with_object([]) do |k, m|
|
|
126
143
|
if m.empty? || m[-1][:unit]&.unit_name != k[:unit]&.unit_name
|
|
@@ -368,14 +385,46 @@ module Unitsml
|
|
|
368
385
|
xml.force_encoding("UTF-8")
|
|
369
386
|
end
|
|
370
387
|
|
|
371
|
-
def quantity(normtext, instance)
|
|
388
|
+
def quantity(normtext, instance, dims = nil)
|
|
372
389
|
unit = unit_instance(normtext)
|
|
373
390
|
return unless unit_or_quantity(unit, instance)
|
|
374
391
|
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
392
|
+
record = instance && quantity_instance(instance)
|
|
393
|
+
# An explicit but unresolvable quantity emits nothing (silent), rather
|
|
394
|
+
# than leaking the raw reference as an xml:id.
|
|
395
|
+
return if instance && record.nil?
|
|
396
|
+
|
|
397
|
+
# unit is nil for a composite; fall back to the record's own identifier
|
|
398
|
+
# so a quantity without a NIST id still emits a usable xml:id.
|
|
399
|
+
id = canonical_nist_id(record) || quantity_reference_id(unit) ||
|
|
400
|
+
record_identifier_id(record)
|
|
401
|
+
model_quantity_xml(id, quantity_dimension_url(unit, dims),
|
|
402
|
+
record&.quantity_type)
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# The Quantity's dimensionURL, or nil when the dimension id cannot be
|
|
406
|
+
# determined (e.g. a composite whose decomposition hits the UNKNOWN
|
|
407
|
+
# sentinel) — the attribute is then omitted rather than emitting a
|
|
408
|
+
# broken "#" pointer, keeping the rest of the Quantity data intact.
|
|
409
|
+
def quantity_dimension_url(unit, dims)
|
|
410
|
+
ref = unit ? unit_dimension_id(unit) : dim_id(dims)
|
|
411
|
+
"##{ref}" if ref
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def canonical_nist_id(record)
|
|
415
|
+
record&.identifiers&.find { |identifier| identifier.type == "nist" }&.id
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
def quantity_reference_id(unit)
|
|
419
|
+
return unless unit
|
|
420
|
+
|
|
421
|
+
unit.quantity_references&.first&.id
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def record_identifier_id(record)
|
|
425
|
+
return unless record
|
|
426
|
+
|
|
427
|
+
record.identifiers&.first&.id
|
|
379
428
|
end
|
|
380
429
|
|
|
381
430
|
def unit_nist_id(unit)
|
|
@@ -409,13 +458,15 @@ module Unitsml
|
|
|
409
458
|
quantity_instance(quantity)
|
|
410
459
|
end
|
|
411
460
|
|
|
412
|
-
def model_quantity_xml(id, url)
|
|
413
|
-
|
|
461
|
+
def model_quantity_xml(id, url, quantity_type = nil)
|
|
462
|
+
attrs = {
|
|
414
463
|
id: id,
|
|
415
464
|
name: quantity_name(id),
|
|
416
465
|
dimension_url: url,
|
|
417
466
|
lutaml_register: Configuration.context.id,
|
|
418
|
-
|
|
467
|
+
}
|
|
468
|
+
attrs[:quantity_type] = quantity_type if quantity_type
|
|
469
|
+
xml = Model::Quantity.new(**attrs).to_xml
|
|
419
470
|
xml.force_encoding("UTF-8")
|
|
420
471
|
end
|
|
421
472
|
|
data/lib/unitsml/version.rb
CHANGED
data/lib/unitsml.rb
CHANGED
|
@@ -6,6 +6,7 @@ require "mml"
|
|
|
6
6
|
module Unitsml
|
|
7
7
|
module_function
|
|
8
8
|
|
|
9
|
+
autoload :Compose, "unitsml/compose"
|
|
9
10
|
autoload :Dimension, "unitsml/dimension"
|
|
10
11
|
autoload :Configuration, "unitsml/configuration"
|
|
11
12
|
autoload :Errors, "unitsml/errors"
|
|
@@ -19,6 +20,7 @@ module Unitsml
|
|
|
19
20
|
autoload :Number, "unitsml/number"
|
|
20
21
|
autoload :Parg, "unitsml/parg"
|
|
21
22
|
autoload :Parser, "unitsml/parser"
|
|
23
|
+
autoload :PowerNumerator, "unitsml/power_numerator"
|
|
22
24
|
autoload :Prefix, "unitsml/prefix"
|
|
23
25
|
autoload :PrefixAdapter, "unitsml/prefix_adapter"
|
|
24
26
|
autoload :Sqrt, "unitsml/sqrt"
|
|
@@ -33,4 +35,12 @@ module Unitsml
|
|
|
33
35
|
def parse(string)
|
|
34
36
|
Parser.new(string).parse
|
|
35
37
|
end
|
|
38
|
+
|
|
39
|
+
def compose(units: nil, dimensions: nil,
|
|
40
|
+
quantity: nil, name: nil, multiplier: nil)
|
|
41
|
+
Unitsml::Compose::Composite.new(
|
|
42
|
+
units: units, dimensions: dimensions,
|
|
43
|
+
quantity: quantity, name: name, multiplier: multiplier
|
|
44
|
+
).to_formula
|
|
45
|
+
end
|
|
36
46
|
end
|