class Wongi::Engine::AggregateNode

Attributes

aggregate[R]
map[R]
over[R]
partition[R]
var[R]

Public Class Methods

new(parent, var, over, partition, aggregate, map) click to toggle source
Calls superclass method
# File lib/wongi-engine/beta/aggregate_node.rb, line 5
def initialize(parent, var, over, partition, aggregate, map)
  super(parent)
  @var = var
  @over = over
  @partition = make_partition_fn(partition)
  @aggregate = make_aggregate_fn(aggregate)
  @map = make_map_fn(map)
end

Public Instance Methods

beta_activate(token) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 32
def beta_activate(token)
  return if tokens.find { |t| t.duplicate? token }

  overlay.add_token(token)
  evaluate
end
beta_deactivate(token) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 39
def beta_deactivate(token)
  overlay.remove_token(token)
  beta_deactivate_children(token: token)
  evaluate
end
evaluate(child: nil) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 49
def evaluate(child: nil)
  return if tokens.empty?

  groups =
    if partition
      tokens.group_by(&partition).values
    else
      # just a single group of everything
      [tokens]
    end

  groups.each do |tokens|
    aggregated = aggregate.call(tokens.map(&map))
    assignment = { var => aggregated }
    children = child ? [child] : self.children
    children.each do |beta|
      new_token = Token.new(beta, tokens, nil, assignment)
      # nothing changed, skip useless traversal
      next if beta.tokens.find { _1.duplicate?(new_token) }

      beta.tokens.select { |child| tokens.any? { child.child_of?(_1) } }.each { beta.beta_deactivate(_1) }
      beta.beta_activate(new_token)
    end
  end
end
make_aggregate_fn(agg) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 20
def make_aggregate_fn(agg)
  agg
end
make_map_fn(map) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 24
def make_map_fn(map)
  if map.nil?
    ->(token) { token[over] }
  else
    map
  end
end
make_partition_fn(partition) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 14
def make_partition_fn(partition)
  return nil if partition.empty?

  ->(token) { token.values_at(*partition) }
end
refresh_child(child) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 45
def refresh_child(child)
  evaluate(child: child)
end

Protected Instance Methods

matches?(token, wme) click to toggle source
# File lib/wongi-engine/beta/aggregate_node.rb, line 77
def matches?(token, wme)
  @tests.each do |test|
    return false unless test.matches?(token, wme)
  end
  true
end