Class: Wongi::Engine::Compiler

Inherits:
Struct
  • Object
show all
Defined in:
lib/wongi-engine/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alpha_deafObject

Returns the value of attribute alpha_deaf

Returns:

  • (Object)

    the current value of alpha_deaf



2
3
4
# File 'lib/wongi-engine/compiler.rb', line 2

def alpha_deaf
  @alpha_deaf
end

#conditionsObject

Returns the value of attribute conditions

Returns:

  • (Object)

    the current value of conditions



2
3
4
# File 'lib/wongi-engine/compiler.rb', line 2

def conditions
  @conditions
end

#nodeObject

Returns the value of attribute node

Returns:

  • (Object)

    the current value of node



2
3
4
# File 'lib/wongi-engine/compiler.rb', line 2

def node
  @node
end

#parametersObject

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



2
3
4
# File 'lib/wongi-engine/compiler.rb', line 2

def parameters
  @parameters
end

#reteObject

Returns the value of attribute rete

Returns:

  • (Object)

    the current value of rete



2
3
4
# File 'lib/wongi-engine/compiler.rb', line 2

def rete
  @rete
end

Instance Method Details

#aggregate_node(var, over, partition, aggregate, map) ⇒ Object



61
62
63
64
# File 'lib/wongi-engine/compiler.rb', line 61

def aggregate_node(var, over, partition, aggregate, map)
  declare(var)
  self.node = AggregateNode.new(node, var, over, partition, aggregate, map).tap(&:refresh)
end

#assignment_node(variable, body) ⇒ Object



27
28
29
30
# File 'lib/wongi-engine/compiler.rb', line 27

def assignment_node(variable, body)
  self.node = AssignmentNode.new(node, variable, body).tap(&:refresh)
  declare(variable)
end

#compileObject



3
4
5
6
7
# File 'lib/wongi-engine/compiler.rb', line 3

def compile
  conditions.inject(self) do |context, condition|
    condition.compile context
  end.node
end

#declare(v) ⇒ Object



13
14
15
# File 'lib/wongi-engine/compiler.rb', line 13

def declare(v)
  declared_variables << v unless declared_variables.include?(v)
end

#declared_variablesObject



17
18
19
# File 'lib/wongi-engine/compiler.rb', line 17

def declared_variables
  @declared_variables ||= []
end

#declares_variable?(v) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/wongi-engine/compiler.rb', line 9

def declares_variable?(v)
  parameters.include?(v) || declared_variables.include?(v)
end

#dupObject



21
22
23
24
25
# File 'lib/wongi-engine/compiler.rb', line 21

def dup
  Compiler.new(rete, node, conditions, parameters, alpha_deaf).tap do |compiler|
    declared_variables.each { |v| compiler.declare(v) }
  end
end

#filter_node(filter) ⇒ Object



102
103
104
105
# File 'lib/wongi-engine/compiler.rb', line 102

def filter_node(filter)
  existing = node.children.find { |n| n.is_a?(FilterNode) && n.test == filter }
  self.node = existing || FilterNode.new(node, filter).tap(&:refresh)
end

#join_node(condition, tests, assignment) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wongi-engine/compiler.rb', line 32

def join_node(condition, tests, assignment)
  alpha = rete.compile_alpha(condition)
  self.node = if (existing = node.children.find { |n| n.is_a?(JoinNode) && n.equivalent?(alpha, tests, assignment) && !n.children.map(&:class).include?(Wongi::Engine::OrNode) })
                existing
              else
                JoinNode.new(node, tests, assignment).tap do |join|
                  join.alpha = alpha
                  alpha.betas << join unless alpha_deaf
                end
              end
  node.tap(&:refresh)
end

#ncc_node(subrule, alpha_deaf) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wongi-engine/compiler.rb', line 83

def ncc_node(subrule, alpha_deaf)
  subcompiler = Compiler.new(rete, node, subrule.conditions, parameters, alpha_deaf)
  declared_variables.each { |v| subcompiler.declare(v) }
  bottom = subcompiler.compile
  if (existing = node.children.find { |n| n.is_a?(NccNode) && n.partner.parent == bottom })
    self.node = existing
    return
  end
  ncc = NccNode.new(node)
  partner = NccPartner.new(subcompiler.node)
  ncc.partner = partner
  partner.ncc = ncc
  partner.divergent = node
  #    partner.conjuncts = condition.children.size
  ncc.refresh
  partner.refresh
  self.node = ncc
end

#neg_node(condition, tests) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/wongi-engine/compiler.rb', line 45

def neg_node(condition, tests)
  alpha = rete.compile_alpha(condition)
  self.node = NegNode.new(node, tests, alpha).tap do |node|
    alpha.betas << node unless alpha_deaf
    node.refresh
  end
end

#opt_node(condition, tests, assignment) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/wongi-engine/compiler.rb', line 53

def opt_node(condition, tests, assignment)
  alpha = rete.compile_alpha(condition)
  self.node = OptionalNode.new(node, alpha, tests, assignment).tap do |node|
    alpha.betas << node unless alpha_deaf
    node.refresh
  end
end

#or_node(variants) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wongi-engine/compiler.rb', line 66

def or_node(variants)
  subvariables = []
  branches = variants.map do |variant|
    subcompiler = Compiler.new(rete, node, variant.conditions, parameters, false)
    declared_variables.each { |v| subcompiler.declare(v) }
    subcompiler.compile
    subvariables << subcompiler.declared_variables
    subcompiler.node
  end
  subvariables.each do |variables|
    variables.each do |v|
      declare(v)
    end
  end
  self.node = OrNode.new(branches).tap(&:refresh)
end