Class: RDF::Query::Variable

Inherits:
Object show all
Includes:
Term, SPARQL::Algebra::Expression
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/extensions.rb

Overview

Extensions for RDF::Query::Variable.

Since:

  • 0.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SPARQL::Algebra::Expression

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

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!

Constructor Details

#initialize(name = nil, value = 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

Since:

  • 0.3.0



68
69
70
71
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 68

def initialize(name = nil, value = nil)
  @name  = (name || "g#{__id__.to_i.abs}").to_sym
  @value = value
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.5.0/bundler/gems/rdf-c7356a6367f8/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.5.0/bundler/gems/rdf-c7356a6367f8/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



196
197
198
199
200
201
202
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 196

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

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

Rebinds this variable to the given value.

Parameters:

Returns:

  • (RDF::Term)

    the previous value, if any.

Since:

  • 0.3.0



129
130
131
132
133
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 129

def bind(value)
  old_value = self.value
  self.value = value
  old_value
end

#bindingsHash{Symbol => RDF::Term}

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

Returns:

Since:

  • 0.3.0



160
161
162
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 160

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

#bound?Boolean

Returns true if this variable is bound.

Returns:

  • (Boolean)

Since:

  • 0.3.0



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

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



120
121
122
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 120

def distinguished=(value)
  @distinguished = value
end

#distinguished?Boolean

Returns true if this variable is distinguished.

Returns:

  • (Boolean)

Since:

  • 0.3.0



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

def distinguished?
  @distinguished.nil? || @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



180
181
182
183
184
185
186
187
188
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 180

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:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object}) (defaults to: {})

    ({}) options passed from query

Returns:

Raises:

  • (TypeError)

    if the variable is not bound

Since:

  • 0.3.0



389
390
391
392
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/extensions.rb', line 389

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

#hashInteger

Returns a hash code for this variable.

Returns:

Since:

  • 0.3.0



169
170
171
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 169

def hash
  @name.hash
end

#named?Boolean

Returns true if this variable has a name.

Returns:

  • (Boolean)

Since:

  • 0.3.0



87
88
89
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 87

def named?
  true
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 ??

Examples:

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

Returns:

Since:

  • 0.3.0



218
219
220
221
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 218

def to_s
  prefix = distinguished? ? '?' : "??"
  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



140
141
142
143
144
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 140

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



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

def unbound?
  value.nil?
end

#variable?Boolean

Returns true.

Returns:

  • (Boolean)

See Also:

  • Term#variable?

Since:

  • 0.1.7



79
80
81
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 79

def variable? 
  true
end

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

Returns this variable as Hash.

Returns:

Since:

  • 0.3.0



151
152
153
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/query/variable.rb', line 151

def variables
  {name => self}
end