Class: Ruleby::Core::RootNode

Inherits:
Object
  • Object
show all
Defined in:
lib/core/nodes.rb

Overview

This class acts as the root-node of the network. It contains the logic for building the node-network from a set of rules, and updating it according to the working memory

Instance Method Summary collapse

Constructor Details

#initialize(working_memory) ⇒ RootNode

Returns a new instance of RootNode.



19
20
21
22
23
24
25
26
27
# File 'lib/core/nodes.rb', line 19

def initialize(working_memory)
  @working_memory = working_memory
  @type_node = nil
  @inherit_nodes = []
  @atom_nodes = [] 
  @join_nodes = []
  @terminal_nodes = []
  @bucket = NetworkBucket.new
end

Instance Method Details

#assert_fact(fact) ⇒ Object

When a new fact is added to working memory, or an existing one is removed this method is called. It finds any nodes that depend on it, and updates them accordingly.



40
41
42
43
44
45
# File 'lib/core/nodes.rb', line 40

def assert_fact(fact) 
  @type_node and fact.token == :plus ? @type_node.assert(fact) : @type_node.retract(fact) 
  @inherit_nodes.each do |node|
    fact.token == :plus ? node.assert(fact) : node.retract(fact) 
  end
end

#assert_rule(rule) ⇒ Object

This method is invoked when a new rule is added to the system. The rule is processed and the appropriate nodes are added to the network.



31
32
33
34
35
# File 'lib/core/nodes.rb', line 31

def assert_rule(rule)
  terminal_node = TerminalNode.new @bucket, rule
  build_network(rule.pattern, terminal_node)
  @terminal_nodes.push terminal_node
end

#child_nodesObject



92
93
94
# File 'lib/core/nodes.rb', line 92

def child_nodes
  return @inherit_nodes + [@type_node]
end

#clear_errorsObject



63
64
65
# File 'lib/core/nodes.rb', line 63

def clear_errors
  @bucket.clear_errors
end

#errorsObject



59
60
61
# File 'lib/core/nodes.rb', line 59

def errors
  @bucket.errors
end

#increment_counterObject

Increments the activation counter. This is just a pass-thru to the static variable in the bucket



49
50
51
# File 'lib/core/nodes.rb', line 49

def increment_counter
  @bucket.increment_counter
end

#matches(initial = true) ⇒ Object

When invoked, this method returns a list of all Action|MatchContext pairs as Activations. The list is generated when facts and rules are asserted, so no comparisions are done here (i.e. no Backward Chaining).



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/core/nodes.rb', line 70

def matches(initial=true)
  agenda = Array.new
  @terminal_nodes.each do |node|
    node.activations.values.each do |a|
      if initial
        a.used = false 
        agenda.push a 
      elsif !a.used
        agenda.push a 
      end          
    end 
  end
  return agenda
end


85
86
87
88
89
90
# File 'lib/core/nodes.rb', line 85

def print
  puts 'NETWORK:'    
  @terminal_nodes.each do |n|
    n.print(' ')
  end
end

#reset_counterObject

Resets the activation counter. This is just a pass-thru to the static variable in the bucket



55
56
57
# File 'lib/core/nodes.rb', line 55

def reset_counter
  @bucket.reset_counter
end