Module: Unitsdb::Commands::CheckSi::SiTtlParser

Defined in:
lib/unitsdb/commands/check_si/si_ttl_parser.rb

Overview

Parser for SI TTL files

Constant Summary collapse

SI_URI_PREFIX =
"http://si-digital-framework.org/SI/"

Class Method Summary collapse

Class Method Details

.add_symbols_to_entities(entities, graph, si) ⇒ Object

Add symbols to entities



78
79
80
81
82
83
84
# File 'lib/unitsdb/commands/check_si/si_ttl_parser.rb', line 78

def add_symbols_to_entities(entities, graph, si)
  entities.each do |entity|
    symbol = RDF::Query.new({ RDF::URI(entity[:uri]) => { si.hasSymbol => :value } })
      .execute(graph).first&.value&.to_s
    entity[:symbol] = symbol if symbol
  end
end

.extract_base_entities(graph, namespace, skos) ⇒ Object

Extract base entities from graph



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
# File 'lib/unitsdb/commands/check_si/si_ttl_parser.rb', line 48

def extract_base_entities(graph, namespace, skos)
  entities = []
  processed_uris = {}

  RDF::Query.new({ entity: { skos.prefLabel => :label } })
    .execute(graph).each do |solution|
    entity_uri = solution.entity.to_s
    next unless entity_uri.start_with?(namespace.to_s)
    next if processed_uris[entity_uri]

    processed_uris[entity_uri] = true

    entity_name = entity_uri.split("/").last
    label = RDF::Query.new({ RDF::URI(entity_uri) => { skos.prefLabel => :value } })
      .execute(graph).first&.value&.to_s
    alt_label = RDF::Query.new({ RDF::URI(entity_uri) => { skos.altLabel => :value } })
      .execute(graph).first&.value&.to_s

    entities << {
      uri: entity_uri,
      name: entity_name,
      label: label,
      alt_label: alt_label,
    }
  end

  entities
end

.extract_entities_from_ttl(entity_type, graph) ⇒ Object

Extract entities from TTL based on entity type



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/unitsdb/commands/check_si/si_ttl_parser.rb', line 29

def extract_entities_from_ttl(entity_type, graph)
  skos = RDF::Vocabulary.new("http://www.w3.org/2004/02/skos/core#")
  si = RDF::Vocabulary.new("http://si-digital-framework.org/SI#")

  namespace_uri = case entity_type
                  when "units" then "http://si-digital-framework.org/SI/units/"
                  when "quantities" then "http://si-digital-framework.org/quantities/"
                  when "prefixes" then "http://si-digital-framework.org/SI/prefixes/"
                  else return []
                  end

  namespace = RDF::Vocabulary.new(namespace_uri)
  entities = extract_base_entities(graph, namespace, skos)
  add_symbols_to_entities(entities, graph, si) if %w[units
                                                     prefixes].include?(entity_type)
  entities
end

.extract_identifying_suffix(uri) ⇒ Object

Extract suffix from URI for display



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/unitsdb/commands/check_si/si_ttl_parser.rb', line 87

def extract_identifying_suffix(uri)
  return "" unless uri

  # For display, we need to format as exactly like the original
  # This helps format the comma-separated multi-units correctly
  if uri.include?("/units/")
    # Return units/name format for units (without duplicating "units/")
    "units/#{uri.split('/').last}"
  else
    # Otherwise strip the prefix
    uri.gsub(SI_URI_PREFIX, "")
  end
end

.parse_ttl_files(dir) ⇒ Object

Parse TTL files and return RDF graph



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unitsdb/commands/check_si/si_ttl_parser.rb', line 16

def parse_ttl_files(dir)
  puts "Parsing TTL files in #{dir}..."
  graph = RDF::Graph.new

  Dir.glob(File.join(dir, "*.ttl")).each do |file|
    puts "  Reading #{File.basename(file)}"
    graph.load(file, format: :ttl)
  end

  graph
end