Class: SPARQL::Algebra::Operator Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sparql/algebra/operator.rb,
lib/sparql/algebra/operator/str.rb,
lib/sparql/algebra/operator/not.rb,
lib/sparql/algebra/operator/plus.rb,
lib/sparql/algebra/operator/lang.rb,
lib/sparql/algebra/operator/minus.rb,
lib/sparql/algebra/operator/bound.rb,
lib/sparql/algebra/operator/is_iri.rb,
lib/sparql/algebra/operator/is_blank.rb,
lib/sparql/algebra/operator/datatype.rb,
lib/sparql/algebra/operator/is_literal.rb

Overview

This class is abstract.

A SPARQL operator.

Direct Known Subclasses

Operator::Binary, Operator::Nullary, Operator::Ternary, Operator::Unary

Defined Under Namespace

Classes: Binary, Bound, Datatype, IsBlank, IsIRI, IsLiteral, Lang, Minus, Not, Nullary, Plus, Str, Ternary, Unary

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Operator) initialize(options = {})

A new instance of Operator

Parameters:

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

    any additional options

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/sparql/algebra/operator.rb', line 30

def initialize(options = {})
  raise ArgumentError, "expected Hash, but got #{options.inspect}" unless options.is_a?(Hash)
  @options = options.dup
end

Instance Attribute Details

- (Array) operands (readonly)

The operands to this operator.

Returns:

  • (Array)


45
46
47
# File 'lib/sparql/algebra/operator.rb', line 45

def operands
  @operands
end

- (Hash) options (readonly)

Any additional options for this operator.

Returns:

  • (Hash)


39
40
41
# File 'lib/sparql/algebra/operator.rb', line 39

def options
  @options
end

Class Method Details

+ (RDF::Term) evaluate(*args)

Parameters:

  • (Array<RDF::Term>) args

Returns:

  • (RDF::Term)

See Also:



23
24
25
# File 'lib/sparql/algebra/operator.rb', line 23

def self.evaluate(*args)
  self.new(*args).evaluate(RDF::Query::Solution.new)
end

Instance Method Details

- (RDF::Literal::Boolean) boolean(literal) (protected)

Returns the effective boolean value (EBV) of the given literal.

Parameters:

  • (RDF::Literal) literal

Returns:

  • (RDF::Literal::Boolean)

    true or false

Raises:

  • (TypeError)

    if the literal could not be coerced to an RDF::Literal::Boolean

See Also:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sparql/algebra/operator.rb', line 111

def boolean(literal)
  case literal
    when FalseClass then RDF::Literal::FALSE
    when TrueClass  then RDF::Literal::TRUE
    # If the argument is a typed literal with a datatype of
    # `xsd:boolean`, the EBV is the value of that argument.
    # However, the EBV of any literal whose type is `xsd:boolean` is
    # false if the lexical form is not valid for that datatype.
    when RDF::Literal::Boolean
      RDF::Literal(literal.valid? && literal.true?)
    # If the argument is a numeric type or a typed literal with a
    # datatype derived from a numeric type, the EBV is false if the
    # operand value is NaN or is numerically equal to zero; otherwise
    # the EBV is true.
    # However, the EBV of any literal whose type is numeric is
    # false if the lexical form is not valid for that datatype.
    when RDF::Literal::Numeric
      RDF::Literal(literal.valid? && !(literal.zero?) && !(literal.respond_to?(:nan?) && literal.nan?))
    # If the argument is a plain literal or a typed literal with a
    # datatype of `xsd:string`, the EBV is false if the operand value
    # has zero length; otherwise the EBV is true.
    else case
      when literal.plain? || literal.datatype.eql?(RDF::XSD.string)
        RDF::Literal(!(literal.value.empty?))
    # All other arguments, including unbound arguments, produce a type error.
      else raise TypeError, "could not coerce #{literal.inspect} to an RDF::Literal::Boolean"
    end
  end
end

- (Boolean) constant?

Returns true if none of the operands are variables, false otherwise.

Returns:

  • (Boolean)

    true or false

See Also:



79
80
81
# File 'lib/sparql/algebra/operator.rb', line 79

def constant?
  !(variable?)
end

- (RDF::Term) evaluate(solution)

This method is abstract.

Parameters:

  • (RDF::Query::Solution) solution

    a query solution containing zero or more variable bindings

Returns:

  • (RDF::Term)

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/sparql/algebra/operator.rb', line 88

def evaluate(solution)
  raise NotImplementedError, "#{self.class}#evaluate"
end

- (RDF::Term) operand(index, bindings = {})

Returns the operand at the given index.

If the optional bindings argument is provided, it is used for performing variable lookup in case the operand is a variable.

Parameters:

  • (Integer) index

    an operand index in the range (0...(operands.count))

  • (RDF::Query::Solution, #[]) bindings (defaults to: {})

    optional bindings for looking up variable values

Returns:

  • (RDF::Term)


58
59
60
61
# File 'lib/sparql/algebra/operator.rb', line 58

def operand(index, bindings = {})
  operand = operands[index]
  operand.is_a?(RDF::Query::Variable) ? bindings[operand.to_sym] : operand
end

- (Array) to_sse

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

Returns:

  • (Array)

See Also:



97
98
99
100
# File 'lib/sparql/algebra/operator.rb', line 97

def to_sse
  operator = [self.class.const_get(:NAME)].flatten.first
  [operator, *(operands || [])]
end

- (Boolean) variable?

Returns true if any of the operands are variables, false otherwise.

Returns:

  • (Boolean)

    true or false

See Also:



69
70
71
# File 'lib/sparql/algebra/operator.rb', line 69

def variable?
  operands.any? { |operand| operand.is_a?(RDF::Query::Variable) }
end