Skip to content

Commit 3265a69

Browse files
committed
Make query validation optional. (Notation-3 patterns will appear to be invalid).
1 parent d502df1 commit 3265a69

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ group :debug do
2222
gem 'psych', platforms: [:mri, :rbx]
2323
gem "redcarpet", platforms: :ruby
2424
gem "byebug", platforms: :mri
25-
gem 'ruby-debug', platform: :jruby
2625
gem 'guard-rspec'
2726
end
2827

lib/rdf/query.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ def self.Solutions(*args)
171171
# Alias for `:graph_name`.
172172
# @param [Hash{Symbol => Object}] options
173173
# any additional keyword options
174+
# @option options [Boolean] validate (false)
175+
# validate patterns
174176
# @yield [query]
175177
# @yieldparam [RDF::Query] query
176178
# @yieldreturn [void] ignored
177-
def initialize(*patterns, solutions: nil, graph_name: nil, name: nil, **options, &block)
179+
def initialize(*patterns, solutions: nil, graph_name: nil, name: nil, validate: false, **options, &block)
178180
@options = options.dup
179181
@solutions = Query::Solutions(solutions)
180182
graph_name = name if graph_name.nil?
@@ -195,6 +197,8 @@ def initialize(*patterns, solutions: nil, graph_name: nil, name: nil, **options,
195197
else instance_eval(&block)
196198
end
197199
end
200+
201+
validate! if validate
198202
end
199203

200204
##
@@ -291,7 +295,6 @@ def optimize!(**options)
291295
# @see http://www.holygoat.co.uk/blog/entry/2005-10-25-1
292296
# @see http://www.w3.org/TR/sparql11-query/#emptyGroupPattern
293297
def execute(queryable, solutions: Solution.new, graph_name: nil, name: nil, **options, &block)
294-
validate!
295298
options = {bindings: {}}.merge(options)
296299

297300
# Use provided solutions to allow for query chaining
@@ -516,12 +519,12 @@ def dup
516519
end
517520

518521
##
519-
# Determine if the URI is a valid according to RFC3987
522+
# Determine if the query containts valid patterns
520523
#
521524
# @return [Boolean] `true` or `false`
522525
# @since 0.3.9
523526
def valid?
524-
!!validate!
527+
!!validate! rescue raise false
525528
rescue
526529
false
527530
end

spec/query_spec.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
end
179179
end
180180
end
181-
181+
182182
context "triple pattern combinations" do
183183
let(:graph) {
184184
# Normally we would not want all of this crap in the graph for each
@@ -295,7 +295,7 @@
295295
sol3 = q3.execute(graph, solutions: sol2)
296296
expect(sol3).to have_result_set [ { s: EX.x6, o: EX.target, o2: EX.target2, o3: EX.target3 } ]
297297
end
298-
298+
299299
it "has bindings" do
300300
query = RDF::Query.new do |query|
301301
query << [:s, EX.p, :o]
@@ -478,7 +478,7 @@
478478
graph << [EX.xu, EX.p, EX.z]
479479
end
480480
}
481-
481+
482482
describe "graph-1" do
483483
subject {
484484
query = RDF::Query.new {pattern [:x, EX.p, RDF::Literal::Integer.new(1)]}
@@ -488,17 +488,17 @@
488488
it "has two solutions" do
489489
expect(subject.count).to eq 2
490490
end
491-
491+
492492
it "has xi1 as a solution" do
493493
expect(subject.filter(x: EX.xi1)).not_to be_empty
494494
end
495-
495+
496496
it "has xi2 as a solution" do
497497
expect(subject.filter(x: EX.xi2)).not_to be_empty
498498
end
499499
end
500500

501-
501+
502502
describe "graph-2" do
503503
subject {
504504
query = RDF::Query.new {pattern [:x, EX.p, RDF::Literal::Double.new("1.0e0")]}
@@ -508,7 +508,7 @@
508508
it "has one solution" do
509509
expect(subject.count).to eq 1
510510
end
511-
511+
512512
it "has xd1 as a solution" do
513513
expect(subject.filter(x: EX.xd1)).not_to be_empty
514514
end
@@ -629,15 +629,15 @@
629629
# restrictions, because the semantics of leading optional patterns
630630
# are hard to get right.
631631
expect do
632-
query = RDF::Query.new do |query|
632+
query = RDF::Query.new(validate: true) do |query|
633633
query.pattern [:s, EX.p2, :o], optional: true
634634
query.pattern [:s, EX.p, EX.o]
635635
end
636636
query.execute(@graph)
637637
end.to raise_error(ArgumentError)
638638

639639
expect do
640-
query = RDF::Query.new do |query|
640+
query = RDF::Query.new(validate: true) do |query|
641641
query.pattern [:s, EX.p, EX.o]
642642
query.pattern [:s, EX.p2, :o], optional: true
643643
query.pattern [:s, EX.x, EX.x]
@@ -716,7 +716,7 @@
716716
count
717717
).to eq 1
718718
end
719-
719+
720720
it "accepts a block" do
721721
expect(
722722
subject.
@@ -725,7 +725,7 @@
725725
).to eq 1
726726
end
727727
end
728-
728+
729729
context "order" do
730730
it "returns ordered solutions using a symbol" do
731731
orig = subject.dup
@@ -750,7 +750,7 @@
750750
subject.order {|a, b| a[:p] <=> b[:p]}
751751
end
752752
end
753-
753+
754754
it "should support duplicate elimination" do
755755
[:distinct, :reduced].each do |op|
756756
solutions = RDF::Query::Solutions(subject.to_a * 2)
@@ -776,7 +776,7 @@
776776
it "returns nil by default" do
777777
expect(subject.graph_name).to be_nil
778778
end
779-
779+
780780
it "sets and returns a graph_name" do
781781
subject.graph_name = RDF.first
782782
expect(subject.graph_name).to eq RDF.first
@@ -794,32 +794,32 @@
794794
it "returns false with no graph_name" do
795795
expect(subject.named?).to be_falsey
796796
end
797-
797+
798798
it "returns true with a graph_name" do
799799
subject.graph_name = RDF.first
800800
expect(subject.named?).to be_truthy
801801
end
802802
end
803-
803+
804804
describe "#unnamed?" do
805805
it "returns true with no graph_name" do
806806
expect(subject.unnamed?).to be_truthy
807807
end
808-
808+
809809
it "returns false with a graph_name" do
810810
subject.graph_name = RDF.first
811811
expect(subject.unnamed?).to be_falsey
812812
end
813813
end
814-
814+
815815
describe "#+" do
816816
it "returns a new RDF::Query" do
817817
rhs = RDF::Query.new
818818
q = subject + rhs
819819
expect(q).not_to be_equal(subject)
820820
expect(q).not_to be_equal(rhs)
821821
end
822-
822+
823823
it "contains patterns from each query in order" do
824824
subject.pattern [EX.first, EX.second, EX.third]
825825
rhs = RDF::Query.new

0 commit comments

Comments
 (0)