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
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Compose
|
|
5
|
+
# Multiplicative composition mixin for units, dimensions and formulas. `*`
|
|
6
|
+
# combines two operands; `/` combines with the right-hand side inverted.
|
|
7
|
+
# A Numeric on either side yields a Quantity instead (see #coerce).
|
|
8
|
+
# The fluent aliases (#unit/#dimension) chain the same way, and
|
|
9
|
+
# #quantity_reference/#name/#multiplier attach render metadata. Everything
|
|
10
|
+
# is non-mutating and returns a fresh root Formula (assembly lives in
|
|
11
|
+
# Compose::Builder). Included by Unit, Dimension and Formula only.
|
|
12
|
+
module Composable
|
|
13
|
+
# A Numeric operand makes a Quantity (this operand at magnitude 1, scaled
|
|
14
|
+
# by the number); any other operand keeps the unit DSL and its Formula.
|
|
15
|
+
def *(other)
|
|
16
|
+
return Compose.quantity_operand(self) * other if numeric?(other)
|
|
17
|
+
|
|
18
|
+
build_formula(other, "*")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def /(other)
|
|
22
|
+
return Compose.quantity_operand(self) / other if numeric?(other)
|
|
23
|
+
|
|
24
|
+
build_formula(other, "/")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Ruby's Numeric#* and Numeric#/ call this for `5 * unit` / `5 / unit`:
|
|
28
|
+
# the number becomes a pure-number Quantity and this operand a
|
|
29
|
+
# magnitude-1 Quantity, so the result is a Quantity either way round.
|
|
30
|
+
def coerce(number)
|
|
31
|
+
unless numeric?(number)
|
|
32
|
+
raise Errors::InvalidUnitEntryError.new(value: number,
|
|
33
|
+
field: :operand)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
[Quantity.new(value: number), Compose.quantity_operand(self)]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Fluent sugar over `*`: `unit("m", -1)` == `self * Unit.new("m", -1)`,
|
|
40
|
+
# so a chain reads like the operator DSL, e.g.
|
|
41
|
+
# Unitsml::Unit.new("W").unit("m", -1).unit("sr", -1)
|
|
42
|
+
def unit(reference, power = nil, prefix: nil)
|
|
43
|
+
self * Unit.new(Compose.unit_ref(reference), Compose.power(power),
|
|
44
|
+
prefix: Compose.prefix_ref(prefix))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def dimension(reference, power = nil)
|
|
48
|
+
self * Dimension.new(Compose.dimension_ref(reference),
|
|
49
|
+
Compose.power(power))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Append an explicit separator glyph ("*", "/" or "//") exactly as a
|
|
53
|
+
# parsed string would carry it. Glyph-only by design: nothing is negated
|
|
54
|
+
# or transformed — powers are always stated explicitly (the parser's
|
|
55
|
+
# division inference belongs to parsing, not to this builder). The next
|
|
56
|
+
# chained term joins without an implicit "*". To mirror parse("W/m"),
|
|
57
|
+
# write: unit_w.extender("/").unit("m", -1).
|
|
58
|
+
def extender(symbol)
|
|
59
|
+
Builder.append_extender(composable_terms,
|
|
60
|
+
Compose.extender_sym(symbol))
|
|
61
|
+
end
|
|
62
|
+
alias ext extender
|
|
63
|
+
|
|
64
|
+
# Attach render metadata. These come AFTER the units/dimensions: each
|
|
65
|
+
# #unit/#dimension (like `*`) builds a fresh root Formula and does not
|
|
66
|
+
# carry earlier metadata forward, so metadata set before another term is
|
|
67
|
+
# dropped. #quantity_reference names the NIST quantity the expression
|
|
68
|
+
# measures (the keyword form's quantity:); it is distinct from Quantity,
|
|
69
|
+
# the value-with-unit class that Numeric arithmetic returns.
|
|
70
|
+
def quantity_reference(value)
|
|
71
|
+
Builder.attach_metadata(self, quantity: value)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def name(value)
|
|
75
|
+
Builder.attach_metadata(self, name: value)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def multiplier(value)
|
|
79
|
+
Builder.attach_metadata(self, multiplier: value)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The term list this operand contributes to a composition. A leaf
|
|
83
|
+
# (Unit/Dimension) contributes itself; Formula overrides this to
|
|
84
|
+
# contribute its already-interleaved value. Public so an operand can
|
|
85
|
+
# supply its terms to the other side of `*`/`/`.
|
|
86
|
+
def composable_terms
|
|
87
|
+
[self]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def build_formula(other, operator)
|
|
93
|
+
# An Extender operand is an explicit separator, not a term to combine:
|
|
94
|
+
# `w * Extender.new("/")` appends the glyph just like `w.extender("/")`
|
|
95
|
+
# (the operator itself is irrelevant — the Extender carries the glyph).
|
|
96
|
+
return extender(other) if Compose.type?(Extender, other)
|
|
97
|
+
|
|
98
|
+
Builder.build_product(composable_terms, operand_terms(other), operator)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Only a Unit, Dimension or Formula is composable; anything else fails
|
|
102
|
+
# fast here rather than exploding deeper inside Builder.
|
|
103
|
+
def operand_terms(other)
|
|
104
|
+
return other.composable_terms if composable?(other)
|
|
105
|
+
|
|
106
|
+
raise Errors::InvalidUnitEntryError.new(value: other, field: :operand)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def numeric?(other)
|
|
110
|
+
Compose.type?(Numeric, other)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def composable?(other)
|
|
114
|
+
Compose.type_any?([Unit, Dimension, Formula], other)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Compose
|
|
5
|
+
# Keyword builder behind Unitsml.compose. Resolves whether the request is a
|
|
6
|
+
# unit or a dimension composition (never both), coerces each entry into a
|
|
7
|
+
# domain object, and hands the list to Builder. A helper, not a Formula
|
|
8
|
+
# subclass: #to_formula returns a plain root Formula. quantity/name/
|
|
9
|
+
# multiplier are metadata passed straight through (the render layer ignores
|
|
10
|
+
# what does not apply, e.g. quantity/name for a dimension composition).
|
|
11
|
+
class Composite
|
|
12
|
+
def initialize(units: nil, dimensions: nil,
|
|
13
|
+
quantity: nil, name: nil, multiplier: nil)
|
|
14
|
+
@quantity = quantity
|
|
15
|
+
@name = name
|
|
16
|
+
@multiplier = multiplier
|
|
17
|
+
classify(normalize(units), normalize(dimensions))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def to_formula
|
|
21
|
+
Builder.from_terms(@entries.map { |entry| coerce_entry(entry) },
|
|
22
|
+
quantity: @quantity, name: @name,
|
|
23
|
+
multiplier: @multiplier)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
# A lone Hash is a single entry; nil is nothing; an Array is the list;
|
|
29
|
+
# anything else (incl. a bare reference or a pathological BasicObject)
|
|
30
|
+
# becomes a one-element list so coerce_entry validates/rejects it.
|
|
31
|
+
def normalize(entries)
|
|
32
|
+
return [] if Compose.type?(NilClass, entries)
|
|
33
|
+
return [entries] if Compose.type?(Hash, entries)
|
|
34
|
+
|
|
35
|
+
Compose.type?(Array, entries) ? entries : [entries]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Exactly one of units:/dimensions: may be non-empty. Store the resolved
|
|
39
|
+
# kind and its entries; both -> mix, neither -> empty.
|
|
40
|
+
def classify(units, dimensions)
|
|
41
|
+
# Presence is emptiness, not .any? — [nil] is a (bad) entry to reject,
|
|
42
|
+
# not an empty list.
|
|
43
|
+
has_units = !units.empty?
|
|
44
|
+
has_dimensions = !dimensions.empty?
|
|
45
|
+
|
|
46
|
+
if has_units && has_dimensions
|
|
47
|
+
raise Errors::MixedTermsError.new(
|
|
48
|
+
"[unitsml] Pass either units: or dimensions:, not both.",
|
|
49
|
+
)
|
|
50
|
+
elsif has_units
|
|
51
|
+
@kind = :unit
|
|
52
|
+
@entries = units
|
|
53
|
+
elsif has_dimensions
|
|
54
|
+
@kind = :dimension
|
|
55
|
+
@entries = dimensions
|
|
56
|
+
else
|
|
57
|
+
raise Errors::EmptyCompositionError
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def coerce_entry(entry)
|
|
62
|
+
case entry
|
|
63
|
+
when Unit, Dimension then typed_entry(entry)
|
|
64
|
+
when Hash then build_from_hash(entry)
|
|
65
|
+
when String, Symbol then build_reference(entry.to_s)
|
|
66
|
+
when nil then blank_reference
|
|
67
|
+
else raise Errors::InvalidUnitEntryError.new(value: entry, field: @kind)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# A pre-built object must match the composition's kind (a Dimension in
|
|
72
|
+
# units:, or a Unit in dimensions:, is rejected). It is then rebuilt
|
|
73
|
+
# through the same validated path as a Hash entry so its name, power and
|
|
74
|
+
# prefix are normalized/validated — otherwise a Symbol name, an invalid
|
|
75
|
+
# Number power, or an unresolved Prefix object slips through and crashes
|
|
76
|
+
# (or renders non-parseably) at render time.
|
|
77
|
+
def typed_entry(entry)
|
|
78
|
+
unless entry.is_a?(expected_class)
|
|
79
|
+
raise Errors::InvalidUnitEntryError.new(value: entry, field: @kind)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
@kind == :dimension ? rebuild_dimension(entry) : rebuild_unit(entry)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def rebuild_unit(unit)
|
|
86
|
+
Unit.new(require_reference(unit.unit_name),
|
|
87
|
+
require_power(unit.power_numerator),
|
|
88
|
+
prefix: prefix_ref(unit.prefix))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def rebuild_dimension(dimension)
|
|
92
|
+
Dimension.new(require_dimension(dimension.dimension_name),
|
|
93
|
+
require_power(dimension.power_numerator))
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def build_from_hash(entry)
|
|
97
|
+
return build_dimension_hash(entry) if @kind == :dimension
|
|
98
|
+
|
|
99
|
+
Unit.new(require_reference(entry[:unit]), require_power(entry[:power]),
|
|
100
|
+
prefix: prefix_ref(entry[:prefix]))
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def build_dimension_hash(entry)
|
|
104
|
+
if entry.key?(:prefix)
|
|
105
|
+
raise Errors::InvalidUnitEntryError.new(value: entry, field: :prefix)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
Dimension.new(require_dimension(entry[:dimension]),
|
|
109
|
+
require_power(entry[:power]))
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def build_reference(reference)
|
|
113
|
+
return Unit.new(require_reference(reference)) if @kind == :unit
|
|
114
|
+
|
|
115
|
+
Dimension.new(require_dimension(reference))
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Reference/power/prefix validation is shared with the fluent chain via
|
|
119
|
+
# the Compose.* validators, so both surfaces reject the same bad input.
|
|
120
|
+
def require_reference(reference)
|
|
121
|
+
Compose.unit_ref(reference)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def require_dimension(reference)
|
|
125
|
+
Compose.dimension_ref(reference)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def require_power(power)
|
|
129
|
+
Compose.power(power)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def prefix_ref(prefix)
|
|
133
|
+
Compose.prefix_ref(prefix)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def blank_reference
|
|
137
|
+
@kind == :dimension ? require_dimension(nil) : require_reference(nil)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def expected_class
|
|
141
|
+
@kind == :dimension ? Dimension : Unit
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Compose
|
|
5
|
+
# Single source of truth for the compose term-tree shape, so every traversal
|
|
6
|
+
# (deep-copy, inversion, unit/dimension detection) is written once, not
|
|
7
|
+
# re-encoded per traversal. The tree is:
|
|
8
|
+
# Array -> each element is a node
|
|
9
|
+
# Formula -> branch over its `value` array
|
|
10
|
+
# Fenced, Sqrt -> branch over a single `value`
|
|
11
|
+
# Unit, Dimension,
|
|
12
|
+
# Extender, Number... -> leaf
|
|
13
|
+
module TermTree
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# Visit every leaf depth-first (branches are traversed, not yielded).
|
|
17
|
+
# Returns the node unchanged — this is a walk for side effects on leaves.
|
|
18
|
+
def each_leaf(node, &block)
|
|
19
|
+
case node
|
|
20
|
+
when Array then node.each { |child| each_leaf(child, &block) }
|
|
21
|
+
when Formula, Fenced, Sqrt then each_leaf(node.value, &block)
|
|
22
|
+
else yield node
|
|
23
|
+
end
|
|
24
|
+
node
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Rebuild the branch structure, replacing each leaf with the block's
|
|
28
|
+
# result. Branches are always reconstructed, so the original is untouched.
|
|
29
|
+
def map_leaves(node, &block)
|
|
30
|
+
case node
|
|
31
|
+
when Array then node.map { |child| map_leaves(child, &block) }
|
|
32
|
+
when Formula then map_formula(node, &block)
|
|
33
|
+
when Fenced
|
|
34
|
+
Fenced.new(node.open_paren, map_leaves(node.value, &block),
|
|
35
|
+
node.close_paren)
|
|
36
|
+
when Sqrt then Sqrt.new(map_leaves(node.value, &block))
|
|
37
|
+
else yield node
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def map_formula(formula, &block)
|
|
42
|
+
copy = formula.dup
|
|
43
|
+
copy.value = map_leaves(formula.value, &block)
|
|
44
|
+
copy
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
# Programmatic composite-unit construction: the operator DSL / fluent chain
|
|
5
|
+
# (Composable), the Unitsml.compose keyword form (Composite), and the shared
|
|
6
|
+
# assembly (Builder). The raw-input validators below are shared by BOTH the
|
|
7
|
+
# keyword form and the fluent chain so a bad reference/power/prefix fails fast
|
|
8
|
+
# with the same Errors::* class no matter which surface is used.
|
|
9
|
+
module Compose
|
|
10
|
+
autoload :Builder, "unitsml/compose/builder"
|
|
11
|
+
autoload :Composable, "unitsml/compose/composable"
|
|
12
|
+
autoload :Composite, "unitsml/compose/composite"
|
|
13
|
+
autoload :TermTree, "unitsml/compose/term_tree"
|
|
14
|
+
|
|
15
|
+
# The parser's grammar knows exactly these separators; anything else is a
|
|
16
|
+
# display concern served by the render-time multiplier: option.
|
|
17
|
+
EXTENDERS = ["*", "/", "//"].freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# A render name is embedded directly into <UnitName>, so it must be a plain
|
|
22
|
+
# String/Symbol (or nil); a Hash/other serializes as garbage. Shared by the
|
|
23
|
+
# compose build path and the render-option path (to_*(name:)).
|
|
24
|
+
def validate_name!(name)
|
|
25
|
+
return if type_any?([NilClass, String, Symbol], name)
|
|
26
|
+
|
|
27
|
+
raise Errors::InvalidUnitEntryError.new(value: name, field: :name)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# A multiplier is a render separator: nil, a String, or :space/:nospace.
|
|
31
|
+
# Reject anything else where it enters (compose metadata, or a
|
|
32
|
+
# to_*(multiplier:) option) so a bad value fails as Errors::*, not later.
|
|
33
|
+
def validate_multiplier!(multiplier)
|
|
34
|
+
return if type_any?([NilClass, String], multiplier)
|
|
35
|
+
return if %i[space nospace].include?(multiplier)
|
|
36
|
+
|
|
37
|
+
raise Errors::InvalidUnitEntryError.new(value: multiplier,
|
|
38
|
+
field: :multiplier)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# An explicit extender: an Extender object (reduced to its symbol), or a
|
|
42
|
+
# glyph the parser could have produced ("*", "/" or "//"). Custom separators
|
|
43
|
+
# belong to the multiplier: render option, not the Formula.
|
|
44
|
+
def extender_sym(value)
|
|
45
|
+
symbol = type?(Extender, value) ? value.symbol : value
|
|
46
|
+
string = safe_string(symbol)
|
|
47
|
+
return string if EXTENDERS.include?(string)
|
|
48
|
+
|
|
49
|
+
raise Errors::InvalidUnitEntryError.new(value: symbol, field: :extender)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# A unit reference just needs to be non-blank; Unit.new then fail-fasts on
|
|
53
|
+
# an unresolvable (or dim_*) name. Rejects nil/"" (which Unit.new would let
|
|
54
|
+
# through as its internal empty sentinel and crash at render).
|
|
55
|
+
def unit_ref(reference)
|
|
56
|
+
string = safe_string(reference)
|
|
57
|
+
return string if string && !string.strip.empty?
|
|
58
|
+
|
|
59
|
+
raise Errors::UnknownUnitError.new(value: reference)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Normalize and validate a dimension reference at the shared compose
|
|
63
|
+
# boundary, so the keyword form, fluent #dimension chain, and Dimension.new
|
|
64
|
+
# all fail fast with the same Errors::UnknownDimensionError behavior.
|
|
65
|
+
def dimension_ref(reference)
|
|
66
|
+
string = safe_string(reference)
|
|
67
|
+
return string if string && Unitsdb.dimensions.parsables.key?(string)
|
|
68
|
+
|
|
69
|
+
raise Errors::UnknownDimensionError.new(value: reference)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The right-hand side of Quantity#* and Quantity#/ as a Quantity: a
|
|
73
|
+
# Quantity passes through; a bare Unit/Dimension or a Formula becomes a
|
|
74
|
+
# magnitude-1 Quantity (nothing is parsed — a Formula is kept, a leaf is
|
|
75
|
+
# wrapped by Builder). Anything else fails as a typed operand error.
|
|
76
|
+
# Numeric operands are scalars and are handled by Quantity itself.
|
|
77
|
+
def quantity_operand(value)
|
|
78
|
+
return value if type?(Quantity, value)
|
|
79
|
+
return Quantity.new(value: 1, unit: value) if type?(Formula, value)
|
|
80
|
+
if type_any?([Unit, Dimension], value)
|
|
81
|
+
return Quantity.new(value: 1, unit: Builder.from_terms([value]))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
raise Errors::InvalidUnitEntryError.new(value: value, field: :operand)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# A prefix supplied as a Prefix object is reduced to its name so Unit.new
|
|
88
|
+
# validates it; other values (String/Symbol/nil) pass through.
|
|
89
|
+
def prefix_ref(prefix)
|
|
90
|
+
type?(Prefix, prefix) ? prefix.prefix_name : prefix
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# A Number power must still be a parser-valid exponent (integer or n/m,
|
|
94
|
+
# optionally signed / double-slashed); a decimal ("0.5") or garbage ("abc")
|
|
95
|
+
# has no parsed equivalent and is rejected. Other types (Integer/Rational/
|
|
96
|
+
# Float/nil) are validated by Builder during assembly.
|
|
97
|
+
def power(value)
|
|
98
|
+
return value unless type?(Number, value)
|
|
99
|
+
return value if value.raw_value.match?(%r{\A-?\d+(//?-?\d+)?\z})
|
|
100
|
+
|
|
101
|
+
raise Errors::InvalidPowerError.new(value: value,
|
|
102
|
+
reason: :invalid_number)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# #to_s for a raw compose input without letting a pathological override (one
|
|
106
|
+
# that raises, has no #to_s, or returns a non-String) escape as a
|
|
107
|
+
# non-Errors::* exception; the caller treats nil as "not usable".
|
|
108
|
+
def safe_string(value)
|
|
109
|
+
string = value.to_s
|
|
110
|
+
string if string.is_a?(String)
|
|
111
|
+
rescue StandardError
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# A raw Numeric exponent, rendered in the parser's string form (a whole
|
|
116
|
+
# integer, or an n/m fraction from a Rational). A decimal (non-integer
|
|
117
|
+
# Float) has no parser representation and is rejected — nothing beyond what
|
|
118
|
+
# the parser accepts is introduced.
|
|
119
|
+
def numeric_exponent_string(numeric)
|
|
120
|
+
case numeric
|
|
121
|
+
when Rational then rational_exponent_string(numeric)
|
|
122
|
+
when Float then integer_float_string(numeric)
|
|
123
|
+
else numeric.to_s # Integer (and any other whole Numeric)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def rational_exponent_string(rational)
|
|
128
|
+
rational.denominator == 1 ? rational.numerator.to_s : rational.to_s
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def integer_float_string(float)
|
|
132
|
+
unless float.finite? && float == float.to_i
|
|
133
|
+
raise Errors::InvalidPowerError.new(value: float,
|
|
134
|
+
reason: :non_integer_float)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
float.to_i.to_s
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Class-membership test via Module#=== rather than #is_a?, so a compose
|
|
141
|
+
# input that is a BasicObject (which has no #is_a?/#nil?) is rejected as a
|
|
142
|
+
# typed Errors::* instead of the check itself raising a raw NoMethodError.
|
|
143
|
+
def type?(klass, value)
|
|
144
|
+
klass === value # rubocop:disable Style/CaseEquality
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def type_any?(klasses, value)
|
|
148
|
+
klasses.any? { |klass| type?(klass, value) }
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
module Conversion
|
|
5
|
+
# Executes a UnitsML conversion definition (the Float64ConversionFrom
|
|
6
|
+
# linear equation): forward y = d + ((b / c) (x + a)); inverse
|
|
7
|
+
# x = (c / b) (y - d) - a. Exact arithmetic throughout: the lexical
|
|
8
|
+
# decimal attributes parse to Rationals, and exact="true" definitions
|
|
9
|
+
# produce exact values. SpecialConversionFrom and WSDLConversionFrom
|
|
10
|
+
# are refused, never approximated.
|
|
11
|
+
class Definition
|
|
12
|
+
attr_reader :model
|
|
13
|
+
|
|
14
|
+
# model: the Model::Units::Float64ConversionFrom. document_units:
|
|
15
|
+
# an optional hash of document-local unit ids (without '#') to
|
|
16
|
+
# anything the caller can resolve, used for '#frag' initialUnit
|
|
17
|
+
# references before the register is consulted.
|
|
18
|
+
def initialize(model, document_units: {})
|
|
19
|
+
@model = model
|
|
20
|
+
@document_units = document_units || {}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.from_model(model, document_units: {})
|
|
24
|
+
case model
|
|
25
|
+
when Model::Units::Float64ConversionFrom
|
|
26
|
+
new(model, document_units: document_units)
|
|
27
|
+
when Model::Units::SpecialConversionFrom
|
|
28
|
+
raise Errors::ConversionError,
|
|
29
|
+
"SpecialConversionFrom #{model.id.inspect} is not " \
|
|
30
|
+
"executable: non-linear conversions have no linear " \
|
|
31
|
+
"approximation"
|
|
32
|
+
when Model::Units::WsdlConversionFrom
|
|
33
|
+
raise Errors::ConversionError,
|
|
34
|
+
"WSDLConversionFrom #{model.id.inspect} is not executable: " \
|
|
35
|
+
"remote conversions are not evaluated"
|
|
36
|
+
else
|
|
37
|
+
raise Errors::ConversionError,
|
|
38
|
+
"unknown conversion definition #{model.class}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def exact?
|
|
43
|
+
model.exact == true
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Forward: this unit's definition converts the initial unit's value
|
|
47
|
+
# into this unit: y = d + ((b / c) (x + a)).
|
|
48
|
+
def forward(value)
|
|
49
|
+
x = Rational(value)
|
|
50
|
+
d + ((b / c) * (x + a))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Inverse: from this unit back to the initial unit:
|
|
54
|
+
# x = (c / b) (y - d) - a.
|
|
55
|
+
def inverse(value)
|
|
56
|
+
y = Rational(value)
|
|
57
|
+
((c / b) * (y - d)) - a
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# The initial unit's identity: the '#fragment' stripped of its
|
|
61
|
+
# leading '#', or the URI as given for register resolution.
|
|
62
|
+
def initial_unit_id
|
|
63
|
+
model.initial_unit.to_s.delete_prefix("#")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The initial unit's register record, for the canonical anchoring of
|
|
67
|
+
# a definition hop. IRDI and URI-reference forms resolve through
|
|
68
|
+
# their fragment; plain URIs through the register's reference index.
|
|
69
|
+
def initial_unit_record
|
|
70
|
+
uri = model.initial_unit.to_s
|
|
71
|
+
reference = Unitsml::Unitsdb.units
|
|
72
|
+
record = if uri.include?("#")
|
|
73
|
+
reference.find_by_id(uri.split("#", 2).last)
|
|
74
|
+
elsif uri.start_with?("http", "https")
|
|
75
|
+
reference.find_by_reference_uri(uri)
|
|
76
|
+
else
|
|
77
|
+
reference.find_by_id(uri)
|
|
78
|
+
end
|
|
79
|
+
return record if record
|
|
80
|
+
|
|
81
|
+
raise Errors::ConversionError,
|
|
82
|
+
"unresolvable initialUnit #{uri.inspect}: not found " \
|
|
83
|
+
"in the register"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Resolve the initialUnit against document-local units first, the
|
|
87
|
+
# register second; unresolvable is a typed error naming the URI.
|
|
88
|
+
def resolve_initial_unit
|
|
89
|
+
uri = model.initial_unit.to_s
|
|
90
|
+
if uri.start_with?("#")
|
|
91
|
+
frag = initial_unit_id
|
|
92
|
+
return @document_units[frag] if @document_units.key?(frag)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
initial_unit_record
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def a
|
|
101
|
+
Rational(model.lexical_decimal(:initial_addend))
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def b
|
|
105
|
+
Rational(model.lexical_decimal(:multiplicand))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def c
|
|
109
|
+
Rational(model.lexical_decimal(:divisor))
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def d
|
|
113
|
+
Rational(model.lexical_decimal(:final_addend))
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|