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.



140
141
142
143
# File 'lib/core/engine.rb', line 140

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

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts.



138
139
140
# File 'lib/core/engine.rb', line 138

def facts
  @facts
end

Instance Method Details

#assert_fact(fact) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/core/engine.rb', line 151

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



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

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


168
169
170
171
172
173
# File 'lib/core/engine.rb', line 168

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