Skip to content

Commit

Permalink
Update to use graph_name instead of context.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Oct 13, 2015
1 parent 94d07e9 commit f51dd32
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 93 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source "http://rubygems.org"
gemspec

gem 'rdf', git: "git://github.com/ruby-rdf/rdf.git", branch: "develop"
gem 'ebnf', git: "git://github.com/gkellogg/ebnf.git", branch: "develop"
gem 'ebnf', git: "git://github.com/gkellogg/ebnf.git", branch: "develop"

group :development do
gem "wirble"
Expand All @@ -12,9 +12,10 @@ group :development do
end

group :development, :test do
gem "redcarpet", :platforms => :ruby
gem "redcarpet", platform: :ruby
gem 'rdf-spec', git: "git://github.com/ruby-rdf/rdf-spec.git", branch: "develop"
gem 'rdf-turtle', git: "git://github.com/ruby-rdf/rdf-turtle.git", branch: "develop"
gem 'json-ld', git: "git://github.com/ruby-rdf/json-ld.git", branch: "develop"
gem 'simplecov', require: false
gem 'coveralls', require: false
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rdf/trig/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ module RDF::TriG
# @example Obtaining an TriG format class
# RDF::Format.for("etc/foaf.trig")
# RDF::Format.for(:file_name => "etc/foaf.trig")
# RDF::Format.for(:file_extension => "trig")
# RDF::Format.for(file_extension: "trig")
# RDF::Format.for(:content_type => "application/trig")
#
# @example Obtaining serialization format MIME types
# RDF::Format.content_types #=> {"application/trig" => [RDF::TriG::Format]}
#
# @example Obtaining serialization format file extension mappings
# RDF::Format.file_extensions #=> {:trig => "application/trig"}
# RDF::Format.file_extensions #=> {trig: "application/trig"}
#
# @see http://www.w3.org/TR/rdf-testcases/#ntriples
class Format < RDF::Format
content_type 'application/trig', :extension => :trig, :alias => 'application/x-trig'
content_type 'application/trig', extension: :trig, alias: 'application/x-trig'
content_encoding 'utf-8'

reader { RDF::TriG::Reader }
Expand Down
6 changes: 3 additions & 3 deletions lib/rdf/trig/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def each_statement(&block)
##
# Iterates the given block for each RDF quad in the input.
#
# @yield [subject, predicate, object, context]
# @yield [subject, predicate, object, graph_name]
# @yieldparam [RDF::Resource] subject
# @yieldparam [RDF::URI] predicate
# @yieldparam [RDF::Value] object
# @yieldparam [RDF::URI] context
# @yieldparam [RDF::URI] graph_name
# @return [void]
def each_quad(&block)
if block_given?
Expand All @@ -88,7 +88,7 @@ def each_quad(&block)
# @raise [RDF::ReaderError] Checks parameter types and raises if they are incorrect if parsing mode is _validate_.
def add_statement(production, statement)
error("Statement is invalid: #{statement.inspect.inspect}", production: produciton) if validate? && statement.invalid?
statement.context = @graph_name if @graph_name
statement.graph_name = @graph_name if @graph_name
@callback.call(statement) if statement.subject &&
statement.predicate &&
statement.object &&
Expand Down
10 changes: 5 additions & 5 deletions lib/rdf/trig/streaming_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module StreamingWriter
# `subject` and `predicate` to create more compact output
# @return [void] `self`
def stream_statement(statement)
if statement.context != @streaming_context
if statement.graph_name != @streaming_graph
stream_epilogue
if statement.context
@output.write "#{format_term(statement.context, options)} {"
if statement.graph_name
@output.write "#{format_term(statement.graph_name, options)} {"
end
@streaming_context, @streaming_subject, @streaming_predicate = statement.context, statement.subject, statement.predicate
@streaming_graph, @streaming_subject, @streaming_predicate = statement.graph_name, statement.subject, statement.predicate
@output.write "#{format_term(statement.subject, options)} "
@output.write "#{format_term(statement.predicate, options)} "
elsif statement.subject != @streaming_subject
Expand All @@ -38,7 +38,7 @@ def stream_statement(statement)
def stream_epilogue
case
when @previous_statement.nil? ;
when @streaming_context then @output.puts " }"
when @streaming_graph then @output.puts " }"
else @output.puts " ."
end
end
Expand Down
72 changes: 36 additions & 36 deletions lib/rdf/trig/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module RDF::TriG
# RDF::Writer.for(:trig) #=> RDF::TriG::Writer
# RDF::Writer.for("etc/test.trig")
# RDF::Writer.for(:file_name => "etc/test.trig")
# RDF::Writer.for(:file_extension => "trig")
# RDF::Writer.for(file_extension: "trig")
# RDF::Writer.for(:content_type => "application/trig")
#
# @example Serializing RDF repo into an TriG file
Expand All @@ -36,7 +36,7 @@ module RDF::TriG
# end
#
# @example Serializing RDF statements to a string in streaming mode
# RDF::TriG::Writer.buffer(:stream => true) do |writer|
# RDF::TriG::Writer.buffer(stream: true) do |writer|
# repo.each_statement do |statement|
# writer << statement
# end
Expand All @@ -45,9 +45,9 @@ module RDF::TriG
# The writer will add prefix definitions, and use them for creating @prefix definitions, and minting QNames
#
# @example Creating @base and @prefix definitions in output
# RDF::TriG::Writer.buffer(:base_uri => "http://example.com/", :prefixes => {
# RDF::TriG::Writer.buffer(base_uri: "http://example.com/", prefixes: {
# nil => "http://example.com/ns#",
# :foaf => "http://xmlns.com/foaf/0.1/"}
# foaf: "http://xmlns.com/foaf/0.1/"}
# ) do |writer|
# repo.each_statement do |statement|
# writer << statement
Expand All @@ -59,27 +59,27 @@ class Writer < RDF::Turtle::Writer
include StreamingWriter
format RDF::TriG::Format

class ContextFilteredRepo
class GraphFilteredRepo
include RDF::Queryable

def initialize(repo, context)
def initialize(repo, graph_name)
@repo = repo
@context = context
@graph_name = graph_name
end

# Filter statements in repository to those having the specified context
# Returns each statement having the specified context, `false` for default context
# Filter statements in repository to those having the specified graph_name
# Returns each statement having the specified graph_name, `false` for default graph
# @yield statement
# @yieldparam [RDF::Statement] statement
# @return [void]
# @see [RDF::Queryable]
def each
@repo.each_statement do |st|
case @context
case @graph_name
when false
yield st if !st.context
yield st if !st.graph_name
else
yield st if st.context == @context
yield st if st.graph_name == @graph_name
end
end
end
Expand All @@ -88,7 +88,7 @@ def each
# Proxy Repository#query_pattern
# @see RDF::Repository#query_pattern
def query_pattern(pattern, &block)
pattern.context = @context || false
pattern.graph_name = @graph_name || false
@repo.send(:query_pattern, pattern, &block)
end
end
Expand Down Expand Up @@ -125,8 +125,8 @@ def initialize(output = $stdout, options = {}, &block)
reset
super do
# Set both @repo and @graph to a new repository.
# When serializing a context, @graph is changed
# to a ContextFilteredRepo
# When serializing a named graph, @graph is changed
# to a GraphFilteredRepo
@repo = @graph = RDF::Repository.new
if block_given?
case block.arity
Expand Down Expand Up @@ -183,19 +183,19 @@ def write_epilogue
preprocess
start_document

order_contexts.each do |ctx|
debug {"context: #{ctx.inspect}"}
order_graphs.each do |ctx|
debug {"graph_name: #{ctx.inspect}"}
reset
@depth = ctx ? 2 : 0

if ctx
@output.write("\n#{format_value(ctx)} {")
end

# Restrict view to the particular context
@graph = ContextFilteredRepo.new(@repo, ctx)
# Restrict view to the particular graph
@graph = GraphFilteredRepo.new(@repo, ctx)

# Pre-process statements again, but in the specified context
# Pre-process statements again, but in the specified graph
@graph.each {|st| preprocess_statement(st)}
order_subjects.each do |subject|
unless is_done?(subject)
Expand All @@ -210,39 +210,39 @@ def write_epilogue

protected

# Add additional constraint that the resource must be in a single context
# Add additional constraint that the resource must be in a single graph
def blankNodePropertyList?(subject)
super && resource_in_single_context?(subject)
super && resource_in_single_graph?(subject)
end

# Add additional constraint that the resource must be in a single context
# Add additional constraint that the resource must be in a single graph
def p_squared?(resource, position)
super && resource_in_single_context?(resource)
super && resource_in_single_graph?(resource)
end

def resource_in_single_context?(resource)
contexts = @repo.query(:subject => resource).map(&:context)
contexts += @repo.query(:object => resource).map(&:context)
contexts.uniq.length <= 1
def resource_in_single_graph?(resource)
graph_names = @repo.query(subject: resource).map(&:graph_name)
graph_names += @repo.query(object: resource).map(&:graph_name)
graph_names.uniq.length <= 1
end

# Order contexts for output
def order_contexts
debug("order_contexts") {@repo.contexts.to_a.inspect}
contexts = @repo.contexts.to_a.sort
# Order graphs for output
def order_graphs
debug("order_graphs") {@repo.graph_names.to_a.inspect}
graph_names = @repo.graph_names.to_a.sort

# include default context, if necessary
contexts.unshift(nil) unless @repo.query(:context => false).to_a.empty?
# include default graph, if necessary
graph_names.unshift(nil) unless @repo.query(graph_name: false).to_a.empty?

contexts
graph_names
end

# Perform any statement preprocessing required. This is used to perform reference counts and determine required
# prefixes.
# @param [Statement] statement
def preprocess_statement(statement)
super
get_pname(statement.context) if statement.has_context?
get_pname(statement.graph_name) if statement.has_graph?
end
end
end
10 changes: 5 additions & 5 deletions spec/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:trig,
'etc/doap.trig',
{:file_name => 'etc/doap.trig'},
{:file_extension => 'trig'},
{file_extension: 'trig'},
{:content_type => 'application/trig'},
{:content_type => 'application/x-trig'},
].each do |arg|
Expand Down Expand Up @@ -72,11 +72,11 @@
:STRING_LITERAL_LONG1 => %(<a> <b> '''\nliteral\n''' .),
:STRING_LITERAL_LONG2 => %(<a> <b> """\nliteral\n""" .),
:n3 => "@prefix foo: <bar> .\nfoo:bar = {<a> <b> <c>} .",
:nquads => "<a> <b> <c> <d> . ",
:rdfxml => '<rdf:RDF about="foo"></rdf:RDF>',
:jsonld => '{"@context" => "foo"}',
nquads: "<a> <b> <c> <d> . ",
rdfxml: '<rdf:RDF about="foo"></rdf:RDF>',
jsonld: '{"@context" => "foo"}',
:rdfa => '<div about="foo"></div>',
:microdata => '<div itemref="bar"></div>',
microdata: '<div itemref="bar"></div>',
}.each do |sym, str|
it "does not detect #{sym}" do
expect(described_class.detect(str)).to be_falsey
Expand Down
16 changes: 8 additions & 8 deletions spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
JSON_STATE = JSON::State.new(
:indent => " ",
:space => " ",
:space_before => "",
space_before: "",
:object_nl => "\n",
:array_nl => "\n"
)
Expand All @@ -13,12 +13,12 @@ def normalize(repo)
case repo
when RDF::Queryable then repo
when IO, StringIO
RDF::Repository.new.load(repo, :base_uri => @info.about)
RDF::Repository.new.load(repo, base_uri: @info.about)
else
# Figure out which parser to use
g = RDF::Repository.new
reader_class = RDF::Reader.for(:sample => repo)
reader_class.new(repo, :base_uri => @info.about).each {|s| g << s}
reader_class = RDF::Reader.for(sample: repo)
reader_class.new(repo, base_uri: @info.about).each {|s| g << s}
g
end
end
Expand All @@ -30,14 +30,14 @@ def normalize(repo)
@info = if info.respond_to?(:input)
info
elsif info.is_a?(Hash)
identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.graph_name : info[:about]
debug = info[:debug]
if debug.is_a?(Array)
debug = debug.map {|s| s.dup.force_encoding(Encoding::UTF_8)}.join("\n")
end
Info.new(identifier, info[:comment] || "", debug)
else
Info.new(expected.is_a?(RDF::Enumerable) ? expected.context : info, "", info.to_s)
Info.new(expected.is_a?(RDF::Enumerable) ? expected.graph_name : info, "", info.to_s)
end
@expected = normalize(expected)
@actual = normalize(actual)
Expand Down Expand Up @@ -68,14 +68,14 @@ def normalize(repo)
@info = if info.respond_to?(:about)
info
#elsif info.is_a?(Hash)
# identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.context : info[:about]
# identifier = info[:identifier] || expected.is_a?(RDF::Graph) ? expected.graph_name : info[:about]
# trace = info[:trace]
# if trace.is_a?(Array)
# trace = trace.map {|s| s.dup.force_encoding(Encoding::UTF_8)}.join("\n")
# end
# Info.new(identifier, info[:comment] || "", trace)
else
Info.new(expected.is_a?(RDF::Graph) ? expected.context : info, info.to_s)
Info.new(expected.is_a?(RDF::Graph) ? expected.graph_name : info, info.to_s)
end
@expected = expected
@actual = actual
Expand Down
2 changes: 1 addition & 1 deletion spec/nquads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
t.debug = [t.inspect, "source:", t.input]

reader = RDF::NQuads::Reader.new(t.input,
:validate => true)
validate: true)

repo = RDF::Repository.new

Expand Down

0 comments on commit f51dd32

Please sign in to comment.