Module: RDF::N3::Algebra::Builtin

Includes:
Enumerable, Util::Logger
Included in:
Formula, ListOperator, Log::Implies, Log::NotEqualTo, Math::Negation, NotImplemented, ResourceOperator, Str::Format, Str::Replace, Str::Scrape
Defined in:
lib/rdf/n3/algebra/builtin.rb

Overview

Behavior for N3 builtin operators

Instance Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Instance Method Details

#each(solutions: RDF::Query::Solutions(), &block) ⇒ Object

By default, operators yield themselves and the operands, recursively.

Pass in solutions to have quantifiers resolved to those solutions.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rdf/n3/algebra/builtin.rb', line 50

def each(solutions: RDF::Query::Solutions(), &block)
  log_debug("(#{self.class.const_get(:NAME)} each)")
  log_depth do
    subject, object = operands.map {|op| op.formula? ? op.graph_name : op}
    block.call(RDF::Statement(subject, self.to_uri, object))
    operands.each do |op|
      next unless op.is_a?(Builtin)
      op.each(solutions: solutions) do |st|
        # Maintain formula graph name for formula operands
        st.graph_name ||= op.graph_name if op.formula?
        block.call(st)
      end
    end
  end
end

#evaluate(bindings, formulae:, **options) ⇒ RDF::N3::Algebra::Builtin

Evaluates the builtin using the given variable bindings by cloning the builtin replacing variables with their bindings recursively.

Parameters:

  • bindings (Hash{Symbol => RDF::Term})

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object})

    ({}) options passed from query

Returns:

See Also:

  • SPARQL::Algebra::Expression.evaluate


40
41
42
43
44
# File 'lib/rdf/n3/algebra/builtin.rb', line 40

def evaluate(bindings, formulae:, **options)
  args = operands.map { |operand| operand.evaluate(bindings, formulae: formulae, **options) }
  # Replace operands with bound operands
  self.class.new(*args, formulae: formulae, **options)
end

#hashObject

The builtin hash is the hash of it’s operands and NAME.

See Also:

  • Value#hash


70
71
72
# File 'lib/rdf/n3/algebra/builtin.rb', line 70

def hash
  ([self.class.const_get(:NAME)] + operands).hash
end

#input_operandRDF::Term

Return subject or object operand, or both, depending on which is considered an input.

Returns:



25
26
27
28
# File 'lib/rdf/n3/algebra/builtin.rb', line 25

def input_operand
  # By default, return the merger of input and output operands
  RDF::N3::List.new(values: operands)
end

#rank(solutions) ⇒ Integer

Determine ordering for running built-in operator considering if subject or object is varaible and considered an input or an output. Accepts a solution set to determine if variable inputs are bound.

Parameters:

  • solutions (RDF::Query::Solutions)

Returns:

  • (Integer)

    rake for ordering, lower numbers have fewer unbound output variables.



15
16
17
18
19
# File 'lib/rdf/n3/algebra/builtin.rb', line 15

def rank(solutions)
  vars = input_operand.vars - solutions.variable_names
  # The rank is the remaining unbound variables
  vars.count
end

#to_uriObject

The URI of this operator.



75
76
77
# File 'lib/rdf/n3/algebra/builtin.rb', line 75

def to_uri
  self.class.const_get(:URI)
end