Class: Factbase::Join

Inherits:
TermBase show all
Defined in:
lib/factbase/terms/join.rb

Overview

The Factbase::Join class is a specialized term that performs join operations between facts based on specified attribute mappings.

Instance Method Summary collapse

Methods inherited from TermBase

#to_s

Constructor Details

#initialize(operands) ⇒ Join

Constructor.

Parameters:

  • operands (Array)

    Operands



13
14
15
16
# File 'lib/factbase/terms/join.rb', line 13

def initialize(operands)
  super()
  @operands = operands
end

Instance Method Details

#evaluate(fact, maps, fb) ⇒ Boolean

Evaluate term on a fact.

Parameters:

Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/factbase/terms/join.rb', line 23

def evaluate(fact, maps, fb)
  assert_args(2)
  jumps = @operands[0]
  raise(ArgumentError, "A string is expected as first argument of 'join'") unless jumps.is_a?(String)
  jumps =
    jumps.split(',')
      .map(&:strip)
      .map! { |j| j.split('<=').map(&:strip) }
      .map! { |j| j.size == 1 ? [j[0], j[0]] : j }
  term = @operands[1]
  raise(ArgumentError, "A term is expected, but '#{term}' provided") unless term.is_a?(Factbase::Term)
  subset = fb.query(term, maps).each(fb, fact).to_a
  subset.each do |s|
    jumps.each do |to, from|
      s[from]&.each do |v|
        fact.__send__(:"#{to}=", v)
      end
    end
  end
  true
end