unitsml 0.6.10 → 0.6.12
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 +2 -0
- data/docs/README.adoc +13 -0
- data/lib/unitsml/conversion.rb +276 -0
- data/lib/unitsml/errors/quantity_errors.rb +11 -0
- data/lib/unitsml/errors.rb +4 -0
- data/lib/unitsml/formula.rb +1 -1
- data/lib/unitsml/opal/database_payload.rb +1 -1
- data/lib/unitsml/opal.rb +3 -0
- data/lib/unitsml/parg/tree_mapper.rb +31 -1
- data/lib/unitsml/quantity.rb +209 -0
- data/lib/unitsml/version.rb +1 -1
- data/lib/unitsml.rb +2 -0
- metadata +5 -2
data/lib/unitsml/opal.rb
CHANGED
|
@@ -37,6 +37,7 @@ require "unitsml/errors/invalid_model_error"
|
|
|
37
37
|
require "unitsml/errors/opal_payload_not_bundled_error"
|
|
38
38
|
require "unitsml/errors/plurimath_load_error"
|
|
39
39
|
require "unitsml/errors/parg_load_error"
|
|
40
|
+
require "unitsml/errors/quantity_errors"
|
|
40
41
|
require "unitsml/errors/unsupported_payload_type_error"
|
|
41
42
|
|
|
42
43
|
require "unitsml/opal/database_payload"
|
|
@@ -92,6 +93,8 @@ require "unitsml/mathml_helper"
|
|
|
92
93
|
require "unitsml/number"
|
|
93
94
|
require "unitsml/parg"
|
|
94
95
|
require "unitsml/parser"
|
|
96
|
+
require "unitsml/quantity"
|
|
97
|
+
require "unitsml/conversion"
|
|
95
98
|
require "unitsml/prefix"
|
|
96
99
|
require "unitsml/prefix_adapter"
|
|
97
100
|
require "unitsml/sqrt"
|
|
@@ -8,6 +8,16 @@ module Unitsml
|
|
|
8
8
|
# (sorted), bottom-up, mirroring rule-for-rule the Transform rules in
|
|
9
9
|
# unitsml/transform.rb.
|
|
10
10
|
class TreeMapper
|
|
11
|
+
# Edition-2 artifacts (parsanol capture edition 2) name every
|
|
12
|
+
# uncaptured rule reference; these four are the names this grammar
|
|
13
|
+
# gains — the fence punctuation, the sign-suffix hyphen and the
|
|
14
|
+
# zero-width guard — none of which the v1 trees ever surfaced and
|
|
15
|
+
# none of which are authored capture names. Stripping them in the
|
|
16
|
+
# normalize pass keeps the dispatch table reading the same shapes
|
|
17
|
+
# for both editions, so this mapper works against artifacts baked
|
|
18
|
+
# before and after the asciiunits edition-2 flip.
|
|
19
|
+
STRUCTURAL_NAMES = %w[lparen rparen hyphen end_guard].freeze
|
|
20
|
+
|
|
11
21
|
def apply(tree)
|
|
12
22
|
map(normalize(tree))
|
|
13
23
|
end
|
|
@@ -18,7 +28,9 @@ module Unitsml
|
|
|
18
28
|
# Slice leaves; the mapper works on string keys and stringified leaves.
|
|
19
29
|
def normalize(node)
|
|
20
30
|
case node
|
|
21
|
-
when Hash
|
|
31
|
+
when Hash
|
|
32
|
+
node.to_h { |key, value| [key.to_s, normalize(value)] }
|
|
33
|
+
.except(*STRUCTURAL_NAMES)
|
|
22
34
|
when Array then node.map { |value| normalize(value) }
|
|
23
35
|
else node
|
|
24
36
|
end
|
|
@@ -122,6 +134,24 @@ module Unitsml
|
|
|
122
134
|
when %w[power_numerator prefix unit]
|
|
123
135
|
Unit.new(text(node["unit"]), node["power_numerator"],
|
|
124
136
|
prefix: Prefix.new(text(node["prefix"])))
|
|
137
|
+
when %w[prefix sequence unit]
|
|
138
|
+
Formula.new(
|
|
139
|
+
[Unit.new(text(node["unit"]),
|
|
140
|
+
prefix: Prefix.new(text(node["prefix"]))),
|
|
141
|
+
node["sequence"]],
|
|
142
|
+
)
|
|
143
|
+
when %w[sequence sqrt]
|
|
144
|
+
Formula.new(
|
|
145
|
+
[Sqrt.new(node["sqrt"]), node["sequence"]],
|
|
146
|
+
)
|
|
147
|
+
when %w[sequence unit]
|
|
148
|
+
Formula.new(
|
|
149
|
+
[Unit.new(text(node["unit"])), node["sequence"]],
|
|
150
|
+
)
|
|
151
|
+
when %w[implicit_extended sequence]
|
|
152
|
+
Formula.new(
|
|
153
|
+
[node["implicit_extended"], node["sequence"]],
|
|
154
|
+
)
|
|
125
155
|
when %w[prefix] then Prefix.new(text(node["prefix"]))
|
|
126
156
|
when %w[prefix unit]
|
|
127
157
|
Unit.new(text(node["unit"]), prefix: Prefix.new(text(node["prefix"])))
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Unitsml
|
|
4
|
+
# A quantity: a magnitude plus a unit identity, with unit-aware
|
|
5
|
+
# arithmetic (unitsml-ruby#82 R-A1/A2/A4-skeleton/A5).
|
|
6
|
+
#
|
|
7
|
+
# Dimensions come from the parsed unit expression via UnitsDB; every
|
|
8
|
+
# operation checks them. Scale legality (R-A4) gates multiplication
|
|
9
|
+
# and division on non-ratio scales; the classification is pluggable
|
|
10
|
+
# per quantity until the unitsdb scale-legality data lands (#130).
|
|
11
|
+
class Quantity
|
|
12
|
+
attr_reader :value, :formula, :scale
|
|
13
|
+
|
|
14
|
+
def initialize(value:, unit_expression: nil, scale: :ratio)
|
|
15
|
+
@value = value
|
|
16
|
+
@formula = unit_expression && Unitsml.parse(unit_expression)
|
|
17
|
+
@scale = scale.to_sym
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Canonical dimension identity; "1" (dimensionless) for pure numbers.
|
|
21
|
+
def dimension_id
|
|
22
|
+
@dimension_id ||= begin
|
|
23
|
+
list = dimension_list
|
|
24
|
+
list && (Utility.dim_id(list) || "1")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# rubocop:disable Metrics/AbcSize, Layout/LineLength
|
|
29
|
+
# Base-dimension exponents summed across the composed expression —
|
|
30
|
+
# V (L²MT⁻³I⁻¹) times A (I¹) merges to the watt vector L²MT⁻³.
|
|
31
|
+
def dimension_vector
|
|
32
|
+
list = raw_dimension_list
|
|
33
|
+
return nil if list.nil?
|
|
34
|
+
|
|
35
|
+
vector = list.each_with_object(Hash.new(0)) do |entry, acc|
|
|
36
|
+
exponent = entry[:exponent]
|
|
37
|
+
exponent = exponent.raw_value if exponent.respond_to?(:raw_value)
|
|
38
|
+
acc[entry[:dimension]] += exponent.to_i
|
|
39
|
+
end
|
|
40
|
+
vector.delete_if { |_, exponent| exponent.zero? }
|
|
41
|
+
vector
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Normalized unit expression text; the value's to_s for pure numbers.
|
|
45
|
+
def norm_text
|
|
46
|
+
formula&.norm_text || value.to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def *(other)
|
|
50
|
+
if other.is_a?(Numeric)
|
|
51
|
+
gate_scaling!
|
|
52
|
+
return Quantity.new(value: value * other, unit_expression: norm_text)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
scale_ratio_gate!(other, "multiplication")
|
|
56
|
+
Quantity.new(value: value * other.value,
|
|
57
|
+
unit_expression: compose(self, other, "*"))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# rubocop:disable-next Metrics/AbcSize
|
|
61
|
+
def /(other)
|
|
62
|
+
if other.is_a?(Numeric)
|
|
63
|
+
gate_zero!(other)
|
|
64
|
+
gate_scaling!
|
|
65
|
+
return Quantity.new(value: value / other, unit_expression: norm_text)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
scale_ratio_gate!(other, "division")
|
|
69
|
+
if other.value.zero?
|
|
70
|
+
raise Errors::IndeterminateResultError,
|
|
71
|
+
"division by a zero quantity is indeterminate"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
Quantity.new(value: value / other.value,
|
|
75
|
+
unit_expression: compose(self, other, "/"))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# R-A4: differences are legal on both ratio and interval scales;
|
|
79
|
+
# only the dimension identity must match.
|
|
80
|
+
def +(other)
|
|
81
|
+
quantity_kind!(other)
|
|
82
|
+
gate_dimension!(other)
|
|
83
|
+
Quantity.new(value: value + other.value, unit_expression: norm_text)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def -(other)
|
|
87
|
+
quantity_kind!(other)
|
|
88
|
+
gate_dimension!(other)
|
|
89
|
+
Quantity.new(value: value - other.value, unit_expression: norm_text)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# R-A3: convert this quantity to another (dimension-compatible) unit
|
|
93
|
+
# using the unitsdb canonical_conversion data. Exact rational
|
|
94
|
+
# arithmetic throughout; the returned value is a Rational when a
|
|
95
|
+
# conversion was applied.
|
|
96
|
+
def convert_to(target_expression)
|
|
97
|
+
target_formula = if target_expression.is_a?(Formula)
|
|
98
|
+
target_expression
|
|
99
|
+
else
|
|
100
|
+
Unitsml.parse(target_expression)
|
|
101
|
+
end
|
|
102
|
+
gate_target = Quantity.new(value: 0,
|
|
103
|
+
unit_expression: target_formula.norm_text)
|
|
104
|
+
gate_dimension!(gate_target)
|
|
105
|
+
|
|
106
|
+
src = Conversion.to_canonical(formula)
|
|
107
|
+
dst = Conversion.to_canonical(target_formula)
|
|
108
|
+
converted = ((Rational(value) * src.factor) + src.offset - dst.offset) / dst.factor
|
|
109
|
+
|
|
110
|
+
Quantity.new(value: converted, unit_expression: target_formula.norm_text)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def ==(other)
|
|
114
|
+
other.is_a?(Quantity) && value == other.value &&
|
|
115
|
+
dimension_vector == other.dimension_vector
|
|
116
|
+
end
|
|
117
|
+
# rubocop:enable Metrics/AbcSize, Layout/LineLength
|
|
118
|
+
|
|
119
|
+
private
|
|
120
|
+
|
|
121
|
+
# Base dimensions in canonical DIMS_VECTOR order; nil when all
|
|
122
|
+
# exponents cancel (dimensionless).
|
|
123
|
+
def dimension_list
|
|
124
|
+
merged = Utility::DIMS_VECTOR.filter_map do |dimension|
|
|
125
|
+
exponent = dimension_vector[dimension]
|
|
126
|
+
next if exponent.zero?
|
|
127
|
+
|
|
128
|
+
{ dimension: dimension, exponent: exponent }
|
|
129
|
+
end
|
|
130
|
+
merged.empty? ? nil : merged
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def raw_dimension_list
|
|
134
|
+
return [] unless formula
|
|
135
|
+
|
|
136
|
+
Utility.units2dimensions(extract_units(formula.value)) ||
|
|
137
|
+
Conversion.dimension_entries(formula)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Formula#units renders XML; the dimension identity needs the raw
|
|
141
|
+
# decomposition, so extract it the same way Formula does internally.
|
|
142
|
+
# rubocop:disable-next Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
143
|
+
def extract_units(formula)
|
|
144
|
+
formula.each_with_object([]) do |term, units_arr|
|
|
145
|
+
case term
|
|
146
|
+
when Unit
|
|
147
|
+
units_arr << term.dup
|
|
148
|
+
when Formula
|
|
149
|
+
units_arr.concat(extract_units(term.value.dup))
|
|
150
|
+
when Sqrt
|
|
151
|
+
next unless term.value.is_a?(Unit)
|
|
152
|
+
|
|
153
|
+
sqrt_unit = term.value.dup
|
|
154
|
+
sqrt_unit.power_numerator = Number.new("0.5")
|
|
155
|
+
units_arr << sqrt_unit
|
|
156
|
+
when Fenced
|
|
157
|
+
units_arr.concat(extract_units(term.value.value.dup))
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# R-A4: ratio-scale operations are specified errors on non-ratio
|
|
163
|
+
# quantities. Differences and comparisons remain legal on both.
|
|
164
|
+
# Scalar multiplication/division scales the magnitude: legal on
|
|
165
|
+
# ratio scales, a specified error on non-ratio ones.
|
|
166
|
+
def gate_scaling!
|
|
167
|
+
return if scale == :ratio
|
|
168
|
+
|
|
169
|
+
raise Errors::ScaleOperationError,
|
|
170
|
+
"scaling is not legal on a #{scale} scale quantity"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def scale_ratio_gate!(other, operation)
|
|
174
|
+
return if scale == :ratio && other.scale == :ratio
|
|
175
|
+
|
|
176
|
+
raise Errors::ScaleOperationError,
|
|
177
|
+
"#{operation} is not legal between #{scale} and " \
|
|
178
|
+
"#{other.scale} scale quantities"
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def quantity_kind!(other)
|
|
182
|
+
unless other.is_a?(Quantity)
|
|
183
|
+
raise ArgumentError, "expected a Unitsml::Quantity, got #{other.class}"
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def gate_dimension!(other)
|
|
188
|
+
# Unknown dimensions (units without a decomposition path) defer the
|
|
189
|
+
# legality decision to the conversion layer.
|
|
190
|
+
return if dimension_vector.nil? || other.dimension_vector.nil?
|
|
191
|
+
return if dimension_vector == other.dimension_vector
|
|
192
|
+
|
|
193
|
+
raise Errors::DimensionMismatchError,
|
|
194
|
+
"dimension mismatch: #{dimension_vector.inspect} " \
|
|
195
|
+
"vs #{other.dimension_vector.inspect}"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def gate_zero!(other)
|
|
199
|
+
return unless other.zero?
|
|
200
|
+
|
|
201
|
+
raise Errors::IndeterminateResultError,
|
|
202
|
+
"division by a zero quantity is indeterminate"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def compose(left, right, operator)
|
|
206
|
+
"#{left.norm_text}#{operator}#{right.norm_text}"
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
data/lib/unitsml/version.rb
CHANGED
data/lib/unitsml.rb
CHANGED
|
@@ -26,6 +26,8 @@ module Unitsml
|
|
|
26
26
|
autoload :Unitsdb, "unitsml/unitsdb"
|
|
27
27
|
autoload :Utility, "unitsml/utility"
|
|
28
28
|
autoload :VERSION, "unitsml/version"
|
|
29
|
+
autoload :Quantity, "unitsml/quantity"
|
|
30
|
+
autoload :Conversion, "unitsml/conversion"
|
|
29
31
|
autoload :Xml, "unitsml/xml"
|
|
30
32
|
|
|
31
33
|
def parse(string)
|
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.12
|
|
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-10-
|
|
11
|
+
date: 2026-10-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: asciiunits
|
|
@@ -108,6 +108,7 @@ files:
|
|
|
108
108
|
- docs/navigation.adoc
|
|
109
109
|
- lib/unitsml.rb
|
|
110
110
|
- lib/unitsml/configuration.rb
|
|
111
|
+
- lib/unitsml/conversion.rb
|
|
111
112
|
- lib/unitsml/dimension.rb
|
|
112
113
|
- lib/unitsml/errors.rb
|
|
113
114
|
- lib/unitsml/errors/base_error.rb
|
|
@@ -115,6 +116,7 @@ files:
|
|
|
115
116
|
- lib/unitsml/errors/opal_payload_not_bundled_error.rb
|
|
116
117
|
- lib/unitsml/errors/parg_load_error.rb
|
|
117
118
|
- lib/unitsml/errors/plurimath_load_error.rb
|
|
119
|
+
- lib/unitsml/errors/quantity_errors.rb
|
|
118
120
|
- lib/unitsml/errors/unsupported_payload_type_error.rb
|
|
119
121
|
- lib/unitsml/extender.rb
|
|
120
122
|
- lib/unitsml/fenced.rb
|
|
@@ -157,6 +159,7 @@ files:
|
|
|
157
159
|
- lib/unitsml/parser.rb
|
|
158
160
|
- lib/unitsml/prefix.rb
|
|
159
161
|
- lib/unitsml/prefix_adapter.rb
|
|
162
|
+
- lib/unitsml/quantity.rb
|
|
160
163
|
- lib/unitsml/sqrt.rb
|
|
161
164
|
- lib/unitsml/unit.rb
|
|
162
165
|
- lib/unitsml/unitsdb.rb
|