Skip to content

Instantly share code, notes, and snippets.

@flyingzumwalt
Last active August 29, 2015 14:23
Show Gist options
  • Save flyingzumwalt/ce003dafed36489cc6e7 to your computer and use it in GitHub Desktop.
Save flyingzumwalt/ce003dafed36489cc6e7 to your computer and use it in GitHub Desktop.
querying rdf graph with BGPs in ruby
container_predicate = ::RDF::URI.new("http://example.com/hasFiles")
options = {type: ::RDF::URI.new("http://example.com/primaryFile") }
# This works but is inefficient
contained_uris = query_node.resource.query(predicate: container_predicate).map { |r| r.object.to_s }
contained_objects = contained_uris.map { |object_uri| klass.find(klass.uri_to_id(object_uri)) }
filtered_objects = contained_objects.select {|o| o.metadata_node.type.include?(options[:type]) }
return filtered_objects.first
# Tried this, but it doesn't work
query = ::RDF::Query.new do
pattern [:parent, container_predicate, :child]
pattern [:child, ::RDF.type, options[:type]]
end
object_uri = query_node.resource.first_object(query)
return klass.find(klass.uri_to_id(object_uri))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment