Class: Ruleby::Core::WorkingMemory
- Inherits:
-
Object
- Object
- Ruleby::Core::WorkingMemory
- 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
-
#facts ⇒ Object
readonly
Returns the value of attribute facts.
Instance Method Summary collapse
- #assert_fact(fact) ⇒ Object
- #each_fact ⇒ Object
-
#initialize ⇒ WorkingMemory
constructor
A new instance of WorkingMemory.
- #print ⇒ Object
Constructor Details
#initialize ⇒ WorkingMemory
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
#facts ⇒ Object (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_fact ⇒ Object
145 146 147 148 149 |
# File 'lib/core/engine.rb', line 145 def each_fact @facts.each do |f| yield(f) end end |
#print ⇒ Object
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 |