Class: Ruleby::Core::WorkingMemory

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

Overview

The working memory is a container for all the facts in the system. The inference engine will compare these facts with the rules to produce some outcomes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorkingMemory

Returns a new instance of WorkingMemory.



145
146
147
148
# File 'lib/core/engine.rb', line 145

def initialize
  @recency = 0
  @facts = Array.new
end

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts.



143
144
145
# File 'lib/core/engine.rb', line 143

def facts
  @facts
end

Instance Method Details

#assert_fact(fact) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/core/engine.rb', line 156

def assert_fact(fact)
  raise 'The fact asserted cannot be nil!' unless fact.object
  if (fact.token == :plus)
    fact.recency = @recency
    @recency += 1
    @facts.push fact
    return fact
  else #if (fact.token == :minus)  

    i = @facts.index(fact)
    raise 'The fact to remove does not exist!' unless i
    existing_fact = @facts[i]
    @facts.delete_at(i)
    existing_fact.token = fact.token
    return existing_fact
  end
end

#each_factObject



150
151
152
153
154
# File 'lib/core/engine.rb', line 150

def each_fact
  @facts.each do |f|
    yield(f)
  end
end


173
174
175
176
177
178
# File 'lib/core/engine.rb', line 173

def print
  puts 'WORKING MEMORY:'
  @facts.each do |fact|
    puts " #{fact.object} - #{fact.id} - #{fact.recency}"
  end
end