Skip to content

Commit 3974363

Browse files
committed
Release 0.1.1:
* Update rdf.rb dependency. * Add more functionality to bin/ldpatch executable. * Add example to README. * Improve test coverage. * Add spec examples to algebra_spec.
2 parents 151f3a2 + 936ef3c commit 3974363

10 files changed

Lines changed: 387 additions & 87 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ This gem implements the [LD Patch][] specification with a couple of changes and/
2929
## Documentation
3030
Full documentation available on [Rubydoc.info][LD-Patch doc]
3131

32+
## Examples
33+
34+
require 'rubygems'
35+
require 'ld/patch'
36+
37+
### Example Patch
38+
39+
queryable = RDF::Repository.load("etc/doap.ttl")
40+
patch = %(
41+
@prefix doap: <http://usefulinc.com/ns/doap#> .
42+
@prefix earl: <http://www.w3.org/ns/earl#> .
43+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
44+
45+
Delete { <> a earl:TestSubject, earl:Software } .
46+
Add {
47+
<http://greggkellogg.net/foaf#me> a foaf:Person;
48+
foaf:name "Gregg Kellogg"
49+
} .
50+
Bind ?ruby <> / doap:programming-language .
51+
Cut ?ruby .
52+
)
53+
operator = LD::Patch.parse(patch, base_uri: "http://rubygems.org/gems/ld-patch")
54+
operator.execute(queryable) # alternatively queryable.query(operator)
55+
3256
## Implementation Notes
3357
The reader uses the [EBNF][] gem to generate first, follow and branch tables, and uses the `Parser` and `Lexer` modules to implement the LD Patch parser.
3458

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.0
1+
0.1.1

bin/ldpatch

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,72 @@ rescue LoadError
1010
end
1111
require 'getoptlong'
1212

13-
def run(input, options = {})
13+
def run(graph, options = {})
1414
if options[:debug]
15-
puts "input graph:\n#{options[:graph].dump(:ttl, standard_prefixes: true)}\n" if options[:graph]
16-
puts "query:\n#{input}\n"
15+
puts "target graph:\n#{graph.dump(:ttl, standard_prefixes: true)}\n"
16+
puts "patch:\n#{options[:patch]}\n"
1717
end
18-
options[:graph] ||= RDF::Graph.new
1918

2019
if options[:verbose]
21-
puts ("\nPATCH:\n" + input)
20+
puts ("\npatch:\n" + options[:patch])
2221
end
2322

24-
patch = if options[:sse]
25-
SPARQL::Algebra.parse(input, options)
26-
else
27-
# Only do grammar debugging if we're generating SSE
28-
LD::Patch.parse(input, options)
29-
end
23+
patch = LD::Patch.parse(options[:patch], options)
3024

3125
puts ("\nSSE:\n" + patch.to_sse) if options[:debug] || options[:to_sse]
3226

3327
unless options[:to_sse]
34-
res = patch.execute(options[:graph], debug: options[:debug])
35-
puts res.inspect if options[:verbose]
28+
res = patch.execute(graph, options)
3629
puts res.dump(:ttl, base_uri: patch.base_uri, prefixes: patch.prefixes, standard_prefixes: true)
3730
end
3831
end
3932

4033
opts = GetoptLong.new(
4134
["--debug", GetoptLong::NO_ARGUMENT],
42-
["--dump", GetoptLong::NO_ARGUMENT],
4335
["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
36+
["--patch", GetoptLong::REQUIRED_ARGUMENT],
4437
["--progress", GetoptLong::NO_ARGUMENT],
45-
["--sse", GetoptLong::NO_ARGUMENT],
4638
["--to-sse", GetoptLong::NO_ARGUMENT],
4739
["--validate", GetoptLong::NO_ARGUMENT],
4840
["--verbose", GetoptLong::NO_ARGUMENT],
4941
["--help", "-?", GetoptLong::NO_ARGUMENT]
5042
)
5143

52-
options = {
53-
graph: RDF::Repository.new,
54-
}
55-
56-
input = nil
44+
options = {}
5745

5846
opts.each do |opt, arg|
5947
case opt
60-
when '--debug' then options[:debug] = true
61-
when '--dump' then $dump = true
62-
when '--execute' then input = arg
63-
when '--progress' then options[:debug] ||= 2
64-
when '--sse' then options[:sse] = true
65-
when '--to-sse' then options[:to_sse] = true
66-
when '--validate' then options[:validate] = true
67-
when '--verbose' then options[:verbose] = true
48+
when '--base' then options[:base_uri] = arg
49+
when '--debug' then options[:debug] = true
50+
when '--execute' then options[:patch] = arg
51+
when '--patch' then options[:patch] = RDF::Util::File.open_file(arg).read
52+
when '--progress' then options[:debug] ||= 2
53+
when '--to-sse' then options[:to_sse] = true
54+
when '--validate' then options[:validate] = true
55+
when '--verbose' then options[:verbose] = true
6856
when "--help"
69-
puts "Usage: #{$0} [options] file-or-uri ..."
57+
puts "Usage: #{$0} [options] target graph file-or-uri ..."
7058
puts "Options:"
71-
puts " --execute,-e: Use option argument as the SPARQL input if no files are given"
72-
puts " --dump: Dump raw output, otherwise serialize to SSE"
73-
puts " --debug: Display detailed debug output"
74-
puts " --sse: Input is in SSE format"
75-
puts " --to-sse: Generate SSE instead of running query"
76-
puts " --verbose: Display details of processing"
77-
puts " --help,-?: This message"
59+
puts " --base: Base URI of target graph, if different from graph location"
60+
puts " --debug: Display detailed debug output"
61+
puts " --execute,-e: Use option argument as the patch input"
62+
puts " --patch: Location of patch document"
63+
puts " --progress Display parse tree"
64+
puts " --to-sse: Generate SSE for patch instead of running query"
65+
puts " --validate: Validate patch document"
66+
puts " --verbose: Display details of processing"
67+
puts " --help,-?: This message"
7868
exit(0)
7969
end
8070
end
8171

72+
raise "No patch defined" unless options[:patch]
8273
if ARGV.empty?
83-
s = input ? input : $stdin.read
84-
run(s, options)
74+
run(RDF::Graph.new, options)
8575
else
8676
ARGV.each do |test_file|
87-
puts "parse #{test_file}"
88-
run(RDF::Util::File.open_file(test_file).read, options.merge(base_uri: RDF::URI(test_file)))
77+
puts "patch #{test_file}"
78+
run(RDF::Graph.load(test_file), options.merge(base_uri: RDF::URI(test_file)))
8979
end
9080
end
9181
puts

ld-patch.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Gem::Specification.new do |gem|
2424

2525
gem.required_ruby_version = '>= 2.0.0'
2626
gem.requirements = []
27-
gem.add_runtime_dependency 'rdf', '~> 1.1', '>= 1.1.13'
27+
gem.add_runtime_dependency 'rdf', '~> 1.1', '>= 1.1.15'
2828
gem.add_runtime_dependency 'ebnf', '~> 0.3', '>= 0.3.9'
2929
gem.add_runtime_dependency 'sparql', '~> 1.1', '>= 1.1.7'
3030
gem.add_runtime_dependency 'sxp', '~> 0.1'

lib/ld/patch.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ module Patch
2323
#
2424
# @param [IO, StringIO, String, #to_s] input
2525
# @param [Hash{Symbol => Object}] options
26-
# @return [Object]
27-
# The parsed Patch
26+
# @option options [#to_s] :base_uri (nil)
27+
# the base URI to use when resolving relative URIs
28+
# @option (see LD::Patch::Parser#initialize)
29+
# @return [SPARQL::Algebra::Operator] The executable parsed Patch
2830
def self.parse(input, options = {})
2931
LD::Patch::Parser.new(input, options).parse
3032
end

lib/ld/patch/algebra/update_list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def execute(queryable, options = {})
3232
# Bind variables to path
3333
if var_or_iri.variable?
3434
raise LD::Patch::Error("Operand uses unbound variable #{var_or_iri.inspect}", code: 400) unless solution.bound?(var_or_iri)
35-
var_or_iri = solution[variable]
35+
var_or_iri = solution[var_or_iri]
3636
end
3737

3838
list_heads = queryable.query(subject: var_or_iri, predicate: predicate).map {|s| s.object}

lib/ld/patch/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def to_s
383383
#
384384
# @param [Symbol, #to_s] prod The starting production for the parser.
385385
# It may be a URI from the grammar, or a symbol representing the local_name portion of the grammar URI.
386-
# @return [SPARQL::Algebra::Operator, Array]
386+
# @return [SPARQL::Algebra::Operator, Object]
387387
# @raise [ParseError] when illegal grammar detected.
388388
def parse(prod = START)
389389
ll1_parse(@input, prod.to_sym, @options.merge(branch: BRANCH,

0 commit comments

Comments
 (0)