Module: SPARQL::Algebra::Expression Abstract

Included in:
RDF::Query::Variable, RDF::Term, Operator
Defined in:
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) ⇒ Boolean

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:

  • (Boolean)

Raises:

  • (TypeError)

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

See Also:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sparql/algebra/expression.rb', line 136

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::Numeric, RDF::Literal::Boolean
      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.boolean
    case value
    when RDF::Literal::Boolean
      value
    when RDF::Literal::Numeric
      RDF::Literal::Boolean.new(value.value != 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::Integer, RDF::Literal::Decimal, RDF::Literal::Boolean
      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 an XSD type"
  end
rescue
  raise TypeError, $!.message
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:



72
73
74
# File 'lib/sparql/algebra/expression.rb', line 72

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

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

Examples:

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

Parameters:

  • sse (Array)

    a SPARQL S-Expression (SSE) form

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

    any additional options (see Operator#initialize)

Returns:

Raises:

  • (TypeError)

    if any of the operands is invalid



87
88
89
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
# File 'lib/sparql/algebra/expression.rb', line 87

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("Map array elements #{sse}", options)
      sse.map {|s| self.new(s, options.merge(:depth => options[:depth].to_i + 1))}
    else
      debug("No operator found for #{sse.first}", options)
      sse.map do |s|
        s.is_a?(Array) ?
          self.new(s, options.merge(:depth => options[:depth].to_i + 1)) :
          s
      end
    end
  end

  operands = sse[1..-1].map do |operand|
    debug("Operator=#{operator.inspect}, Operand=#{operand.inspect}", options)
    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("#{operator.inspect}(#{operands.map(&:inspect).join(',')})", options)
  options.delete_if {|k, v| [:debug, :depth, :prefixes, :base_uri].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:

  • filename (String, #to_s)
  • 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:



55
56
57
58
59
60
61
62
# File 'lib/sparql/algebra/expression.rb', line 55

def self.open(filename, options = {}, &block)
  RDF::Util::File.open_file(filename, options) do |file|
    options[:base_uri] ||= filename
    expression = Expression.parse(file, options)
    yield(expression) if block_given?
    expression
  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
# File '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'
  
  sxp = SXP::Reader::SPARQL.new(sse)
  sxp_result = sxp.read

  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

Instance Method Details

#constant?Boolean

Returns true.

Returns:

  • (Boolean)

    true or false

See Also:



199
200
201
# File 'lib/sparql/algebra/expression.rb', line 199

def constant?
  !(variable?)
end

#evaluate(bindings = {}) ⇒ 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, #[]) (defaults to: {})

Returns:



224
225
226
# File 'lib/sparql/algebra/expression.rb', line 224

def evaluate(bindings = {})
  self
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:



211
212
213
# File 'lib/sparql/algebra/expression.rb', line 211

def optimize
  self
end

#to_sseArray

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:



237
238
239
# File 'lib/sparql/algebra/expression.rb', line 237

def to_sse
  self
end

#variable?Boolean

Returns false.

Returns:

  • (Boolean)

    true or false

See Also:



190
191
192
# File 'lib/sparql/algebra/expression.rb', line 190

def variable?
  false
end