Class: SPARQL::Algebra::Operator::Sample

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Aggregate
Defined in:
vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ffa592c8f275/lib/sparql/algebra/operator/sample.rb

Overview

The SPARQL sample set function.

Examples:

(prefix ((: <http://www.example.org/>))
  (filter (|| (|| (= ?sample 1.0) (= ?sample 2.2)) (= ?sample 3.5))
    (project (?sample)
      (extend ((?sample ??.0))
        (group () ((??.0 (sample ?o)))
          (bgp (triple ?s :dec ?o)))))))

See Also:

Constant Summary collapse

NAME =
:sample

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Aggregate

#aggregate, #replace_aggregate!, #replace_vars!

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #formulae, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

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

Constructor Details

#initialize(*operands, **options) ⇒ Sample

Returns a new instance of Sample.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ffa592c8f275/lib/sparql/algebra/operator/sample.rb', line 20

def initialize(*operands, **options)
  raise ArgumentError,
    "sample operator accepts at most one argument with an optional :distinct" if
    (operands - %i{distinct}).length != 1
  super
end

Instance Method Details

#apply(enum) ⇒ RDF::Term

Sample is a set function which returns an arbitrary value from the multiset passed to it.

Parameters:

Returns:

Raises:

  • (TypeError)

    If enum is empty



34
35
36
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ffa592c8f275/lib/sparql/algebra/operator/sample.rb', line 34

def apply(enum)
  enum.detect(lambda {raise TypeError, "Sampling an empty multiset"}) {|e| e.first}.first
end