Class: Factbase::Unique

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

Overview

of terms based on provided operands and facts.

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ Unique

Constructor.

Parameters:

  • operands (Array)

    Operands



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

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

Instance Method Details

#evaluate(fact, maps, fb) ⇒ Boolean

Evaluate term on a fact.

Parameters:

Returns:

  • (Boolean)

    True if the value is unique, false otherwise



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/factbase/terms/unique.rb', line 22

def evaluate(fact, maps, fb)
  @seen = Set.new if @seen.nil?
  raise "Too few operands for 'unique' (at least 1 expected)" if @operands.empty?
  vv = (0..(@operands.size - 1)).map { |i| _values(i, fact, maps, fb) }
  return false if vv.any?(nil)
  pass = true
  Enumerator.product(*vv).to_a.each do |t|
    pass = false if @seen.include?(t)
    @seen << t
  end
  pass
end