unitsml 0.6.12 → 0.6.14
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 +341 -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 +32 -11
- data/lib/unitsml/power_numerator.rb +72 -0
- data/lib/unitsml/quantity.rb +95 -23
- data/lib/unitsml/unit.rb +58 -10
- 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/conversion.rb
CHANGED
|
@@ -11,6 +11,8 @@ module Unitsml
|
|
|
11
11
|
# the whole expression is that single unit with power 1 — an affine unit
|
|
12
12
|
# inside a composite raises.
|
|
13
13
|
module Conversion
|
|
14
|
+
autoload :Definition, "unitsml/conversion/definition"
|
|
15
|
+
|
|
14
16
|
Result = Struct.new(:factor, :offset, keyword_init: true)
|
|
15
17
|
|
|
16
18
|
module_function
|
|
@@ -19,12 +21,16 @@ module Unitsml
|
|
|
19
21
|
# branchiness is inherent and gated by the conformance suite.
|
|
20
22
|
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength, Layout/LineLength, Lint/DuplicateBranch, Lint/ElseLayout
|
|
21
23
|
|
|
22
|
-
# Returns a Result for the parsed expression.
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
# Returns a Result for the parsed expression. definitions: an
|
|
25
|
+
# optional hash of unit identifiers (register ids or symbols) to
|
|
26
|
+
# Conversion::Definition (or Float64ConversionFrom model) instances;
|
|
27
|
+
# a definition replaces the canonical factor/offset for its unit so
|
|
28
|
+
# the two paths are never applied in sequence.
|
|
29
|
+
def to_canonical(formula, definitions: nil)
|
|
30
|
+
walk(formula, 1, definitions: definitions)
|
|
25
31
|
end
|
|
26
32
|
|
|
27
|
-
def walk(node, sign)
|
|
33
|
+
def walk(node, sign, definitions: nil)
|
|
28
34
|
case node
|
|
29
35
|
when Formula
|
|
30
36
|
# The parser already normalizes "/" and "//" by inverting the
|
|
@@ -33,17 +39,17 @@ module Unitsml
|
|
|
33
39
|
node.value.reduce(Result.new(factor: Rational(1), offset: 0)) do |acc, part|
|
|
34
40
|
next acc if part.is_a?(Extender)
|
|
35
41
|
|
|
36
|
-
merge(acc, walk_with_sign(part, sign))
|
|
42
|
+
merge(acc, walk_with_sign(part, sign, definitions: definitions))
|
|
37
43
|
end
|
|
38
44
|
when Fenced
|
|
39
|
-
walk(node.value, sign)
|
|
45
|
+
walk(node.value, sign, definitions: definitions)
|
|
40
46
|
when Sqrt
|
|
41
|
-
sub = walk(node.value, sign)
|
|
47
|
+
sub = walk(node.value, sign, definitions: definitions)
|
|
42
48
|
Result.new(factor: sqrt_rational(sub.factor), offset: sub.offset)
|
|
43
49
|
when Extender
|
|
44
50
|
Result.new(factor: Rational(1), offset: 0)
|
|
45
51
|
when Unit
|
|
46
|
-
unit_result(node, sign)
|
|
52
|
+
unit_result(node, sign, definitions: definitions)
|
|
47
53
|
when Dimension, Number_, Prefix
|
|
48
54
|
Result.new(factor: Rational(1), offset: 0)
|
|
49
55
|
else
|
|
@@ -51,36 +57,8 @@ module Unitsml
|
|
|
51
57
|
end
|
|
52
58
|
end
|
|
53
59
|
|
|
54
|
-
def
|
|
55
|
-
|
|
56
|
-
when Formula
|
|
57
|
-
# The parser already normalizes "/" and "//" by inverting the
|
|
58
|
-
# following exponents (update_units_exponents), so the tree walks
|
|
59
|
-
# multiplicatively with no sign tracking.
|
|
60
|
-
node.value.reduce(Result.new(factor: Rational(1),
|
|
61
|
-
offset: 0)) do |acc, part|
|
|
62
|
-
next acc if part.is_a?(Extender)
|
|
63
|
-
|
|
64
|
-
merge(acc, walk_with_sign(part, sign))
|
|
65
|
-
end
|
|
66
|
-
when Fenced
|
|
67
|
-
walk(node.value, sign)
|
|
68
|
-
when Sqrt
|
|
69
|
-
sub = walk(node.value, sign)
|
|
70
|
-
Result.new(factor: sqrt_rational(sub.factor), offset: sub.offset)
|
|
71
|
-
when Extender
|
|
72
|
-
Result.new(factor: Rational(1), offset: 0)
|
|
73
|
-
when Unit
|
|
74
|
-
unit_result(node, sign)
|
|
75
|
-
when Dimension, Number_, Prefix
|
|
76
|
-
Result.new(factor: Rational(1), offset: 0)
|
|
77
|
-
else
|
|
78
|
-
raise Errors::ConversionError, "cannot convert #{node.class} node"
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def walk_with_sign(part, sign)
|
|
83
|
-
result = walk(part, sign)
|
|
60
|
+
def walk_with_sign(part, sign, definitions: nil)
|
|
61
|
+
result = walk(part, sign, definitions: definitions)
|
|
84
62
|
sign.negative? ? Result.new(factor: 1 / result.factor, offset: 0) : result
|
|
85
63
|
end
|
|
86
64
|
|
|
@@ -96,7 +74,7 @@ module Unitsml
|
|
|
96
74
|
)
|
|
97
75
|
end
|
|
98
76
|
|
|
99
|
-
def unit_result(unit, _sign)
|
|
77
|
+
def unit_result(unit, _sign, definitions: nil)
|
|
100
78
|
prefix_factor = if unit.prefix
|
|
101
79
|
Rational(unit.prefix.base)**unit.prefix.power
|
|
102
80
|
else
|
|
@@ -109,11 +87,86 @@ module Unitsml
|
|
|
109
87
|
"unknown unit #{unit.unit_name.inspect}"
|
|
110
88
|
end
|
|
111
89
|
|
|
90
|
+
# A supplied definition replaces the canonical factor/offset for its
|
|
91
|
+
# unit — the two paths are never applied in sequence; when the
|
|
92
|
+
# register also carries canonical data, the two must agree.
|
|
93
|
+
if (definition = definition_for(record, definitions))
|
|
94
|
+
def_factor, def_offset = definition_result(definition, power, record)
|
|
95
|
+
return Result.new(factor: def_factor * (prefix_factor**power),
|
|
96
|
+
offset: def_offset)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The prefix scales with the unit power: 1 km2 = 1 000 000 m2, not
|
|
100
|
+
# 1 000 m2 — the prefix is as squared as the unit is.
|
|
112
101
|
factor, offset = record_factor(record, power, [record_identity(record)])
|
|
113
|
-
factor *= prefix_factor
|
|
102
|
+
factor *= prefix_factor**power
|
|
114
103
|
Result.new(factor: factor, offset: offset)
|
|
115
104
|
end
|
|
116
105
|
|
|
106
|
+
# The definition supplied for this record, if any. Lookup keys are the
|
|
107
|
+
# register ids and symbols of the record.
|
|
108
|
+
def definition_for(record, definitions)
|
|
109
|
+
return nil if definitions.nil? || definitions.empty?
|
|
110
|
+
|
|
111
|
+
keys = record.identifiers.to_a.map(&:id) +
|
|
112
|
+
record.symbols.to_a.map(&:id)
|
|
113
|
+
entry = keys.filter_map { |key| definitions[key.to_s] }.first
|
|
114
|
+
return nil if entry.nil?
|
|
115
|
+
|
|
116
|
+
case entry
|
|
117
|
+
when Definition then entry
|
|
118
|
+
when Model::Units::Float64ConversionFrom then Definition.new(entry)
|
|
119
|
+
else Definition.from_model(entry)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The definition expressed as a canonical SI-anchored Result for the
|
|
124
|
+
# unit carrying it: value_SI = f_i((c / b)^p value - d) - f_i a + o_i,
|
|
125
|
+
# where (f_i, o_i) is the initial unit's canonical Result. Affine
|
|
126
|
+
# definitions (non-zero addends) are legal only at power 1; purely
|
|
127
|
+
# multiplicative definitions power legally.
|
|
128
|
+
def definition_result(definition, power, record)
|
|
129
|
+
a = Rational(definition.model.lexical_decimal(:initial_addend))
|
|
130
|
+
b = Rational(definition.model.lexical_decimal(:multiplicand))
|
|
131
|
+
c = Rational(definition.model.lexical_decimal(:divisor))
|
|
132
|
+
d = Rational(definition.model.lexical_decimal(:final_addend))
|
|
133
|
+
if (a != 0 || d != 0) && power != 1
|
|
134
|
+
raise Errors::ConversionError,
|
|
135
|
+
"affine unit raised to power #{power}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
factor = (c / b)**power
|
|
139
|
+
offset = a.zero? && d.zero? ? Rational(0) : (-(c / b) * d) - a
|
|
140
|
+
initial = definition.initial_unit_record
|
|
141
|
+
initial_factor, initial_offset = record_factor(initial, 1)
|
|
142
|
+
computed = [initial_factor * factor, (initial_offset * factor) + offset]
|
|
143
|
+
agree!(record, definition, power, computed)
|
|
144
|
+
computed
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# A definition and the register's canonical data describing the same
|
|
148
|
+
# unit must agree; disagreement is a diagnostic, not a silent choice.
|
|
149
|
+
# Both are taken at the same power. Canonical offsets are
|
|
150
|
+
# decimal-truncated register data, so the comparison uses the
|
|
151
|
+
# importer's 1e-9 relative tolerance.
|
|
152
|
+
def agree!(record, definition, power, computed)
|
|
153
|
+
return unless record.canonical_conversion
|
|
154
|
+
|
|
155
|
+
factor, offset = record_factor(record, power)
|
|
156
|
+
agreed = [factor, offset].zip(computed).all? do |ours, theirs|
|
|
157
|
+
delta = (ours - theirs).abs
|
|
158
|
+
delta.zero? || delta <= ours.abs / Rational(10**9)
|
|
159
|
+
end
|
|
160
|
+
return if agreed
|
|
161
|
+
|
|
162
|
+
raise Errors::ConversionError,
|
|
163
|
+
"conversion definition #{definition.model.id.inspect} " \
|
|
164
|
+
"disagrees with canonical data for " \
|
|
165
|
+
"#{record.symbols&.first&.id.inspect}: canonical " \
|
|
166
|
+
"(#{factor}, #{offset}) vs definition (#{computed[0]}, " \
|
|
167
|
+
"#{computed[1]})"
|
|
168
|
+
end
|
|
169
|
+
|
|
117
170
|
# [multiplicative factor, additive offset] for one unit record at the
|
|
118
171
|
# given power. Cyclic derivations (e.g. a unit defined in terms of
|
|
119
172
|
# itself) have no linear canonical conversion and raise.
|
|
@@ -148,7 +201,10 @@ module Unitsml
|
|
|
148
201
|
"affine unit inside a derived composition"
|
|
149
202
|
end
|
|
150
203
|
|
|
151
|
-
|
|
204
|
+
# sub_factor already carries base_power (base_factor exponentiates the
|
|
205
|
+
# canonical factor by it); prefix scales with the base power too, so a
|
|
206
|
+
# squared prefixed unit squares its prefix (km2 -> 1 000 000 m2).
|
|
207
|
+
factor *= (prefix_factor**base_power) * sub_factor
|
|
152
208
|
end
|
|
153
209
|
[factor, Rational(0)]
|
|
154
210
|
end
|
data/lib/unitsml/dimension.rb
CHANGED
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
class Dimension
|
|
5
5
|
include MathmlHelper
|
|
6
|
+
include Compose::Composable
|
|
7
|
+
include PowerNumerator
|
|
6
8
|
|
|
7
|
-
attr_accessor :dimension_name
|
|
9
|
+
attr_accessor :dimension_name
|
|
8
10
|
|
|
9
11
|
def initialize(dimension_name, power_numerator = nil)
|
|
10
|
-
@dimension_name = dimension_name
|
|
11
|
-
|
|
12
|
+
@dimension_name = safe_dimension_name(dimension_name)
|
|
13
|
+
self.power_numerator = power_numerator
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
def ==(other)
|
|
@@ -79,8 +81,33 @@ module Unitsml
|
|
|
79
81
|
value&.split("_")&.map(&:capitalize)&.join
|
|
80
82
|
end
|
|
81
83
|
|
|
84
|
+
# Parser-style source text for the dimension, e.g. "dim_L" or "dim_L^2".
|
|
85
|
+
# Mirrors Unit#xml_postprocess_name so composed text is built the same way.
|
|
86
|
+
def xml_postprocess_name
|
|
87
|
+
"#{dimension_name}#{display_exp}"
|
|
88
|
+
end
|
|
89
|
+
|
|
82
90
|
private
|
|
83
91
|
|
|
92
|
+
# Resolve a dimension reference to a known parsable id, as Unit#resolve_ref
|
|
93
|
+
# does for units: a pathological name (BasicObject / bad #to_s) or an
|
|
94
|
+
# unresolvable one fails fast as UnknownDimensionError instead of crashing
|
|
95
|
+
# at render on a nil dim_instance. The parser only ever supplies ids the
|
|
96
|
+
# grammar produced, so valid names pass through unchanged.
|
|
97
|
+
def safe_dimension_name(name)
|
|
98
|
+
string = Compose.safe_string(name)
|
|
99
|
+
return string if string && Unitsdb.dimensions.parsables.key?(string)
|
|
100
|
+
|
|
101
|
+
raise Errors::UnknownDimensionError.new(value: name)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def display_exp
|
|
105
|
+
return unless power_numerator
|
|
106
|
+
|
|
107
|
+
exp = power_numerator.raw_value
|
|
108
|
+
"^#{exp}" if exp != "1"
|
|
109
|
+
end
|
|
110
|
+
|
|
84
111
|
def html_numerator_conversion(options)
|
|
85
112
|
"<sup>#{power_numerator.to_html(options)}</sup>"
|
|
86
113
|
end
|
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
module Errors
|
|
5
5
|
class BaseError < StandardError
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
# Render a user-supplied value for an error message without letting a
|
|
9
|
+
# pathological #inspect (one that raises, or a BasicObject that has none)
|
|
10
|
+
# escape as a non-Errors::* exception while building the message.
|
|
11
|
+
def describe(value)
|
|
12
|
+
rendered = value.inspect
|
|
13
|
+
rendered.is_a?(String) ? rendered : "(unprintable value)"
|
|
14
|
+
rescue StandardError
|
|
15
|
+
"(unprintable value)"
|
|
16
|
+
end
|
|
6
17
|
end
|
|
7
18
|
end
|
|
8
19
|
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised when Unitsml.compose is given nothing to build from — neither a
|
|
6
|
+
# non-empty units: nor dimensions: list.
|
|
7
|
+
class EmptyCompositionError < Unitsml::Errors::BaseError
|
|
8
|
+
def initialize(msg = nil)
|
|
9
|
+
super(msg || "[unitsml] Nothing to compose — supply a non-empty " \
|
|
10
|
+
"`units:` or `dimensions:` list.")
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised when a builder-supplied power cannot become a parser-style
|
|
6
|
+
# exponent: an unsupported type, or a non-integer / non-finite Float (the
|
|
7
|
+
# parser has no decimal exponent — pass a Rational instead).
|
|
8
|
+
class InvalidPowerError < Unitsml::Errors::BaseError
|
|
9
|
+
attr_reader :value, :reason
|
|
10
|
+
|
|
11
|
+
def initialize(value:, reason: :unsupported_type)
|
|
12
|
+
@value = value
|
|
13
|
+
@reason = reason
|
|
14
|
+
super(message_for(reason, value))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def message_for(reason, value)
|
|
20
|
+
case reason
|
|
21
|
+
when :non_integer_float
|
|
22
|
+
"[unitsml] Non-integer Float power: #{describe(value)} — use a " \
|
|
23
|
+
"Rational (e.g. Rational(1, 2)) for a fractional exponent."
|
|
24
|
+
when :invalid_number
|
|
25
|
+
"[unitsml] Invalid Number power: #{describe(value)} — a Number " \
|
|
26
|
+
"exponent must hold an integer or n/m fraction (e.g. \"2\", \"1/2\")."
|
|
27
|
+
when :unsupported_storage
|
|
28
|
+
"[unitsml] Cannot store #{describe(value)} as an exponent — " \
|
|
29
|
+
"expected a Numeric, Unitsml::Number, or Unitsml::Fenced."
|
|
30
|
+
else
|
|
31
|
+
"[unitsml] Unsupported power: #{describe(value)} — expected an " \
|
|
32
|
+
"Integer, Rational, Float or Unitsml::Number."
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised when a compose entry (or operator operand) is not something the
|
|
6
|
+
# builder can turn into a term: the wrong type, the wrong kind for the mode
|
|
7
|
+
# (a Dimension in units: / a Unit in dimensions:), a prefix on a dimension,
|
|
8
|
+
# or a non-composable `*`/`/` operand (unit DSL or Quantity arithmetic).
|
|
9
|
+
class InvalidUnitEntryError < Unitsml::Errors::BaseError
|
|
10
|
+
attr_reader :value, :field
|
|
11
|
+
|
|
12
|
+
def initialize(value:, field: :entry)
|
|
13
|
+
@value = value
|
|
14
|
+
@field = field
|
|
15
|
+
super(message_for(field, value))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Message per field; %s is the described value.
|
|
19
|
+
MESSAGES = {
|
|
20
|
+
operand: "[unitsml] Cannot compose with %s — not a supported " \
|
|
21
|
+
"operand for this operator.",
|
|
22
|
+
prefix: "[unitsml] A dimension cannot take a prefix: %s.",
|
|
23
|
+
multiplier: "[unitsml] Invalid multiplier: %s — expected a String, " \
|
|
24
|
+
":space, or :nospace.",
|
|
25
|
+
name: "[unitsml] Invalid name: %s — expected a String or Symbol.",
|
|
26
|
+
unit: "[unitsml] Invalid unit: %s — expected a String, Unit or " \
|
|
27
|
+
"Formula.",
|
|
28
|
+
extender: "[unitsml] Invalid extender: %s — expected \"*\", \"/\" " \
|
|
29
|
+
"or \"//\"; custom separators are a render option " \
|
|
30
|
+
"(multiplier:).",
|
|
31
|
+
}.freeze
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def message_for(field, value)
|
|
36
|
+
template = MESSAGES.fetch(field) do
|
|
37
|
+
"[unitsml] Invalid #{field} entry: %s — expected a reference " \
|
|
38
|
+
"(String/Symbol), a Hash, or a matching Unit/Dimension."
|
|
39
|
+
end
|
|
40
|
+
format(template, describe(value))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised at render time when a composed Formula puts a separator (extender)
|
|
6
|
+
# where no operand follows it: the expression ends with an extender ("W/"),
|
|
7
|
+
# or two extenders sit together ("W/*"). Such a term list has no valid
|
|
8
|
+
# rendering; only a raw extender chain (#extender/#ext or an Extender
|
|
9
|
+
# operand) can build one — the parser grammar never produces it.
|
|
10
|
+
class MisplacedExtenderError < Unitsml::Errors::BaseError
|
|
11
|
+
def initialize(text = nil)
|
|
12
|
+
super(text || "[unitsml] Composed expression has a separator " \
|
|
13
|
+
"(extender) with no operand after it; it cannot be " \
|
|
14
|
+
"rendered.")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised when a composition mixes units and dimensions in one expression.
|
|
6
|
+
# The XML serializer emits only the dimension block for such a mix, silently
|
|
7
|
+
# dropping the units; the parser rejects the same input outright.
|
|
8
|
+
class MixedTermsError < Unitsml::Errors::BaseError
|
|
9
|
+
def initialize(msg = nil)
|
|
10
|
+
super(msg || "[unitsml] Cannot combine units and dimensions in a " \
|
|
11
|
+
"single expression.")
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised by Unitsml.parse for text that is not a UnitsML expression. The
|
|
6
|
+
# parser's own error (Parsanol::ParseFailed) is kept as #cause, so
|
|
7
|
+
# callers rescue this class instead of the parser library's.
|
|
8
|
+
class ParseError < BaseError
|
|
9
|
+
attr_reader :text
|
|
10
|
+
|
|
11
|
+
def initialize(text)
|
|
12
|
+
@text = text
|
|
13
|
+
super("[unitsml] Error: cannot parse #{text.inspect} as a UnitsML " \
|
|
14
|
+
"expression.")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
# Raised when a dimension reference cannot be resolved — by Dimension.new
|
|
6
|
+
# (which validates its name against the parsable ids) or by the compose
|
|
7
|
+
# validators, which fail fast with the same error before construction.
|
|
8
|
+
class UnknownDimensionError < Unitsml::Errors::BaseError
|
|
9
|
+
attr_reader :value
|
|
10
|
+
|
|
11
|
+
def initialize(value:)
|
|
12
|
+
@value = value
|
|
13
|
+
super("[unitsml] Unknown dimension reference: #{describe(value)} — " \
|
|
14
|
+
"expected a dimension id (e.g. \"dim_L\").")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
class UnknownPrefixError < Unitsml::Errors::BaseError
|
|
6
|
+
attr_reader :value, :field
|
|
7
|
+
|
|
8
|
+
def initialize(value:, field: :prefix)
|
|
9
|
+
@value = value
|
|
10
|
+
@field = field
|
|
11
|
+
super("[unitsml] Unknown prefix reference: #{describe(value)}.")
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Errors
|
|
5
|
+
class UnknownUnitError < Unitsml::Errors::BaseError
|
|
6
|
+
attr_reader :value, :field
|
|
7
|
+
|
|
8
|
+
def initialize(value:, field: :unit)
|
|
9
|
+
@value = value
|
|
10
|
+
@field = field
|
|
11
|
+
super("[unitsml] Unknown unit reference: #{describe(value)} — " \
|
|
12
|
+
"expected a unit symbol id (e.g. \"W\").")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/unitsml/errors.rb
CHANGED
|
@@ -3,15 +3,26 @@
|
|
|
3
3
|
module Unitsml
|
|
4
4
|
module Errors
|
|
5
5
|
autoload :BaseError, "unitsml/errors/base_error"
|
|
6
|
+
autoload :ConversionError, "unitsml/errors/quantity_errors"
|
|
7
|
+
autoload :EmptyCompositionError, "unitsml/errors/empty_composition_error"
|
|
6
8
|
autoload :InvalidModelError, "unitsml/errors/invalid_model_error"
|
|
9
|
+
autoload :InvalidPowerError, "unitsml/errors/invalid_power_error"
|
|
10
|
+
autoload :InvalidUnitEntryError, "unitsml/errors/invalid_unit_entry_error"
|
|
11
|
+
autoload :MisplacedExtenderError,
|
|
12
|
+
"unitsml/errors/misplaced_extender_error"
|
|
13
|
+
autoload :MixedTermsError, "unitsml/errors/mixed_terms_error"
|
|
7
14
|
autoload :OpalPayloadNotBundledError,
|
|
8
15
|
"unitsml/errors/opal_payload_not_bundled_error"
|
|
9
16
|
autoload :PlurimathLoadError, "unitsml/errors/plurimath_load_error"
|
|
10
17
|
autoload :PargLoadError, "unitsml/errors/parg_load_error"
|
|
18
|
+
autoload :ParseError, "unitsml/errors/parse_error"
|
|
11
19
|
autoload :QuantityOperationError, "unitsml/errors/quantity_errors"
|
|
12
20
|
autoload :DimensionMismatchError, "unitsml/errors/quantity_errors"
|
|
13
21
|
autoload :ScaleOperationError, "unitsml/errors/quantity_errors"
|
|
14
22
|
autoload :IndeterminateResultError, "unitsml/errors/quantity_errors"
|
|
23
|
+
autoload :UnknownDimensionError, "unitsml/errors/unknown_dimension_error"
|
|
24
|
+
autoload :UnknownPrefixError, "unitsml/errors/unknown_prefix_error"
|
|
25
|
+
autoload :UnknownUnitError, "unitsml/errors/unknown_unit_error"
|
|
15
26
|
autoload :UnsupportedPayloadTypeError,
|
|
16
27
|
"unitsml/errors/unsupported_payload_type_error"
|
|
17
28
|
end
|
data/lib/unitsml/formula.rb
CHANGED
|
@@ -6,6 +6,7 @@ require "htmlentities"
|
|
|
6
6
|
module Unitsml
|
|
7
7
|
class Formula
|
|
8
8
|
include MathmlHelper
|
|
9
|
+
include Compose::Composable
|
|
9
10
|
|
|
10
11
|
attr_accessor :value, :explicit_value, :root, :norm_text
|
|
11
12
|
|
|
@@ -29,6 +30,7 @@ module Unitsml
|
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
def to_mathml(options = {})
|
|
33
|
+
guard_renderable!
|
|
32
34
|
if root
|
|
33
35
|
options = update_options(options)
|
|
34
36
|
math = mml_v4_new(:math, display: "block")
|
|
@@ -42,27 +44,32 @@ module Unitsml
|
|
|
42
44
|
|
|
43
45
|
generated_math.force_encoding("UTF-8")
|
|
44
46
|
else
|
|
45
|
-
value.map { |obj| obj.to_mathml(options) }
|
|
47
|
+
value.map { |obj| obj.to_mathml(update_options(options)) }
|
|
46
48
|
end
|
|
47
49
|
end
|
|
48
50
|
|
|
49
51
|
def to_latex(options = {})
|
|
52
|
+
guard_renderable!
|
|
50
53
|
value.map { |obj| obj.to_latex(update_options(options)) }.join
|
|
51
54
|
end
|
|
52
55
|
|
|
53
56
|
def to_asciimath(options = {})
|
|
57
|
+
guard_renderable!
|
|
54
58
|
value.map { |obj| obj.to_asciimath(update_options(options)) }.join
|
|
55
59
|
end
|
|
56
60
|
|
|
57
61
|
def to_html(options = {})
|
|
62
|
+
guard_renderable!
|
|
58
63
|
value.map { |obj| obj.to_html(update_options(options)) }.join
|
|
59
64
|
end
|
|
60
65
|
|
|
61
66
|
def to_unicode(options = {})
|
|
67
|
+
guard_renderable!
|
|
62
68
|
value.map { |obj| obj.to_unicode(update_options(options)) }.join
|
|
63
69
|
end
|
|
64
70
|
|
|
65
71
|
def to_xml(options = {})
|
|
72
|
+
guard_renderable!
|
|
66
73
|
options = update_options(options)
|
|
67
74
|
if (dimensions_array = extract_dimensions(value)).any?
|
|
68
75
|
dimensions(sort_dims(dimensions_array), options)
|
|
@@ -74,6 +81,7 @@ module Unitsml
|
|
|
74
81
|
end
|
|
75
82
|
|
|
76
83
|
def to_plurimath(options = {})
|
|
84
|
+
guard_renderable!
|
|
77
85
|
ensure_plurimath_defined!
|
|
78
86
|
options = update_options(options)
|
|
79
87
|
if @orig_text.match?(/-$/)
|
|
@@ -89,8 +97,54 @@ module Unitsml
|
|
|
89
97
|
extract_dimensions(value)
|
|
90
98
|
end
|
|
91
99
|
|
|
100
|
+
# A composed Formula contributes its already-interleaved term list (the
|
|
101
|
+
# Composable default of [self] is only right for a single leaf).
|
|
102
|
+
def composable_terms
|
|
103
|
+
value
|
|
104
|
+
end
|
|
105
|
+
|
|
92
106
|
private
|
|
93
107
|
|
|
108
|
+
# A composed term list must not dangle on a separator or place two
|
|
109
|
+
# separators together ("W/", "W/*"); such a term list has no valid
|
|
110
|
+
# rendering. The parser never builds these — only a raw extender chain
|
|
111
|
+
# (#extender/#ext or an Extender operand) can — so this only ever fires on
|
|
112
|
+
# compose misuse. Walks nested Formula/Fenced/Sqrt so a separator misplaced
|
|
113
|
+
# inside a group is caught too, regardless of the render path taken.
|
|
114
|
+
def guard_renderable!(node = value)
|
|
115
|
+
if node.is_a?(Array)
|
|
116
|
+
reject_misplaced_extenders!(node)
|
|
117
|
+
node.each { |term| guard_renderable!(term) }
|
|
118
|
+
elsif renderable_container?(node)
|
|
119
|
+
guard_container!(node.value)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# A group (Fenced/Sqrt) wrapping a bare separator — Fenced.new("(", ext) —
|
|
124
|
+
# is as unrenderable as a dangling one; its value is never a lone Extender
|
|
125
|
+
# in valid parser/compose output.
|
|
126
|
+
def guard_container!(inner)
|
|
127
|
+
raise Errors::MisplacedExtenderError if inner.is_a?(Extender)
|
|
128
|
+
|
|
129
|
+
guard_renderable!(inner)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def reject_misplaced_extenders!(terms)
|
|
133
|
+
misplaced = terms.first.is_a?(Extender) ||
|
|
134
|
+
terms.last.is_a?(Extender) || adjacent_extenders?(terms)
|
|
135
|
+
raise Errors::MisplacedExtenderError if misplaced
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def adjacent_extenders?(terms)
|
|
139
|
+
terms.each_cons(2).any? do |left, right|
|
|
140
|
+
left.is_a?(Extender) && right.is_a?(Extender)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def renderable_container?(node)
|
|
145
|
+
node.is_a?(Formula) || node.is_a?(Fenced) || node.is_a?(Sqrt)
|
|
146
|
+
end
|
|
147
|
+
|
|
94
148
|
def extract_dimensions(formula)
|
|
95
149
|
formula.each_with_object([]) do |term, dimensions|
|
|
96
150
|
case term
|
|
@@ -135,10 +189,12 @@ module Unitsml
|
|
|
135
189
|
dims = Utility.units2dimensions(extract_units(value))
|
|
136
190
|
[
|
|
137
191
|
Utility.unit(all_units, self, dims, norm_text,
|
|
138
|
-
explicit_value&.dig(:name), options),
|
|
192
|
+
options[:name] || explicit_value&.dig(:name), options),
|
|
139
193
|
Utility.prefixes(all_units, options),
|
|
140
194
|
*unique_dimensions(dims, norm_text),
|
|
141
|
-
Utility.quantity(norm_text,
|
|
195
|
+
Utility.quantity(norm_text,
|
|
196
|
+
options[:quantity] || explicit_value&.dig(:quantity),
|
|
197
|
+
dims),
|
|
142
198
|
].join
|
|
143
199
|
end
|
|
144
200
|
|
|
@@ -173,7 +229,8 @@ module Unitsml
|
|
|
173
229
|
[
|
|
174
230
|
Utility.prefixes([prefix_object], options),
|
|
175
231
|
Utility.dimension(norm_text),
|
|
176
|
-
Utility.quantity(norm_text,
|
|
232
|
+
Utility.quantity(norm_text,
|
|
233
|
+
options[:quantity] || explicit_value&.dig(:quantity)),
|
|
177
234
|
].join
|
|
178
235
|
end
|
|
179
236
|
|
|
@@ -210,6 +267,10 @@ module Unitsml
|
|
|
210
267
|
end
|
|
211
268
|
|
|
212
269
|
def update_options(options)
|
|
270
|
+
# Validate render options on every path (a directly-rendered non-root
|
|
271
|
+
# Formula would otherwise skip this and leak at the extender).
|
|
272
|
+
Compose.validate_name!(options[:name])
|
|
273
|
+
Compose.validate_multiplier!(options[:multiplier])
|
|
213
274
|
return options unless root
|
|
214
275
|
|
|
215
276
|
multiplier = options[:multiplier] || explicit_value&.dig(:multiplier)
|
data/lib/unitsml/model/unit.rb
CHANGED
|
@@ -9,6 +9,7 @@ module Unitsml
|
|
|
9
9
|
attribute :symbol, Units::Symbol, collection: true
|
|
10
10
|
attribute :system, Units::System, collection: true
|
|
11
11
|
attribute :root_units, Units::RootUnits
|
|
12
|
+
attribute :conversions, Units::Conversions
|
|
12
13
|
|
|
13
14
|
xml do
|
|
14
15
|
namespace ::Unitsml::Namespace
|
|
@@ -20,6 +21,7 @@ module Unitsml
|
|
|
20
21
|
map_element :UnitName, to: :name
|
|
21
22
|
map_element :UnitSymbol, to: :symbol
|
|
22
23
|
map_element :RootUnits, to: :root_units
|
|
24
|
+
map_element :Conversions, to: :conversions
|
|
23
25
|
end
|
|
24
26
|
end
|
|
25
27
|
end
|