unitsdb 2.1.0 → 2.1.1
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/.rubocop_todo.yml +92 -15
- data/Gemfile +1 -0
- data/README.adoc +108 -2
- data/lib/unitsdb/cli.rb +7 -0
- data/lib/unitsdb/commands/_modify.rb +2 -2
- data/lib/unitsdb/commands/base.rb +7 -33
- data/lib/unitsdb/commands/normalize.rb +1 -1
- data/lib/unitsdb/commands/release.rb +47 -86
- data/lib/unitsdb/commands/ucum/check.rb +126 -0
- data/lib/unitsdb/commands/ucum/formatter.rb +141 -0
- data/lib/unitsdb/commands/ucum/matcher.rb +301 -0
- data/lib/unitsdb/commands/ucum/update.rb +84 -0
- data/lib/unitsdb/commands/ucum/updater.rb +98 -0
- data/lib/unitsdb/commands/ucum/xml_parser.rb +34 -0
- data/lib/unitsdb/commands/ucum.rb +43 -0
- data/lib/unitsdb/commands/validate/identifiers.rb +3 -1
- data/lib/unitsdb/commands/validate/references.rb +3 -1
- data/lib/unitsdb/commands/validate/si_references.rb +0 -6
- data/lib/unitsdb/commands/validate.rb +3 -3
- data/lib/unitsdb/database.rb +1 -0
- data/lib/unitsdb/dimensions.rb +1 -0
- data/lib/unitsdb/prefixes.rb +1 -0
- data/lib/unitsdb/quantities.rb +1 -0
- data/lib/unitsdb/scales.rb +1 -0
- data/lib/unitsdb/ucum.rb +198 -0
- data/lib/unitsdb/unit_systems.rb +1 -0
- data/lib/unitsdb/units.rb +1 -0
- data/lib/unitsdb/version.rb +1 -1
- data/unitsdb.gemspec +0 -2
- metadata +10 -2
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../base"
|
4
|
+
require_relative "../../database"
|
5
|
+
require_relative "../../errors"
|
6
|
+
require_relative "xml_parser"
|
7
|
+
require_relative "formatter"
|
8
|
+
require_relative "matcher"
|
9
|
+
require_relative "updater"
|
10
|
+
require "fileutils"
|
11
|
+
|
12
|
+
module Unitsdb
|
13
|
+
module Commands
|
14
|
+
module Ucum
|
15
|
+
class Check < Base
|
16
|
+
# Constants
|
17
|
+
ENTITY_TYPES = %w[units prefixes].freeze
|
18
|
+
|
19
|
+
def run
|
20
|
+
# Get options
|
21
|
+
entity_type = @options[:entity_type]&.downcase
|
22
|
+
direction = @options[:direction]&.downcase || "both"
|
23
|
+
output_dir = @options[:output_updated_database]
|
24
|
+
include_potential = @options[:include_potential_matches] || false
|
25
|
+
database_path = @options[:database]
|
26
|
+
ucum_file = @options[:ucum_file]
|
27
|
+
|
28
|
+
# Validate parameters
|
29
|
+
validate_parameters(direction, ucum_file)
|
30
|
+
|
31
|
+
# Use the path as-is without expansion
|
32
|
+
puts "Using database directory: #{database_path}"
|
33
|
+
|
34
|
+
@db = Unitsdb::Database.from_db(database_path)
|
35
|
+
|
36
|
+
puts "Using UCUM file: #{ucum_file}"
|
37
|
+
puts "Include potential matches: #{include_potential ? "Yes" : "No"}"
|
38
|
+
|
39
|
+
# Parse UCUM XML file
|
40
|
+
ucum_data = XmlParser.parse_ucum_file(ucum_file)
|
41
|
+
|
42
|
+
# Process entity types
|
43
|
+
process_entities(entity_type, ucum_data, direction, output_dir, include_potential)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# Process all entity types or a specific one
|
49
|
+
def process_entities(entity_type, ucum_data, direction, output_dir, include_potential)
|
50
|
+
if entity_type && ENTITY_TYPES.include?(entity_type)
|
51
|
+
process_entity_type(entity_type, ucum_data, direction, output_dir, include_potential)
|
52
|
+
else
|
53
|
+
ENTITY_TYPES.each do |type|
|
54
|
+
process_entity_type(type, ucum_data, direction, output_dir, include_potential)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Process a specific entity type
|
60
|
+
def process_entity_type(entity_type, ucum_data, direction, output_dir, include_potential = false)
|
61
|
+
puts "\n========== Processing #{entity_type.upcase} References ==========\n"
|
62
|
+
|
63
|
+
db_entities = @db.send(entity_type)
|
64
|
+
ucum_entities = XmlParser.get_entities_from_ucum(entity_type, ucum_data)
|
65
|
+
|
66
|
+
puts "Found #{ucum_entities.size} #{entity_type} in UCUM"
|
67
|
+
puts "Found #{db_entities.size} #{entity_type} in database"
|
68
|
+
|
69
|
+
check_from_ucum(entity_type, ucum_entities, db_entities, output_dir, include_potential) if %w[from_ucum
|
70
|
+
both].include?(direction)
|
71
|
+
|
72
|
+
return unless %w[to_ucum both].include?(direction)
|
73
|
+
|
74
|
+
check_to_ucum(entity_type, ucum_entities, db_entities, output_dir, include_potential)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Validation helpers
|
78
|
+
def validate_parameters(direction, ucum_file)
|
79
|
+
unless %w[to_ucum from_ucum both].include?(direction)
|
80
|
+
puts "Invalid direction: #{direction}. Must be one of: to_ucum, from_ucum, both"
|
81
|
+
exit(1)
|
82
|
+
end
|
83
|
+
|
84
|
+
return if File.exist?(ucum_file)
|
85
|
+
|
86
|
+
puts "UCUM file not found: #{ucum_file}"
|
87
|
+
exit(1)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Direction handler: UCUM → UnitsDB
|
91
|
+
def check_from_ucum(entity_type, ucum_entities, db_entities, output_dir, include_potential = false)
|
92
|
+
Formatter.print_direction_header("UCUM → UnitsDB")
|
93
|
+
|
94
|
+
matches, missing_matches, unmatched_ucum = Matcher.match_ucum_to_db(entity_type, ucum_entities, db_entities)
|
95
|
+
|
96
|
+
# Print results
|
97
|
+
Formatter.display_ucum_results(entity_type, matches, missing_matches, unmatched_ucum)
|
98
|
+
|
99
|
+
# Update references if needed
|
100
|
+
return unless output_dir && !missing_matches.empty?
|
101
|
+
|
102
|
+
output_file = File.join(output_dir, "#{entity_type}.yaml")
|
103
|
+
Updater.update_references(entity_type, missing_matches, db_entities, output_file, include_potential)
|
104
|
+
puts "\nUpdated references written to #{output_file}"
|
105
|
+
end
|
106
|
+
|
107
|
+
# Direction handler: UnitsDB → UCUM
|
108
|
+
def check_to_ucum(entity_type, ucum_entities, db_entities, output_dir, include_potential = false)
|
109
|
+
Formatter.print_direction_header("UnitsDB → UCUM")
|
110
|
+
|
111
|
+
matches, missing_refs, unmatched_db = Matcher.match_db_to_ucum(entity_type, ucum_entities, db_entities)
|
112
|
+
|
113
|
+
# Print results
|
114
|
+
Formatter.display_db_results(entity_type, matches, missing_refs, unmatched_db)
|
115
|
+
|
116
|
+
# Update references if needed
|
117
|
+
return unless output_dir && !missing_refs.empty?
|
118
|
+
|
119
|
+
output_file = File.join(output_dir, "#{entity_type}.yaml")
|
120
|
+
Updater.update_references(entity_type, missing_refs, db_entities, output_file, include_potential)
|
121
|
+
puts "\nUpdated references written to #{output_file}"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Unitsdb
|
4
|
+
module Commands
|
5
|
+
module Ucum
|
6
|
+
# Formats output for UCUM matching results
|
7
|
+
module Formatter
|
8
|
+
module_function
|
9
|
+
|
10
|
+
# Print a direction header (UnitsDB → UCUM or UCUM → UnitsDB)
|
11
|
+
def print_direction_header(direction)
|
12
|
+
puts "\n=== #{direction} ===\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Display results for UCUM → UnitsDB matching
|
16
|
+
def display_ucum_results(entity_type, matches, missing_matches, unmatched_ucum)
|
17
|
+
puts "\nResults for #{entity_type.capitalize} (UCUM → UnitsDB):"
|
18
|
+
puts " Matched: #{matches.size}"
|
19
|
+
puts " Missing matches (could be added): #{missing_matches.size}"
|
20
|
+
puts " Unmatched UCUM entities: #{unmatched_ucum.size}"
|
21
|
+
|
22
|
+
unless unmatched_ucum.empty?
|
23
|
+
puts "\nUnmatched UCUM #{entity_type}:"
|
24
|
+
unmatched_ucum.each do |entity|
|
25
|
+
case entity
|
26
|
+
when Unitsdb::UcumPrefix
|
27
|
+
puts " - #{entity.name} (#{entity.code_sensitive})"
|
28
|
+
when Unitsdb::UcumBaseUnit
|
29
|
+
puts " - #{entity.name} (#{entity.code_sensitive}, dimension: #{entity.dimension})"
|
30
|
+
when Unitsdb::UcumUnit
|
31
|
+
name = entity.name.is_a?(Array) ? entity.name.first : entity.name
|
32
|
+
puts " - #{name} (#{entity.code_sensitive}, class: #{entity.klass})"
|
33
|
+
else
|
34
|
+
puts " - Unknown entity type"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
return if missing_matches.empty?
|
40
|
+
|
41
|
+
puts "\nPotential additions (UCUM #{entity_type} that could be added to UnitsDB):"
|
42
|
+
missing_matches.each do |match|
|
43
|
+
ucum_entity = match[:ucum_entity]
|
44
|
+
db_entity = match[:db_entity]
|
45
|
+
|
46
|
+
# Get entity IDs and names
|
47
|
+
db_id = get_db_entity_id(db_entity)
|
48
|
+
db_name = get_db_entity_name(db_entity)
|
49
|
+
ucum_name = get_ucum_entity_name(ucum_entity)
|
50
|
+
ucum_code = ucum_entity.respond_to?(:code_sensitive) ? ucum_entity.code_sensitive : "unknown"
|
51
|
+
|
52
|
+
case ucum_entity
|
53
|
+
when Unitsdb::UcumPrefix
|
54
|
+
puts " - UnitsDB prefix '#{db_name}' (#{db_id}) → UCUM prefix '#{ucum_name}' (#{ucum_code})"
|
55
|
+
when Unitsdb::UcumBaseUnit
|
56
|
+
puts " - UnitsDB unit '#{db_name}' (#{db_id}) → UCUM base unit '#{ucum_name}' (#{ucum_code})"
|
57
|
+
when Unitsdb::UcumUnit
|
58
|
+
puts " - UnitsDB unit '#{db_name}' (#{db_id}) → UCUM unit '#{ucum_name}' (#{ucum_code})"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Display results for UnitsDB → UCUM matching
|
64
|
+
def display_db_results(entity_type, matches, missing_refs, unmatched_db)
|
65
|
+
puts "\nResults for #{entity_type.capitalize} (UnitsDB → UCUM):"
|
66
|
+
puts " Matched: #{matches.size}"
|
67
|
+
puts " Missing references (could be added): #{missing_refs.size}"
|
68
|
+
puts " Unmatched UnitsDB entities: #{unmatched_db.size}"
|
69
|
+
|
70
|
+
unless unmatched_db.empty?
|
71
|
+
puts "\nUnmatched UnitsDB #{entity_type}:"
|
72
|
+
unmatched_db.each do |entity|
|
73
|
+
id = get_db_entity_id(entity)
|
74
|
+
name = get_db_entity_name(entity)
|
75
|
+
puts " - #{name} (#{id})"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
return if missing_refs.empty?
|
80
|
+
|
81
|
+
puts "\nPotential references (UCUM references that could be added to UnitsDB):"
|
82
|
+
missing_refs.each do |match|
|
83
|
+
ucum_entity = match[:ucum_entity]
|
84
|
+
db_entity = match[:db_entity]
|
85
|
+
|
86
|
+
# Get entity IDs and names
|
87
|
+
db_id = get_db_entity_id(db_entity)
|
88
|
+
db_name = get_db_entity_name(db_entity)
|
89
|
+
ucum_name = get_ucum_entity_name(ucum_entity)
|
90
|
+
ucum_code = ucum_entity.respond_to?(:code_sensitive) ? ucum_entity.code_sensitive : "unknown"
|
91
|
+
|
92
|
+
case ucum_entity
|
93
|
+
when Unitsdb::UcumPrefix
|
94
|
+
puts " - UnitsDB prefix '#{db_name}' (#{db_id}) → UCUM prefix '#{ucum_name}' (#{ucum_code})"
|
95
|
+
when Unitsdb::UcumBaseUnit
|
96
|
+
puts " - UnitsDB unit '#{db_name}' (#{db_id}) → UCUM base unit '#{ucum_name}' (#{ucum_code})"
|
97
|
+
when Unitsdb::UcumUnit
|
98
|
+
puts " - UnitsDB unit '#{db_name}' (#{db_id}) → UCUM unit '#{ucum_name}' (#{ucum_code})"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Helper to get db entity id
|
104
|
+
def get_db_entity_id(entity)
|
105
|
+
if entity.respond_to?(:identifiers) && entity.identifiers && !entity.identifiers.empty?
|
106
|
+
entity.identifiers.first.id
|
107
|
+
elsif entity.respond_to?(:id)
|
108
|
+
entity.id
|
109
|
+
else
|
110
|
+
"unknown-id"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Helper to get db entity name
|
115
|
+
def get_db_entity_name(entity)
|
116
|
+
if entity.respond_to?(:names) && entity.names && !entity.names.empty?
|
117
|
+
entity.names.first.value
|
118
|
+
elsif entity.respond_to?(:short) && entity.short
|
119
|
+
entity.short
|
120
|
+
elsif entity.respond_to?(:name)
|
121
|
+
entity.name
|
122
|
+
else
|
123
|
+
"unknown-name"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Helper to get ucum entity name
|
128
|
+
def get_ucum_entity_name(entity)
|
129
|
+
case entity
|
130
|
+
when Unitsdb::UcumPrefix, Unitsdb::UcumBaseUnit
|
131
|
+
entity.name
|
132
|
+
when Unitsdb::UcumUnit
|
133
|
+
entity.name.is_a?(Array) ? entity.name.first : entity.name
|
134
|
+
else
|
135
|
+
"unknown-name"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,301 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Unitsdb
|
4
|
+
module Commands
|
5
|
+
module Ucum
|
6
|
+
# Matcher for UCUM and UnitsDB entities
|
7
|
+
module Matcher
|
8
|
+
module_function
|
9
|
+
|
10
|
+
# Match UCUM entities to UnitsDB entities (UCUM → UnitsDB)
|
11
|
+
def match_ucum_to_db(entity_type, ucum_entities, db_entities)
|
12
|
+
puts "Matching UCUM #{entity_type} to UnitsDB #{entity_type}..."
|
13
|
+
|
14
|
+
# Initialize result arrays
|
15
|
+
matches = []
|
16
|
+
missing_matches = []
|
17
|
+
unmatched_ucum = []
|
18
|
+
|
19
|
+
# Process each UCUM entity
|
20
|
+
ucum_entities.each do |ucum_entity|
|
21
|
+
match_data = find_db_match_for_ucum(ucum_entity, db_entities, entity_type)
|
22
|
+
|
23
|
+
if match_data[:match]
|
24
|
+
matches << { ucum_entity: ucum_entity, db_entity: match_data[:match] }
|
25
|
+
elsif match_data[:potential_match]
|
26
|
+
missing_matches << { ucum_entity: ucum_entity, db_entity: match_data[:potential_match] }
|
27
|
+
else
|
28
|
+
unmatched_ucum << ucum_entity
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
[matches, missing_matches, unmatched_ucum]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Match UnitsDB entities to UCUM entities (UnitsDB → UCUM)
|
36
|
+
def match_db_to_ucum(entity_type, ucum_entities, db_entities)
|
37
|
+
puts "Matching UnitsDB #{entity_type} to UCUM #{entity_type}..."
|
38
|
+
|
39
|
+
# Initialize result arrays
|
40
|
+
matches = []
|
41
|
+
missing_refs = []
|
42
|
+
unmatched_db = []
|
43
|
+
|
44
|
+
# Process each UnitsDB entity
|
45
|
+
db_entities.send(entity_type).each do |db_entity|
|
46
|
+
# Skip entities that already have UCUM references
|
47
|
+
if has_ucum_reference?(db_entity)
|
48
|
+
matches << { db_entity: db_entity, ucum_entity: find_referenced_ucum_entity(db_entity, ucum_entities) }
|
49
|
+
next
|
50
|
+
end
|
51
|
+
|
52
|
+
match_data = find_ucum_match_for_db(db_entity, ucum_entities, entity_type)
|
53
|
+
|
54
|
+
if match_data[:match]
|
55
|
+
missing_refs << { db_entity: db_entity, ucum_entity: match_data[:match] }
|
56
|
+
else
|
57
|
+
unmatched_db << db_entity
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
[matches, missing_refs, unmatched_db]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Check if a UnitsDB entity already has a UCUM reference
|
65
|
+
def has_ucum_reference?(entity)
|
66
|
+
return false unless entity.respond_to?(:references) && entity.references
|
67
|
+
|
68
|
+
entity.references.any? { |ref| ref.authority == "ucum" }
|
69
|
+
end
|
70
|
+
|
71
|
+
# Find the referenced UCUM entity based on the reference URI
|
72
|
+
def find_referenced_ucum_entity(db_entity, ucum_entities)
|
73
|
+
return nil unless db_entity.respond_to?(:references) && db_entity.references
|
74
|
+
|
75
|
+
ucum_ref = db_entity.references.find { |ref| ref.authority == "ucum" }
|
76
|
+
return nil unless ucum_ref
|
77
|
+
|
78
|
+
ref_uri = ucum_ref.uri
|
79
|
+
ucum_entities.find { |ucum_entity| ucum_entity.identifier == ref_uri }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Get the ID of a UnitsDB entity
|
83
|
+
def get_entity_id(entity)
|
84
|
+
entity.respond_to?(:id) ? entity.id : nil
|
85
|
+
end
|
86
|
+
|
87
|
+
# Find a matching UnitsDB entity for a UCUM entity
|
88
|
+
def find_db_match_for_ucum(ucum_entity, db_entities, entity_type)
|
89
|
+
result = { match: nil, potential_match: nil }
|
90
|
+
|
91
|
+
# Different matching logic based on entity type
|
92
|
+
case entity_type
|
93
|
+
when "prefixes"
|
94
|
+
result = match_prefix_ucum_to_db(ucum_entity, db_entities)
|
95
|
+
when "units"
|
96
|
+
result = match_unit_ucum_to_db(ucum_entity, db_entities)
|
97
|
+
end
|
98
|
+
|
99
|
+
result
|
100
|
+
end
|
101
|
+
|
102
|
+
# Find a matching UCUM entity for a UnitsDB entity
|
103
|
+
def find_ucum_match_for_db(db_entity, ucum_entities, entity_type)
|
104
|
+
result = { match: nil }
|
105
|
+
|
106
|
+
# Different matching logic based on entity type
|
107
|
+
case entity_type
|
108
|
+
when "prefixes"
|
109
|
+
result = match_prefix_db_to_ucum(db_entity, ucum_entities)
|
110
|
+
when "units"
|
111
|
+
result = match_unit_db_to_ucum(db_entity, ucum_entities)
|
112
|
+
end
|
113
|
+
|
114
|
+
result
|
115
|
+
end
|
116
|
+
|
117
|
+
# Match UCUM prefix to UnitsDB prefix
|
118
|
+
def match_prefix_ucum_to_db(ucum_prefix, db_prefixes)
|
119
|
+
result = { match: nil, potential_match: nil }
|
120
|
+
|
121
|
+
# Try exact name match first
|
122
|
+
name_match = db_prefixes.find do |db_prefix|
|
123
|
+
db_prefix.names&.any? do |name_obj|
|
124
|
+
name_obj.value.downcase == ucum_prefix.name.downcase
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
if name_match
|
129
|
+
result[:match] = name_match
|
130
|
+
return result
|
131
|
+
end
|
132
|
+
|
133
|
+
# Try symbol match
|
134
|
+
symbol_match = db_prefixes.find do |db_prefix|
|
135
|
+
db_prefix.symbols&.any? do |symbol|
|
136
|
+
symbol.ascii == ucum_prefix.print_symbol ||
|
137
|
+
symbol.unicode == ucum_prefix.print_symbol
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
if symbol_match
|
142
|
+
result[:match] = symbol_match
|
143
|
+
return result
|
144
|
+
end
|
145
|
+
|
146
|
+
# Try value match if available (using base^power)
|
147
|
+
if ucum_prefix.value&.value
|
148
|
+
value_match = db_prefixes.find do |db_prefix|
|
149
|
+
if db_prefix.base && db_prefix.power
|
150
|
+
calculated_value = db_prefix.base**db_prefix.power
|
151
|
+
calculated_value.to_s == ucum_prefix.value.value
|
152
|
+
else
|
153
|
+
false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
result[:potential_match] = value_match if value_match
|
158
|
+
end
|
159
|
+
|
160
|
+
result
|
161
|
+
end
|
162
|
+
|
163
|
+
# Match UnitsDB prefix to UCUM prefix
|
164
|
+
def match_prefix_db_to_ucum(db_prefix, ucum_prefixes)
|
165
|
+
result = { match: nil }
|
166
|
+
|
167
|
+
# Try exact name match first
|
168
|
+
if db_prefix.names && !db_prefix.names.empty?
|
169
|
+
db_prefix_names = db_prefix.names.map { |name_obj| name_obj.value.downcase }
|
170
|
+
|
171
|
+
name_match = ucum_prefixes.find do |ucum_prefix|
|
172
|
+
db_prefix_names.include?(ucum_prefix.name.downcase)
|
173
|
+
end
|
174
|
+
|
175
|
+
if name_match
|
176
|
+
result[:match] = name_match
|
177
|
+
return result
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# Try symbol match
|
182
|
+
if db_prefix.symbols && !db_prefix.symbols.empty?
|
183
|
+
symbol_match = ucum_prefixes.find do |ucum_prefix|
|
184
|
+
db_prefix.symbols.any? do |symbol|
|
185
|
+
ucum_prefix.print_symbol == symbol.ascii ||
|
186
|
+
ucum_prefix.print_symbol == symbol.unicode
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
result[:match] = symbol_match if symbol_match
|
191
|
+
end
|
192
|
+
|
193
|
+
result
|
194
|
+
end
|
195
|
+
|
196
|
+
# Match UCUM unit to UnitsDB unit
|
197
|
+
def match_unit_ucum_to_db(ucum_unit, db_units)
|
198
|
+
result = { match: nil, potential_match: nil }
|
199
|
+
|
200
|
+
# Get UCUM unit name(s)
|
201
|
+
ucum_names = case ucum_unit
|
202
|
+
when Unitsdb::UcumBaseUnit
|
203
|
+
[ucum_unit.name]
|
204
|
+
when Unitsdb::UcumUnit
|
205
|
+
ucum_unit.name.is_a?(Array) ? ucum_unit.name : [ucum_unit.name]
|
206
|
+
else
|
207
|
+
[]
|
208
|
+
end
|
209
|
+
|
210
|
+
# Try name match
|
211
|
+
ucum_names.each do |ucum_name|
|
212
|
+
name_match = db_units.find do |db_unit|
|
213
|
+
db_unit.names&.any? do |name_obj|
|
214
|
+
name_obj.value.downcase == ucum_name.downcase
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
if name_match
|
219
|
+
result[:match] = name_match
|
220
|
+
return result
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# Try symbol match
|
225
|
+
symbol_match = db_units.find do |db_unit|
|
226
|
+
db_unit.symbols&.any? do |symbol|
|
227
|
+
symbol.ascii == ucum_unit.print_symbol ||
|
228
|
+
symbol.unicode == ucum_unit.print_symbol
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
if symbol_match
|
233
|
+
result[:match] = symbol_match
|
234
|
+
return result
|
235
|
+
end
|
236
|
+
|
237
|
+
# Try property/dimension match for potential matches
|
238
|
+
property = case ucum_unit
|
239
|
+
when Unitsdb::UcumBaseUnit
|
240
|
+
ucum_unit.property
|
241
|
+
when Unitsdb::UcumUnit
|
242
|
+
ucum_unit.property
|
243
|
+
end
|
244
|
+
|
245
|
+
if property
|
246
|
+
property_matches = db_units.select do |db_unit|
|
247
|
+
db_unit.quantity_references&.any? do |qref|
|
248
|
+
qref.id&.downcase&.include?(property.downcase)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
result[:potential_match] = property_matches.first if property_matches.any?
|
253
|
+
end
|
254
|
+
|
255
|
+
result
|
256
|
+
end
|
257
|
+
|
258
|
+
# Match UnitsDB unit to UCUM unit
|
259
|
+
def match_unit_db_to_ucum(db_unit, ucum_units)
|
260
|
+
result = { match: nil }
|
261
|
+
|
262
|
+
# Try name match first
|
263
|
+
if db_unit.names && !db_unit.names.empty?
|
264
|
+
db_unit_names = db_unit.names.map { |name_obj| name_obj.value.downcase }
|
265
|
+
|
266
|
+
name_match = ucum_units.find do |ucum_unit|
|
267
|
+
case ucum_unit
|
268
|
+
when Unitsdb::UcumBaseUnit
|
269
|
+
db_unit_names.include?(ucum_unit.name.downcase)
|
270
|
+
when Unitsdb::UcumUnit
|
271
|
+
ucum_names = ucum_unit.name.is_a?(Array) ? ucum_unit.name : [ucum_unit.name]
|
272
|
+
ucum_names.any? { |name| db_unit_names.include?(name.downcase) }
|
273
|
+
else
|
274
|
+
false
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
if name_match
|
279
|
+
result[:match] = name_match
|
280
|
+
return result
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# Try symbol match
|
285
|
+
if db_unit.symbols && !db_unit.symbols.empty?
|
286
|
+
symbol_match = ucum_units.find do |ucum_unit|
|
287
|
+
db_unit.symbols.any? do |symbol|
|
288
|
+
ucum_unit.print_symbol == symbol.ascii ||
|
289
|
+
ucum_unit.print_symbol == symbol.unicode
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
result[:match] = symbol_match if symbol_match
|
294
|
+
end
|
295
|
+
|
296
|
+
result
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../base"
|
4
|
+
require_relative "../../database"
|
5
|
+
require_relative "xml_parser"
|
6
|
+
require_relative "matcher"
|
7
|
+
require_relative "updater"
|
8
|
+
require "fileutils"
|
9
|
+
|
10
|
+
module Unitsdb
|
11
|
+
module Commands
|
12
|
+
module Ucum
|
13
|
+
# Command to update UnitsDB with UCUM references
|
14
|
+
class Update < Base
|
15
|
+
# Constants
|
16
|
+
ENTITY_TYPES = %w[units prefixes].freeze
|
17
|
+
|
18
|
+
def run
|
19
|
+
# Get options
|
20
|
+
entity_type = @options[:entity_type]&.downcase
|
21
|
+
database_path = @options[:database]
|
22
|
+
ucum_file = @options[:ucum_file]
|
23
|
+
output_dir = @options[:output_dir] || database_path
|
24
|
+
include_potential = @options[:include_potential_matches] || false
|
25
|
+
|
26
|
+
# Validate database path
|
27
|
+
unless File.exist?(database_path) && Dir.exist?(database_path)
|
28
|
+
puts "Database directory path: #{database_path}"
|
29
|
+
puts "ERROR: Database directory not found: #{database_path}"
|
30
|
+
return 1
|
31
|
+
end
|
32
|
+
puts "Using database directory: #{database_path}"
|
33
|
+
|
34
|
+
# Validate UCUM file
|
35
|
+
unless File.exist?(ucum_file)
|
36
|
+
puts "ERROR: UCUM file not found: #{ucum_file}"
|
37
|
+
return 1
|
38
|
+
end
|
39
|
+
puts "Using UCUM file: #{ucum_file}"
|
40
|
+
puts "Include potential matches: #{include_potential ? "Yes" : "No"}"
|
41
|
+
|
42
|
+
# Parse UCUM XML file
|
43
|
+
ucum_data = XmlParser.parse_ucum_file(ucum_file)
|
44
|
+
|
45
|
+
# Process entity types
|
46
|
+
if entity_type && ENTITY_TYPES.include?(entity_type)
|
47
|
+
process_entity_type(entity_type, ucum_data, output_dir, include_potential)
|
48
|
+
else
|
49
|
+
ENTITY_TYPES.each do |type|
|
50
|
+
process_entity_type(type, ucum_data, output_dir, include_potential)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
0
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def process_entity_type(entity_type, ucum_data, output_dir, include_potential)
|
60
|
+
puts "\n========== Processing #{entity_type.upcase} References =========="
|
61
|
+
|
62
|
+
# Get entities
|
63
|
+
klass = Unitsdb.const_get(entity_type.capitalize)
|
64
|
+
yaml_path = File.join(@options[:database], "#{entity_type}.yaml")
|
65
|
+
entity_collection = klass.from_yaml(File.read(yaml_path))
|
66
|
+
|
67
|
+
ucum_entities = XmlParser.get_entities_from_ucum(entity_type, ucum_data)
|
68
|
+
|
69
|
+
return if ucum_entities.nil? || ucum_entities.empty?
|
70
|
+
|
71
|
+
# Match entities
|
72
|
+
_, missing_refs, = Matcher.match_db_to_ucum(entity_type, ucum_entities, entity_collection)
|
73
|
+
|
74
|
+
# Create output directory if it doesn't exist
|
75
|
+
FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
|
76
|
+
|
77
|
+
# Update references in UnitsDB entities
|
78
|
+
output_file = File.join(output_dir, "#{entity_type}.yaml")
|
79
|
+
Updater.update_references(entity_type, missing_refs, entity_collection, output_file, include_potential)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|