glossarist 2.14.15 → 2.14.17
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/release.yml +7 -0
- data/docs/data-format.adoc +22 -8
- data/glossarist.gemspec +3 -0
- data/lib/glossarist/cli/proposal_command.rb +131 -0
- data/lib/glossarist/cli/units_import_command.rb +40 -0
- data/lib/glossarist/cli.rb +40 -0
- data/lib/glossarist/concept.rb +4 -0
- data/lib/glossarist/default_materialization.rb +62 -0
- data/lib/glossarist/errors/invalid_proposal_error.rb +10 -0
- data/lib/glossarist/errors.rb +1 -0
- data/lib/glossarist/gcr_metadata.rb +5 -0
- data/lib/glossarist/managed_concept.rb +4 -0
- data/lib/glossarist/managed_concept_collection.rb +5 -0
- data/lib/glossarist/proposals/change.rb +44 -0
- data/lib/glossarist/proposals/git_bridge.rb +78 -0
- data/lib/glossarist/proposals/github_bridge.rb +76 -0
- data/lib/glossarist/proposals/payload.rb +25 -0
- data/lib/glossarist/proposals/pr_description.rb +51 -0
- data/lib/glossarist/proposals.rb +15 -0
- data/lib/glossarist/register_data.rb +7 -1
- data/lib/glossarist/resolution_adapter/bibliography.rb +6 -1
- data/lib/glossarist/sts/importer.rb +9 -9
- data/lib/glossarist/units/ingestor.rb +428 -0
- data/lib/glossarist/units/quantity_kind.rb +49 -0
- data/lib/glossarist/units/unit.rb +58 -0
- data/lib/glossarist/units/unit_prefix.rb +42 -0
- data/lib/glossarist/units.rb +21 -0
- data/lib/glossarist/v1/concept.rb +3 -0
- data/lib/glossarist/v1/cross_references.rb +3 -0
- data/lib/glossarist/v2/localized_concept.rb +5 -0
- data/lib/glossarist/v2/managed_concept.rb +5 -0
- data/lib/glossarist/v3/localized_concept.rb +5 -0
- data/lib/glossarist/v3/managed_concept.rb +5 -0
- data/lib/glossarist/version.rb +1 -1
- data/lib/glossarist.rb +3 -0
- metadata +31 -2
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Glossarist
|
|
4
|
+
module Proposals
|
|
5
|
+
# An editorial proposal: metadata (id, title, requester, register) plus
|
|
6
|
+
# the concept changes to apply to the dataset repository. Serializes
|
|
7
|
+
# from YAML or JSON through lutaml-model.
|
|
8
|
+
class Payload < Lutaml::Model::Serializable
|
|
9
|
+
attribute :id, :string
|
|
10
|
+
attribute :title, :string
|
|
11
|
+
attribute :requester, :string
|
|
12
|
+
attribute :register, :string
|
|
13
|
+
attribute :changes, Change, collection: true
|
|
14
|
+
|
|
15
|
+
def validate!
|
|
16
|
+
raise Errors::InvalidProposalError, "proposal id is required" if id.nil? || id.empty?
|
|
17
|
+
raise Errors::InvalidProposalError, "proposal title is required" if title.nil? || title.empty?
|
|
18
|
+
raise Errors::InvalidProposalError, "proposal requester is required" if requester.nil? || requester.empty?
|
|
19
|
+
raise Errors::InvalidProposalError, "proposal needs at least one change" if changes.nil? || changes.empty?
|
|
20
|
+
|
|
21
|
+
changes.each(&:validate!)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Glossarist
|
|
4
|
+
module Proposals
|
|
5
|
+
# Renders the pull-request body for a proposal: a human summary plus
|
|
6
|
+
# the FERIN register-process metadata block. Accepting the PR is the
|
|
7
|
+
# deliberation→published transition of the register procedure.
|
|
8
|
+
module PrDescription
|
|
9
|
+
SUBMISSION_STAGE = "proposal-input"
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def render(payload)
|
|
14
|
+
<<~BODY
|
|
15
|
+
## Proposal
|
|
16
|
+
|
|
17
|
+
#{payload.title}
|
|
18
|
+
|
|
19
|
+
Submitted by #{payload.requester} under the FERIN register
|
|
20
|
+
procedure (#{SUBMISSION_STAGE} stage). Merging this pull request
|
|
21
|
+
moves the proposal to the published stage of the register.
|
|
22
|
+
|
|
23
|
+
## Changes
|
|
24
|
+
|
|
25
|
+
#{payload.changes.map { |c| "- #{c.op} `#{c.resolve_path}`" }.join("\n")}
|
|
26
|
+
|
|
27
|
+
## FERIN process metadata
|
|
28
|
+
|
|
29
|
+
- Register: #{payload.register}
|
|
30
|
+
- Proposal ID: #{payload.id}
|
|
31
|
+
- Requester: #{payload.requester}
|
|
32
|
+
- Stage at submission: #{SUBMISSION_STAGE}
|
|
33
|
+
BODY
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Commit-message trailers mirroring the metadata above, so the
|
|
37
|
+
# proposal provenance travels with the change even when the PR body
|
|
38
|
+
# is rewritten by a merge.
|
|
39
|
+
def commit_message(payload)
|
|
40
|
+
<<~MSG
|
|
41
|
+
proposal #{payload.id}: #{payload.title}
|
|
42
|
+
|
|
43
|
+
FERIN-Proposal: #{payload.id}
|
|
44
|
+
FERIN-Register: #{payload.register}
|
|
45
|
+
FERIN-Requester: #{payload.requester}
|
|
46
|
+
FERIN-Stage: #{SUBMISSION_STAGE}
|
|
47
|
+
MSG
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Editorial proposals (FERIN register procedure, glossarist-server#45):
|
|
4
|
+
# a proposal is a payload of concept changes against a dataset repository
|
|
5
|
+
# (the git SSOT). Bridges turn a payload into a proposal branch — locally
|
|
6
|
+
# via GitBridge or as a GitHub pull request via GithubBridge.
|
|
7
|
+
module Glossarist
|
|
8
|
+
module Proposals
|
|
9
|
+
autoload :Change, "glossarist/proposals/change"
|
|
10
|
+
autoload :Payload, "glossarist/proposals/payload"
|
|
11
|
+
autoload :GitBridge, "glossarist/proposals/git_bridge"
|
|
12
|
+
autoload :GithubBridge, "glossarist/proposals/github_bridge"
|
|
13
|
+
autoload :PrDescription, "glossarist/proposals/pr_description"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -15,7 +15,7 @@ module Glossarist
|
|
|
15
15
|
attribute :repository, :string
|
|
16
16
|
attribute :license, :string
|
|
17
17
|
attribute :tags, :string, collection: true
|
|
18
|
-
attribute :translations, :hash, default: -> {}
|
|
18
|
+
attribute :translations, :hash, default: -> { {} }
|
|
19
19
|
|
|
20
20
|
# Identity and lifecycle fields. These MUST round-trip through GCR
|
|
21
21
|
# packaging so consumers (concept-browser's lineage-series renderer,
|
|
@@ -61,6 +61,12 @@ module Glossarist
|
|
|
61
61
|
map "source_repo", to: :source_repo
|
|
62
62
|
end
|
|
63
63
|
|
|
64
|
+
# lutaml-model#1272 hardening for the plain-Hash defaults — see
|
|
65
|
+
# DefaultMaterialization. Prepended (not included) so they wrap the
|
|
66
|
+
# generated accessors for `attribute :subregisters`/`:translations`.
|
|
67
|
+
prepend DefaultMaterialization::SubregistersDefault
|
|
68
|
+
prepend DefaultMaterialization::TranslationsDefault
|
|
69
|
+
|
|
64
70
|
def self.from_file(path)
|
|
65
71
|
return nil unless File.exist?(path)
|
|
66
72
|
|
|
@@ -15,7 +15,12 @@ module Glossarist
|
|
|
15
15
|
return nil unless reference.is_a?(ConceptReference)
|
|
16
16
|
return nil unless reference.source == @source_id
|
|
17
17
|
|
|
18
|
-
concepts
|
|
18
|
+
# concepts is any Enumerable of ManagedConcepts (Array or
|
|
19
|
+
# GlossaryStore); match the uuid or the data id, with version.
|
|
20
|
+
concepts.find do |c|
|
|
21
|
+
(c.uuid == reference.concept_id || c.data&.id == reference.concept_id) &&
|
|
22
|
+
(reference.version.nil? || c.version == reference.version)
|
|
23
|
+
end
|
|
19
24
|
end
|
|
20
25
|
end
|
|
21
26
|
end
|
|
@@ -58,7 +58,7 @@ module Glossarist
|
|
|
58
58
|
save_to_path(existing, dataset_path)
|
|
59
59
|
|
|
60
60
|
ImportResult.new(
|
|
61
|
-
concepts: existing
|
|
61
|
+
concepts: existing,
|
|
62
62
|
conflicts: result_state.conflicts,
|
|
63
63
|
source_files: xml_files,
|
|
64
64
|
skipped_count: result_state.skipped,
|
|
@@ -78,7 +78,7 @@ module Glossarist
|
|
|
78
78
|
existing_mc = index[key]
|
|
79
79
|
|
|
80
80
|
if existing_mc.nil?
|
|
81
|
-
existing
|
|
81
|
+
existing << mc
|
|
82
82
|
index[key] = mc
|
|
83
83
|
else
|
|
84
84
|
state.conflicts << DuplicateConflict.new(
|
|
@@ -179,26 +179,26 @@ module Glossarist
|
|
|
179
179
|
end
|
|
180
180
|
|
|
181
181
|
def replace_in_collection(collection, old_mc, new_mc)
|
|
182
|
-
collection.
|
|
183
|
-
collection
|
|
182
|
+
collection.delete(old_mc)
|
|
183
|
+
collection << new_mc
|
|
184
184
|
end
|
|
185
185
|
|
|
186
186
|
def load_existing(path)
|
|
187
|
-
collection =
|
|
187
|
+
collection = []
|
|
188
188
|
if path.end_with?(".gcr")
|
|
189
189
|
package = GcrPackage.load(path)
|
|
190
|
-
package.concepts.each { |mc| collection
|
|
190
|
+
package.concepts.each { |mc| collection << mc }
|
|
191
191
|
else
|
|
192
192
|
GlossaryStore.new.tap do |s|
|
|
193
193
|
s.load(path)
|
|
194
|
-
end.each_concept { |mc| collection
|
|
194
|
+
end.each_concept { |mc| collection << mc }
|
|
195
195
|
end
|
|
196
196
|
collection
|
|
197
197
|
end
|
|
198
198
|
|
|
199
199
|
def save_to_path(collection, path)
|
|
200
200
|
if path.end_with?(".gcr")
|
|
201
|
-
tmpdir = build_temp_dataset(collection
|
|
201
|
+
tmpdir = build_temp_dataset(collection)
|
|
202
202
|
begin
|
|
203
203
|
GC.start
|
|
204
204
|
tmp_gcr = "#{path}.tmp.#{Process.pid}"
|
|
@@ -215,7 +215,7 @@ module Glossarist
|
|
|
215
215
|
FileUtils.rm_f(tmp_gcr)
|
|
216
216
|
end
|
|
217
217
|
else
|
|
218
|
-
save_dataset(collection
|
|
218
|
+
save_dataset(collection, path)
|
|
219
219
|
end
|
|
220
220
|
end
|
|
221
221
|
|
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "tmpdir"
|
|
4
|
+
require "unitsdb"
|
|
5
|
+
|
|
6
|
+
module Glossarist
|
|
7
|
+
module Units
|
|
8
|
+
# Converts unitsdb data (https://github.com/unitsml/unitsdb, parsed
|
|
9
|
+
# via the unitsdb gem) into glossarist dataset content.
|
|
10
|
+
#
|
|
11
|
+
# The source may be a unitsdb data directory (containing units.yaml,
|
|
12
|
+
# quantities.yaml, prefixes.yaml, dimensions.yaml, unit_systems.yaml),
|
|
13
|
+
# a ZIP archive of such a directory, or nil — meaning the data bundled
|
|
14
|
+
# inside the unitsdb gem.
|
|
15
|
+
#
|
|
16
|
+
# #ingest produces {Unit}, {QuantityKind} and {UnitPrefix} model
|
|
17
|
+
# instances (+#units+, +#quantity_kinds+, +#unit_prefixes+) and
|
|
18
|
+
# {Glossarist::ManagedConcept} dataset entries (+#concepts+);
|
|
19
|
+
# #write_to persists the entries as a {Glossarist::GlossaryStore}
|
|
20
|
+
# dataset directory or .gcr package.
|
|
21
|
+
#
|
|
22
|
+
# Dataset entry conventions follow the deployed units tenant
|
|
23
|
+
# (glossarist-units-tenant):
|
|
24
|
+
#
|
|
25
|
+
# * concept identifier (data.id) = unitsdb short ("ampere");
|
|
26
|
+
# * the first unitsdb name per language is the preferred expression,
|
|
27
|
+
# remaining names become admitted expressions, and the canonical
|
|
28
|
+
# symbol becomes a symbol designation;
|
|
29
|
+
# * normative unitsdb references become authoritative concept
|
|
30
|
+
# sources, mirrored into every localization;
|
|
31
|
+
# * QUDT / SI Digital Framework references become exact_match
|
|
32
|
+
# relations and UCUM references close_match relations;
|
|
33
|
+
# * unit -> quantity kind links become "references" relations on
|
|
34
|
+
# the unit (the one direction the dataset carries; the inverse
|
|
35
|
+
# stays on QuantityKind#units);
|
|
36
|
+
# * units carry their unit systems as domains, quantities their
|
|
37
|
+
# quantity type ("base"/"derived"), prefixes a "prefix" domain;
|
|
38
|
+
# * concept status and entry status are "valid".
|
|
39
|
+
class Ingestor
|
|
40
|
+
DEFAULT_LANG_MAP = { "en" => "eng", "fr" => "fra" }.freeze
|
|
41
|
+
|
|
42
|
+
EXACT_MATCH_AUTHORITIES = %w[qudt si-digital-framework].freeze
|
|
43
|
+
NOTATION_AUTHORITIES = %w[ucum].freeze
|
|
44
|
+
|
|
45
|
+
PREFIX_DOMAIN = "prefix"
|
|
46
|
+
|
|
47
|
+
attr_reader :units, :quantity_kinds, :unit_prefixes
|
|
48
|
+
|
|
49
|
+
# @param source [String, nil] unitsdb data directory, ZIP archive
|
|
50
|
+
# path, or nil for the data bundled in the unitsdb gem
|
|
51
|
+
# @param lang_map [Hash] unitsdb 2-letter language codes mapped to
|
|
52
|
+
# ISO 639-3 codes; unmapped codes pass through unchanged
|
|
53
|
+
def initialize(source = nil, lang_map: DEFAULT_LANG_MAP)
|
|
54
|
+
@source = source
|
|
55
|
+
@lang_map = lang_map
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def ingest
|
|
59
|
+
@database = load_database
|
|
60
|
+
build_models
|
|
61
|
+
build_concepts
|
|
62
|
+
self
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def concepts
|
|
66
|
+
@concepts ||= []
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def each_concept(&)
|
|
70
|
+
concepts.each(&)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# @param path [String] output dataset directory or .gcr path
|
|
74
|
+
# @param shortname [String, nil] required for .gcr output
|
|
75
|
+
# @param version [String, nil] required for .gcr output
|
|
76
|
+
def write_to(path, shortname: nil, version: nil)
|
|
77
|
+
if path.end_with?(".gcr")
|
|
78
|
+
write_gcr(path, shortname, version)
|
|
79
|
+
else
|
|
80
|
+
write_dataset(path)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def load_database
|
|
87
|
+
if @source.nil?
|
|
88
|
+
Unitsdb::Database.from_db(bundled_data_dir)
|
|
89
|
+
elsif File.directory?(@source)
|
|
90
|
+
Unitsdb::Database.from_db(@source)
|
|
91
|
+
elsif File.file?(@source) && @source.end_with?(".zip")
|
|
92
|
+
database_from_zip
|
|
93
|
+
else
|
|
94
|
+
raise ArgumentError,
|
|
95
|
+
"unitsdb source not found " \
|
|
96
|
+
"(expected a data directory or .zip archive): #{@source}"
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def bundled_data_dir
|
|
101
|
+
File.join(Gem::Specification.find_by_name("unitsdb").gem_dir, "data")
|
|
102
|
+
rescue Gem::LoadError
|
|
103
|
+
raise ArgumentError, "the unitsdb gem is not available"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def database_from_zip
|
|
107
|
+
Dir.mktmpdir("glossarist-unitsdb") do |dir|
|
|
108
|
+
extract_zip(dir)
|
|
109
|
+
Unitsdb::Database.from_db(database_dir_within(dir))
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# rubyzip 3 does its own zip-slip guard: entries whose archive path
|
|
114
|
+
# escapes the destination directory are skipped with a warning.
|
|
115
|
+
def extract_zip(dir)
|
|
116
|
+
require "zip"
|
|
117
|
+
Zip::File.open(@source) do |zip|
|
|
118
|
+
zip.each do |entry|
|
|
119
|
+
next unless entry.file?
|
|
120
|
+
|
|
121
|
+
entry.extract(destination_directory: dir,
|
|
122
|
+
create_parent_directories: true)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def database_dir_within(dir)
|
|
128
|
+
files = Unitsdb::Database::Loader::REQUIRED_DATABASE_FILES.values
|
|
129
|
+
return dir if files.all? { |name| File.exist?(File.join(dir, name)) }
|
|
130
|
+
|
|
131
|
+
child = Dir.children(dir).find do |name|
|
|
132
|
+
path = File.join(dir, name)
|
|
133
|
+
File.directory?(path) &&
|
|
134
|
+
files.all? { |file| File.exist?(File.join(path, file)) }
|
|
135
|
+
end
|
|
136
|
+
return File.join(dir, child) if child
|
|
137
|
+
|
|
138
|
+
raise ArgumentError,
|
|
139
|
+
"no unitsdb database (#{files.join(', ')}) in #{@source}"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# ── model building ──
|
|
143
|
+
|
|
144
|
+
def build_models
|
|
145
|
+
@quantity_short_by_nist = index_shorts_by_nist(@database.quantities)
|
|
146
|
+
@dimension_id_by_nist = index_unitsml_by_nist(@database.dimensions)
|
|
147
|
+
@raw_units_by_short = {}
|
|
148
|
+
@units = @database.units.map do |raw|
|
|
149
|
+
@raw_units_by_short[raw.short] = raw
|
|
150
|
+
build_unit(raw)
|
|
151
|
+
end
|
|
152
|
+
@quantity_kinds = @database.quantities.map { |raw| build_quantity_kind(raw) }
|
|
153
|
+
@unit_prefixes = @database.prefixes.map { |raw| build_unit_prefix(raw) }
|
|
154
|
+
link_quantities_to_units
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def index_shorts_by_nist(entities)
|
|
158
|
+
entities.each_with_object({}) do |entity, hash|
|
|
159
|
+
nist = identifier_of(entity, "nist")
|
|
160
|
+
hash[nist] = entity.short if nist && entity.short
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def index_unitsml_by_nist(entities)
|
|
165
|
+
entities.each_with_object({}) do |entity, hash|
|
|
166
|
+
nist = identifier_of(entity, "nist")
|
|
167
|
+
unitsml = identifier_of(entity, "unitsml")
|
|
168
|
+
hash[nist] = unitsml if nist && unitsml
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def identifier_of(entity, type)
|
|
173
|
+
Array(entity.identifiers).find { |id| id.type == type }&.id
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def build_unit(raw)
|
|
177
|
+
Unit.new(
|
|
178
|
+
id: identifier_of(raw, "unitsml") || raw.short,
|
|
179
|
+
short: raw.short,
|
|
180
|
+
name: primary_names(raw.names),
|
|
181
|
+
alt_names: alternate_names(raw.names),
|
|
182
|
+
symbol: Array(raw.symbols).first&.unicode,
|
|
183
|
+
notations: notations_of(raw),
|
|
184
|
+
exact_matches: matches_of(raw),
|
|
185
|
+
systems: Array(raw.unit_system_reference).map(&:id),
|
|
186
|
+
quantity_kinds: Array(raw.quantity_references).map do |ref|
|
|
187
|
+
ConceptRef.new(id: @quantity_short_by_nist.fetch(ref.id, ref.id))
|
|
188
|
+
end,
|
|
189
|
+
dimension: dimension_of(raw),
|
|
190
|
+
root: raw.root,
|
|
191
|
+
references: authoritative_sources(raw),
|
|
192
|
+
)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def build_quantity_kind(raw)
|
|
196
|
+
QuantityKind.new(
|
|
197
|
+
id: identifier_of(raw, "unitsml") || raw.short,
|
|
198
|
+
short: raw.short,
|
|
199
|
+
name: primary_names(raw.names),
|
|
200
|
+
alt_names: alternate_names(raw.names),
|
|
201
|
+
notations: notations_of(raw),
|
|
202
|
+
exact_matches: matches_of(raw),
|
|
203
|
+
quantity_type: raw.quantity_type,
|
|
204
|
+
dimension: dimension_of(raw),
|
|
205
|
+
references: authoritative_sources(raw),
|
|
206
|
+
)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def build_unit_prefix(raw)
|
|
210
|
+
UnitPrefix.new(
|
|
211
|
+
id: identifier_of(raw, "unitsml") || raw.short,
|
|
212
|
+
short: raw.short,
|
|
213
|
+
name: primary_names(raw.names),
|
|
214
|
+
alt_names: alternate_names(raw.names),
|
|
215
|
+
symbol: Array(raw.symbols).first&.unicode,
|
|
216
|
+
notations: notations_of(raw),
|
|
217
|
+
base: raw.base,
|
|
218
|
+
power: raw.power,
|
|
219
|
+
references: authoritative_sources(raw),
|
|
220
|
+
)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Inverse of Unit#quantity_kinds: every quantity kind gets the
|
|
224
|
+
# units that measure it (concept-model QuantityKind#units).
|
|
225
|
+
def link_quantities_to_units
|
|
226
|
+
kinds_by_short = @quantity_kinds.each_with_object({}) do |kind, hash|
|
|
227
|
+
hash[kind.short] = kind if kind.short
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
@units.each do |unit|
|
|
231
|
+
Array(unit.quantity_kinds).each do |ref|
|
|
232
|
+
kind = kinds_by_short[ref.id]
|
|
233
|
+
kind&.units = Array(kind&.units) + [ConceptRef.new(id: unit.short)]
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# ── shared field builders ──
|
|
239
|
+
|
|
240
|
+
# Ordered { lang => [names] } — the first name per language is the
|
|
241
|
+
# primary designation, the rest are alternates.
|
|
242
|
+
def names_by_lang(names)
|
|
243
|
+
Array(names).each_with_object({}) do |name, hash|
|
|
244
|
+
lang = @lang_map.fetch(name.lang, name.lang)
|
|
245
|
+
(hash[lang] ||= []) << name.value
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
def primary_names(names)
|
|
250
|
+
names_by_lang(names).transform_values(&:first)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def alternate_names(names)
|
|
254
|
+
alts = names_by_lang(names)
|
|
255
|
+
.transform_values { |list| list.drop(1) }
|
|
256
|
+
.delete_if { |_lang, list| list.empty? }
|
|
257
|
+
alts.empty? ? nil : alts
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def notations_of(raw)
|
|
261
|
+
Array(raw.identifiers).map(&:id) +
|
|
262
|
+
external_refs(raw, *NOTATION_AUTHORITIES).map(&:uri)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def matches_of(raw)
|
|
266
|
+
external_refs(raw, *EXACT_MATCH_AUTHORITIES).map(&:uri)
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def external_refs(raw, *authorities)
|
|
270
|
+
Array(raw.references)
|
|
271
|
+
.select { |ref| authorities.include?(ref.authority) }
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
def dimension_of(raw)
|
|
275
|
+
ref = raw.dimension_reference
|
|
276
|
+
ref && @dimension_id_by_nist.fetch(ref.id, ref.id)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Normative unitsdb references become authoritative glossarist
|
|
280
|
+
# concept sources; informative ones surface as exact/close matches
|
|
281
|
+
# and notations instead (the glossarist source-type enum has no
|
|
282
|
+
# "informative").
|
|
283
|
+
def authoritative_sources(raw)
|
|
284
|
+
Array(raw.references)
|
|
285
|
+
.select { |ref| ref.type == "normative" }
|
|
286
|
+
.map { |ref| source_of(ref) }
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def source_of(ref)
|
|
290
|
+
ConceptSource.new(
|
|
291
|
+
type: "authoritative",
|
|
292
|
+
origin: Citation.new(
|
|
293
|
+
ref: Citation::Ref.new(source: ref.authority, id: ref.uri),
|
|
294
|
+
),
|
|
295
|
+
)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# ── concept building ──
|
|
299
|
+
|
|
300
|
+
def build_concepts
|
|
301
|
+
@concepts = []
|
|
302
|
+
@units.each { |unit| @concepts << unit_concept(unit) }
|
|
303
|
+
@quantity_kinds.each { |kind| @concepts << quantity_concept(kind) }
|
|
304
|
+
@unit_prefixes.each { |prefix| @concepts << prefix_concept(prefix) }
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def unit_concept(unit)
|
|
308
|
+
concept = base_concept(unit)
|
|
309
|
+
concept.data.domains = Array(unit.systems).map do |system|
|
|
310
|
+
ConceptReference.domain(system)
|
|
311
|
+
end
|
|
312
|
+
concept.related = unit_related(unit, @raw_units_by_short[unit.short])
|
|
313
|
+
concept
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
def quantity_concept(kind)
|
|
317
|
+
concept = base_concept(kind)
|
|
318
|
+
concept.data.domains = [kind.quantity_type].compact.map do |type|
|
|
319
|
+
ConceptReference.domain(type)
|
|
320
|
+
end
|
|
321
|
+
concept
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def prefix_concept(prefix)
|
|
325
|
+
concept = base_concept(prefix)
|
|
326
|
+
concept.data.domains = [ConceptReference.domain(PREFIX_DOMAIN)]
|
|
327
|
+
concept
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def base_concept(model)
|
|
331
|
+
concept = ManagedConcept.new(data: { id: model.short })
|
|
332
|
+
concept.status = "valid"
|
|
333
|
+
concept.sources = model.references
|
|
334
|
+
add_localizations(concept, model)
|
|
335
|
+
concept
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
def add_localizations(concept, model)
|
|
339
|
+
langs = (model.name || {}).keys + (model.alt_names || {}).keys
|
|
340
|
+
langs.uniq.each { |lang| concept.add_l10n(localized_concept(model, lang)) }
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# Built via of_yaml (the TermMapper pattern): attribute-assignment
|
|
344
|
+
# on a default-initialized ConceptData is skipped by lutaml's
|
|
345
|
+
# defaults-tracking serialization.
|
|
346
|
+
def localized_concept(model, lang)
|
|
347
|
+
LocalizedConcept.of_yaml(
|
|
348
|
+
"data" => {
|
|
349
|
+
"language_code" => lang,
|
|
350
|
+
"entry_status" => "valid",
|
|
351
|
+
"terms" => designation_hashes(model, lang),
|
|
352
|
+
"sources" => Array(model.references).map(&:to_hash),
|
|
353
|
+
},
|
|
354
|
+
)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
def designation_hashes(model, lang)
|
|
358
|
+
terms = []
|
|
359
|
+
if (preferred = (model.name || {})[lang])
|
|
360
|
+
terms << designation_hash("expression", preferred, "preferred")
|
|
361
|
+
end
|
|
362
|
+
Array((model.alt_names || {})[lang]).each do |value|
|
|
363
|
+
terms << designation_hash("expression", value, "admitted")
|
|
364
|
+
end
|
|
365
|
+
terms << designation_hash("symbol", model.symbol, "preferred") if model.symbol
|
|
366
|
+
terms
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def designation_hash(type, designation, normative_status)
|
|
370
|
+
{ "type" => type, "designation" => designation,
|
|
371
|
+
"normative_status" => normative_status }
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def unit_related(unit, raw)
|
|
375
|
+
quantity_edges(unit) +
|
|
376
|
+
match_edges(raw, "exact_match", EXACT_MATCH_AUTHORITIES) +
|
|
377
|
+
match_edges(raw, "close_match", NOTATION_AUTHORITIES)
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
def quantity_edges(unit)
|
|
381
|
+
Array(unit.quantity_kinds).map do |ref|
|
|
382
|
+
RelatedConcept.new(
|
|
383
|
+
type: "references", ref: ConceptRef.new(id: ref.id),
|
|
384
|
+
)
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
def match_edges(raw, type, authorities)
|
|
389
|
+
external_refs(raw, *authorities).map do |ref|
|
|
390
|
+
RelatedConcept.new(
|
|
391
|
+
type: type,
|
|
392
|
+
ref: ConceptRef.new(source: ref.authority, id: ref.uri),
|
|
393
|
+
)
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
# ── output ──
|
|
398
|
+
|
|
399
|
+
def write_dataset(path)
|
|
400
|
+
FileUtils.mkdir_p(path)
|
|
401
|
+
store = GlossaryStore.new
|
|
402
|
+
concepts.each { |concept| store.add_concept(concept) }
|
|
403
|
+
store.save_directory(path)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def write_gcr(path, shortname, version)
|
|
407
|
+
unless shortname && version
|
|
408
|
+
raise ArgumentError, "shortname and version are required for GCR output"
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
Dir.mktmpdir("glossarist-units-import") do |dir|
|
|
412
|
+
write_dataset(dir)
|
|
413
|
+
GC.start
|
|
414
|
+
tmp_gcr = "#{path}.tmp.#{Process.pid}"
|
|
415
|
+
begin
|
|
416
|
+
GcrPackage.create_from_directory(
|
|
417
|
+
dir, output: tmp_gcr, shortname: shortname, version: version
|
|
418
|
+
)
|
|
419
|
+
FileUtils.rm_f(path)
|
|
420
|
+
FileUtils.mv(tmp_gcr, path)
|
|
421
|
+
ensure
|
|
422
|
+
FileUtils.rm_f(tmp_gcr)
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
end
|
|
428
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Glossarist
|
|
4
|
+
module Units
|
|
5
|
+
# A kind of physical quantity (ISQ, ISO 80000) as dataset content.
|
|
6
|
+
#
|
|
7
|
+
# Aligned with concept-model models/units/QuantityKind.lutaml
|
|
8
|
+
# (concept-model#104): +dimension+ carries the ISQ dimension reference
|
|
9
|
+
# (as a unitsdb dimension reference id), +quantity_type+ is
|
|
10
|
+
# "base"/"derived", +broader+ references superordinate quantity kinds
|
|
11
|
+
# (skos:broader semantics; unitsdb states no quantity hierarchy, so
|
|
12
|
+
# unitsdb ingest leaves it empty), +units+ references the units that
|
|
13
|
+
# measure this quantity (the inverse of Unit#quantity_kinds).
|
|
14
|
+
# +short+ and +alt_names+ are glossarist-side additions: the dataset
|
|
15
|
+
# concept identifier convention and the remaining names per language
|
|
16
|
+
# (tenant prefLabel/altLabel split; +name+ holds the first unitsdb
|
|
17
|
+
# name per language). Localized names use the glossarist
|
|
18
|
+
# LocalizedString hash convention ({ "eng" => "length" }).
|
|
19
|
+
class QuantityKind < Lutaml::Model::Serializable
|
|
20
|
+
attribute :id, :string
|
|
21
|
+
attribute :short, :string
|
|
22
|
+
attribute :name, :hash
|
|
23
|
+
attribute :alt_names, :hash
|
|
24
|
+
attribute :symbol, :string
|
|
25
|
+
attribute :notations, :string, collection: true
|
|
26
|
+
attribute :exact_matches, :string, collection: true
|
|
27
|
+
attribute :quantity_type, :string
|
|
28
|
+
attribute :dimension, :string
|
|
29
|
+
attribute :units, Glossarist::ConceptRef, collection: true
|
|
30
|
+
attribute :broader, Glossarist::ConceptRef, collection: true
|
|
31
|
+
attribute :references, Glossarist::ConceptSource, collection: true
|
|
32
|
+
|
|
33
|
+
key_value do
|
|
34
|
+
map :id, to: :id
|
|
35
|
+
map :short, to: :short
|
|
36
|
+
map :name, to: :name
|
|
37
|
+
map %i[alt_names altNames], to: :alt_names
|
|
38
|
+
map :symbol, to: :symbol
|
|
39
|
+
map :notations, to: :notations
|
|
40
|
+
map %i[exact_matches exactMatches], to: :exact_matches
|
|
41
|
+
map %i[quantity_type quantityType], to: :quantity_type
|
|
42
|
+
map :dimension, to: :dimension
|
|
43
|
+
map :units, to: :units
|
|
44
|
+
map :broader, to: :broader
|
|
45
|
+
map :references, to: :references
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|