Class: RDF::Query::Solutions

Inherits:
Array
  • Object
show all
Defined in:
lib/rdf/query/solutions.rb

Overview

An RDF basic graph pattern (BGP) query solution sequence.

Examples:

Filtering solutions using a hash

solutions.filter(:author  => RDF::URI("http://ar.to/#self"))
solutions.filter(:author  => "Arto Bendiken")
solutions.filter(:author  => [RDF::URI("http://ar.to/#self"), "Arto Bendiken"])
solutions.filter(:updated => RDF::Literal(Date.today))

Filtering solutions using a block

solutions.filter { |solution| solution.author.literal? }
solutions.filter { |solution| solution.title =~ /^SPARQL/ }
solutions.filter { |solution| solution.price < 30.5 }
solutions.filter { |solution| solution.bound?(:date) }
solutions.filter { |solution| solution.age.datatype == RDF::XSD.integer }
solutions.filter { |solution| solution.name.language == :es }

Reordering solutions based on a variable

solutions.order_by(:updated)
solutions.order_by(:updated, :created)

Selecting particular variables only

solutions.select(:title)
solutions.select(:title, :description)

Eliminating duplicate solutions

solutions.distinct

Limiting the number of solutions

solutions.offset(20).limit(10)

Counting the number of matching solutions

solutions.count
solutions.count { |solution| solution.price < 30.5 }

Iterating over all found solutions

solutions.each { |solution| puts solution.inspect }

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Integer) count - (Integer) count({ |solution| ... }) {|solution| ... }

Returns the number of matching query solutions.

Overloads:

  • - (Integer) count

    Returns:

    • (Integer)
  • - (Integer) count({ |solution| ... }) {|solution| ... }

    Yields:

    • (solution)

    Yield Parameters:

    Yield Returns:

    • (Boolean)

    Returns:

    • (Integer)

Returns:

  • (Integer)

Since:

  • 0.3.0



57
58
59
# File 'lib/rdf/query/solutions.rb', line 57

def count(&block)
  super
end

- distinct Also known as: reduced!, reduced, distinct!

This method returns an undefined value.

Ensures that the solutions in this solution sequence are unique.

Since:

  • 0.3.0



130
131
132
133
# File 'lib/rdf/query/solutions.rb', line 130

def distinct
  self.uniq!
  self
end

- filter(criteria = {}) {|solution| ... } Also known as: filter!

This method returns an undefined value.

Filters this solution sequence by the given criteria.

Parameters:

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

Yields:

  • (solution)

Yield Parameters:

Yield Returns:

  • (Boolean)

Since:

  • 0.3.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rdf/query/solutions.rb', line 69

def filter(criteria = {}, &block)
  if block_given?
    self.reject! do |solution|
      !block.call(solution.is_a?(Solution) ? solution : Solution.new(solution))
    end
  else
    self.reject! do |solution|
      solution = solution.is_a?(Solution) ? solution : Solution.new(solution)
      results = criteria.map do |name, value|
        solution[name] == value
      end
      !results.all?
    end
  end
  self
end

- (Boolean) have_variables?(variables) Also known as: has_variables?

Returns true if this solution sequence contains bindings for any of the given variables.

Parameters:

  • (Array<Symbol, #to_sym>) variables

    an array of variables to check

Returns:

  • (Boolean)

    true or false

See Also:

Since:

  • 0.3.0



197
198
199
# File 'lib/rdf/query/solutions.rb', line 197

def have_variables?(variables)
  self.any? { |solution| solution.has_variables?(variables) }
end

- limit(length) Also known as: limit!

This method returns an undefined value.

Limits the number of solutions in this solution sequence to a maximum of length.

Parameters:

  • (Integer, #to_i) length

    zero or a positive integer

Raises:

  • (ArgumentError)

    if length is negative

Since:

  • 0.3.0



162
163
164
165
166
167
168
169
170
# File 'lib/rdf/query/solutions.rb', line 162

def limit(length)
  length = length.to_i
  raise ArgumentError, "expected zero or a positive integer, got #{length}" if length < 0
  case length
    when 0 then self.clear
    else self.slice!(length..-1) if length < self.size
  end
  self
end

- offset(start) Also known as: offset!

This method returns an undefined value.

Limits this solution sequence to bindings starting from the start offset in the overall solution sequence.

Parameters:

  • (Integer, #to_i) start

    zero or a positive or negative integer

Since:

  • 0.3.0



145
146
147
148
149
150
151
# File 'lib/rdf/query/solutions.rb', line 145

def offset(start)
  case start = start.to_i
    when 0 then nil
    else self.slice!(0...start)
  end
  self
end

- order(*variables) Also known as: order_by

This method returns an undefined value.

Reorders this solution sequence by the given variables.

Parameters:

  • (Array<Symbol, #to_sym>) variables

Since:

  • 0.3.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rdf/query/solutions.rb', line 92

def order(*variables)
  if variables.empty?
    raise ArgumentError, "wrong number of arguments (0 for 1)"
  else
    # TODO: support for descending sort, e.g. `order(:s => :asc, :p => :desc)`
    variables.map!(&:to_sym)
    self.sort! do |a, b|
      a = variables.map { |variable| a[variable].to_s } # FIXME
      b = variables.map { |variable| b[variable].to_s } # FIXME
      a <=> b
    end
  end
  self
end

- project(*variables) Also known as: select

This method returns an undefined value.

Restricts this solution sequence to the given variables only.

Parameters:

  • (Array<Symbol, #to_sym>) variables

Since:

  • 0.3.0



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rdf/query/solutions.rb', line 113

def project(*variables)
  if variables.empty?
    raise ArgumentError, "wrong number of arguments (0 for 1)"
  else
    variables.map!(&:to_sym)
    self.each do |solution|
      solution.bindings.delete_if { |k, v| !variables.include?(k.to_sym) }
    end
  end
  self
end

- (Array<Symbol>) variable_names

Returns an array of the distinct variable names used in this solution sequence.

Returns:

  • (Array<Symbol>)

Since:

  • 0.3.0



178
179
180
181
182
183
184
185
186
# File 'lib/rdf/query/solutions.rb', line 178

def variable_names
  variables = self.inject({}) do |result, solution|
    solution.each_name do |name|
      result[name] ||= true
    end
    result
  end
  variables.keys
end