diff --git a/scripts/project/config.json b/scripts/project/config.json index 1ef0d69..66a03a0 100644 --- a/scripts/project/config.json +++ b/scripts/project/config.json @@ -17,6 +17,22 @@ "app": "/app/" } }, + { + "documentation": { + "command": "correct-location-labels", + "description": "generate migrations to correct the labels of aggregated locations", + "arguments": [] + }, + "environment": { + "image": "nvdk/ruby-semantic-works-cli", + "interactive": true, + "script": "correct-location-labels/run.rb", + "join_networks": true + }, + "mounts": { + "app": "/app/" + } + }, { "documentation": { "command": "mu-search-delta", diff --git a/scripts/project/correct-location-labels/run.rb b/scripts/project/correct-location-labels/run.rb new file mode 100755 index 0000000..e86626f --- /dev/null +++ b/scripts/project/correct-location-labels/run.rb @@ -0,0 +1,106 @@ +#!/usr/bin/env ruby +require 'date' +require 'rdf/turtle' +require 'fileutils' + +$stdout.sync = true +ENV['LOG_SPARQL_ALL']='false' +ENV['MU_SPARQL_ENDPOINT']='http://triplestore:8890/sparql' +ENV['MU_SPARQL_TIMEOUT']='180' +require 'mu/auth-sudo' + +def ensure_dir(path) + unless Dir.exist?(path) + FileUtils.mkdir_p(path) + end +end + +def sparql_escape_uri(value) + '<' + value.to_s.gsub(/[\\"<>]/) { |s| '\\' + s } + '>' +end + +def sparql_escape_string(str) + '"""' + str.gsub(/[\\"]/) { |s| '\\' + s } + '"""' +end + +def get_locations() + query = <<~EOF +PREFIX prov: +PREFIX rdfs: +PREFIX ext: + +SELECT DISTINCT ?location ?label +WHERE { + GRAPH { + ?location a prov:Location ; + rdfs:label ?label ; + ext:werkingsgebiedNiveau """Samengesteld werkingsgebied""" . + } +} +EOF + Mu::AuthSudo.query(query) +end + +def sort_literal(literal) + literal + .humanize + .split(", ") + .sort + .join(", ") +end + +# Create output directory +timestamp=`date +%Y%0m%0d%0H%0M%0S`.strip.to_i +output_dir = "/app/config/migrations/local/2025/#{timestamp}-correct-labels-aggregate-locations/" +ensure_dir(output_dir) + + +# Generate SPARQL migration to remove existing labels +path = File.join(output_dir, "#{timestamp}-remove-labels-aggregate-locations.sparql") +File.open(path, 'w') do |file| + file.write < +PREFIX rdfs: +PREFIX ext: + +DELETE { + GRAPH { + ?location rdfs:label ?label + } +} WHERE { + GRAPH { + ?location a prov:Location ; + rdfs:label ?label ; + ext:werkingsgebiedNiveau """Samengesteld werkingsgebied""" . + } +} +EOF +end + + +# Generate TTL migration to insert alphabetically sorted labels +timestamp += 1 +filename = "#{timestamp}-insert-labels-aggregate-locations" + +path = File.join(output_dir, "#{filename}.graph") +File.open(path, 'w') do |file| + file.write < . +EOF +end + +locations = get_locations() +locations.each do |location| + File.open(path, 'a') do |file| + file.write <