Class: RDF::Query::Variable

Inherits:
Object show all
Includes:
Term, SPARQL::Algebra::Expression
Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/extensions.rb,
vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb

Overview

An RDF query variable.

Examples:

Creating a named unbound variable

var = RDF::Query::Variable.new(:x)
var.unbound?   #=> true
var.value      #=> nil

Creating an anonymous unbound variable

var = RDF::Query::Variable.new
var.name       #=> :g2166151240

Unbound variables match any value

var === RDF::Literal(42)     #=> true

Creating a bound variable

var = RDF::Query::Variable.new(:y, 123)
var.bound?     #=> true
var.value      #=> 123

Bound variables match only their actual value

var = RDF::Query::Variable.new(:y, 123)
var === 42     #=> false
var === 123    #=> true

Getting the variable name

var = RDF::Query::Variable.new(:y, 123)
var.named?     #=> true
var.name       #=> :y
var.to_sym     #=> :y

Rebinding a variable returns the previous value

var.bind!(456) #=> 123
var.value      #=> 456

Unbinding a previously bound variable

var.unbind!
var.unbound?   #=> true

Getting the string representation of a variable

var = RDF::Query::Variable.new(:x)
var.to_s       #=> "?x"
var = RDF::Query::Variable.new(:y, 123)
var.to_s       #=> "?y=123"

Since:

  • 0.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Term

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

Methods included from Value

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

Methods included from SPARQL::Algebra::Expression

cast, #constant?, extension, extensions, for, #invalid?, new, #node?, open, #optimize!, parse, register_extension, #to_sxp_bin, #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.

  • 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/2.7.0/bundler/gems/rdf-24ba7cdc342f/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/2.7.0/bundler/gems/rdf-24ba7cdc342f/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/2.7.0/bundler/gems/rdf-24ba7cdc342f/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



241
242
243
244
245
246
247
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 241

def ===(other)
  if unbound?
    other.is_a?(RDF::Term) # match any Term when unbound
  else
    value === other
  end
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



167
168
169
170
171
172
173
174
175
176
177
178
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 167

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



205
206
207
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 205

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

#bound?Boolean

Returns true if this variable is bound.

Returns:

  • (Boolean)

Since:

  • 0.3.0



111
112
113
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 111

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



136
137
138
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 136

def distinguished=(value)
  @distinguished = value
end

#distinguished?Boolean

Returns true if this variable is distinguished.

Returns:

  • (Boolean)

Since:

  • 0.3.0



127
128
129
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 127

def distinguished?
  @distinguished
end

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

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



225
226
227
228
229
230
231
232
233
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 225

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



486
487
488
489
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/extensions.rb', line 486

def evaluate(bindings, **options)
  raise TypeError if bindings.respond_to?(:bound?) && !bindings.bound?(self)
  bindings[name.to_sym]
end

#existential=(value) ⇒ Boolean

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

Returns:

  • (Boolean)

Since:

  • 0.3.0



153
154
155
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 153

def existential=(value)
  @existential = value
end

#existential?Boolean

Returns true if this variable is existential.

Returns:

  • (Boolean)

Since:

  • 0.3.0



144
145
146
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 144

def existential?
  @existential
end

#hashInteger

Returns a hash code for this variable.

Returns:

Since:

  • 0.3.0



214
215
216
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 214

def hash
  @name.hash
end

#named?Boolean

Returns true if this variable has a name.

Returns:

  • (Boolean)

Since:

  • 0.3.0



103
104
105
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 103

def named?
  true
end

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

Return self

Returns:

See Also:



496
497
498
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/extensions.rb', line 496

def optimize(**options)
  self
end

#to_sString

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



264
265
266
267
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 264

def to_s
  prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
  unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}"
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



185
186
187
188
189
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 185

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



119
120
121
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 119

def unbound?
  value.nil?
end

#variable?Boolean

Returns true.

Returns:

  • (Boolean)

See Also:

  • Term#variable?

Since:

  • 0.1.7



95
96
97
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 95

def variable? 
  true
end

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

Returns this variable as Hash.

Returns:

Since:

  • 0.3.0



196
197
198
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-24ba7cdc342f/lib/rdf/query/variable.rb', line 196

def variables
  {name => self}
end