Module: Unitsdb::Commands::SiFormatter

Defined in:
lib/unitsdb/commands/si_formatter.rb

Overview

Formatter for SI check results

Class Method Summary collapse

Class Method Details

.display_db_results(entity_type, matches, missing_refs, unmatched_db) ⇒ Object

Display DB → TTL results



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/unitsdb/commands/si_formatter.rb', line 226

def display_db_results(entity_type, matches, missing_refs, unmatched_db)
  puts "\n=== Summary of database entities referencing SI ==="
  puts "#{entity_type.capitalize} with SI references: #{matches.size}"
  puts "#{entity_type.capitalize} missing SI references: #{missing_refs.size}"
  puts "Database #{entity_type} not matching any SI entity: #{unmatched_db.size}"

  # Show entities with valid references
  unless matches.empty?
    puts "\n=== #{entity_type.capitalize} with SI references ==="
    rows = []
    matches.each do |match|
      db_entity = match[:db_entity]
      entity_id = match[:entity_id] || db_entity.short
      entity_name = db_entity.respond_to?(:names) ? db_entity.names&.first : "unnamed"
      si_suffix = SiTtlParser.extract_identifying_suffix(match[:ttl_uri])

      ttl_label = match[:ttl_entity] ? (match[:ttl_entity][:label] || match[:ttl_entity][:name]) : "Unknown"

      rows << [
        "UnitsDB: #{entity_id}",
        "(#{entity_name})"
      ]
      rows << [
        "SI TTL:  #{si_suffix}",
        "(#{ttl_label})"
      ]
      rows << :separator unless match == matches.last
    end

    table = Terminal::Table.new(
      title: "Valid SI References",
      rows: rows
    )
    puts table
  end

  puts "\n=== #{entity_type.capitalize} that should reference SI ==="
  if missing_refs.empty?
    puts "None"
  else
    # Split missing_refs into exact and potential matches
    exact_matches = []
    potential_matches = []

    missing_refs.each do |match|
      # Determine match type
      ttl_entities = match[:ttl_entities]
      uri = ttl_entities.first[:uri]
      match_type = "Exact match" # Default
      match_type = match[:match_types][uri] if match[:match_types] && match[:match_types][uri]

      # Get match description if available
      entity_id = match[:db_entity].short
      match_pair_key = "#{entity_id}:#{ttl_entities.first[:uri]}"
      match_details = Unitsdb::Commands::SiMatcher.instance_variable_get(:@match_details)&.dig(match_pair_key)
      match_desc = match_details[:match_desc] if match_details && match_details[:match_desc]

      # Symbol matches and partial matches should always be potential matches
      if %w[symbol_match partial_match].include?(match_desc)
        potential_matches << match
      elsif match_type == "Exact match"
        exact_matches << match
      else
        potential_matches << match
      end
    end

    # Display exact matches
    puts "\n=== Exact Matches (#{exact_matches.size}) ==="
    if exact_matches.empty?
      puts "None"
    else
      rows = []
      exact_matches.each do |match|
        db_entity = match[:db_entity]
        entity_id = match[:entity_id] || db_entity.short
        entity_name = db_entity.respond_to?(:names) ? db_entity.names&.first : "unnamed"

        # Handle multiple TTL entities in a single row
        ttl_entities = match[:ttl_entities]
        if ttl_entities.size == 1
          # Single TTL entity
          ttl_entity = ttl_entities.first
          si_suffix = SiTtlParser.extract_identifying_suffix(ttl_entity[:uri])

          rows << [
            "UnitsDB: #{entity_id}",
            "(#{entity_name})"
          ]
          rows << [
            "SI TTL:  #{si_suffix}",
            "(#{ttl_entity[:label] || ttl_entity[:name] || "unnamed"})"
          ]
        else
          # Multiple TTL entities, combine them - ensure no duplicates
          si_text_parts = []
          seen_uris = {}

          ttl_entities.each do |ttl_entity|
            uri = ttl_entity[:uri]
            next if seen_uris[uri] # Skip if we've already seen this URI

            seen_uris[uri] = true

            suffix = SiTtlParser.extract_identifying_suffix(uri)
            si_text_parts << "#{suffix} (#{ttl_entity[:label] || ttl_entity[:name] || "unnamed"})"
          end

          si_text = si_text_parts.join(", ")

          rows << [
            "UnitsDB: #{entity_id}",
            "(#{entity_name})"
          ]
          rows << [
            "SI TTL:  #{si_text}",
            ""
          ]
        end

        # Get match details for this match
        match_pair_key = "#{db_entity.short}:#{ttl_entities.first[:uri]}"
        match_details = Unitsdb::Commands::SiMatcher.instance_variable_get(:@match_details)&.dig(match_pair_key)

        # Format match info
        match_info = ""
        match_info = format_match_info(match_details[:match_desc]) if match_details

        status_text = match_info.empty? ? "Missing reference" : "Missing reference (#{match_info})"
        rows << [
          "Status: #{status_text}",
          "✗"
        ]
        rows << :separator unless match == exact_matches.last
      end

      table = Terminal::Table.new(
        title: "Exact Match Missing SI References",
        rows: rows
      )
      puts table
    end

    # Display potential matches
    puts "\n=== Potential Matches (#{potential_matches.size}) ==="
    if potential_matches.empty?
      puts "None"
    else
      rows = []
      potential_matches.each do |match|
        db_entity = match[:db_entity]
        entity_id = match[:entity_id] || db_entity.short
        entity_name = db_entity.respond_to?(:names) ? db_entity.names&.first : "unnamed"

        # Handle multiple TTL entities in a single row
        ttl_entities = match[:ttl_entities]
        if ttl_entities.size == 1
          # Single TTL entity
          ttl_entity = ttl_entities.first
          si_suffix = SiTtlParser.extract_identifying_suffix(ttl_entity[:uri])

          rows << [
            "UnitsDB: #{entity_id}",
            "(#{entity_name})"
          ]
          rows << [
            "SI TTL:  #{si_suffix}",
            "(#{ttl_entity[:label] || ttl_entity[:name] || "unnamed"})"
          ]
        else
          # Multiple TTL entities, combine them - ensure no duplicates
          si_text_parts = []
          seen_uris = {}

          ttl_entities.each do |ttl_entity|
            uri = ttl_entity[:uri]
            next if seen_uris[uri] # Skip if we've already seen this URI

            seen_uris[uri] = true

            suffix = SiTtlParser.extract_identifying_suffix(uri)
            si_text_parts << "#{suffix} (#{ttl_entity[:label] || ttl_entity[:name] || "unnamed"})"
          end

          si_text = si_text_parts.join(", ")

          rows << [
            "UnitsDB: #{entity_id}",
            "(#{entity_name})"
          ]
          rows << [
            "SI TTL:  #{si_text}",
            ""
          ]
        end

        # Get match details
        match_pair_key = "#{db_entity.short}:#{ttl_entities.first[:uri]}"
        match_details = Unitsdb::Commands::SiMatcher.instance_variable_get(:@match_details)&.dig(match_pair_key)

        # Format match info
        match_info = ""
        match_info = format_match_info(match_details[:match_desc]) if match_details

        status_text = match_info.empty? ? "Missing reference" : "Missing reference (#{match_info})"
        rows << [
          "Status: #{status_text}",
          "✗"
        ]
        rows << :separator unless match == potential_matches.last
      end

      table = Terminal::Table.new(
        title: "Potential Match Missing SI References",
        rows: rows
      )
      puts table
    end
  end
end

.display_si_results(entity_type, matches, missing_matches, unmatched_ttl) ⇒ Object

Display TTL → DB results



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/unitsdb/commands/si_formatter.rb', line 13

def display_si_results(entity_type, matches, missing_matches, unmatched_ttl)
  puts "\n=== #{entity_type.capitalize} with matching SI references ==="
  if matches.empty?
    puts "None"
  else
    rows = []
    matches.each do |match|
      si_suffix = SiTtlParser.extract_identifying_suffix(match[:si_uri])
      rows << [
        "UnitsDB: #{match[:entity_id]}",
        "(#{match[:entity_name] || "unnamed"})"
      ]
      rows << [
        "SI TTL:  #{si_suffix}",
        "(#{match[:si_label] || match[:si_name] || "unnamed"})"
      ]
      rows << :separator unless match == matches.last
    end

    table = Terminal::Table.new(
      title: "Valid SI Reference Mappings",
      rows: rows
    )
    puts table
  end

  puts "\n=== #{entity_type.capitalize} without SI references ==="
  if missing_matches.empty?
    puts "None"
  else
    # Split matches into exact and potential
    exact_matches = []
    potential_matches = []

    missing_matches.each do |match|
      # Get match details
      match_details = match[:match_details]
      match_desc = match_details&.dig(:match_desc) || ""

      # Symbol matches and partial matches should always be potential matches
      if %w[symbol_match partial_match].include?(match_desc)
        potential_matches << match
      elsif match_details&.dig(:exact) == false
        potential_matches << match
      else
        exact_matches << match
      end
    end

    # Display exact matches
    puts "\n=== Exact Matches (#{exact_matches.size}) ==="
    if exact_matches.empty?
      puts "None"
    else
      rows = []
      exact_matches.each do |match|
        # First row: UnitsDB entity
        rows << [
          "UnitsDB: #{match[:entity_id]}",
          "(#{match[:entity_name] || "unnamed"})"
        ]

        # Handle multiple SI matches in a single cell if present
        if match[:multiple_si]
          # Ensure no duplicate URIs
          si_text_parts = []
          si_label_parts = []
          seen_uris = {}

          match[:multiple_si].each do |si_data|
            uri = si_data[:uri]
            next if seen_uris[uri] # Skip if we've already seen this URI

            seen_uris[uri] = true

            suffix = SiTtlParser.extract_identifying_suffix(uri)
            si_text_parts << suffix
            si_label_parts << (si_data[:label] || si_data[:name])
          end

          rows << [
            "SI TTL:  #{si_text_parts.join(", ")}",
            "(#{si_label_parts.join(", ")})"
          ]
        else
          # Second row: SI TTL suffix and label/name
          si_suffix = SiTtlParser.extract_identifying_suffix(match[:si_uri])
          rows << [
            "SI TTL:  #{si_suffix}",
            "(#{match[:si_label] || match[:si_name] || "unnamed"})"
          ]
        end

        # Status line with match type
        match_details = match[:match_details]
        match_desc = match_details&.dig(:match_desc) || ""
        match_info = format_match_info(match_desc)
        status_text = match_info.empty? ? "Missing reference" : "Missing reference (#{match_info})"

        rows << [
          "Status: #{status_text}",
          "✗"
        ]
        rows << :separator unless match == exact_matches.last
      end

      table = Terminal::Table.new(
        title: "Exact Match Missing SI References",
        rows: rows
      )
      puts table
    end

    # Display potential matches
    puts "\n=== Potential Matches (#{potential_matches.size}) ==="
    if potential_matches.empty?
      puts "None"
    else
      rows = []
      potential_matches.each do |match|
        # First row: UnitsDB entity
        rows << [
          "UnitsDB: #{match[:entity_id]}",
          "(#{match[:entity_name] || "unnamed"})"
        ]

        # Handle multiple SI matches in a single cell if present
        if match[:multiple_si]
          # Ensure no duplicate URIs
          si_text_parts = []
          seen_uris = {}

          match[:multiple_si].each do |si_data|
            uri = si_data[:uri]
            next if seen_uris[uri] # Skip if we've already seen this URI

            seen_uris[uri] = true

            suffix = SiTtlParser.extract_identifying_suffix(uri)
            si_text_parts << "#{suffix} (#{si_data[:label] || si_data[:name]})"
          end

          rows << [
            "SI TTL:  #{si_text_parts.join(", ")}",
            ""
          ]
        else
          # Single TTL entity
          si_suffix = SiTtlParser.extract_identifying_suffix(match[:si_uri])
          rows << [
            "SI TTL:  #{si_suffix}",
            "(#{match[:si_label] || match[:si_name] || "unnamed"})"
          ]
        end

        # Status line
        match_details = match[:match_details]
        match_desc = match_details&.dig(:match_desc) || ""
        match_info = format_match_info(match_desc)
        status_text = match_info.empty? ? "Missing reference" : "Missing reference"

        rows << [
          "Status: #{status_text}",
          "✗"
        ]
        rows << :separator unless match == potential_matches.last
      end

      table = Terminal::Table.new(
        title: "Potential Match Missing SI References",
        rows: rows
      )
      puts table
    end
  end

  puts "\n=== SI #{entity_type.capitalize} not mapped to our database ==="
  if unmatched_ttl.empty?
    puts "None (All TTL entities are referenced - Good job!)"
  else
    # Group unmatched ttl entities by their URI to avoid duplicates
    grouped_unmatched = {}

    unmatched_ttl.each do |entity|
      uri = entity[:uri]
      grouped_unmatched[uri] = entity unless grouped_unmatched.key?(uri)
    end

    rows = []
    unique_entities = grouped_unmatched.values

    unique_entities.each do |entity|
      # Create the SI TTL row
      si_suffix = SiTtlParser.extract_identifying_suffix(entity[:uri])
      ttl_row = ["SI TTL:  #{si_suffix}", "(#{entity[:label] || entity[:name] || "unnamed"})"]

      rows << ttl_row
      rows << [
        "Status: No matching UnitsDB entity",
        "?"
      ]
      rows << :separator unless entity == unique_entities.last
    end

    table = Terminal::Table.new(
      title: "Unmapped SI Entities",
      rows: rows
    )
    puts table
  end
end

.format_match_info(match_desc) ⇒ Object

Format match info for display



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/unitsdb/commands/si_formatter.rb', line 472

def format_match_info(match_desc)
  {
    "short_to_name" => "short → name",
    "short_to_label" => "short → label",
    "name_to_name" => "name → name",
    "name_to_label" => "name → label",
    "name_to_alt_label" => "name → alt_label",
    "symbol_match" => "symbol → symbol",
    "partial_match" => "partial match"
  }[match_desc] || ""
end

Print direction header



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/unitsdb/commands/si_formatter.rb', line 448

def print_direction_header(direction)
  case direction
  when "SI → UnitsDB"
    puts "\n=== Checking SI → UnitsDB (TTL entities referenced by database) ==="
  when "UnitsDB → SI"
    puts "\n=== Checking UnitsDB → SI (database entities referencing TTL) ==="
  end

  puts "\n=== Instructions for #{direction} direction ==="
  case direction
  when "SI → UnitsDB"
    puts "If you are the UnitsDB Register Manager, please ensure that all SI entities have proper references in the UnitsDB database."
    puts "For each missing reference, add a reference with the appropriate URI and 'authority: \"si-digital-framework\"'."
  when "UnitsDB → SI"
    puts "If you are the UnitsDB Register Manager, please add SI references to UnitsDB entities that should have them."
    puts "For each entity that should reference SI, add a reference with 'authority: \"si-digital-framework\"' and the SI TTL URI."
  end
end

.set_match_details(details) ⇒ Object



467
468
469
# File 'lib/unitsdb/commands/si_formatter.rb', line 467

def set_match_details(details)
  @match_details = details
end