Class: RDF::Query::Variable

Inherits:
Object show all
Includes:
Term, SPARQL::Algebra::Expression
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/extensions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/refinements.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/extensions.rb

Overview

Extensions for RDF::Query::Variable.

Since:

  • 0.3.0

Constant Summary

Constants included from SPARQL::Algebra::Expression

SPARQL::Algebra::Expression::PATTERN_PARENTS

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SPARQL::Algebra::Expression

cast, #constant?, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods included from Term

#<=>, #aggregate?, #as_datetime, #compatible?, #escape, #ndvars, #term?, #terms, #to_term, #vars

Methods included from Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #formula?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_ndvar, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!

Constructor Details

#initialize(name = nil, value = nil, distinguished: nil, existential: nil) ⇒ Variable

Returns a new instance of Variable.

Parameters:

  • name (Symbol, #to_sym) (defaults to: nil)

    the variable name

  • value (RDF::Term) (defaults to: nil)

    an optional variable value

  • distinguished (Boolean) (defaults to: nil)

    (true) Also interpreted by leading '?' or '$' in name. If non-distinguished, '??' or '$$'.

  • existential (Boolean) (defaults to: nil)

    (true) Also interpreted by leading '$' in name

Since:

  • 0.3.0



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 70

def initialize(name = nil, value = nil, distinguished: nil, existential: nil)
  name = (name || "g#{__id__.to_i.abs}").to_s
  if name.start_with?('??')
    name, dis, ex = name[2..-1], false, false
  elsif name.start_with?('?')
    name, dis, ex = name[1..-1], true, false
  elsif name.start_with?('$$')
    name, dis, ex = name[2..-1], false, true
  elsif name.start_with?('$')
    name, dis, ex = name[1..-1], true, true
  else
    dis, ex = true, false
  end
  @name = name.to_sym
  @value = value
  @distinguished = distinguished.nil? ? dis : distinguished
  @existential = existential.nil? ? ex : existential
end

Instance Attribute Details

#nameSymbol Also known as: to_sym

The variable's name.

Returns:

Since:

  • 0.3.0



54
55
56
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 54

def name
  @name
end

#valueRDF::Term

The variable's value.

Returns:

Since:

  • 0.3.0



61
62
63
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 61

def value
  @value
end

Instance Method Details

#===(other) ⇒ Boolean

Compares this variable with the given value.

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.3.0



257
258
259
260
261
262
263
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 257

def ===(other)
  if unbound?
    other.is_a?(RDF::Term) # match any Term when unbound
  else
    value === other
  end
end

#as_numberRDF::Literal::Numeric

Parse the value as a numeric literal, or return 0.

Returns:

Since:

  • 0.3.0



194
195
196
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/extensions.rb', line 194

def as_number
  RDF::Literal(0)
end

#bind(value) ⇒ self #bind(value) ⇒ RDF::Term Also known as: bind!

Rebinds this variable to the given value.

Overloads:

  • #bind(value) ⇒ self

    Returns the bound variable.

    Parameters:

    Returns:

    • (self)

      the bound variable

  • #bind(value) ⇒ RDF::Term

    Returns the previous value, if any.

    Parameters:

    Returns:

    • (RDF::Term)

      the previous value, if any.

Since:

  • 0.3.0



181
182
183
184
185
186
187
188
189
190
191
192
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 181

def bind(value)
  if value.is_a?(RDF::Query::Solution)
    self.value = value.to_h.fetch(name, self.value)
    self
  else
    warn "[DEPRECATION] RDF::Query::Variable#bind should be used with a solution, not a term.\n" +
         "Called from #{Gem.location_of_caller.join(':')}"
    old_value = self.value
    self.value = value
    old_value
  end
end

#bindingsHash{Symbol => RDF::Term}

Returns this variable's bindings (if any) as a Hash.

Returns:

Since:

  • 0.3.0



219
220
221
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 219

def bindings
  unbound? ? {} : {name => value}
end

#bound?Boolean

Returns true if this variable is bound.

Returns:

  • (Boolean)

Since:

  • 0.3.0



125
126
127
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 125

def bound?
  !unbound?
end

#distinguished=(value) ⇒ Boolean

Sets if variable is distinguished or non-distinguished. By default, variables are distinguished

Returns:

  • (Boolean)

Since:

  • 0.3.0



150
151
152
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 150

def distinguished=(value)
  @distinguished = value
end

#distinguished?Boolean

Returns true if this variable is distinguished.

Returns:

  • (Boolean)

Since:

  • 0.3.0



141
142
143
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 141

def distinguished?
  @distinguished
end

#eql?(other) ⇒ Boolean Also known as: ==

Note:

when comparing against the default graph in an Dataset, other will be false and not be equal to an unbound variable.

Returns true if this variable is equivalent to a given other variable. Or, to another Term if bound, or to any other Term

Parameters:

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



241
242
243
244
245
246
247
248
249
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 241

def eql?(other)
  if unbound?
    other.is_a?(RDF::Term) # match any Term when unbound
  elsif other.is_a?(RDF::Query::Variable)
    @name.eql?(other.name)
  else
    value.eql?(other)
  end
end

#evaluate(bindings, **options) ⇒ RDF::Term

Returns the value of this variable in the given bindings.

Parameters:

Returns:

Raises:

  • (TypeError)

    if the variable is not bound

Since:

  • 0.3.0



12
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/refinements.rb', line 12

def evaluate(bindings, formulae:, **options); end

#existential=(value) ⇒ Boolean

Sets if variable is existential or univeresal. By default, variables are universal

Returns:

  • (Boolean)

Since:

  • 0.3.0



167
168
169
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 167

def existential=(value)
  @existential = value
end

#existential?Boolean

Returns true if this variable is existential.

Returns:

  • (Boolean)

Since:

  • 0.3.0



158
159
160
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 158

def existential?
  @existential
end

#hashInteger

Returns a hash code for this variable.

Returns:

Since:

  • 0.3.0



228
229
230
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 228

def hash
  @name.hash
end

#named?Boolean

Returns true if this variable has a name.

Returns:

  • (Boolean)

Since:

  • 0.3.0



117
118
119
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 117

def named?
  true
end

#optimize(**options) ⇒ RDF::Query::Variable

Return self

Returns:

See Also:

Since:

  • 0.3.0



608
609
610
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/extensions.rb', line 608

def optimize(**options)
  self
end

#sameTerm?(other) ⇒ Boolean

True if the other is the same variable

Returns:

  • (Boolean)

Since:

  • 0.3.0



186
187
188
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/extensions.rb', line 186

def sameTerm?(other)
  other.is_a?(::RDF::Query::Variable) && name.eql?(other.name)
end

#to_sString Also known as: to_base

Returns a string representation of this variable.

Distinguished variables are indicated with a single ?.

Non-distinguished variables are indicated with a double ??

Existential variables are indicated using a single $, or with $$ if also non-distinguished

Examples:

v = Variable.new("a")
v.to_s => '?a'
v.distinguished = false
v.to_s => '??a'

Returns:

Since:

  • 0.3.0



290
291
292
293
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 290

def to_s
  prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
  unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}"
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this term.

The Non-distinguished form (??xxx) is not part of the grammar, so replace with a blank-node

Returns:

Since:

  • 0.3.0



619
620
621
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/extensions.rb', line 619

def to_sparql(**options)
  self.distinguished? ? super : "_:_nd#{self.name}"
end

#unbindRDF::Term Also known as: unbind!

Unbinds this variable, discarding any currently bound value.

Returns:

  • (RDF::Term)

    the previous value, if any.

Since:

  • 0.3.0



199
200
201
202
203
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 199

def unbind
  old_value = self.value
  self.value = nil
  old_value
end

#unbound?Boolean

Returns true if this variable is unbound.

Returns:

  • (Boolean)

Since:

  • 0.3.0



133
134
135
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 133

def unbound?
  value.nil?
end

#var_values(var, term) ⇒ RDF::Term

Returns term if var is the same as this variable.

Parameters:

Returns:

Since:

  • 0.3.0



271
272
273
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 271

def var_values(var, term)
  term if var == name
end

#variable?Boolean #variable?(variable) ⇒ Boolean

Overloads:

  • #variable?Boolean

    Returns true if self is a RDF::Query::Variable, or does it contain a variable?

    Returns:

    • (Boolean)
  • #variable?(variable) ⇒ Boolean

    Returns true if self contains the given variable.

    Parameters:

    Returns:

    • (Boolean)

Since:

  • 0.1.7



100
101
102
103
104
105
106
107
108
109
110
111
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 100

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

#variablesHash{Symbol => RDF::Query::Variable} Also known as: to_h

Returns this variable as Hash.

Returns:

Since:

  • 0.3.0



210
211
212
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-884e3ef78084/lib/rdf/query/variable.rb', line 210

def variables
  {name => self}
end