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
# 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 = []
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.



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

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.



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

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

#child_nodesObject



83
84
85
# File 'lib/core/nodes.rb', line 83

def child_nodes
  return @inherit_nodes + [@type_node]
end

#increment_counterObject

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



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

def increment_counter
  TerminalNode.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).



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/core/nodes.rb', line 61

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


76
77
78
79
80
81
# File 'lib/core/nodes.rb', line 76

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 terminal node



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

def reset_counter
  TerminalNode.reset_counter
end