Class: Wongi::Engine::AggregateNode

Inherits:
BetaNode
  • Object
show all
Defined in:
lib/wongi-engine/beta/aggregate_node.rb

Instance Attribute Summary collapse

Attributes inherited from BetaNode

#children, #parent, #rete

Instance Method Summary collapse

Methods inherited from BetaNode

#assignment_node, #beta_deactivate_children, #depth, #empty?, #overlay, #refresh, #root?, #size, #tokens

Methods included from CoreExt

included

Constructor Details

#initialize(parent, var, over, partition, aggregate, map) ⇒ AggregateNode

Returns a new instance of AggregateNode.



5
6
7
8
9
10
11
12
# 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

Instance Attribute Details

#aggregateObject (readonly)

Returns the value of attribute aggregate.



3
4
5
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 3

def aggregate
  @aggregate
end

#mapObject (readonly)

Returns the value of attribute map.



3
4
5
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 3

def map
  @map
end

#overObject (readonly)

Returns the value of attribute over.



3
4
5
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 3

def over
  @over
end

#partitionObject (readonly)

Returns the value of attribute partition.



3
4
5
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 3

def partition
  @partition
end

#varObject (readonly)

Returns the value of attribute var.



3
4
5
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 3

def var
  @var
end

Instance Method Details

#beta_activate(token) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
43
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



20
21
22
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 20

def make_aggregate_fn(agg)
  agg
end

#make_map_fn(map) ⇒ Object



24
25
26
27
28
29
30
# 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) ⇒ Object



14
15
16
17
18
# 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) ⇒ Object



45
46
47
# File 'lib/wongi-engine/beta/aggregate_node.rb', line 45

def refresh_child(child)
  evaluate(child: child)
end