Module: SPARQL::Algebra::Expression Abstract

Included in:
RDF::Query::Variable, RDF::Term, Operator
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb

Overview

This module is abstract.

A SPARQL algebra expression.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast(datatype, value) ⇒ RDF::Term

Casts operand as the specified datatype

Parameters:

  • datatype (RDF::URI)

    Datatype to evaluate, one of: xsd:integer, xsd:decimal xsd:float, xsd:double, xsd:string, xsd:boolean, or xsd:dateTime

  • value (RDF::Term)

    Value, which should be a typed literal, where the type must be that specified

Returns:

Raises:

  • (TypeError)

    if datatype is not a URI or value cannot be cast to datatype

See Also:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 201

def self.cast(datatype, value)
  case datatype
  when RDF::XSD.dateTime
    case value
    when RDF::Literal::DateTime, RDF::Literal::Date, RDF::Literal::Time
      RDF::Literal.new(value, datatype: datatype)
    when RDF::Literal::Numeric, RDF::Literal::Boolean, RDF::URI, RDF::Node
      raise TypeError, "Value #{value.inspect} cannot be cast as #{datatype}"
    else
      RDF::Literal.new(value.value, datatype: datatype, validate: true)
    end
  when RDF::XSD.float, RDF::XSD.double
    case value
    when RDF::Literal::Boolean
      RDF::Literal.new(value.object ? 1 : 0, datatype: datatype)
    when RDF::Literal::Numeric
      RDF::Literal.new(value.to_f, datatype: datatype)
    when RDF::Literal::DateTime, RDF::Literal::Date, RDF::Literal::Time, RDF::URI, RDF::Node
      raise TypeError, "Value #{value.inspect} cannot be cast as #{datatype}"
    else
      RDF::Literal.new(value.value, datatype: datatype, validate: true)
    end
  when RDF::XSD.boolean
    case value
    when RDF::Literal::Boolean
      value
    when RDF::Literal::Numeric
      RDF::Literal::Boolean.new(value.object != 0)
    when RDF::Literal::DateTime, RDF::Literal::Date, RDF::Literal::Time, RDF::URI, RDF::Node
      raise TypeError, "Value #{value.inspect} cannot be cast as #{datatype}"
    else
      RDF::Literal.new(!value.to_s.empty?, datatype: datatype, validate: true)
    end
  when RDF::XSD.decimal, RDF::XSD.integer
    case value
    when RDF::Literal::Boolean
      RDF::Literal.new(value.object ? 1 : 0, datatype: datatype)
    when RDF::Literal::Integer, RDF::Literal::Decimal
      RDF::Literal.new(value, datatype: datatype)
    when RDF::Literal::DateTime, RDF::Literal::Date, RDF::Literal::Time, RDF::URI, RDF::Node
      raise TypeError, "Value #{value.inspect} cannot be cast as #{datatype}"
    else
      RDF::Literal.new(value.value, datatype: datatype, validate: true)
    end
  when RDF::XSD.string
     RDF::Literal.new(value, datatype: datatype)
  else
    raise TypeError, "Expected datatype (#{datatype}) to be a recognized XPath function"
  end
rescue
  raise TypeError, $!.message
end

.extension(function, *args) ⇒ RDF::Term

Invoke an extension function.

Applies a registered extension function, if registered. Otherwise, if it is an XSD Constructor function, apply that.



180
181
182
183
184
185
186
187
188
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 180

def self.extension(function, *args)
  if function.to_s.start_with?(RDF::XSD.to_s)
    self.cast(function, args.first)
  elsif extension_function = self.extensions[function]
    extension_function.call(*args)
  else
    raise TypeError, "Extension function #{function} not recognized"
  end
end

.extensionsHash{RDF:URI: Proc}

Registered extensions

Returns:

  • (Hash{RDF:URI: Proc})


164
165
166
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 164

def self.extensions
  @extensions ||= {}
end

.for(*sse) ⇒ Expression Also known as: []

Examples:

Expression.for(:isLiteral, RDF::Literal(3.1415))
Expression[:isLiteral, RDF::Literal(3.1415)]

Parameters:

  • sse (Array)

    a SPARQL S-Expression (SSE) form

Returns:



75
76
77
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 75

def self.for(*sse)
  self.new(sse)
end

.new(sse, options = {}) ⇒ Expression

Examples:

Expression.new([:isLiteral, RDF::Literal(3.1415)], version: 1.0)

Parameters:

Returns:

Raises:

  • (TypeError)

    if any of the operands is invalid



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 90

def self.new(sse, options = {})
  raise ArgumentError, "invalid SPARQL::Algebra::Expression form: #{sse.inspect}" unless sse.is_a?(Array)

  operator = Operator.for(sse.first, sse.length - 1)
  unless operator
    return case sse.first
    when Array
      debug(options) {"Map array elements #{sse}"}
      sse.map {|s| self.new(s, options.merge(depth: options[:depth].to_i + 1))}
    else
      debug(options) {"No operator found for #{sse.first}"}
      sse.map do |s|
        s.is_a?(Array) ?
          self.new(s, depth: options[:depth].to_i + 1) :
          s
      end
    end
  end

  operands = sse[1..-1].map do |operand|
    debug(options) {"Operator=#{operator.inspect}, Operand=#{operand.inspect}"}
    case operand
      when Array
        self.new(operand, options.merge(depth: options[:depth].to_i + 1))
      when Operator, Variable, RDF::Term, RDF::Query, Symbol
        operand
      when TrueClass, FalseClass, Numeric, String, DateTime, Date, Time
        RDF::Literal(operand)
      else raise TypeError, "invalid SPARQL::Algebra::Expression operand: #{operand.inspect}"
    end
  end

  debug(options) {"#{operator.inspect}(#{operands.map(&:inspect).join(',')})"}
  options.delete_if {|k, v| [:debug, :depth, :prefixes, :base_uri, :update, :validate].include?(k) }
  operands << options unless options.empty?
  operator.new(*operands)
end

.open(filename, options = {}) {|expression| ... } ⇒ Expression

Parses input from the given file name or URL.

Parameters:

Options Hash (options):

  • :base_uri (RDF::URI, #to_s)

    Base URI used for loading relative URIs.

Yields:

  • (expression)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:



60
61
62
63
64
65
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 60

def self.open(filename, options = {}, &block)
  RDF::Util::File.open_file(filename, options) do |file|
    options[:base_uri] ||= filename
    Expression.parse(file, options, &block)
  end
end

.parse(sse, options = {}) {|expression| ... } ⇒ Expression

Examples:

Expression.parse('(isLiteral 3.1415)')

Parameters:

  • sse (IO, String, #read, #to_s)

    a SPARQL S-Expression (SSE) string or IO object responding to #read

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

    any additional options (see Operator#initialize)

Options Hash (options):

  • :base_uri (RDF::URI, #to_s)

    Base URI used for loading relative URIs.

Yields:

  • (expression)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 22

def self.parse(sse, options = {}, &block)
  begin
    require 'sxp' # @see http://rubygems.org/gems/sxp
  rescue LoadError
    abort "SPARQL::Algebra::Expression.parse requires the SXP gem (hint: `gem install sxp')."
  end
  require 'sparql/algebra/sxp_extensions'
  
  sse = sse.encode(Encoding::UTF_8)
  sxp = SXP::Reader::SPARQL.new(sse) do |reader|
    # Set base_uri if we have one
    reader.base_uri = options[:base_uri] if options[:base_uri]
  end
  sxp_result = sxp.read

  debug(options) {"base_uri: #{options[:base_uri]}"}
  Operator.base_uri = options.delete(:base_uri) if options.has_key?(:base_uri)
  Operator.prefixes = sxp.prefixes || {}

  expression = self.new(sxp_result, options)

  yield(expression) if block_given?
  expression
end

.register_extension(uri) {|*args| ... }

This method returns an undefined value.

Register an extension function.

Extension functions take zero or more arguments of type RDF::Term and return an argument of type RDF::Term, or raise TypeError.

Functions are identified using the uri parameter and specified using a block.

Arguments are evaluated, and the block is called with argument values (if a variable was unbound, an error will have been generated).

It is possible to get unevaluated arguments but care must be taken not to violate the rules of function evaluation.

Normally, block should be a pure evaluation based on it's arguments. It should not access a graph nor return different values for the same arguments (to allow expression optimization). Blocks can't bind a variables.

Examples:

registering a function definition applying the Ruby crypt method to its unary argument.

SPARQL::Algebra::Expression.register_extension(RDF::URI("http://example/crypt") do |literal|
  raise TypeError, "argument must be a literal" unless literal.literal?
  RDF::Literal(literal.to_s.crypt("salt"))
end

Parameters:

Yields:

  • *args

Yield Parameters:

Yield Returns:

Raises:

  • (TypeError)

    if uri is not an RDF::URI or no block is given



154
155
156
157
158
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 154

def self.register_extension(uri, &block)
  raise TypeError, "uri must be an IRI" unless uri.is_a?(RDF::URI)
  raise TypeError, "must pass a block" unless block_given?
  self.extensions[uri] = block
end

Instance Method Details

#constant?Boolean

Returns true.

Returns:

  • (Boolean)

    true or false

See Also:



276
277
278
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 276

def constant?
  !(variable?)
end

#evaluate(bindings, options = {}) ⇒ Expression

Evaluates this expression using the given variable bindings.

This is the default implementation, which simply returns self. Subclasses can override this method in order to implement something more useful.

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:



304
305
306
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 304

def evaluate(bindings, options = {})
  self
end

#invalid?Boolean

Is this value invalid, or is it composed of any invalid components?

Returns:

  • (Boolean)

    true or false



333
334
335
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 333

def invalid?
  !valid?
end

#node?Boolean

Returns false.

Returns:

  • (Boolean)


267
268
269
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 267

def node?
  false
end

#optimizeExpression

Returns an optimized version of this expression.

This is the default implementation, which simply returns self. Subclasses can override this method in order to implement something more useful.

Returns:



288
289
290
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 288

def optimize
  self
end

#to_sxp_binArray

Returns the SPARQL S-Expression (SSE) representation of this expression.

This is the default implementation, which simply returns self. Subclasses can override this method in order to implement something more useful.

Returns:

See Also:



317
318
319
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 317

def to_sxp_bin
  self
end

#valid?Boolean

Is this value valid, and composed only of valid components?

Returns:

  • (Boolean)

    true or false



325
326
327
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 325

def valid?
  true
end

#validate!SPARQL::Algebra::Expression Also known as: validate

Default validate! implementation, overridden in concrete classes

Returns:

Raises:

  • (ArgumentError)

    if the value is invalid



341
342
343
344
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 341

def validate!
  raise ArgumentError if invalid?
  self
end

#variable?Boolean

Returns false.

Returns:

  • (Boolean)

    true or false

See Also:



259
260
261
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/expression.rb', line 259

def variable?
  false
end