Class: KBS::Blackboard::Engine

Inherits:
Engine
  • Object
show all
Defined in:
lib/kbs/blackboard/engine.rb

Overview

KBS engine integrated with Blackboard memory

Instance Attribute Summary collapse

Attributes inherited from Engine

#alpha_memories, #production_nodes, #rules, #working_memory

Instance Method Summary collapse

Methods inherited from Engine

#add_rule

Constructor Details

#initialize(db_path: ':memory:', store: nil) ⇒ Engine

Returns a new instance of Engine.



12
13
14
15
16
17
# File 'lib/kbs/blackboard/engine.rb', line 12

def initialize(db_path: ':memory:', store: nil)
  super()
  @blackboard = Memory.new(db_path: db_path, store: store)
  @working_memory = @blackboard
  @blackboard.add_observer(self)
end

Instance Attribute Details

#blackboardObject (readonly)

Returns the value of attribute blackboard.



10
11
12
# File 'lib/kbs/blackboard/engine.rb', line 10

def blackboard
  @blackboard
end

Instance Method Details

#add_fact(type, attributes = {}) ⇒ Object



19
20
21
# File 'lib/kbs/blackboard/engine.rb', line 19

def add_fact(type, attributes = {})
  @blackboard.add_fact(type, attributes)
end

#consume_message(topic, consumer) ⇒ Object



58
59
60
# File 'lib/kbs/blackboard/engine.rb', line 58

def consume_message(topic, consumer)
  @blackboard.consume_message(topic, consumer)
end

#post_message(sender, topic, content, priority: 0) ⇒ Object



54
55
56
# File 'lib/kbs/blackboard/engine.rb', line 54

def post_message(sender, topic, content, priority: 0)
  @blackboard.post_message(sender, topic, content, priority: priority)
end

#remove_fact(fact) ⇒ Object



23
24
25
# File 'lib/kbs/blackboard/engine.rb', line 23

def remove_fact(fact)
  @blackboard.remove_fact(fact)
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kbs/blackboard/engine.rb', line 39

def run
  @production_nodes.values.each do |node|
    node.tokens.each do |token|
      # Only fire if not already fired
      next if token.fired?

      fact_uuids = token.facts.map { |f| f.respond_to?(:uuid) ? f.uuid : f.object_id.to_s }
      bindings = extract_bindings_from_token(token, node.rule)

      @blackboard.log_rule_firing(node.rule.name, fact_uuids, bindings)
      node.fire_rule(token)
    end
  end
end

#statsObject



62
63
64
# File 'lib/kbs/blackboard/engine.rb', line 62

def stats
  @blackboard.stats
end

#update(action, fact) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kbs/blackboard/engine.rb', line 27

def update(action, fact)
  if action == :add
    @alpha_memories.each do |pattern, memory|
      memory.activate(fact) if fact.matches?(pattern)
    end
  elsif action == :remove
    @alpha_memories.each do |pattern, memory|
      memory.deactivate(fact) if fact.matches?(pattern)
    end
  end
end