unitsml 0.6.11 → 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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +5 -0
  3. data/Gemfile +0 -1
  4. data/docs/README.adoc +234 -1
  5. data/lib/unitsml/compose/builder.rb +347 -0
  6. data/lib/unitsml/compose/composable.rb +118 -0
  7. data/lib/unitsml/compose/composite.rb +145 -0
  8. data/lib/unitsml/compose/term_tree.rb +48 -0
  9. data/lib/unitsml/compose.rb +151 -0
  10. data/lib/unitsml/conversion/definition.rb +117 -0
  11. data/lib/unitsml/conversion.rb +332 -0
  12. data/lib/unitsml/dimension.rb +30 -3
  13. data/lib/unitsml/errors/base_error.rb +11 -0
  14. data/lib/unitsml/errors/empty_composition_error.rb +14 -0
  15. data/lib/unitsml/errors/invalid_power_error.rb +37 -0
  16. data/lib/unitsml/errors/invalid_unit_entry_error.rb +44 -0
  17. data/lib/unitsml/errors/misplaced_extender_error.rb +18 -0
  18. data/lib/unitsml/errors/mixed_terms_error.rb +15 -0
  19. data/lib/unitsml/errors/parse_error.rb +18 -0
  20. data/lib/unitsml/errors/quantity_errors.rb +1 -0
  21. data/lib/unitsml/errors/unknown_dimension_error.rb +18 -0
  22. data/lib/unitsml/errors/unknown_prefix_error.rb +15 -0
  23. data/lib/unitsml/errors/unknown_unit_error.rb +16 -0
  24. data/lib/unitsml/errors.rb +11 -0
  25. data/lib/unitsml/formula.rb +65 -4
  26. data/lib/unitsml/model/unit.rb +2 -0
  27. data/lib/unitsml/model/units/conversion_note.rb +19 -0
  28. data/lib/unitsml/model/units/conversions.rb +25 -0
  29. data/lib/unitsml/model/units/float64_conversion_from.rb +62 -0
  30. data/lib/unitsml/model/units/special_conversion_from.rb +27 -0
  31. data/lib/unitsml/model/units/wsdl_conversion_from.rb +26 -0
  32. data/lib/unitsml/model/units.rb +5 -0
  33. data/lib/unitsml/number.rb +5 -1
  34. data/lib/unitsml/opal/database_payload.rb +1 -1
  35. data/lib/unitsml/opal.rb +23 -0
  36. data/lib/unitsml/parg/tree_mapper.rb +31 -1
  37. data/lib/unitsml/parser.rb +12 -1
  38. data/lib/unitsml/power_numerator.rb +61 -0
  39. data/lib/unitsml/quantity.rb +122 -22
  40. data/lib/unitsml/unit.rb +60 -4
  41. data/lib/unitsml/unitsdb/quantities.rb +21 -0
  42. data/lib/unitsml/unitsdb/units.rb +6 -0
  43. data/lib/unitsml/utility.rb +61 -10
  44. data/lib/unitsml/version.rb +1 -1
  45. data/lib/unitsml.rb +11 -0
  46. metadata +24 -2
@@ -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, explicit_value&.dig(:quantity)),
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, explicit_value&.dig(:quantity)),
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)
@@ -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
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ module Model
5
+ module Units
6
+ # Descriptive note attached to a conversion definition (NoteType).
7
+ class ConversionNote < Lutaml::Model::Serializable
8
+ attribute :content, :string
9
+
10
+ xml do
11
+ element "ConversionNote"
12
+ namespace ::Unitsml::Namespace
13
+
14
+ map_content to: :content
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ module Model
5
+ module Units
6
+ # The Conversions container of a unit: linear definitions
7
+ # (Float64ConversionFrom) and the non-linear / remote stand-ins
8
+ # (SpecialConversionFrom, WSDLConversionFrom).
9
+ class Conversions < Lutaml::Model::Serializable
10
+ attribute :float64_conversion_from, Float64ConversionFrom
11
+ attribute :special_conversion_from, SpecialConversionFrom
12
+ attribute :wsdl_conversion_from, WsdlConversionFrom
13
+
14
+ xml do
15
+ element "Conversions"
16
+ namespace ::Unitsml::Namespace
17
+
18
+ map_element :Float64ConversionFrom, to: :float64_conversion_from
19
+ map_element :SpecialConversionFrom, to: :special_conversion_from
20
+ map_element :WSDLConversionFrom, to: :wsdl_conversion_from
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ module Model
5
+ module Units
6
+ # The linear conversion definition of UnitsML 1.0 (Float64ConversionFrom):
7
+ # y = d + ((b / c) (x + a)) from the initial unit x to this unit y.
8
+ #
9
+ # Decimal attributes are lexical strings, preserved verbatim per the
10
+ # numeric contract (CalConnect 36813): "273.15" never becomes 273.15.
11
+ # The XSD defaults (a=0, b=1, c=1, d=0) apply where an attribute is
12
+ # absent; the conversion engine resolves them at application time.
13
+ class Float64ConversionFrom < Lutaml::Model::Serializable
14
+ XSD_DEFAULTS = {
15
+ initial_addend: "0",
16
+ multiplicand: "1",
17
+ divisor: "1",
18
+ final_addend: "0",
19
+ }.freeze
20
+
21
+ attribute :id, :xml_id
22
+ attribute :initial_unit, :string
23
+ attribute :initial_addend, :string
24
+ attribute :initial_addend_decimal_place, :integer
25
+ attribute :multiplicand, :string
26
+ attribute :multiplicand_digits, :integer
27
+ attribute :divisor, :string
28
+ attribute :divisor_digits, :integer
29
+ attribute :final_addend, :string
30
+ attribute :final_addend_decimal_place, :integer
31
+ attribute :exact, :boolean
32
+ attribute :conversion_note, ConversionNote, collection: true
33
+
34
+ xml do
35
+ element "Float64ConversionFrom"
36
+ namespace ::Unitsml::Namespace
37
+
38
+ map_attribute :id, to: :id
39
+ map_attribute :initialUnit, to: :initial_unit
40
+ map_attribute :initialAddend, to: :initial_addend
41
+ map_attribute :initialAddendDecimalPlace,
42
+ to: :initial_addend_decimal_place
43
+ map_attribute :multiplicand, to: :multiplicand
44
+ map_attribute :multiplicandDigits, to: :multiplicand_digits
45
+ map_attribute :divisor, to: :divisor
46
+ map_attribute :divisorDigits, to: :divisor_digits
47
+ map_attribute :finalAddend, to: :final_addend
48
+ map_attribute :finalAddendDecimalPlace,
49
+ to: :final_addend_decimal_place
50
+ map_attribute :exact, to: :exact
51
+ map_element :ConversionNote, to: :conversion_note
52
+ end
53
+
54
+ # Lexical attribute with the XSD default applied.
55
+ def lexical_decimal(name)
56
+ value = public_send(name)
57
+ value.nil? ? XSD_DEFAULTS.fetch(name) : value
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ module Model
5
+ module Units
6
+ # A conversion more complex than the linear equation
7
+ # (SpecialConversionFrom). Never approximated by a linear factor;
8
+ # the conversion engine refuses it with a typed error.
9
+ class SpecialConversionFrom < Lutaml::Model::Serializable
10
+ attribute :id, :xml_id
11
+ attribute :initial_unit, :string
12
+ attribute :conversion_url, :string
13
+ attribute :conversion_description, :string, collection: true
14
+
15
+ xml do
16
+ element "SpecialConversionFrom"
17
+ namespace ::Unitsml::Namespace
18
+
19
+ map_attribute :id, to: :id
20
+ map_attribute :initialUnit, to: :initial_unit
21
+ map_attribute :conversionURL, to: :conversion_url
22
+ map_element :ConversionDescription, to: :conversion_description
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ module Model
5
+ module Units
6
+ # A SOAP/WSDL-based conversion (WSDLConversionFrom). Remote by
7
+ # definition; the conversion engine refuses it with a typed error.
8
+ class WsdlConversionFrom < Lutaml::Model::Serializable
9
+ attribute :id, :xml_id
10
+ attribute :initial_unit, :string
11
+ attribute :wsdl_url, :string
12
+ attribute :wsdl_description, :string, collection: true
13
+
14
+ xml do
15
+ element "WSDLConversionFrom"
16
+ namespace ::Unitsml::Namespace
17
+
18
+ map_attribute :id, to: :id
19
+ map_attribute :initialUnit, to: :initial_unit
20
+ map_attribute :wsdlURL, to: :wsdl_url
21
+ map_element :WSDLDescription, to: :wsdl_description
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -3,11 +3,16 @@
3
3
  module Unitsml
4
4
  module Model
5
5
  module Units
6
+ autoload :ConversionNote, "unitsml/model/units/conversion_note"
7
+ autoload :Conversions, "unitsml/model/units/conversions"
6
8
  autoload :EnumeratedRootUnit, "unitsml/model/units/enumerated_root_unit"
9
+ autoload :Float64ConversionFrom, "unitsml/model/units/float64_conversion_from"
7
10
  autoload :Name, "unitsml/model/units/name"
8
11
  autoload :RootUnits, "unitsml/model/units/root_units"
9
12
  autoload :Symbol, "unitsml/model/units/symbol"
13
+ autoload :SpecialConversionFrom, "unitsml/model/units/special_conversion_from"
10
14
  autoload :System, "unitsml/model/units/system"
15
+ autoload :WsdlConversionFrom, "unitsml/model/units/wsdl_conversion_from"
11
16
  end
12
17
  end
13
18
  end
@@ -9,7 +9,11 @@ module Unitsml
9
9
  alias raw_value value
10
10
 
11
11
  def initialize(value)
12
- @value = value
12
+ # Number always holds a String representation; the render/compare helpers
13
+ # (to_html, negative?, update_negative_sign, ...) assume String, so a
14
+ # numeric value (e.g. a Float exponent from unit decomposition) is
15
+ # normalized here rather than crashing later.
16
+ @value = value.to_s
13
17
  end
14
18
 
15
19
  def ==(other)