From ca0bed48c75d416c92f6e3108116fd0a70fdfb56 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Mon, 15 Feb 2021 17:49:57 -0800 Subject: [PATCH 1/3] Fix Graph#graph?, Statement#graph?, and Value#graph? to take zero or one arguments, as it is used in different ways as has_graph? --- lib/rdf/model/graph.rb | 19 +++++++++++++++---- lib/rdf/model/statement.rb | 19 ++++++++++++++++--- lib/rdf/model/value.rb | 17 ++++++++++++++--- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/rdf/model/graph.rb b/lib/rdf/model/graph.rb index f9a277da..41fe2920 100644 --- a/lib/rdf/model/graph.rb +++ b/lib/rdf/model/graph.rb @@ -138,11 +138,22 @@ def load!(*args) end ## - # Returns `true` to indicate that this is a graph. + # @overload graph? + # Returns `true` to indicate that this is a graph. # - # @return [Boolean] - def graph? - true + # @return [Boolean] + # @overload graph?(name) + # Returns `true` if `self` contains the given RDF graph_name. + # + # @param [RDF::Resource, false] graph_name + # Use value `false` to query for the default graph_name + # @return [Boolean] + def graph?(*args) + case args.length + when 0 then true + when 1 then graph_name == args.first + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## diff --git a/lib/rdf/model/statement.rb b/lib/rdf/model/statement.rb index 0bedc29d..abcbf6b4 100644 --- a/lib/rdf/model/statement.rb +++ b/lib/rdf/model/statement.rb @@ -215,9 +215,22 @@ def complete? end ## - # @return [Boolean] - def graph? - !!graph_name + # @overload graph? + # Returns `true` if the statement has a graph name. + # + # @return [Boolean] + # @overload graph?(name) + # Returns `true` if `self` contains the given RDF graph_name. + # + # @param [RDF::Resource, false] graph_name + # Use value `false` to query for the default graph_name + # @return [Boolean] + def graph?(*args) + case args.length + when 0 then !!graph_name + when 1 then graph_name == args.first + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :name?, :graph? alias_method :has_graph?, :graph? diff --git a/lib/rdf/model/value.rb b/lib/rdf/model/value.rb index 375d9a4a..64447958 100644 --- a/lib/rdf/model/value.rb +++ b/lib/rdf/model/value.rb @@ -29,11 +29,22 @@ module RDF # @see RDF::Statement module Value ## - # Returns `true` if `self` is a {RDF::Graph}. + # @overload graph? + # Returns `true` if `self` is a {RDF::Graph}. # - # @return [Boolean] - def graph? + # @return [Boolean] + # @overload graph?(name) + # Returns `true` if `self` contains the given RDF graph_name. + # + # @param [RDF::Resource, false] graph_name + # Use value `false` to query for the default graph_name + # @return [Boolean] + def graph?(*args) false + case args.length + when 0, 1 then false + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## From 82345f2ab74af0380974fcb08f77f31129fc28f5 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Tue, 16 Feb 2021 14:35:52 -0800 Subject: [PATCH 2/3] Normalize existential accessors (e.g. `#statement?`) to take both 0-ary and 1-ary variations. * `#term?` * `#statement?` * `#variable?` * `#graph?` --- lib/rdf/mixin/enumerable.rb | 17 +++++++++--- lib/rdf/model/graph.rb | 14 ++++++---- lib/rdf/model/statement.rb | 44 ++++++++++++++++++++++-------- lib/rdf/model/term.rb | 18 ++++++++++--- lib/rdf/model/value.rb | 52 +++++++++++++++++++++++++++--------- lib/rdf/query.rb | 19 ++++++++++--- lib/rdf/query/solution.rb | 21 ++++++++++----- lib/rdf/query/solutions.rb | 21 ++++++++++----- lib/rdf/query/variable.rb | 24 +++++++++++++---- lib/rdf/repository.rb | 28 ++++++++++++++----- lib/rdf/transaction.rb | 8 ++++-- spec/model_statement_spec.rb | 3 +++ spec/query_spec.rb | 2 ++ 13 files changed, 205 insertions(+), 66 deletions(-) diff --git a/lib/rdf/mixin/enumerable.rb b/lib/rdf/mixin/enumerable.rb index fceeb376..8f1bc567 100644 --- a/lib/rdf/mixin/enumerable.rb +++ b/lib/rdf/mixin/enumerable.rb @@ -136,8 +136,13 @@ def statements(**options) # # @param [RDF::Statement] statement # @return [Boolean] - def statement?(statement = nil) - statement && !enum_statement.find { |s| s.eql?(statement) }.nil? + def statement?(*args) + case args.length + when 0 then false + when 1 + args.first && !enum_statement.find { |s| s.eql?(args.first) }.nil? + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_statement?, :statement? alias_method :include?, :statement? @@ -541,8 +546,12 @@ def terms(unique: true) # @param [RDF::Resource] value # @return [Boolean] # @since 2.0 - def term?(value = nil) - value && enum_term.include?(value) + def term?(*args) + case args.length + when 0 then super + when 1 then args.first && enum_term.include?(args.first) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_term?, :term? diff --git a/lib/rdf/model/graph.rb b/lib/rdf/model/graph.rb index 41fe2920..cdb8b39e 100644 --- a/lib/rdf/model/graph.rb +++ b/lib/rdf/model/graph.rb @@ -238,11 +238,15 @@ def count # @param [Statement] statement # @return [Boolean] # @see RDF::Enumerable#statement? - def statement?(statement = nil) - return false if statement.nil? - statement = statement.dup - statement.graph_name = graph_name - @data.statement?(statement) + def statement?(*args) + case args.length + when 0 then false + when 1 + statement = args.first.dup + statement.graph_name = graph_name + @data.statement?(statement) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_statement?, :statement? diff --git a/lib/rdf/model/statement.rb b/lib/rdf/model/statement.rb index abcbf6b4..8bcc9c70 100644 --- a/lib/rdf/model/statement.rb +++ b/lib/rdf/model/statement.rb @@ -136,23 +136,45 @@ def initialize! end ## - # Returns `true` to indicate that this value is a statement. + # @overload statement? + # Returns `true` if `self` is a {RDF::Statement}. # - # @return [Boolean] - def statement? - true + # @return [Boolean] + # @overload statement?(statement) + # Returns `true` if `self` contains the given {RDF::Statement}. + # + # @param [RDF::Statement] statement + # @return [Boolean] + def statement?(*args) + case args.length + when 0 then true + when 1 then self == args.first || subject.statement?(*args) || object.statement?(*args) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## - # Returns `true` if any element of the statement is not a + # @overload variable? + # Returns `true` if any element of the statement is not a # URI, Node or Literal. # - # @return [Boolean] - def variable? - !(subject? && subject.constant? && - predicate? && predicate.constant? && - object? && object.constant? && - (graph? ? graph_name.constant? : true)) + # @return [Boolean] + # @overload variable?(variables) + # Returns `true` if this statement contains any of the variables. + # + # @param [Array] variables + # @return [Boolean] + def variable?(*args) + case args.length + when 0 + !(subject? && subject.constant? && + predicate? && predicate.constant? && + object? && object.constant? && + (graph? ? graph_name.constant? : true)) + when 1 + to_quad.any? {|t| t.respond_to?(:variable?) && t.variable?(*args)} + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## diff --git a/lib/rdf/model/term.rb b/lib/rdf/model/term.rb index 9c068e03..64ed147a 100644 --- a/lib/rdf/model/term.rb +++ b/lib/rdf/model/term.rb @@ -57,11 +57,21 @@ def eql?(other) end ## - # Returns `true` if `self` is a {RDF::Term}. + # @overload term? + # Returns `true` if `self` is a {RDF::Term}. # - # @return [Boolean] - def term? - true + # @return [Boolean] + # @overload term?(name) + # Returns `true` if `self` contains the given RDF subject term. + # + # @param [RDF::Resource] value + # @return [Boolean] + def term?(*args) + case args.length + when 0 then true + when 1 then false + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## diff --git a/lib/rdf/model/value.rb b/lib/rdf/model/value.rb index 64447958..528c2ffb 100644 --- a/lib/rdf/model/value.rb +++ b/lib/rdf/model/value.rb @@ -40,7 +40,6 @@ module Value # Use value `false` to query for the default graph_name # @return [Boolean] def graph?(*args) - false case args.length when 0, 1 then false else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") @@ -48,11 +47,20 @@ def graph?(*args) end ## - # Is this a {RDF::Statement}? + # @overload statement? + # Returns `true` if `self` is a {RDF::Statement}. # - # @return [Boolean] - def statement? - false + # @return [Boolean] + # @overload statement?(statement) + # Returns `true` if `self` contains the given {RDF::Statement}. + # + # @param [RDF::Statement] statement + # @return [Boolean] + def statement?(*args) + case args.length + when 0, 1 then false + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## @@ -64,11 +72,20 @@ def list? end ## - # Is this a {RDF::Term}? + # @overload term? + # Returns `true` if `self` is a {RDF::Term}. # - # @return [Boolean] - def term? - false + # @return [Boolean] + # @overload term?(name) + # Returns `true` if `self` contains the given RDF subject term. + # + # @param [RDF::Resource] value + # @return [Boolean] + def term?(*args) + case args.length + when 0, 1 then false + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## @@ -114,12 +131,21 @@ def uri? end ## - # Is this a {RDF::Query::Variable}, or does it contain a variable? + # @overload variable? + # Returns `true` if `self` is a {RDF::Query::Variable}, or does it contain a variable? # - # @return [Boolean] + # @return [Boolean] + # @overload variable?(variable) + # Returns `true` if `self` contains the given variable. + # + # @param [RDF::Resource] value + # @return [Boolean] # @since 0.1.7 - def variable? - false + def variable?(*args) + case args.length + when 0, 1 then false + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## diff --git a/lib/rdf/query.rb b/lib/rdf/query.rb index 6192b1ce..809a4381 100644 --- a/lib/rdf/query.rb +++ b/lib/rdf/query.rb @@ -443,11 +443,22 @@ def apply_graph_name(graph_name = nil) end ## - # Returns `true` if any pattern contains a variable. + # @overload variable? + # Returns `true` if any pattern contains a variable. # - # @return [Boolean] - def variable? - !variables.empty? + # @return [Boolean] + # @overload variable?(variables) + # Returns `true` if any pattern contains any of the variables. + # + # @param [Array] variables + # @return [Boolean] + def variable?(*args) + case args.length + when 0 then !variables.empty? + when 1 + patterns.any? {|p| p.variable?(*args)} + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :variables?, :variable? alias_method :has_variables?, :variable? diff --git a/lib/rdf/query/solution.rb b/lib/rdf/query/solution.rb index b4abc2a0..ac6ffc11 100644 --- a/lib/rdf/query/solution.rb +++ b/lib/rdf/query/solution.rb @@ -114,15 +114,24 @@ def enum_value end ## - # Returns `true` if this solution contains bindings for any of the given + # @overload variable? + # Returns `false`. + # + # @return [Boolean] + # @overload variable?(variables) + # Returns `true` if this solution contains bindings for any of the given # `variables`. # - # @param [Array] variables - # an array of variables to check - # @return [Boolean] `true` or `false` + # @param [Array] variables + # @return [Boolean] # @since 0.3.0 - def variable?(variables) - variables.any? { |variable| bound?(variable) } + def variable?(*args) + case args.length + when 0 then false + when 1 + args.first.any? { |variable| bound?(variable) } + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :variables?, :variable? alias_method :has_variables?, :variable? diff --git a/lib/rdf/query/solutions.rb b/lib/rdf/query/solutions.rb index 6c33d2c6..0a780cc5 100644 --- a/lib/rdf/query/solutions.rb +++ b/lib/rdf/query/solutions.rb @@ -78,16 +78,25 @@ def variable_names end ## - # Returns `true` if this solution sequence contains bindings for any of + # @overload variable? + # Returns `false`. + # + # @return [Boolean] + # @overload variable?(variables) + # Returns `true` if this solution sequence contains bindings for any of # the given `variables`. # - # @param [Array] variables - # an array of variables to check - # @return [Boolean] `true` or `false` + # @param [Array] variables + # @return [Boolean] # @see RDF::Query::Solution#variable? # @see RDF::Query#execute - def variable?(variables) - self.any? { |solution| solution.variables?(variables) } + def variable?(*args) + case args.length + when 0 then false + when 1 + self.any? { |solution| solution.variables?(args.first) } + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :variables?, :variable? alias_method :have_variables?, :variable? diff --git a/lib/rdf/query/variable.rb b/lib/rdf/query/variable.rb index 62241e67..7d28deeb 100644 --- a/lib/rdf/query/variable.rb +++ b/lib/rdf/query/variable.rb @@ -87,13 +87,27 @@ def initialize(name = nil, value = nil, distinguished: nil, existential: nil) end ## - # Returns `true`. + # @overload variable? + # Returns `true` if `self` is a {RDF::Query::Variable}, or does it contain a variable? # - # @return [Boolean] - # @see RDF::Term#variable? + # @return [Boolean] + # @overload variable?(variable) + # Returns `true` if `self` contains the given variable. + # + # @param [RDF::Resource] value + # @return [Boolean] # @since 0.1.7 - def variable? - true + def variable?(*args) + case args.length + when 0 then true + when 1 + case variable = args.first + when RDF::Query::Variable then self == variable + when Symbol then to_sym == variable + else false + end + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end ## diff --git a/lib/rdf/repository.rb b/lib/rdf/repository.rb index 6844a664..4667baba 100644 --- a/lib/rdf/repository.rb +++ b/lib/rdf/repository.rb @@ -287,10 +287,22 @@ def count end ## - # @private - # @see RDF::Enumerable#graph? - def graph?(graph) - @data.key?(graph) + # @overload graph? + # Returns `false` to indicate that this is not a graph. + # + # @return [Boolean] + # @overload graph?(name) + # Returns `true` if `self` contains the given RDF graph_name. + # + # @param [RDF::Resource, false] graph_name + # Use value `false` to query for the default graph_name + # @return [Boolean] + def graph?(*args) + case args.length + when 0 then false + when 1 then @data.key?(args.first) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_graph?, :graph? @@ -322,8 +334,12 @@ def each_graph(&block) # @overload statement?(statement) # @private # @see RDF::Enumerable#statement? - def statement?(statement = nil) - statement && statement_in?(@data, statement) + def statement?(*args) + case args.length + when 0 then false + when 1 then args.first && statement_in?(@data, args.first) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_statement?, :statement? diff --git a/lib/rdf/transaction.rb b/lib/rdf/transaction.rb index de6380d4..c97deb94 100644 --- a/lib/rdf/transaction.rb +++ b/lib/rdf/transaction.rb @@ -213,8 +213,12 @@ def readable? # @see RDF::Value#statement? # @overload statement?(statement) # @see RDF::Enumerable#statement? - def statement?(statement = nil) - statement && read_target.has_statement?(statement) + def statement?(*args) + case args.length + when 0 then false + when 1 then read_target.statement?(*args) + else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") + end end alias_method :has_statement?, :statement? diff --git a/spec/model_statement_spec.rb b/spec/model_statement_spec.rb index ed19f5c5..12718ead 100644 --- a/spec/model_statement_spec.rb +++ b/spec/model_statement_spec.rb @@ -65,6 +65,9 @@ expect { RDF::Statement.new }.not_to raise_error end + it {is_expected.to be_statement} + it {is_expected.to be_statement(subject)} + it {is_expected.not_to be_statement(RDF::Statement.new)} it {is_expected.to have_subject} its(:subject) {is_expected.not_to be_nil} it {expect(subject.predicate?).to be_truthy} diff --git a/spec/query_spec.rb b/spec/query_spec.rb index adaf386b..4e63284b 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -560,6 +560,8 @@ context "?s p o" do subject {RDF::Query.new(RDF::Query::Pattern.new(:s, EX.p, 1))} it {expect(subject.execute(graph)).to have_result_set([{ s: EX.x1 }])} + it {is_expected.to be_variable} + it {is_expected.to be_variable(:s)} its(:variables?) {is_expected.to be_truthy} its(:variable_count) {is_expected.to eql 1} its(:variables) {is_expected.to include(:s)} From 878d42cd7a52f26803eeb60ed5d0dba38d7a00c6 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Tue, 16 Feb 2021 14:41:33 -0800 Subject: [PATCH 3/3] Version 3.1.12 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index efd03d13..b48ce58f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.11 +3.1.12