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.



154
155
156
157
# File 'lib/core/engine.rb', line 154

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

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts.



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

def facts
  @facts
end

Instance Method Details

#assert_fact(fact) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/core/engine.rb', line 165

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



159
160
161
162
163
# File 'lib/core/engine.rb', line 159

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


182
183
184
185
186
187
# File 'lib/core/engine.rb', line 182

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