Class: Ruleby::Core::Engine
- Inherits:
-
Object
- Object
- Ruleby::Core::Engine
- Defined in:
- lib/core/engine.rb
Overview
This is the core class of the library. A new rule engine is created by instantiating it. Each rule engine has one inference engine, one rule set and one working memory.
Instance Method Summary collapse
-
#assert(object, &block) ⇒ Object
This method id called to add a new fact to working memory.
-
#assert_rule(rule) ⇒ Object
This method adds a new rule to the system.
- #clear_errors ⇒ Object
- #errors ⇒ Object
- #facts ⇒ Object
-
#initialize(wm = WorkingMemory.new, cr = RulebyConflictResolver.new) ⇒ Engine
constructor
A new instance of Engine.
-
#match(agenda = nil, used_agenda = []) ⇒ Object
This method executes the activations that were generated by the rules that match facts in working memory.
-
#modify(object, &block) ⇒ Object
This method is called to alter an existing fact.
- #print ⇒ Object
-
#retract(object, &block) ⇒ Object
This method is called to remove an existing fact from working memory.
- #retrieve(c) ⇒ Object
Constructor Details
#initialize(wm = WorkingMemory.new, cr = RulebyConflictResolver.new) ⇒ Engine
Returns a new instance of Engine.
205 206 207 208 209 210 211 |
# File 'lib/core/engine.rb', line 205 def initialize(wm=WorkingMemory.new,cr=RulebyConflictResolver.new) @root = nil @working_memory = wm @conflict_resolver = cr @wm_altered = false assert InitialFact.new end |
Instance Method Details
#assert(object, &block) ⇒ Object
This method id called to add a new fact to working memory
218 219 220 221 |
# File 'lib/core/engine.rb', line 218 def assert(object,&block) @wm_altered = true fact_helper(object,:plus,&block) end |
#assert_rule(rule) ⇒ Object
This method adds a new rule to the system.
242 243 244 245 246 247 248 |
# File 'lib/core/engine.rb', line 242 def assert_rule(rule) if @root == nil @root = RootNode.new(@working_memory) @root.reset_counter end @root.assert_rule rule end |
#clear_errors ⇒ Object
274 275 276 |
# File 'lib/core/engine.rb', line 274 def clear_errors @root.clear_errors if @root end |
#errors ⇒ Object
270 271 272 |
# File 'lib/core/engine.rb', line 270 def errors @root.nil? ? [] : @root.errors end |
#facts ⇒ Object
213 214 215 |
# File 'lib/core/engine.rb', line 213 def facts @working_memory.facts.collect{|f| f.object}.select{|f| !f.is_a?(InitialFact)} end |
#match(agenda = nil, used_agenda = []) ⇒ Object
This method executes the activations that were generated by the rules that match facts in working memory.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/core/engine.rb', line 252 def match(agenda=nil, used_agenda=[]) if @root @root.reset_counter agenda = @root.matches unless agenda while (agenda.length > 0) agenda = @conflict_resolver.resolve agenda activation = agenda.pop used_agenda.push activation activation.fire self if @wm_altered agenda = @root.matches(false) @root.increment_counter @wm_altered = false end end end end |
#modify(object, &block) ⇒ Object
This method is called to alter an existing fact. It is essentially a retract followed by an assert.
232 233 234 235 |
# File 'lib/core/engine.rb', line 232 def modify(object,&block) retract(object,&block) assert(object,&block) end |
#print ⇒ Object
278 279 280 281 |
# File 'lib/core/engine.rb', line 278 def print @working_memory.print @root.print end |
#retract(object, &block) ⇒ Object
This method is called to remove an existing fact from working memory
224 225 226 227 228 |
# File 'lib/core/engine.rb', line 224 def retract(object,&block) @wm_altered = true fact_helper(object,:minus,&block) object end |
#retrieve(c) ⇒ Object
237 238 239 |
# File 'lib/core/engine.rb', line 237 def retrieve(c) facts.select {|f| f.kind_of?(c)} end |