Class: Factbase::Sorted

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

Overview

It evaluates whether the given facts satisfy the sorted condition.

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ Sorted

Constructor.

Parameters:

  • operands (Array)

    Operands



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

def initialize(operands)
  super()
  @operands = operands
  @op = 'sorted'
end

Instance Method Details

#evaluate(_fact, _maps, _fb) ⇒ Boolean

Evaluate term on a fact.

Parameters:

Returns:

  • (Boolean)

    Whether the value is sorted



23
24
25
# File 'lib/factbase/terms/sorted.rb', line 23

def evaluate(_fact, _maps, _fb)
  true
end

#predict(maps, fb, params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/factbase/terms/sorted.rb', line 27

def predict(maps, fb, params)
  assert_args(2)
  prop = @operands[0]
  raise "A symbol is expected as first argument of 'sorted'" unless prop.is_a?(Symbol)
  term = @operands[1]
  raise "A term is expected, but '#{term}' provided" unless term.is_a?(Factbase::Term)
  fb.query(term, maps).each(fb, params).to_a
    .reject { |m| m[prop].nil? }
    .sort_by { |m| m[prop].first }
    .map { |m| m.all_properties.to_h { |k| [k, m[k]] } }
end