From 6dfb0a656fe4e9751503b375f274d73f986956a2 Mon Sep 17 00:00:00 2001 From: mirdono Date: Thu, 10 Jul 2025 11:10:09 +0200 Subject: [PATCH] feat: add project script to correct aggregate location labels The previously added migrations can create new aggregate location resources for worship administrative units that have multiple locations as scope of operation. While those migrations did insert a label for these new location resources, the contained names are typically not sorted alphabetically as `GROUP_CONCAT` does not provide guarantees on sorting. This script allows to generate additional migrations to correct those labels. Co-authored-by: @cecemel --- scripts/project/config.json | 16 +++ .../project/correct-location-labels/run.rb | 106 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100755 scripts/project/correct-location-labels/run.rb 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 <