Skip to content

Instantly share code, notes, and snippets.

@jamesshipton
Created May 11, 2011 15:17
Show Gist options
  • Select an option

  • Save jamesshipton/966652 to your computer and use it in GitHub Desktop.

Select an option

Save jamesshipton/966652 to your computer and use it in GitHub Desktop.
Ruby::TripleStore::Jena input an rdf/xml file, with reified addition/removals for a changeset and output either the additions or removals as triples
require 'java'
dir = Dir.new("***CLASSPATH_DIR***")
dir.each {|file| $CLASSPATH << dir.path + file if file.end_with?('jar')}
java_import com.hp.hpl.jena.rdf.model.ModelFactory;
java_import com.hp.hpl.jena.rdf.model.impl.StatementImpl;
java_import java.lang.System;
input_model = ModelFactory.createDefaultModel().read(Java::java.io.FileInputStream.new('***INPUT_RDF***'), nil, 'RDF/XML')
# p "----------ADDITIONS----------"
# output_model = ModelFactory.createDefaultModel()
#
# input_model.list_objects_of_property(input_model.createProperty('http://purl.org/vocab/changeset/schema#addition')).each {|resource|output_model.add(StatementImpl.new( input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#subject')).get_object, input_model.create_property(input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate')).get_object.to_s), input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#object')).get_object))}
#
# output_model.write(System.out, 'N3')
p "----------REMOVALS----------"
output_model = ModelFactory.createDefaultModel()
input_model.list_objects_of_property(input_model.createProperty('http://purl.org/vocab/changeset/schema#removal')).each do |resource|
s = input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#subject')).get_object
p = input_model.create_property(input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate')).get_object.to_s)
o = input_model.get_required_property(resource,input_model.create_property('http://www.w3.org/1999/02/22-rdf-syntax-ns#object')).get_object
output_model.add(StatementImpl.new(s, p, o))
end
output_model.write(System.out, 'N3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment