#!/usr/bin/env ruby # frozen_string_literal: true # Step 2: Transform literal values into RDF objects using SPARQL UPDATE queries require 'rdf' require 'rdf/turtle' require 'sparql' input_path = File.expand_path('../data/graph-01.ttl', __dir__) output_path = File.expand_path('../data/graph-02.ttl', __dir__) updates_dir = File.expand_path('../updates', __dir__) puts "Loading graph from #{input_path}..." graph = RDF::Graph.load(input_path) puts "Loaded #{graph.count} triples." Dir.glob(File.join(updates_dir, '*.rq')).sort.each do |query_file| query = File.read(query_file) name = File.basename(query_file) before = graph.count SPARQL.execute(query, graph, update: true) after = graph.count puts "Applied #{name}: #{before} -> #{after} triples (#{after - before >= 0 ? '+' : ''}#{after - before})" end puts "Writing #{graph.count} triples to #{output_path}..." RDF::Turtle::Writer.open(output_path, prefixes: { rdf: RDF.to_uri, rdfs: RDF::RDFS.to_uri, xsd: RDF::XSD.to_uri, base: RDF::URI.new('http://example.org/migrants/') }) do |writer| graph.each_statement { |stmt| writer << stmt } end puts "Done."